Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WebServer.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Arduino.h>
- #include <WiFi.h>
- #include "DHT.h"
- const char* ssid = "PTCL-BB1";
- const char* password = "shoaibmectec";
- #define DHTPIN 32
- #define oneWireBus 33
- #define DHTTYPE DHT22
- #define moistPin 35
- #define LDRPin 34
- OneWire oneWire(oneWireBus);
- DallasTemperature sensors(&oneWire);
- String postData;
- String postVariable = "temp=";
- int LDRValue = 0;
- int moistVal;
- String values;
- unsigned long currentMillis;
- unsigned long previousMillis;
- const int interval = 2500;
- DHT dht(DHTPIN, DHTTYPE);
- float t, h, moisture, light;
- WebServer server(80);
- void setup() {
- Serial.begin(115200);
- delay(100);
- sensors.begin(); //starts ds18b20
- dht.begin(); //give 2 second delay to all sensors
- Serial.println("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- //check wi-fi is connected to wi-fi network
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected..!");
- Serial.print("Got IP: "); Serial.println(WiFi.localIP());
- server.on("/", handle_OnConnect);
- server.onNotFound(handle_NotFound);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- server.handleClient();
- }
- void handle_OnConnect() {
- h = dht.readHumidity();
- t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println(F("Failed to read from DHT sensor!")); //print to server
- return;
- }
- LDRValue = analogRead(LDRPin); // read the value from the LDR
- light = LDRValue;
- moistVal = analogRead(moistPin);
- moisture = moistVal;
- delay(2000);
- server.send(200, "text/html", SendHTML(t, h, moisture, light));
- }
- void handle_NotFound() {
- server.send(404, "text/plain", "Not found");
- }
- String SendHTML(float t, float h, float moisture, float light) {
- String ptr = "<!DOCTYPE html> <html>\n";
- ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
- ptr += "<title>ESP32 Weather Station</title>\n";
- ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
- ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
- ptr += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
- ptr += "</style>\n";
- ptr += "<script>\n";
- ptr += "setInterval(loadDoc,1000);\n";
- ptr += "function loadDoc() {\n";
- ptr += "var xhttp = new XMLHttpRequest();\n";
- ptr += "xhttp.onreadystatechange = function() {\n";
- ptr += "if (this.readyState == 4 && this.status == 200) {\n";
- ptr += "document.body.innerHTML =this.responseText}\n";
- ptr += "};\n";
- ptr += "xhttp.open(\"GET\", \"/\", true);\n";
- ptr += "xhttp.send();\n";
- ptr += "}\n";
- ptr += "</script>\n";
- ptr += "</head>\n";
- ptr += "<body>\n";
- ptr += "<div id=\"webpage\">\n";
- ptr += "<h1>ESP32 Weather Station</h1>\n";
- ptr += "<p>Temperature: ";
- ptr += t;
- ptr += "°C</p>";
- ptr += "<p>Humidity: ";
- ptr += h;
- ptr += "%</p>";
- ptr += "<p>moisture: ";
- ptr += moisture;
- ptr += "</p>";
- ptr += "<p>light: ";
- ptr += light;
- ptr += "</p>";
- ptr += "</div>\n";
- ptr += "</body>\n";
- ptr += "</html>\n";
- return ptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement