manhoosbilli1

esp thingspeak

Nov 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1.  sensors.begin();#include <WiFi.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <Arduino.h>
  5. #include "DHT.h"
  6. #define DHTPIN 32
  7. #define oneWireBus  33
  8. #define DHTTYPE DHT22
  9. #define moistPin  35
  10. #define LDRPin  34
  11. int LDRValue = 0;
  12. int moistVal;
  13. float soilTemp, t, h, moisture, light;
  14. DHT dht(DHTPIN, DHTTYPE);
  15. OneWire oneWire(oneWireBus);
  16. DallasTemperature sensors(&oneWire);
  17.  
  18. String apiKey = "I0ESQ19COY10AIUJ";                  //  Enter your Write API key from ThingSpeak
  19. const char *ssid =  "Mi A2 Lite-Shoaib";                                    // replace with your wifi ssid and wpa2 key
  20. const char *pass =  "D01234567890";
  21. const char* server = "api.thingspeak.com";
  22.  
  23.  
  24. WiFiClient client;
  25. void setup()
  26. {
  27.   Serial.begin(115200);
  28.   delay(10);
  29.   dht.begin();
  30.   sensors.begin();
  31.   Serial.println("Connecting to ");
  32.   Serial.println(ssid);
  33.   WiFi.begin(ssid, pass);
  34.   while (WiFi.status() != WL_CONNECTED)
  35.   {
  36.     delay(500);
  37.     Serial.print(".");
  38.   }
  39.   Serial.println("");
  40.   Serial.println("WiFi connected");
  41.  
  42. }
  43. void loop() {
  44.   getSensorValues();
  45.   delay(15000);
  46.   if (client.connect(server, 80))                                //   "184.106.153.149" or api.thingspeak.com
  47.   {
  48.     Serial.println("Connected to server");
  49.     String postStr = apiKey;
  50.     postStr += "&field1=";
  51.     postStr += String(soilTemp);
  52.     postStr += "&field2=";
  53.     postStr += String(t);
  54.     postStr += "&field3=";
  55.     postStr += String(h);
  56.     postStr += "&field4=";
  57.     postStr += String(moisture);
  58.     postStr += "&field5=";
  59.     postStr += String(light);
  60.     postStr += "\r\n\r\n";
  61.  
  62.     client.print("POST /update HTTP/1.1\n");
  63.     client.print("Host: api.thingspeak.com\n");
  64.     client.print("Connection: close\n");
  65.     client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  66.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  67.     client.print("Content-Length: ");
  68.     client.print(postStr.length());
  69.     client.print("\n\n");
  70.     client.print(postStr);
  71.     Serial.println(postStr);
  72.     Serial.println("Posted");
  73.    
  74.  
  75.   }
  76.   client.stop();
  77. }
  78.  
  79.  
  80. void getSensorValues() {
  81.   sensors.requestTemperatures();
  82.   soilTemp = sensors.getTempCByIndex(0);
  83.   h = dht.readHumidity();
  84.   t = dht.readTemperature();
  85.   if (isnan(h) || isnan(t)) {
  86.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  87.     return;
  88.   }
  89.   light = analogRead(LDRPin); // read the value from the LDR
  90.   moisture = analogRead(moistPin);
  91. }
Add Comment
Please, Sign In to add comment