Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- String apiKey = "L7U75MY7WHC9AEKU";
- const char* ssid = "dd-wrt";
- const char* password = "mobilne2018";
- const char* server = "https://dweet.io/dweet/for/subsequent-shoe?";
- #define DHTPIN D2
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- WiFiClient client;
- void setup()
- {
- Serial.begin(115200);
- delay(10);
- dht.begin();
- WiFi.begin(ssid, password);
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- }
- void loop()
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t))
- {
- Serial.println( h );
- Serial.println(t);
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- String sf(t, 2);
- String sh(h, 2);
- HTTPClient http; //Declare object of class HTTPClient
- http.begin("http://dweet.io/dweet/for/subsequent-shoe?temp=" + sf + "&humidity=" + sh);
- http.addHeader("Content-Type", "text/plain"); //Specify content-type header
- int httpCode = http.POST(""); //Send the request
- String payload = http.getString(); //Get the response payload
- Serial.println(httpCode); //Print HTTP return code
- Serial.println(payload); //Print request response payload
- http.end(); //Close connection
- if (client.connect(server,80)) {
- String postStr = apiKey;
- postStr +="&field1=";
- postStr += String(t);
- postStr +="&field2=";
- postStr += String(h);
- postStr += "\r\n\r\n";
- client.print("POST /dweet/for/subsequent-shoe?field1=" + String(t) + "&field2=" + String(h) + " HTTP/1.1\n");
- client.print("Host: https://dweet.io\n");
- client.print("Connection: close\n");
- //client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
- //client.print("Content-Type: application/json\n");
- //client.print("Content-Length: ");
- //client.print(postStr.length());
- client.print("\n\n");
- //client.print(postStr);
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" degrees Celsius Humidity: ");
- Serial.print(h);
- Serial.println("Sending data to Thingspeak");
- }
- client.stop();
- Serial.println("Waiting 20 secs");
- // thingspeak needs at least a 15 sec delay between updates
- // 20 seconds to be safe
- delay(20000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement