Advertisement
manhoosbilli1

esp-32 post sensor data to esp server

Nov 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WebServer.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <Arduino.h>
  5. #include <WiFi.h>
  6. #include "DHT.h"
  7. const char* ssid = "PTCL-BB1";
  8. const char* password = "shoaibmectec";
  9. #define DHTPIN 32
  10. #define oneWireBus  33
  11. #define DHTTYPE DHT22
  12. #define moistPin  35
  13. #define LDRPin  34
  14. OneWire oneWire(oneWireBus);
  15. DallasTemperature sensors(&oneWire);
  16. String postData;
  17. String postVariable = "temp=";
  18. int LDRValue = 0;
  19. int moistVal;
  20. String values;
  21. unsigned long currentMillis;
  22. unsigned long previousMillis;
  23. const int interval = 2500;
  24. DHT dht(DHTPIN, DHTTYPE);
  25. float t, h, moisture, light;
  26. WebServer server(80);
  27.  
  28. void setup() {
  29.   Serial.begin(115200);
  30.   delay(100);
  31.   sensors.begin();  //starts ds18b20
  32.   dht.begin();   //give 2 second delay to all sensors
  33.   Serial.println("Connecting to ");
  34.   Serial.println(ssid);
  35.  
  36.   WiFi.begin(ssid, password);
  37.  
  38.   //check wi-fi is connected to wi-fi network
  39.   while (WiFi.status() != WL_CONNECTED) {
  40.     delay(1000);
  41.     Serial.print(".");
  42.   }
  43.   Serial.println("");
  44.   Serial.println("WiFi connected..!");
  45.   Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  46.  
  47.   server.on("/", handle_OnConnect);
  48.   server.onNotFound(handle_NotFound);
  49.  
  50.   server.begin();
  51.   Serial.println("HTTP server started");
  52.  
  53. }
  54. void loop() {
  55.  
  56.   server.handleClient();
  57. }
  58.  
  59.  
  60. void handle_OnConnect() {
  61.   h = dht.readHumidity();
  62.   t = dht.readTemperature();
  63.   if (isnan(h) || isnan(t)) {
  64.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  65.     return;
  66.   }
  67.   LDRValue = analogRead(LDRPin); // read the value from the LDR
  68.   light = LDRValue;
  69.   moistVal = analogRead(moistPin);
  70.   moisture = moistVal;
  71.   delay(2000);
  72.   server.send(200, "text/html", SendHTML(t, h, moisture, light));
  73. }
  74.  
  75. void handle_NotFound() {
  76.   server.send(404, "text/plain", "Not found");
  77. }
  78.  
  79. String SendHTML(float t, float h, float moisture, float light) {
  80.   String ptr = "<!DOCTYPE html> <html>\n";
  81.   ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  82.   ptr += "<title>ESP32 Weather Station</title>\n";
  83.   ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  84.   ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  85.   ptr += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  86.   ptr += "</style>\n";
  87.   ptr += "<script>\n";
  88.   ptr += "setInterval(loadDoc,1000);\n";
  89.   ptr += "function loadDoc() {\n";
  90.   ptr += "var xhttp = new XMLHttpRequest();\n";
  91.   ptr += "xhttp.onreadystatechange = function() {\n";
  92.   ptr += "if (this.readyState == 4 && this.status == 200) {\n";
  93.   ptr += "document.body.innerHTML =this.responseText}\n";
  94.   ptr += "};\n";
  95.   ptr += "xhttp.open(\"GET\", \"/\", true);\n";
  96.   ptr += "xhttp.send();\n";
  97.   ptr += "}\n";
  98.   ptr += "</script>\n";
  99.   ptr += "</head>\n";
  100.   ptr += "<body>\n";
  101.   ptr += "<div id=\"webpage\">\n";
  102.   ptr += "<h1>ESP32 Weather Station</h1>\n";
  103.   ptr += "<p>Temperature: ";
  104.   ptr += t;
  105.   ptr += "&deg;C</p>";
  106.   ptr += "<p>Humidity: ";
  107.   ptr += h;
  108.   ptr += "%</p>";
  109.   ptr += "<p>moisture: ";
  110.   ptr += moisture;
  111.   ptr += "</p>";
  112.   ptr += "<p>light: ";
  113.   ptr += light;
  114.   ptr += "</p>";
  115.   ptr += "</div>\n";
  116.   ptr += "</body>\n";
  117.   ptr += "</html>\n";
  118.   return ptr;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement