NodeMCU Clock with weather,Time and alot more
Hey, guys today I made one of the best Nodemcu clock which is budget friendly it shows like corona count, time, date, year and weather
Components and supplies
1
Solderless Breadboard Half Size
1
Battery, 9 V
1
Alphanumeric LCD, 16 x 2
1
DHT11 Temperature & Humidity Sensor (4 pins)
1
Arduino UNO
Tools and machines
1
Wire Cutter / Stripper, 5.25 " Overall Length
Apps and platforms
1
Arduino IDE
1
ThingSpeak API
Project description
Code
The code
c_cpp
1 #include <ESP8266WiFi.h> //the library used //Use ESP8266 functions 2 #include <ESP8266HTTPClient.h> 3 #include <LiquidCrystal_I2C.h> 4 #include "WiFiUdp.h" 5 #include "NTPClient.h" 6 #include <ArduinoJson.h> 7 #include <ESPAsyncTCP.h> 8#include <ESPAsyncWebServer.h> 9#include <dht11.h> 10#define DHT11PIN 14// defining the pin for dht11 sensor 11 12dht11 DHT11; 13AsyncWebServer server(80);//starting the server for the custom message display feature 14 const long utcOffsetInSeconds = 19800;//change according to your timezone 15char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 16String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};//for the months and day feature do not change 17 18// Define NTP Client to get time 19WiFiUDP ntpUDP; 20NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); 21 22 #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier 23 24 LiquidCrystal_I2C lcd(0x27,16,2); 25 String APIKEY = ""; //open weather api key 26String CityID = "1273294"; //Your City ID 27 28 29 const char* ssid = "wifi name"; //WIFI SSID Name 30 const char* password = "wifi password"; //WIFI Password 31 const char* host = "api.thingspeak.com"; //We read the data from this host 32 const int httpPortRead = 80; 33 34 const char* url1 = "/apps/thinghttp/send_request?api_key=5WX50KDX3R0IBCXQ"; //Change this URL Cases 35 const char* url2 = "/apps/thinghttp/send_request?api_key=AHZYGT5QSWRDGCE5"; //Deaths 36 const char* url3 = "/apps/thinghttp/send_request?api_key=KCHIW1XDQ4J8G3EE"; //Recovered 37 int To_remove; //There are some irrelevant data on the string and here's how I keep the index 38 //of those characters 39 String Cases,Deaths,Recovered,Time,Data_Raw,Data_Raw_1,Data_Raw_2,Data_Raw_3,Data_Raw_4,Data_Raw_5; //Here I keep the numbers that I got 40 41 WiFiClient client; //Create a WiFi client and http client 42 HTTPClient http; 43 char servername[]="api.openweathermap.org"; // remote server we will connect to 44String result; 45 46int counter = 60; 47 48String weatherDescription =""; 49String weatherLocation = ""; 50String Country; 51String cases = "cases"; 52float Temperature; 53float Humidity; 54float Pressure; 55String inputMessage="request->getParam(PARAM_INPUT_1)->value()"; 56String inputMessage2="request->getParam(PARAM_INPUT_2)->value()"; 57String inputMessage3="request->getParam(PARAM_INPUT_3)->value()"; 58String inputMessage4="request->getParam(PARAM_INPUT_4)->value()";//for the custom message display 59 const char* PARAM_INPUT_1 = "input1"; 60 const char* PARAM_INPUT_2 = "Alarm Hours"; 61 const char* PARAM_INPUT_3 = "Alarm Minutes"; 62 const char* PARAM_INPUT_4 = "Select Screen to display"; 63 const char index_html[] PROGMEM = R"rawliteral( 64<!DOCTYPE HTML><html><head> 65 <title>LCD CLOCK CONTROL PANNEL</title> 66 <meta name="viewport" content="width=device-width, initial-scale=1"> 67 </head><body> 68 <form action="/get"> 69 input1: <input type="text" name="input1"> 70 <input type="submit" value="Submit"> 71 </form><br> 72 <form action="/get"> 73 Alarm Hours: <input type="text" name="Alarm Hours"> 74 <input type="submit" value="Submit"> 75 </form><br> 76 <form action="/get"> 77 Alarm Minutes: <input type="text" name="Alarm Minutes"> 78 <input type="submit" value="Submit"> 79 </form><br> 80 <form action="/get"> 81 Select Screen to show: <input type="text" name="Select Screen to display"> 82 <input type="submit" value="Submit"> 83 </form><br> 84 </body></html>)rawliteral"; 85 void notFound(AsyncWebServerRequest *request) { 86 request->send(404, "text/plain", "Not found");//making the webserver for the custom message display 87} 88 89 void setup() { 90 lcd.begin(16, 2); 91 lcd.init(); 92 lcd.backlight(); 93 lcd.setCursor(1, 0); 94 lcd.print("SMART CLOCK"); //Change Your Country Name 95 lcd.setCursor(1, 1); 96 lcd.print("EEP"); //Change Name if you want 97 98 Serial.begin(115200); 99 WiFi.disconnect(); //Disconnect and reconnect to the Wifi you set 100 delay(1000); 101 WiFi.begin(ssid, password); 102 Serial.println("Connected to the WiFi network"); //Display feedback on the serial monitor 103 Serial.println(WiFi.localIP()); 104 timeClient.begin(); 105 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ 106 request->send_P(200, "text/html", index_html); 107 }); 108 109 // Send a GET request to <ESP_IP>/get?input1=<inputMessage> 110 server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { 111 String inputParam; 112 // GET input1 value on <ESP_IP>/get?input1=<inputMessage> 113 if (request->hasParam(PARAM_INPUT_1)) { 114 inputMessage = request->getParam(PARAM_INPUT_1)->value(); 115 inputParam = PARAM_INPUT_1; 116 } 117 else if (request->hasParam(PARAM_INPUT_2)) { 118 inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); 119 inputParam = PARAM_INPUT_2; 120 } 121 else if (request->hasParam(PARAM_INPUT_3)) { 122 inputMessage3 = request->getParam(PARAM_INPUT_3)->value(); 123 inputParam = PARAM_INPUT_3; 124 } 125 else if (request->hasParam(PARAM_INPUT_4)) { 126 inputMessage4 = request->getParam(PARAM_INPUT_4)->value(); 127 inputParam = PARAM_INPUT_4; 128 } 129 else { 130 inputMessage = "No message sent"; 131 132 inputParam = "none"; 133 134 } 135 Serial.println(inputMessage); 136 Serial.println(inputMessage2); 137 Serial.println(inputMessage3); 138 Serial.println(inputMessage4); 139 request->send(200, "text/html", "HTTP GET request sent to your ESP on input field (" 140 + inputParam + ") with value: " + inputMessage + 141 "<br><a href=\\"/\\">Return to Home Page</a>"); 142 }); 143 server.onNotFound(notFound); 144 server.begin(); 145 } 146 147 148void loop() { 149 int chk = DHT11.read(DHT11PIN); 150 if( http.begin(host,httpPortRead,url1)) //Connect to the host and the url 151 { 152 int httpCode = http.GET(); //Check feedback if there's a response 153 if (httpCode > 0) 154 { 155 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) 156 { 157 158 Data_Raw = http.getString(); //Here we store the raw data string 159 160 161 Data_Raw_1 = Data_Raw; 162 To_remove = Data_Raw_1.indexOf(">"); // here I remove the /span data 163 Data_Raw_1.remove(0,To_remove+1); 164 To_remove = Data_Raw_1.indexOf("<"); 165 Data_Raw_1.remove(To_remove,Data_Raw_1.length()); 166 Cases=Data_Raw_1; 167 Serial.print("Cases: "); //I choosed to display it on the serial monitor to help you debug 168 Serial.println(Cases); 169 170 Data_Raw_2=Data_Raw; 171 To_remove = Data_Raw_2.indexOf("<span>"); 172 Data_Raw_2.remove(0,To_remove+6); 173 Data_Raw_3=Data_Raw_2; 174 To_remove = Data_Raw_2.indexOf("</span>"); 175 Data_Raw_2.remove(To_remove,Data_Raw_2.length()); 176 177 Deaths=Data_Raw_2; 178 Serial.print("Deaths: "); 179 Serial.println(Deaths); 180 181 To_remove = Data_Raw_3.indexOf("<span>"); 182 Data_Raw_3.remove(0,To_remove+6); 183 184 To_remove = Data_Raw_3.indexOf("<"); 185 Data_Raw_3.remove(To_remove,Data_Raw_3.length()); 186 187 Recovered=Data_Raw_3; 188 189 Serial.print("Recovered: "); 190 Serial.println(Recovered); 191 192 } 193 } 194 else //If we can't get data 195 { 196 Serial.printf("[HTTP] GET... failed, error: %s\ 197", http.errorToString(httpCode).c_str()); 198 } 199 200 http.end(); 201 } 202 else //If we can't connect to the HTTP 203 { 204 Serial.printf("[HTTP} Unable to connect\ 205"); 206 } 207 208 while (WiFi.status() != WL_CONNECTED) //In case the Wifi connexion is lost 209 { 210 211 WiFi.disconnect(); 212 delay(1000); 213 214 WiFi.begin(ssid, password); 215 Serial.println("Reconnecting to WiFi.."); 216 delay(10000); 217 } 218 timeClient.update(); 219 unsigned long epochTime = timeClient.getEpochTime(); 220 Serial.print("Epoch Time: "); 221 Serial.println(epochTime); 222 223 struct tm *ptm = gmtime ((time_t *)&epochTime); 224 225delay(1000); 226 if(counter == 60) //Get new data every 10 minutes 227 { 228 counter = 0; 229 displayGettingData(); 230 delay(1000); 231 getWeatherData(); 232 }else 233 { 234 counter++; 235 displayWeather(weatherLocation,weatherDescription); 236 delay(5000); 237 displayConditions(Temperature,Humidity,Pressure); 238 delay(5000); 239 } 240 241 242 lcd.clear(); 243 lcd.setCursor(0,0); 244 lcd.print("Cases"); 245 lcd.setCursor(0,1); 246 lcd.print(Cases); 247 delay (10000); 248 249 250 //change delay if you want 251 252 lcd.clear(); 253 254 lcd.setCursor(0,0); 255 lcd.print("Deaths"); 256 lcd.setCursor(0,1); 257 lcd.print(Deaths); 258 delay (10000); 259 260 lcd.clear(); 261 lcd.setCursor(0,0); 262 lcd.print("Recovered"); 263 lcd.setCursor(0,1); 264 lcd.print(Recovered); 265 delay (10000); 266 267 268 lcd.clear(); 269 270 lcd.setCursor(0,0); 271 lcd.print("Time is="); 272 lcd.setCursor(0,1); 273 lcd.print (timeClient.getHours()); 274 timeClient.update(); 275 lcd.setCursor(2,1); 276 lcd.print(":"); 277 lcd.setCursor(3,1); 278 lcd.print(timeClient.getMinutes()); 279 timeClient.update(); 280 lcd.setCursor(5,1); 281 lcd.print(":"); 282 lcd.setCursor(6,1); 283 lcd.print(timeClient.getSeconds()); 284 timeClient.update(); 285 286 287 288 delay (10000); 289 lcd.clear(); 290 lcd.setCursor(0,0); 291 lcd.print("Day is"); 292 lcd.setCursor(1,1); 293 lcd.print(daysOfTheWeek[timeClient.getDay()]); 294 delay (10000); 295 lcd.clear(); 296 int monthDay = ptm->tm_mday; 297 Serial.print("Month day: "); 298 Serial.println(monthDay); 299 300 int currentMonth = ptm->tm_mon+1; 301 Serial.print("Month: "); 302 Serial.println(currentMonth); 303 304 String currentMonthName = months[currentMonth-1]; 305 Serial.print("Month name: "); 306 Serial.println(currentMonthName); 307 308 int currentYear = ptm->tm_year+1900; 309 Serial.print("Year: "); 310 Serial.println(currentYear); 311 312 String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); 313 lcd.print("Current date: "); 314 lcd.setCursor(1,1); 315 lcd.print(currentDate); 316 delay (10000); 317 lcd.clear(); 318 lcd.setCursor(0,0); 319 320 321 lcd.clear(); 322 lcd.print(inputMessage); 323 324 delay(10000); 325 lcd.clear(); 326 lcd.setCursor (0,0); 327 lcd.print("Room Temperature"); 328 lcd.setCursor(0,1); 329 lcd.print((float)DHT11.temperature, 2); 330 lcd.setCursor(5,1); 331 lcd.print("C"); 332 delay (10000); 333 lcd.clear(); 334 lcd.setCursor (0,0); 335 lcd.print("Room Humidity"); 336 lcd.setCursor(0,1); 337 lcd.print((float)DHT11.humidity, 2); 338 lcd.setCursor(5,1); 339 lcd.print("%"); 340 delay (10000); 341 lcd.clear(); 342} 343 void getWeatherData() //client function to send/receive GET request data. 344{ 345 346 347 if (client.connect(servername, 80)) 348 { //starts client connection, checks for connection 349 client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY); 350 client.println("Host: api.openweathermap.org"); 351 client.println("User-Agent: ArduinoWiFi/1.1"); 352 client.println("Connection: close"); 353 client.println(); 354 } 355 else { 356 Serial.println("connection failed"); //error message if no client connect 357 Serial.println(); 358 } 359 360 while(client.connected() && !client.available()) 361 delay(1); //waits for data 362 while (client.connected() || client.available()) 363 { //connected or data available 364 char c = client.read(); //gets byte from ethernet buffer 365 result = result+c; 366 } 367 368client.stop(); //stop client 369result.replace('[', ' '); 370result.replace(']', ' '); 371Serial.println(result); 372char jsonArray [result.length()+1]; 373result.toCharArray(jsonArray,sizeof(jsonArray)); 374jsonArray[result.length() + 1] = '\\0'; 375StaticJsonBuffer<1024> json_buf; 376JsonObject &root = json_buf.parseObject(jsonArray); 377 378if (!root.success()) 379 { 380 Serial.println("parseObject() failed"); 381 } 382 383String location = root["name"]; 384String country = root["sys"]["country"]; 385float temperature = root["main"]["temp"]; 386float humidity = root["main"]["humidity"]; 387String weather = root["weather"]["main"]; 388String description = root["weather"]["description"]; 389float pressure = root["main"]["pressure"]; 390weatherDescription = description; 391weatherLocation = location; 392Country = country; 393Temperature = temperature; 394Humidity = humidity; 395Pressure = pressure; 396//for taking the external weather conditions 397} 398 399void displayWeather(String location,String description) 400{ 401 lcd.clear(); 402 lcd.setCursor(0,0); 403 lcd.print(location); 404 lcd.print(", "); 405 lcd.print(Country); 406 lcd.setCursor(0,1); 407 lcd.print(description); 408} 409 410void displayConditions(float Temperature,float Humidity, float Pressure) 411{ 412 lcd.clear(); //Printing Temperature 413 lcd.print("T:"); 414 lcd.print(Temperature,1); 415 lcd.print((char)223); 416 lcd.print("C "); 417 418 lcd.print(" H:"); //Printing Humidity 419 lcd.print(Humidity,0); 420 lcd.print(" %"); 421 422 lcd.setCursor(0,1); //Printing Pressure 423 lcd.print("P: "); 424 lcd.print(Pressure,1); 425 lcd.print(" hPa"); 426} 427 428void displayGettingData() 429{ 430 lcd.clear(); 431 lcd.print("Getting data"); 432} 433//thats all in the code
The code
c_cpp
1 #include <ESP8266WiFi.h> //the library used //Use ESP8266 functions 2 #include <ESP8266HTTPClient.h> 3 #include <LiquidCrystal_I2C.h> 4 #include "WiFiUdp.h" 5 #include "NTPClient.h" 6 #include <ArduinoJson.h> 7 #include <ESPAsyncTCP.h> 8#include <ESPAsyncWebServer.h> 9#include <dht11.h> 10#define DHT11PIN 14// defining the pin for dht11 sensor 11 12dht11 DHT11; 13AsyncWebServer server(80);//starting the server for the custom message display feature 14 const long utcOffsetInSeconds = 19800;//change according to your timezone 15char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 16String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};//for the months and day feature do not change 17 18// Define NTP Client to get time 19WiFiUDP ntpUDP; 20NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); 21 22 #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier 23 24 LiquidCrystal_I2C lcd(0x27,16,2); 25 String APIKEY = ""; //open weather api key 26String CityID = "1273294"; //Your City ID 27 28 29 const char* ssid = "wifi name"; //WIFI SSID Name 30 const char* password = "wifi password"; //WIFI Password 31 const char* host = "api.thingspeak.com"; //We read the data from this host 32 const int httpPortRead = 80; 33 34 const char* url1 = "/apps/thinghttp/send_request?api_key=5WX50KDX3R0IBCXQ"; //Change this URL Cases 35 const char* url2 = "/apps/thinghttp/send_request?api_key=AHZYGT5QSWRDGCE5"; //Deaths 36 const char* url3 = "/apps/thinghttp/send_request?api_key=KCHIW1XDQ4J8G3EE"; //Recovered 37 int To_remove; //There are some irrelevant data on the string and here's how I keep the index 38 //of those characters 39 String Cases,Deaths,Recovered,Time,Data_Raw,Data_Raw_1,Data_Raw_2,Data_Raw_3,Data_Raw_4,Data_Raw_5; //Here I keep the numbers that I got 40 41 WiFiClient client; //Create a WiFi client and http client 42 HTTPClient http; 43 char servername[]="api.openweathermap.org"; // remote server we will connect to 44String result; 45 46int counter = 60; 47 48String weatherDescription =""; 49String weatherLocation = ""; 50String Country; 51String cases = "cases"; 52float Temperature; 53float Humidity; 54float Pressure; 55String inputMessage="request->getParam(PARAM_INPUT_1)->value()"; 56String inputMessage2="request->getParam(PARAM_INPUT_2)->value()"; 57String inputMessage3="request->getParam(PARAM_INPUT_3)->value()"; 58String inputMessage4="request->getParam(PARAM_INPUT_4)->value()";//for the custom message display 59 const char* PARAM_INPUT_1 = "input1"; 60 const char* PARAM_INPUT_2 = "Alarm Hours"; 61 const char* PARAM_INPUT_3 = "Alarm Minutes"; 62 const char* PARAM_INPUT_4 = "Select Screen to display"; 63 const char index_html[] PROGMEM = R"rawliteral( 64<!DOCTYPE HTML><html><head> 65 <title>LCD CLOCK CONTROL PANNEL</title> 66 <meta name="viewport" content="width=device-width, initial-scale=1"> 67 </head><body> 68 <form action="/get"> 69 input1: <input type="text" name="input1"> 70 <input type="submit" value="Submit"> 71 </form><br> 72 <form action="/get"> 73 Alarm Hours: <input type="text" name="Alarm Hours"> 74 <input type="submit" value="Submit"> 75 </form><br> 76 <form action="/get"> 77 Alarm Minutes: <input type="text" name="Alarm Minutes"> 78 <input type="submit" value="Submit"> 79 </form><br> 80 <form action="/get"> 81 Select Screen to show: <input type="text" name="Select Screen to display"> 82 <input type="submit" value="Submit"> 83 </form><br> 84 </body></html>)rawliteral"; 85 void notFound(AsyncWebServerRequest *request) { 86 request->send(404, "text/plain", "Not found");//making the webserver for the custom message display 87} 88 89 void setup() { 90 lcd.begin(16, 2); 91 lcd.init(); 92 lcd.backlight(); 93 lcd.setCursor(1, 0); 94 lcd.print("SMART CLOCK"); //Change Your Country Name 95 lcd.setCursor(1, 1); 96 lcd.print("EEP"); //Change Name if you want 97 98 Serial.begin(115200); 99 WiFi.disconnect(); //Disconnect and reconnect to the Wifi you set 100 delay(1000); 101 WiFi.begin(ssid, password); 102 Serial.println("Connected to the WiFi network"); //Display feedback on the serial monitor 103 Serial.println(WiFi.localIP()); 104 timeClient.begin(); 105 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ 106 request->send_P(200, "text/html", index_html); 107 }); 108 109 // Send a GET request to <ESP_IP>/get?input1=<inputMessage> 110 server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { 111 String inputParam; 112 // GET input1 value on <ESP_IP>/get?input1=<inputMessage> 113 if (request->hasParam(PARAM_INPUT_1)) { 114 inputMessage = request->getParam(PARAM_INPUT_1)->value(); 115 inputParam = PARAM_INPUT_1; 116 } 117 else if (request->hasParam(PARAM_INPUT_2)) { 118 inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); 119 inputParam = PARAM_INPUT_2; 120 } 121 else if (request->hasParam(PARAM_INPUT_3)) { 122 inputMessage3 = request->getParam(PARAM_INPUT_3)->value(); 123 inputParam = PARAM_INPUT_3; 124 } 125 else if (request->hasParam(PARAM_INPUT_4)) { 126 inputMessage4 = request->getParam(PARAM_INPUT_4)->value(); 127 inputParam = PARAM_INPUT_4; 128 } 129 else { 130 inputMessage = "No message sent"; 131 132 inputParam = "none"; 133 134 } 135 Serial.println(inputMessage); 136 Serial.println(inputMessage2); 137 Serial.println(inputMessage3); 138 Serial.println(inputMessage4); 139 request->send(200, "text/html", "HTTP GET request sent to your ESP on input field (" 140 + inputParam + ") with value: " + inputMessage + 141 "<br><a href=\\"/\\">Return to Home Page</a>"); 142 }); 143 server.onNotFound(notFound); 144 server.begin(); 145 } 146 147 148void loop() { 149 int chk = DHT11.read(DHT11PIN); 150 if( http.begin(host,httpPortRead,url1)) //Connect to the host and the url 151 { 152 int httpCode = http.GET(); //Check feedback if there's a response 153 if (httpCode > 0) 154 { 155 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) 156 { 157 158 Data_Raw = http.getString(); //Here we store the raw data string 159 160 161 Data_Raw_1 = Data_Raw; 162 To_remove = Data_Raw_1.indexOf(">"); // here I remove the /span data 163 Data_Raw_1.remove(0,To_remove+1); 164 To_remove = Data_Raw_1.indexOf("<"); 165 Data_Raw_1.remove(To_remove,Data_Raw_1.length()); 166 Cases=Data_Raw_1; 167 Serial.print("Cases: "); //I choosed to display it on the serial monitor to help you debug 168 Serial.println(Cases); 169 170 Data_Raw_2=Data_Raw; 171 To_remove = Data_Raw_2.indexOf("<span>"); 172 Data_Raw_2.remove(0,To_remove+6); 173 Data_Raw_3=Data_Raw_2; 174 To_remove = Data_Raw_2.indexOf("</span>"); 175 Data_Raw_2.remove(To_remove,Data_Raw_2.length()); 176 177 Deaths=Data_Raw_2; 178 Serial.print("Deaths: "); 179 Serial.println(Deaths); 180 181 To_remove = Data_Raw_3.indexOf("<span>"); 182 Data_Raw_3.remove(0,To_remove+6); 183 184 To_remove = Data_Raw_3.indexOf("<"); 185 Data_Raw_3.remove(To_remove,Data_Raw_3.length()); 186 187 Recovered=Data_Raw_3; 188 189 Serial.print("Recovered: "); 190 Serial.println(Recovered); 191 192 } 193 } 194 else //If we can't get data 195 { 196 Serial.printf("[HTTP] GET... failed, error: %s\ 197", http.errorToString(httpCode).c_str()); 198 } 199 200 http.end(); 201 } 202 else //If we can't connect to the HTTP 203 { 204 Serial.printf("[HTTP} Unable to connect\ 205"); 206 } 207 208 while (WiFi.status() != WL_CONNECTED) //In case the Wifi connexion is lost 209 { 210 211 WiFi.disconnect(); 212 delay(1000); 213 214 WiFi.begin(ssid, password); 215 Serial.println("Reconnecting to WiFi.."); 216 delay(10000); 217 } 218 timeClient.update(); 219 unsigned long epochTime = timeClient.getEpochTime(); 220 Serial.print("Epoch Time: "); 221 Serial.println(epochTime); 222 223 struct tm *ptm = gmtime ((time_t *)&epochTime); 224 225delay(1000); 226 if(counter == 60) //Get new data every 10 minutes 227 { 228 counter = 0; 229 displayGettingData(); 230 delay(1000); 231 getWeatherData(); 232 }else 233 { 234 counter++; 235 displayWeather(weatherLocation,weatherDescription); 236 delay(5000); 237 displayConditions(Temperature,Humidity,Pressure); 238 delay(5000); 239 } 240 241 242 lcd.clear(); 243 lcd.setCursor(0,0); 244 lcd.print("Cases"); 245 lcd.setCursor(0,1); 246 lcd.print(Cases); 247 delay (10000); 248 249 250 //change delay if you want 251 252 lcd.clear(); 253 254 lcd.setCursor(0,0); 255 lcd.print("Deaths"); 256 lcd.setCursor(0,1); 257 lcd.print(Deaths); 258 delay (10000); 259 260 lcd.clear(); 261 lcd.setCursor(0,0); 262 lcd.print("Recovered"); 263 lcd.setCursor(0,1); 264 lcd.print(Recovered); 265 delay (10000); 266 267 268 lcd.clear(); 269 270 lcd.setCursor(0,0); 271 lcd.print("Time is="); 272 lcd.setCursor(0,1); 273 lcd.print (timeClient.getHours()); 274 timeClient.update(); 275 lcd.setCursor(2,1); 276 lcd.print(":"); 277 lcd.setCursor(3,1); 278 lcd.print(timeClient.getMinutes()); 279 timeClient.update(); 280 lcd.setCursor(5,1); 281 lcd.print(":"); 282 lcd.setCursor(6,1); 283 lcd.print(timeClient.getSeconds()); 284 timeClient.update(); 285 286 287 288 delay (10000); 289 lcd.clear(); 290 lcd.setCursor(0,0); 291 lcd.print("Day is"); 292 lcd.setCursor(1,1); 293 lcd.print(daysOfTheWeek[timeClient.getDay()]); 294 delay (10000); 295 lcd.clear(); 296 int monthDay = ptm->tm_mday; 297 Serial.print("Month day: "); 298 Serial.println(monthDay); 299 300 int currentMonth = ptm->tm_mon+1; 301 Serial.print("Month: "); 302 Serial.println(currentMonth); 303 304 String currentMonthName = months[currentMonth-1]; 305 Serial.print("Month name: "); 306 Serial.println(currentMonthName); 307 308 int currentYear = ptm->tm_year+1900; 309 Serial.print("Year: "); 310 Serial.println(currentYear); 311 312 String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); 313 lcd.print("Current date: "); 314 lcd.setCursor(1,1); 315 lcd.print(currentDate); 316 delay (10000); 317 lcd.clear(); 318 lcd.setCursor(0,0); 319 320 321 lcd.clear(); 322 lcd.print(inputMessage); 323 324 delay(10000); 325 lcd.clear(); 326 lcd.setCursor (0,0); 327 lcd.print("Room Temperature"); 328 lcd.setCursor(0,1); 329 lcd.print((float)DHT11.temperature, 2); 330 lcd.setCursor(5,1); 331 lcd.print("C"); 332 delay (10000); 333 lcd.clear(); 334 lcd.setCursor (0,0); 335 lcd.print("Room Humidity"); 336 lcd.setCursor(0,1); 337 lcd.print((float)DHT11.humidity, 2); 338 lcd.setCursor(5,1); 339 lcd.print("%"); 340 delay (10000); 341 lcd.clear(); 342} 343 void getWeatherData() //client function to send/receive GET request data. 344{ 345 346 347 if (client.connect(servername, 80)) 348 { //starts client connection, checks for connection 349 client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY); 350 client.println("Host: api.openweathermap.org"); 351 client.println("User-Agent: ArduinoWiFi/1.1"); 352 client.println("Connection: close"); 353 client.println(); 354 } 355 else { 356 Serial.println("connection failed"); //error message if no client connect 357 Serial.println(); 358 } 359 360 while(client.connected() && !client.available()) 361 delay(1); //waits for data 362 while (client.connected() || client.available()) 363 { //connected or data available 364 char c = client.read(); //gets byte from ethernet buffer 365 result = result+c; 366 } 367 368client.stop(); //stop client 369result.replace('[', ' '); 370result.replace(']', ' '); 371Serial.println(result); 372char jsonArray [result.length()+1]; 373result.toCharArray(jsonArray,sizeof(jsonArray)); 374jsonArray[result.length() + 1] = '\\0'; 375StaticJsonBuffer<1024> json_buf; 376JsonObject &root = json_buf.parseObject(jsonArray); 377 378if (!root.success()) 379 { 380 Serial.println("parseObject() failed"); 381 } 382 383String location = root["name"]; 384String country = root["sys"]["country"]; 385float temperature = root["main"]["temp"]; 386float humidity = root["main"]["humidity"]; 387String weather = root["weather"]["main"]; 388String description = root["weather"]["description"]; 389float pressure = root["main"]["pressure"]; 390weatherDescription = description; 391weatherLocation = location; 392Country = country; 393Temperature = temperature; 394Humidity = humidity; 395Pressure = pressure; 396//for taking the external weather conditions 397} 398 399void displayWeather(String location,String description) 400{ 401 lcd.clear(); 402 lcd.setCursor(0,0); 403 lcd.print(location); 404 lcd.print(", "); 405 lcd.print(Country); 406 lcd.setCursor(0,1); 407 lcd.print(description); 408} 409 410void displayConditions(float Temperature,float Humidity, float Pressure) 411{ 412 lcd.clear(); //Printing Temperature 413 lcd.print("T:"); 414 lcd.print(Temperature,1); 415 lcd.print((char)223); 416 lcd.print("C "); 417 418 lcd.print(" H:"); //Printing Humidity 419 lcd.print(Humidity,0); 420 lcd.print(" %"); 421 422 lcd.setCursor(0,1); //Printing Pressure 423 lcd.print("P: "); 424 lcd.print(Pressure,1); 425 lcd.print(" hPa"); 426} 427 428void displayGettingData() 429{ 430 lcd.clear(); 431 lcd.print("Getting data"); 432} 433//thats all in the code
Downloadable files
The connections
The connections

Comments
Only logged in users can leave comments