Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <dht.h>
  5. //#define DEBUG
  6.  
  7. //Wifi SSID and password respectively
  8. const char* ssid = "Nxtkh";
  9. const char* password = "glnf5265";
  10. const char* IP_ADDR = "192.168.43.168:8000/plants";
  11.  
  12. WiFiServer server(80);
  13.  
  14. dht DHT;
  15.  
  16. #define DHT11_PIN 13
  17. #define SOIL_PIN A0
  18. int soilMoistureValue = 0;
  19.  
  20. void setup(){
  21.   delay(1000);
  22.   Serial.begin(115200);
  23.   delay(10);
  24.   WiFi.mode(WIFI_OFF);
  25.   delay(1000);
  26.   WiFi.mode(WIFI_STA);
  27.  
  28.   while(!Serial);
  29.  
  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.  
  43.  
  44.   Serial.println("");
  45.   Serial.println("WiFi connected");
  46. }
  47.  
  48. void loop()
  49. {
  50.   HTTPClient http;
  51.   int chk = DHT.read11(DHT11_PIN);
  52.   soilMoistureValue = analogRead(SOIL_PIN);
  53.   #ifdef DEBUG
  54.     Serial.print("Temperature = ");
  55.     Serial.println(DHT.temperature);
  56.     Serial.print("Humidity = ");
  57.     Serial.println(DHT.humidity);
  58.     Serial.println(soilMoistureValue);
  59.   #endif
  60.  
  61.  
  62.    StaticJsonDocument<200> doc;
  63.  
  64.   doc["id"]=1;
  65.   doc["temperature"]=DHT.temperature;
  66.   doc["humidity"]=DHT.humidity;
  67.   doc["moisture"]=soilMoistureValue;
  68.  
  69.   http.begin(IP_ADDR);
  70.   http.addHeader("Content-Type", "application/json");
  71.  
  72.   String postData = "";
  73.   serializeJson(doc, postData);
  74.   int httpCode = http.POST(postData);
  75.   String response = http.getString();
  76.   http.end();
  77.   #ifdef DEBUG
  78.     Serial.print("HTTP Code: ");
  79.     Serial.println(httpCode);
  80.     Serial.print("Response: ");
  81.     Serial.println(response);
  82.   #endif
  83.   delay(5000);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement