RuiViana

CK_Teste

Aug 25th, 2016
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // www.arduinesp.com
  2. //
  3. // Plot DTH11 data on thingspeak.com using an ESP8266
  4. // April 11 2015
  5. // Author: Jeroen Beemster
  6. // Website: www.arduinesp.com
  7.  
  8.  
  9. #include <DHT.h>
  10. //#include <ESP8266WiFi.h>
  11.  
  12. // replace with your channel's thingspeak API key,
  13. String apiKey = "4XZ9NA14HQZ7BYD7";
  14. const char* ssid = "ssid";
  15. const char* password = "password";
  16.  
  17. const char* server = "api.thingspeak.com";
  18. #define DHTPIN 2 // what pin we're connected to
  19.  
  20. DHT dht(DHTPIN, DHT11,15);
  21. //WiFiClient client;
  22.  
  23.  
  24. void setup() {
  25. Serial.begin(115200);
  26. delay(10);
  27. dht.begin();
  28.  
  29. // WiFi.begin(ssid, password);
  30.  
  31. Serial.println();
  32. Serial.println();
  33. Serial.print("Connecting to ");
  34. Serial.println(ssid);
  35.  
  36. // WiFi.begin(ssid, password);
  37.  
  38. // while (WiFi.status() != WL_CONNECTED) {
  39. // delay(500);
  40. // Serial.print(".");
  41. // }
  42. Serial.println("");
  43. Serial.println("WiFi connected");
  44.  
  45. }
  46.  
  47.  
  48. void loop() {
  49.  
  50. float h = dht.readHumidity();
  51. float t = dht.readTemperature();
  52. if (isnan(h) || isnan(t)) {
  53. Serial.println("Failed to read from DHT sensor!");
  54. return;
  55. }
  56.  
  57. /* if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
  58. String postStr = apiKey;
  59. postStr +="&field1=";
  60. postStr += String(t);
  61. postStr +="&field2=";
  62. postStr += String(h);
  63. postStr += "\r\n\r\n";
  64.  
  65. client.print("POST /update HTTP/1.1\n");
  66. client.print("Host: api.thingspeak.com\n");
  67. client.print("Connection: close\n");
  68. client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  69. client.print("Content-Type: application/x-www-form-urlencoded\n");
  70. client.print("Content-Length: ");
  71. client.print(postStr.length());
  72. client.print("\n\n");
  73. client.print(postStr);
  74.  
  75.  
  76. Serial.print("Temperature: ");
  77. Serial.print(t);
  78. Serial.print(" degrees Celcius Humidity: ");
  79. Serial.print(h);
  80. Serial.println("% send to Thingspeak");
  81. }*/
  82. // client.stop();
  83.  
  84. Serial.println("Waiting...");
  85. // thingspeak needs minimum 15 sec delay between updates
  86. delay(20000);
  87. }
RAW Paste Data