|
| 1 | +// #include <ESP8266WiFi.h> |
| 2 | +// #include <ESP8266WebServer.h> |
| 3 | +// #include <ArduinoJson.h> |
| 4 | +// #include <DHT.h> |
| 5 | + |
| 6 | +// #define DHTPIN 14 // D5 |
| 7 | + |
| 8 | +// // Uncomment whatever type you're using! |
| 9 | +// #define DHTTYPE DHT11 // DHT 11 |
| 10 | + |
| 11 | +// DHT dht(DHTPIN, DHTTYPE); |
| 12 | + |
| 13 | +// // JsonDocument jsonBuffer; |
| 14 | + |
| 15 | +// /*Put your SSID & Password*/ |
| 16 | +// const char* ssid = "pi"; // Enter SSID here |
| 17 | +// const char* password = "raspberry"; //Enter Password here |
| 18 | + |
| 19 | +// ESP8266WebServer server(80); |
| 20 | + |
| 21 | +// uint8_t LEDpin = 2; |
| 22 | +// bool LEDstatus = LOW; |
| 23 | + |
| 24 | +// void setup() { |
| 25 | +// Serial.begin(9600); |
| 26 | +// delay(100); |
| 27 | +// pinMode(LEDpin, OUTPUT); |
| 28 | + |
| 29 | +// Serial.println("Connecting to "); |
| 30 | +// Serial.println(ssid); |
| 31 | + |
| 32 | +// //connect to your local wi-fi network |
| 33 | +// WiFi.begin(ssid, password); |
| 34 | + |
| 35 | +// //check wi-fi is connected to wi-fi network |
| 36 | +// while (WiFi.status() != WL_CONNECTED) { |
| 37 | +// delay(1000); |
| 38 | +// Serial.print("."); |
| 39 | +// } |
| 40 | +// Serial.println(""); |
| 41 | +// Serial.println("WiFi connected..!"); |
| 42 | +// Serial.print("Got IP: "); Serial.println(WiFi.localIP()); |
| 43 | + |
| 44 | +// server.on("/", handle_OnConnect); |
| 45 | +// server.on("/ledon", handle_ledon); |
| 46 | +// server.on("/ledoff", handle_ledoff); |
| 47 | +// // server.on("/temp", handle_temp); |
| 48 | +// // server.on("/humidity", handle_humdity); |
| 49 | +// server.onNotFound(handle_NotFound); |
| 50 | + |
| 51 | +// server.begin(); |
| 52 | +// Serial.println("HTTP server started"); |
| 53 | +// } |
| 54 | + |
| 55 | +// void loop() { |
| 56 | +// server.handleClient(); |
| 57 | +// if(LEDstatus) |
| 58 | +// digitalWrite(LEDpin, HIGH); |
| 59 | +// else |
| 60 | +// digitalWrite(LEDpin, LOW); |
| 61 | +// } |
| 62 | + |
| 63 | +// // void handle_temp() { |
| 64 | +// // JsonObject root; |
| 65 | +// // root["temperature"] = dht2.readTemperature(); |
| 66 | +// // server.send(200, "application/json", ); |
| 67 | +// // } |
| 68 | + |
| 69 | +// void handle_OnConnect() { |
| 70 | +// LEDstatus = LOW; |
| 71 | +// server.send(200, "text/html", SendHTML(false)); |
| 72 | +// } |
| 73 | + |
| 74 | +// void handle_ledon() { |
| 75 | +// LEDstatus = HIGH; |
| 76 | +// server.send(200, "text/html", SendHTML(true)); |
| 77 | +// } |
| 78 | + |
| 79 | +// void handle_ledoff() { |
| 80 | +// LEDstatus = LOW; |
| 81 | +// server.send(200, "text/html", SendHTML(false)); |
| 82 | +// } |
| 83 | + |
| 84 | +// void handle_NotFound(){ |
| 85 | +// server.send(404, "text/plain", "Not found"); |
| 86 | +// } |
| 87 | + |
| 88 | +// String SendHTML(uint8_t led){ |
| 89 | +// String ptr = "<!DOCTYPE html>\n"; |
| 90 | +// ptr +="<html>\n"; |
| 91 | +// ptr +="<head>\n"; |
| 92 | +// ptr +="<title>LED Control</title>\n"; |
| 93 | +// ptr +="</head>\n"; |
| 94 | +// ptr +="<body>\n"; |
| 95 | +// ptr +="<h1>LED</h1>\n"; |
| 96 | +// ptr +="<p>Click to switch LED on and off.</p>\n"; |
| 97 | +// ptr +="<form method=\"get\">\n"; |
| 98 | +// if(led) |
| 99 | +// ptr +="<input type=\"button\" value=\"LED ON\" onclick=\"window.location.href='/ledoff'\">\n"; |
| 100 | +// else |
| 101 | +// ptr +="<input type=\"button\" value=\"LED OFF\" onclick=\"window.location.href='/ledon'\">\n"; |
| 102 | +// ptr +="</form>\n"; |
| 103 | +// ptr +="</body>\n"; |
| 104 | +// ptr +="</html>\n"; |
| 105 | +// return ptr; |
| 106 | +// } |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +#include <ESP8266WiFi.h> |
| 111 | +#include <ESP8266WebServer.h> |
| 112 | +#include <ArduinoJson.h> |
| 113 | +#include <DHT.h> |
| 114 | + |
| 115 | +#include <Wire.h> |
| 116 | + |
| 117 | +#include <LiquidCrystal_I2C.h> |
| 118 | + |
| 119 | +LiquidCrystal_I2C lcd(0x3F, 16, 2); |
| 120 | + |
| 121 | +#define DHTPIN 14 // D5 |
| 122 | +#define DHTTYPE DHT11 // DHT 11 |
| 123 | + |
| 124 | +DHT dht(DHTPIN, DHTTYPE); |
| 125 | + |
| 126 | +const char* ssid = "pi"; // Enter SSID here |
| 127 | +const char* password = "raspberry"; //Enter Password here |
| 128 | + |
| 129 | +ESP8266WebServer server(80); |
| 130 | + |
| 131 | +uint8_t LEDpin = 2; |
| 132 | +bool LEDstatus = LOW; |
| 133 | + |
| 134 | +void setup() { |
| 135 | + Serial.begin(9600); |
| 136 | + delay(100); |
| 137 | + pinMode(LEDpin, OUTPUT); |
| 138 | + Wire.begin(2,0); |
| 139 | + lcd.init(); // initializing the LCD |
| 140 | + lcd.backlight(); // Enable or Turn On the backlight |
| 141 | + lcd.print(" Hello Makers "); // Start Printing |
| 142 | + |
| 143 | + // Initialize DHT sensor |
| 144 | + dht.begin(); |
| 145 | + |
| 146 | + // Connect to WiFi |
| 147 | + Serial.println("Connecting to " + String(ssid)); |
| 148 | + WiFi.begin(ssid, password); |
| 149 | + |
| 150 | + while (WiFi.status() != WL_CONNECTED) { |
| 151 | + delay(1000); |
| 152 | + Serial.print("."); |
| 153 | + } |
| 154 | + |
| 155 | + Serial.println(""); |
| 156 | + Serial.println("WiFi connected..!"); |
| 157 | + Serial.print("Got IP: "); |
| 158 | + Serial.println(WiFi.localIP()); |
| 159 | + |
| 160 | + // Setup HTTP server endpoints |
| 161 | + server.on("/", handle_OnConnect); |
| 162 | + server.on("/ledon", handle_ledon); |
| 163 | + server.on("/ledoff", handle_ledoff); |
| 164 | + server.on("/temp", handle_temp); |
| 165 | + server.on("/humidity", handle_humidity); |
| 166 | + server.onNotFound(handle_NotFound); |
| 167 | + |
| 168 | + server.begin(); |
| 169 | + Serial.println("HTTP server started"); |
| 170 | +} |
| 171 | + |
| 172 | +void loop() { |
| 173 | + server.handleClient(); |
| 174 | + |
| 175 | + // Control LED based on LEDstatus |
| 176 | + digitalWrite(LEDpin, LEDstatus ? HIGH : LOW); |
| 177 | +} |
| 178 | + |
| 179 | +void handle_temp() { |
| 180 | + float temperature = dht.readTemperature(); |
| 181 | + if (isnan(temperature)) { |
| 182 | + Serial.println("Failed to read temperature from DHT sensor!"); |
| 183 | + server.send(500, "text/plain", "Failed to read temperature from DHT sensor!"); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + Serial.print("Temperature: "); |
| 188 | + Serial.println(temperature); |
| 189 | + |
| 190 | + StaticJsonDocument<200> doc; |
| 191 | + doc["temperature"] = temperature; |
| 192 | + |
| 193 | + String output; |
| 194 | + serializeJson(doc, output); |
| 195 | + server.send(200, "application/json", output); |
| 196 | +} |
| 197 | + |
| 198 | +void handle_humidity() { |
| 199 | + float humidity = dht.readHumidity(); |
| 200 | + if (isnan(humidity)) { |
| 201 | + Serial.println("Failed to read humidity from DHT sensor!"); |
| 202 | + server.send(500, "text/plain", "Failed to read humidity from DHT sensor!"); |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + Serial.print("Humidity: "); |
| 207 | + Serial.println(humidity); |
| 208 | + |
| 209 | + StaticJsonDocument<200> doc; |
| 210 | + doc["humidity"] = humidity; |
| 211 | + |
| 212 | + String output; |
| 213 | + serializeJson(doc, output); |
| 214 | + server.send(200, "application/json", output); |
| 215 | +} |
| 216 | + |
| 217 | +void handle_OnConnect() { |
| 218 | + LEDstatus = LOW; |
| 219 | + server.send(200, "text/html", SendHTML(false)); |
| 220 | +} |
| 221 | + |
| 222 | +void handle_ledon() { |
| 223 | + LEDstatus = HIGH; |
| 224 | + server.send(200, "text/html", SendHTML(true)); |
| 225 | +} |
| 226 | + |
| 227 | +void handle_ledoff() { |
| 228 | + LEDstatus = LOW; |
| 229 | + server.send(200, "text/html", SendHTML(false)); |
| 230 | +} |
| 231 | + |
| 232 | +void handle_NotFound(){ |
| 233 | + server.send(404, "text/plain", "Not found"); |
| 234 | +} |
| 235 | + |
| 236 | +String SendHTML(uint8_t led){ |
| 237 | + String ptr = "<!DOCTYPE html>\n"; |
| 238 | + ptr +="<html>\n"; |
| 239 | + ptr +="<head>\n"; |
| 240 | + ptr +="<title>LED Control</title>\n"; |
| 241 | + ptr +="</head>\n"; |
| 242 | + ptr +="<body>\n"; |
| 243 | + ptr +="<h1>LED</h1>\n"; |
| 244 | + ptr +="<p>Click to switch LED on and off.</p>\n"; |
| 245 | + ptr +="<form method=\"get\">\n"; |
| 246 | + if(led) |
| 247 | + ptr +="<input type=\"button\" value=\"LED ON\" onclick=\"window.location.href='/ledoff'\">\n"; |
| 248 | + else |
| 249 | + ptr +="<input type=\"button\" value=\"LED OFF\" onclick=\"window.location.href='/ledon'\">\n"; |
| 250 | + ptr +="</form>\n"; |
| 251 | + ptr +="<p><a href=\"/temp\">Get Temperature</a></p>\n"; |
| 252 | + ptr +="<p><a href=\"/humidity\">Get Humidity</a></p>\n"; |
| 253 | + ptr +="</body>\n"; |
| 254 | + ptr +="</html>\n"; |
| 255 | + return ptr; |
| 256 | +} |
0 commit comments