Advertisement
manhoosbilli1

esp-32 post to thingspeak

Nov 22nd, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <WiFi.h>                  //already included in esp32 package
  4. #include "DHT.h"
  5.  
  6. #define DHTPIN 32
  7. #define oneWireBus  33
  8. #define DHTTYPE DHT22
  9. #define moistPin  35
  10. #define LDRPin  34
  11. DHT dht(DHTPIN, DHTTYPE);
  12. OneWire oneWire(oneWireBus);
  13. DallasTemperature sensors(&oneWire);
  14. String apiKey = "BE7U15RJYY57AV91";                  //  Enter your Write API key from ThingSpeak
  15. const char *ssid =  "SSID";                                    // replace with your wifi ssid and wpa2 key
  16. const char *pass =  "PASSWORD";
  17. const char* server = "api.thingspeak.com";           //don't change
  18. float soilTemperature, h, t, lightIntensity, soilMoisture;
  19. WiFiClient client;
  20. void setup()
  21. {
  22.   Serial.begin(115200);
  23.   delay(10);
  24.   Serial.begin(115200);
  25.   sensors.begin();  //starts ds18b20
  26.   dht.begin();   //give 2 second delay to all sensors
  27.   Serial.println("Connecting to ");
  28.   Serial.println(ssid);
  29.   WiFi.begin(ssid, pass);
  30.   while (WiFi.status() != WL_CONNECTED)
  31.   {
  32.     delay(500);
  33.     Serial.print(".");
  34.   }
  35.   Serial.println("");
  36.   Serial.println("WiFi connected");
  37.  
  38. }
  39. void loop()
  40. {
  41.   if (client.connect(server, 80))                                //   "184.106.153.149" or api.thingspeak.com
  42.   {
  43.  
  44.     String postStr = apiKey;                          //keep the apikey variable unchanged just add fields and values
  45.     postStr += "&field1=";
  46.     postStr += String(h);
  47.     postStr += "&field2=";
  48.     postStr += String(t);
  49.     postStr += "&field3=";
  50.     postStr += String(soilTemperature);
  51.     postStr += "&field4=";
  52.     postStr += String(lightIntensity);
  53.     postStr += "&field5=";
  54.     postStr += String(soilMoisture);
  55.     postStr += "\r\n\r\n";
  56.  
  57.     client.print("POST /update HTTP/1.1\n");               //keep this part unchanged
  58.     client.print("Host: api.thingspeak.com\n");
  59.     client.print("Connection: close\n");
  60.     client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  61.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  62.     client.print("Content-Length: ");
  63.     client.print(postStr.length());
  64.     client.print("\n\n");
  65.     client.print(postStr);
  66.     client.stop();
  67.     Serial.println("Waiting...");
  68.     delay(30000);                                      //the website accepts values in 30 seconds so we add a delay of 30 seconds before loop restarts
  69.     getSensorValues();
  70.   }
  71. }
  72.  
  73.  
  74.   void getSensorValues() {
  75.     sensors.requestTemperatures();
  76.     float soilTemp = sensors.getTempCByIndex(0);
  77.     float h = dht.readHumidity();
  78.     t = dht.readTemperature();
  79.     if (isnan(h) || isnan(t)) {
  80.       Serial.println(F("Failed to read from DHT sensor!")); //print to server
  81.       return;
  82.     }
  83.     lightIntensity = analogRead(LDRPin); // read the value from the LDR
  84.     soilMoisture = analogRead(moistPin);
  85.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement