Advertisement
Guest User

Untitled

a guest
May 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266HTTPClient.h>
  4.  
  5. String apiKey = "L7U75MY7WHC9AEKU";
  6. const char* ssid = "dd-wrt";
  7. const char* password = "mobilne2018";
  8. const char* server = "https://dweet.io/dweet/for/subsequent-shoe?";
  9.  
  10. #define DHTPIN D2
  11. #define DHTTYPE DHT11
  12.  
  13. DHT dht(DHTPIN, DHTTYPE);
  14. WiFiClient client;
  15.  
  16. void setup()
  17. {
  18. Serial.begin(115200);
  19. delay(10);
  20. dht.begin();
  21.  
  22. WiFi.begin(ssid, password);
  23.  
  24. Serial.println();
  25. Serial.println();
  26. Serial.print("Connecting to ");
  27. Serial.println(ssid);
  28.  
  29. WiFi.begin(ssid, password);
  30.  
  31. while (WiFi.status() != WL_CONNECTED)
  32. {
  33. delay(500);
  34. Serial.print(".");
  35. }
  36. Serial.println("");
  37. Serial.println("WiFi connected");
  38.  
  39. }
  40.  
  41. void loop()
  42. {
  43.  
  44. float h = dht.readHumidity();
  45. float t = dht.readTemperature();
  46. if (isnan(h) || isnan(t))
  47. {
  48. Serial.println( h );
  49. Serial.println(t);
  50. Serial.println("Failed to read from DHT sensor!");
  51. return;
  52. }
  53.  
  54.  
  55. String sf(t, 2);
  56. String sh(h, 2);
  57.  
  58. HTTPClient http; //Declare object of class HTTPClient
  59.  
  60. http.begin("http://dweet.io/dweet/for/subsequent-shoe?temp=" + sf + "&humidity=" + sh);
  61. http.addHeader("Content-Type", "text/plain"); //Specify content-type header
  62.  
  63. int httpCode = http.POST(""); //Send the request
  64. String payload = http.getString(); //Get the response payload
  65.  
  66. Serial.println(httpCode); //Print HTTP return code
  67. Serial.println(payload); //Print request response payload
  68.  
  69. http.end(); //Close connection
  70.  
  71. if (client.connect(server,80)) {
  72. String postStr = apiKey;
  73. postStr +="&field1=";
  74. postStr += String(t);
  75. postStr +="&field2=";
  76. postStr += String(h);
  77. postStr += "\r\n\r\n";
  78.  
  79. client.print("POST /dweet/for/subsequent-shoe?field1=" + String(t) + "&field2=" + String(h) + " HTTP/1.1\n");
  80. client.print("Host: https://dweet.io\n");
  81. client.print("Connection: close\n");
  82. //client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  83. //client.print("Content-Type: application/json\n");
  84. //client.print("Content-Length: ");
  85. //client.print(postStr.length());
  86. client.print("\n\n");
  87. //client.print(postStr);
  88.  
  89. Serial.print("Temperature: ");
  90. Serial.print(t);
  91. Serial.print(" degrees Celsius Humidity: ");
  92. Serial.print(h);
  93. Serial.println("Sending data to Thingspeak");
  94. }
  95. client.stop();
  96.  
  97. Serial.println("Waiting 20 secs");
  98. // thingspeak needs at least a 15 sec delay between updates
  99. // 20 seconds to be safe
  100. delay(20000);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement