COVID-19 Monitoring
The project is to make Covid-19 monitoring tools from the API
Components and supplies
1
Tactile Switch, Top Actuated
1
Alphanumeric LCD, 16 x 2
1
Plastic Enclosure, Project Box
1
Rocker Switch, SPST
1
NodeMCU ESP8266 Breakout Board
Apps and platforms
1
Arduino IDE
Project description
Code
covid-19-monitor.ino
arduino
Please to install the ESP8266 package in Arduino IDE, as well as the required libraries. The Arduino JSON library uses version 6.
1#include <ESP8266WiFi.h> 2#include <WiFiClientSecure.h> 3#include <ESP8266HTTPClient.h> 4#include <WiFiManager.h> 5#include <ArduinoJson.h> 6 7#include <Wire.h> 8#include <LiquidCrystal_I2C.h> 9 10LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display 11 12//You can use a host with data from your country check in : services5.arcgis.com 13//I took data from every province in my country (Indonesia) from the host 14const char* host = "services5.arcgis.com"; 15String request = "/VS6HdKS0VfIhv8Ct/arcgis/rest/services/COVID19_Indonesia_per_Provinsi/FeatureServer/0/query?where=1%3D1&outFields=Provinsi,Kasus_Posi,Kasus_Semb,Kasus_Meni&returnGeometry=false&outSR=4326&f=json"; 16const int httpsPort = 443; 17const char fingerprint[] PROGMEM = "70580e780c9d727550619d3e4efdb21d64d1e91e"; //SHA1 finger print 18 19//integer data array for COVID-19 20int dataCovid[9]; 21unsigned long previousMillis = 0; 22const long intervalGetData = 300000; 23 24unsigned long currentMillis, sleep; 25 26//defines the LCD interrupt pin and backlight pin 27#define BUTTON D3 28#define BACKLIGHT D4 29 30//function to turn on the LCD when the LCD is off 31ICACHE_RAM_ATTR void wakeup(){ 32 lcd.display(); 33 digitalWrite(BACKLIGHT, HIGH); 34 sleep = millis()+120000; 35 Serial.print("lcd Off"); 36} 37 38//function for displaying data on the LCD 39void displayData(int c = 0){ 40if(c==0){ 41 42 lcd.clear(); 43 lcd.setCursor(4,0); 44 lcd.print("W"); delay(100); lcd.print("E"); delay(100); 45 lcd.print("L"); delay(100); lcd.print("C"); delay(100); 46 lcd.print("O"); delay(100); lcd.print("M"); delay(100); 47 lcd.print("E"); delay(500); 48 49 lcd.clear(); 50 lcd.setCursor(20,0); lcd.print(F("COVID-19")); 51 lcd.setCursor(19,1); lcd.print(F("MONITORING")); 52 53 int u; 54 for(u=0;u<16;u++){ 55 lcd.scrollDisplayLeft(); 56 delay(500); 57 } 58 } 59 else{ 60 61 //DKI JAKARTA province 62 lcd.clear(); 63 lcd.setCursor(0,0); 64 lcd.print(F("DKI ")); 65 lcd.print(F(">POSITIF")); 66 lcd.setCursor(4,1); 67 lcd.print(dataCovid[0]); 68 delay(2000); 69 70 lcd.clear(); 71 lcd.setCursor(0,0); 72 lcd.print(F("DKI ")); 73 lcd.print(F(">SEMBUH")); 74 lcd.setCursor(4,1); 75 lcd.print(dataCovid[1]); 76 delay(2000); 77 78 lcd.clear(); 79 lcd.setCursor(0,0); 80 lcd.print(F("DKI ")); 81 lcd.print(F(">MENINGGAL")); 82 lcd.setCursor(4,1); 83 lcd.print(dataCovid[2]); 84 delay(2000); 85 86 //JABAR province 87 lcd.clear(); 88 lcd.setCursor(0,0); 89 lcd.print(F("JABAR ")); 90 lcd.print(F(">POSITIF")); 91 lcd.setCursor(4,1); 92 lcd.print(dataCovid[3]); 93 delay(2000); 94 95 lcd.clear(); 96 lcd.setCursor(0,0); 97 lcd.print(F("JABAR ")); 98 lcd.print(F(">SEMBUH")); 99 lcd.setCursor(4,1); 100 lcd.print(dataCovid[4]); 101 delay(2000); 102 103 lcd.clear(); 104 lcd.setCursor(0,0); 105 lcd.print(F("JABAR ")); 106 lcd.print(F(">MENINGGAL")); 107 lcd.setCursor(4,1); 108 lcd.print(dataCovid[5]); 109 delay(2000); 110 111 //JATENG province 112 lcd.clear(); 113 lcd.setCursor(0,0); 114 lcd.print(F("JATENG ")); 115 lcd.print(F(">Confirmed")); 116 lcd.setCursor(4,1); 117 lcd.print(dataCovid[6]); 118 delay(2000); 119 120 lcd.clear(); 121 lcd.setCursor(0,0); 122 lcd.print(F("JATENG ")); 123 lcd.print(F(">Recovered")); 124 lcd.setCursor(4,1); 125 lcd.print(dataCovid[7]); 126 delay(2000); 127 128 lcd.clear(); 129 lcd.setCursor(0,0); 130 lcd.print(F("JATENG ")); 131 lcd.print(F(">Deaths")); 132 lcd.setCursor(4,1); 133 lcd.print(dataCovid[8]); 134 delay(2000); 135 } 136} 137 138//function to retrieve data with the API from the host that we access 139void getData(){ 140 WiFiClientSecure httpsClient; //Declare WiFiClient class object 141 142 Serial.println(host); 143 Serial.printf("Using fingerprint '%s'\ 144", fingerprint); 145 146 httpsClient.setFingerprint(fingerprint); 147 httpsClient.setTimeout(15000); // 15 Seconds 148 delay(1000); 149 150 Serial.println("HTTPS Connecting"); 151 152 int r = 0; //repeat counter 153 while ((!httpsClient.connect(host, httpsPort)) && (r < 30)) { 154 delay(100); 155 Serial.print("."); 156 r++; 157 } 158 159 if (r == 30) { 160 Serial.println("Connection failed"); 161 } 162 else { 163 Serial.println("Connected"); 164 } 165 166 Serial.print("Requesting: "); 167 Serial.println(host + request); 168 169 httpsClient.print(String("GET ") + request + " HTTP/1.1\ \ 170" + 171 "Host: " + host + "\ \ 172" + 173 "Connection: close\ \ 174\ \ 175"); 176 177 Serial.println("Request sent"); 178 179 while (httpsClient.connected()) { 180 String line = httpsClient.readStringUntil('\n'); 181 if (line == "\ ") { 182 Serial.println("Headers received"); 183 break; 184 } 185 } 186 187 Serial.println("Payload received:"); 188 189 String payload; 190 while (httpsClient.available()) { 191 payload = httpsClient.readStringUntil('\n'); //Read each row of data 192 Serial.println(payload); //print response 193 } 194 195 Serial.println("Closing connection"); 196 197 //MEMBUAT JSON BARU, INI HARUS ADA KARENA JIKA TIDAK KITA AKAN SUSAH MENGAMBIL DATA YANG DIMAU KARENA TERBATASNYA LENGTH UNTUK MEMBUAT ARRAY 198 //create new json data to retrieve data from several provinces 199 int i; 200 String dataCOVID = "{\\"title\\":\\"Data Covid-19\\",\\"features\\":["; 201 for(i=1660-28 202 ;i<1908-1;i++){ 203 204 dataCOVID += payload[i]; 205 206 } 207 dataCOVID+="]}"; 208 Serial.println("JSON BARU: "+ dataCOVID); 209 210 char charBuf[300]; 211 dataCOVID.toCharArray(charBuf, 300); 212 213 214 //parsing json data from API response 215 const size_t capacity = JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(4) + 250; 216 DynamicJsonDocument doc(capacity); 217 218 //const char* json = "{\\"title\\":\\"Data Covid-19\\",\\"features\\":[{\\"attributes\\":{\\"Provinsi\\":\\"DKI Jakarta\\",\\"Kasus_Posi\\":2815,\\"Kasus_Semb\\":204,\\"Kasus_Meni\\":246}},{\\"attributes\\":{\\"Provinsi\\":\\"Jawa Barat\\",\\"Kasus_Posi\\":632,\\"Kasus_Semb\\":41,\\"Kasus_Meni\\":56}},{\\"attributes\\":{\\"Provinsi\\":\\"Jawa Tengah\\",\\"Kasus_Posi\\":304,\\"Kasus_Semb\\":36,\\"Kasus_Meni\\":41}}]}"; 219 220 deserializeJson(doc, dataCOVID); 221 222 const char* title = doc["title"]; // "Data Covid-19" 223 224 JsonArray features = doc["features"]; 225 226 JsonObject features_0_attributes = features[0]["attributes"]; 227 const char* features_0_attributes_Provinsi = features_0_attributes["Provinsi"]; // "DKI Jakarta" 228 dataCovid[0] = features_0_attributes["Kasus_Posi"]; // 2815 229 dataCovid[1] = features_0_attributes["Kasus_Semb"]; // 204 230 dataCovid[2] = features_0_attributes["Kasus_Meni"]; // 246 231 232 JsonObject features_1_attributes = features[1]["attributes"]; 233 const char* features_1_attributes_Provinsi = features_1_attributes["Provinsi"]; // "Jawa Barat" 234 dataCovid[3] = features_1_attributes["Kasus_Posi"]; // 632 235 dataCovid[4] = features_1_attributes["Kasus_Semb"]; // 41 236 dataCovid[5] = features_1_attributes["Kasus_Meni"]; // 56 237 238 JsonObject features_2_attributes = features[2]["attributes"]; 239 const char* features_2_attributes_Provinsi = features_2_attributes["Provinsi"]; // "Jawa Tengah" 240 dataCovid[6] = features_2_attributes["Kasus_Posi"]; // 304 241 dataCovid[7] = features_2_attributes["Kasus_Semb"]; // 36 242 dataCovid[8] = features_2_attributes["Kasus_Meni"]; // 41 243} 244 245void setup() { 246 //LCD initialization and interrupt button 247 pinMode(BACKLIGHT, OUTPUT); 248 pinMode(BUTTON, INPUT_PULLUP); 249 lcd.init(); 250 lcd.backlight(); 251 sleep = 60000; 252 digitalWrite(BACKLIGHT, HIGH); 253 attachInterrupt(digitalPinToInterrupt(BUTTON), wakeup, CHANGE); 254 255 displayData(); 256 257 Serial.begin(115200); 258 Serial.print("Connecting"); 259 260 lcd.clear(); 261 lcd.setCursor(6,0); 262 lcd.print("...."); 263 lcd.setCursor(0,1); 264 lcd.print(F("WAITING FOR WIFI")); 265 266 //Connection to WIFI, look for WIFI "COVID-19 MONITORING" then set by you 267 WiFiManager wifiManager; 268 wifiManager.autoConnect("COVID-19 MONITORING"); 269 270 //If the connection is successful to an SSID, it will print an IP Address 271 Serial.println(""); 272 Serial.println("Connection Success "); 273 Serial.print("IP address: "); 274 Serial.println(WiFi.localIP()); 275 276 lcd.clear(); 277 lcd.setCursor(3,0); 278 lcd.print(F("CONNECTED")); 279 lcd.clear(); 280 lcd.setCursor(4,0); 281 lcd.print(F("GETTING")); 282 lcd.setCursor(2,0); 283 lcd.print(F("FIRST DATA")); 284 285 //retrieve data the first time the device is turned on 286 getData(); 287 displayData(1); 288 289} 290 291void loop() { 292 293 294 currentMillis = millis(); 295 296 int selisih = currentMillis - previousMillis; 297 298 //retrieve data every 5 minutes 299 if(currentMillis - previousMillis >= intervalGetData){ 300 301 previousMillis = currentMillis; 302 303 //debug time 304 Serial.println(currentMillis); 305 306 getData(); 307 displayData(1); 308 } 309 310 //turn off the LCD if the device has been on for 2 minutes 311 if(currentMillis > sleep) { 312 lcd.noDisplay(); 313 digitalWrite(BACKLIGHT, LOW); 314 } 315 316 Serial.println(selisih); 317 displayData(1); 318} 319
Downloadable files
Schematic
Schematic

Schematic
Schematic

Comments
Only logged in users can leave comments