Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Arduino Uno com Ethernet Shield e sensor DHT11
- em comunicação com a plataforma ThingSpeak
- Mais informacoes:
- http://drbitblog.wordpress.com/
- 02/05/2014
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <avr/wdt.h>
- #include <dht.h>
- #define dht_dpin A1 //Pino DATA do Sensor ligado na porta Analogica A1 do Arduino
- dht DHT; //Inicializa o sensor
- // Local Network Settings
- byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
- // ThingSpeak Settings
- char thingSpeakAddress[] = "api.thingspeak.com";
- String writeAPIKey = "sua chave de escrita aqui";
- // atualizacao a cada 60 segundos
- const int updateThingSpeakInterval = 20 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
- // Variable Setup
- long lastConnectionTime = 0;
- boolean lastConnected = false;
- int failedCounter = 0;
- // Initialize Arduino Ethernet Client
- EthernetClient client;
- void setup()
- {
- // Start Serial for debugging on the Serial Monitor
- Serial.begin(9600);
- wdt_enable(WDTO_8S); // watchdog habilitado para 8 segundos
- Serial.println("Iniciado...");
- //Start Ethernet on Arduino
- startEthernet();
- }
- void loop()
- {
- wdt_reset(); // reset do watcdog
- // Print Update Response to Serial Monitor
- if (client.available())
- {
- char c = client.read();
- Serial.print(c); // exibe dados enviados pelo servidor
- }
- // Disconnect from ThingSpeak
- if (!client.connected() && lastConnected)
- {
- Serial.println("...disconnected");
- Serial.println();
- client.stop();
- }
- // Update ThingSpeak
- if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
- {
- DHT.read11(dht_dpin); //Lê as informações do sensor
- String v_umidade = String(int(DHT.humidity)); //obtem umidade
- String v_temperatura = String(int(DHT.temperature)); //obtem temperatura
- // exibe dados do sensor
- Serial.print("Umidade = ");
- Serial.print(v_umidade);
- Serial.print(" % ");
- Serial.print("Temperatura = ");
- Serial.print(v_temperatura);
- Serial.println(" Celsius ");
- delay(2000);
- // envia dados ao ThingSpeak
- updateThingSpeak("field1=" + v_temperatura + "&field2=" + v_umidade);
- }
- // Check if Arduino Ethernet needs to be restarted
- if (failedCounter > 3 ) {startEthernet();}
- lastConnected = client.connected();
- }
- void updateThingSpeak(String tsData)
- {
- if (client.connect(thingSpeakAddress, 80))
- {
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(tsData.length());
- client.print("\n\n");
- client.print(tsData);
- lastConnectionTime = millis();
- if (client.connected())
- {
- Serial.println("Connecting to ThingSpeak...");
- failedCounter = 0;
- }
- else
- {
- failedCounter++;
- Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
- Serial.println();
- }
- }
- else
- {
- failedCounter++;
- Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
- Serial.println();
- lastConnectionTime = millis();
- }
- }
- void startEthernet()
- {
- client.stop();
- Serial.println("Connecting Arduino to network...");
- Serial.println();
- delay(1000);
- // Connect to network amd obtain an IP address using DHCP
- if (Ethernet.begin(mac) == 0)
- {
- Serial.println("DHCP Failed, reset Arduino to try again");
- Serial.println();
- }
- else
- {
- Serial.println("Arduino connected to network using DHCP");
- Serial.println();
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement