Advertisement
Guest User

Arduino e ThinkSpeak

a guest
May 2nd, 2014
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. /*
  2. Arduino Uno com Ethernet Shield e sensor DHT11
  3. em comunicação com a plataforma ThingSpeak
  4. Mais informacoes:
  5. http://drbitblog.wordpress.com/
  6. 02/05/2014
  7. */
  8.  
  9. #include <SPI.h>
  10. #include <Ethernet.h>
  11. #include <avr/wdt.h>
  12. #include <dht.h>
  13. #define dht_dpin A1 //Pino DATA do Sensor ligado na porta Analogica A1 do Arduino
  14.  
  15. dht DHT; //Inicializa o sensor
  16.  
  17. // Local Network Settings
  18. byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
  19.  
  20. // ThingSpeak Settings
  21. char thingSpeakAddress[] = "api.thingspeak.com";
  22. String writeAPIKey = "sua chave de escrita aqui";
  23. // atualizacao a cada 60 segundos
  24. const int updateThingSpeakInterval = 20 * 1000;      // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
  25.  
  26. // Variable Setup
  27. long lastConnectionTime = 0;
  28. boolean lastConnected = false;
  29. int failedCounter = 0;
  30.  
  31. // Initialize Arduino Ethernet Client
  32. EthernetClient client;
  33.  
  34.  
  35. void setup()
  36. {    
  37.   // Start Serial for debugging on the Serial Monitor
  38.   Serial.begin(9600);  
  39.  
  40.   wdt_enable(WDTO_8S); // watchdog habilitado para 8 segundos
  41.   Serial.println("Iniciado...");
  42.   //Start Ethernet on Arduino
  43.   startEthernet();  
  44. }
  45.  
  46. void loop()
  47. {  
  48.   wdt_reset(); // reset do watcdog
  49.   // Print Update Response to Serial Monitor
  50.   if (client.available())
  51.   {
  52.     char c = client.read();
  53.     Serial.print(c); // exibe dados enviados pelo servidor
  54.   }
  55.   // Disconnect from ThingSpeak
  56.   if (!client.connected() && lastConnected)
  57.   {
  58.     Serial.println("...disconnected");
  59.     Serial.println();
  60.    
  61.     client.stop();
  62.   }  
  63.  
  64.  
  65.   // Update ThingSpeak
  66.   if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
  67.   {
  68.    
  69.    
  70.     DHT.read11(dht_dpin); //Lê as informações do sensor
  71.     String v_umidade = String(int(DHT.humidity)); //obtem umidade
  72.     String v_temperatura = String(int(DHT.temperature)); //obtem temperatura      
  73.     // exibe dados do sensor
  74.     Serial.print("Umidade = ");
  75.     Serial.print(v_umidade);
  76.     Serial.print(" %  ");
  77.     Serial.print("Temperatura = ");
  78.     Serial.print(v_temperatura);
  79.     Serial.println(" Celsius  ");
  80.     delay(2000);      
  81.    
  82.     // envia dados ao ThingSpeak
  83.     updateThingSpeak("field1=" + v_temperatura + "&field2=" + v_umidade);
  84.   }
  85.  
  86.   // Check if Arduino Ethernet needs to be restarted
  87.   if (failedCounter > 3 ) {startEthernet();}  
  88.   lastConnected = client.connected();
  89. }
  90.  
  91. void updateThingSpeak(String tsData)
  92. {
  93.   if (client.connect(thingSpeakAddress, 80))
  94.   {        
  95.     client.print("POST /update HTTP/1.1\n");
  96.     client.print("Host: api.thingspeak.com\n");
  97.     client.print("Connection: close\n");
  98.     client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
  99.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  100.     client.print("Content-Length: ");
  101.     client.print(tsData.length());
  102.     client.print("\n\n");
  103.     client.print(tsData);
  104.    
  105.     lastConnectionTime = millis();
  106.    
  107.     if (client.connected())
  108.     {
  109.       Serial.println("Connecting to ThingSpeak...");        
  110.       failedCounter = 0;
  111.     }
  112.     else
  113.     {
  114.       failedCounter++;  
  115.       Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");  
  116.       Serial.println();
  117.     }
  118.    
  119.   }
  120.   else
  121.   {
  122.     failedCounter++;
  123.    
  124.     Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");  
  125.     Serial.println();
  126.    
  127.     lastConnectionTime = millis();
  128.   }
  129. }
  130.  
  131. void startEthernet()
  132. {
  133.  
  134.   client.stop();
  135.   Serial.println("Connecting Arduino to network...");
  136.   Serial.println();  
  137.  
  138.   delay(1000);
  139.  
  140.   // Connect to network amd obtain an IP address using DHCP
  141.   if (Ethernet.begin(mac) == 0)
  142.   {
  143.     Serial.println("DHCP Failed, reset Arduino to try again");
  144.     Serial.println();
  145.   }
  146.   else
  147.   {
  148.     Serial.println("Arduino connected to network using DHCP");
  149.     Serial.println();
  150.   }
  151.  
  152.   delay(1000);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement