COVID-19 CORONA Tracker: ESP32 & Arduino IDE
|
If you want to follow the situation with regard to the distribution of COVID-19 24/7, you can use a microcontroller for this. Together with an LCD, you can compile a live COVID-19 CORONA Tracker. For example with an M5StickC, which is equipped with an ESP32 and a color LCD. If you want to do the same but with an ESP8266 microcontroller, read the blog “ESP8266 Real-time COVID-19 Data Monitor“.


M5StickC and the Arduino IDE
First of all, we need an ESP32-compatible development board with LCD, such as the M5StickC. You can, of course, use a different ESP32, as long as you adjust the sketch. As usual, install the Arduino IDE with the ESP32 core. When you use the M5StickC, you also have to install the M5StickC libraries. For more information, you can consult the previous blog “Programming the M5StickC with the Arduino IDE“.
Finally, you still need the ArduinoJson library. You can install it with the library manager in the Arduino IDE.


Coronavirus dataset
Then, of course, we need the data. They can be found on the Coronavirus Disease (COVID-19) GIS Hub website, for example. That website also provides a REST API, where you can request the data in JSON format. Through the API Explorer on that website, you can easily compile the correct query URL.


In this case, you choose the following settings:
- At “Where” you choose “Country_Region” and then, for example, enter “Netherlands”.
- Under “From fields”, check “Last_Update”, “Confirmed”, “Deaths”, “Recovered” and the rest off.
- At “Output options” you set “Request geometry” to “False”, and the rest too.
At the top right you now see the generated query URL:
https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=Country_Region%20like%20'%25NETHERLANDS%25'&outFields=Last_Update,Confirmed,Deaths,Recovered&returnGeometry=false&outSR=4326&f=json
Click on “Try Now” or paste the URL into a new browser window, the output of that query will look like this:
{ "objectIdFieldName": "OBJECTID", "uniqueIdField": { "name": "OBJECTID", "isSystemMaintained": true }, "globalIdFieldName": "", "geometryType": "esriGeometryPoint", "spatialReference": { "wkid": 4326, "latestWkid": 4326 }, "fields": [ { "name": "Last_Update", "type": "esriFieldTypeDate", "alias": "Last Update", "sqlType": "sqlTypeOther", "length": 8, "domain": null, "defaultValue": null }, { "name": "Confirmed", "type": "esriFieldTypeInteger", "alias": "Confirmed", "sqlType": "sqlTypeOther", "domain": null, "defaultValue": null }, { "name": "Deaths", "type": "esriFieldTypeInteger", "alias": "Deaths", "sqlType": "sqlTypeOther", "domain": null, "defaultValue": null }, { "name": "Recovered", "type": "esriFieldTypeInteger", "alias": "Recovered", "sqlType": "sqlTypeOther", "domain": null, "defaultValue": null } ], "features": [ { "attributes": { "Last_Update": 1583777591000, "Confirmed": 321, "Deaths": 3, "Recovered": 0 } } ] }


Parse JSON data
The code we need to read in the JSON data can be generated automatically with the online “ArduinoJson Assistant“. Paste the JSON data that we generated above into the ArduinoJson Assistant, and the website generates the program code.


From that program code, we only copy the necessary rules, and we adjust the “deserializeJson” line. See the end result below:
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + 3 * JSON_OBJECT_SIZE(6) + 2 * JSON_OBJECT_SIZE(7) + 690; DynamicJsonDocument doc(capacity); deserializeJson(doc, payload); JsonArray fields = doc["fields"]; JsonObject features_0_attributes = doc["features"][0]["attributes"]; long features_0_attributes_Last_Update = features_0_attributes["Last_Update"]; int features_0_attributes_Confirmed = features_0_attributes["Confirmed"]; int features_0_attributes_Deaths = features_0_attributes["Deaths"]; int features_0_attributes_Recovered = features_0_attributes["Recovered"];
You will see this fragment in the sketch at the bottom of this page.
COVID-19 CORONA Tracker sketch
With this information, you can now easily put together your own Corona tracker. The sketch at the bottom of this article does nothing but retrieve new data every 10 minutes (the red LED will light up). On the screen of the M5StickC, you can see the number of infections in the area concerned (in yellow numbers), below that the number of deaths (red numbers) and the number recovered (green numbers).
Do not forget to enter your network name and password in the sketch.
The sketch is also available on GitHub: https://github.com/oneguyoneblog/COVID-19-Corona-ESP32
COVID-19 CORONA Tracker: just an example
As usual on this blog, the code is only a starting point for your own project. The sketch is very basic, but you can make something cool yourself. Have you made something interesting, then let it be known in the comments section below this article!
If you want to know how to retrieve the data with PHP, read the blog post “COVID-19 CORONA Get data with PHP“.
#include <M5StickC.h> #include <WiFi.h> #include <HTTPClient.h> #include <ArduinoJson.h> const char* url = "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=Country_Region%20like%20'%25NETHERLANDS%25'&outFields=Last_Update,Confirmed,Deaths,Recovered&returnGeometry=false&outSR=4326&f=json"; const char* ssid = "mijnwifinetwerk"; const char* password = "mijnwifiwachtwoord"; const int redLED = 10; void setup() { pinMode(redLED, OUTPUT); Serial.begin(115200); delay(2000); M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.setTextColor(WHITE); M5.Lcd.setTextSize(2); digitalWrite(redLED, LOW); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 10); M5.Lcd.printf("Connecting.."); } M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 10); M5.Lcd.printf("Connected"); digitalWrite(redLED, HIGH); } void loop() { HTTPClient http; String data; digitalWrite(redLED, LOW); http.begin(url); digitalWrite(redLED, HIGH); int httpCode = http.GET(); if (httpCode > 0) { //Check for the returning code String payload = http.getString(); char charBuf[500]; payload.toCharArray(charBuf, 500); M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 10); Serial.println(payload); const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + 3 * JSON_OBJECT_SIZE(6) + 2 * JSON_OBJECT_SIZE(7) + 690; DynamicJsonDocument doc(capacity); deserializeJson(doc, payload); JsonArray fields = doc["fields"]; JsonObject features_0_attributes = doc["features"][0]["attributes"]; long features_0_attributes_Last_Update = features_0_attributes["Last_Update"]; int features_0_attributes_Confirmed = features_0_attributes["Confirmed"]; int features_0_attributes_Deaths = features_0_attributes["Deaths"]; int features_0_attributes_Recovered = features_0_attributes["Recovered"]; M5.Lcd.setTextSize(4); M5.Lcd.setTextColor(ORANGE); M5.Lcd.println(features_0_attributes_Confirmed); M5.Lcd.setTextSize(3); M5.Lcd.setTextColor(RED); M5.Lcd.println(features_0_attributes_Deaths); M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(GREEN); M5.Lcd.println(features_0_attributes_Recovered); } else { M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 10); M5.Lcd.printf("Error on HTTP request"); } http.end(); delay(60000); }
Make your own ESP32 COVID-19 tracker project
Use the above information to make your own awesome project. Like Saลกa ฤavloviฤ from Croatia, he used the ESP-32S together with a color LCD:
Have you made something cool yourself? Let us know in the comments!
Nice project idea
Awesome project, I just got it working for a esp8266 + oled wifi kit 8.
It would be helpful if you put in a footnote on the need of the HTTPS thumbprint in the http.begin(url, thumbprint); section.
Hi Kyle, thank you for your message! I’m glad you got it working on the ESP8266. I have not tried it on the ESP8266 yet, but I will see if I can figure it out. Have you published the code somewhere?
I have added a blog post on how to do this on the ESP8266: https://oneguyoneblog.com/2020/03/28/esp8266-real-time-covid-19-data-monitor/.
Great work
Based on your code i created a covid-19 tracker on an esp32 based ttgo-T-Display.
It has a color 1.14 inch 135×240 pixels display.
Hi Labros, thanks for your message, I’m glad the information was useful for you.
I have TTGO-T-Display too, I want to adapt the sketch to it sometime later. Have you published your project somewhere?
I uploaded it for you or everyone else that wants it.
It is currently based on Greece but you can change that to fit your needs.
Hope you like it ๐
https://pastebin.com/YU9z2Ncq
Let me know if it worked on your device ๐
Thank you for sharing the code, I will try it out and let you know!
Hi @oneguyoneblog I did a copy, hope Labros will not get mad at me ๐ Here it is: https://pastebin.com/VNzi1VuF
Hi Labros, I use your port for T-Display, works great, only one thing I cannot do. I can get any country to diplay, but not the world statistics… Could you try to modify your code so that it cycles between world and selected country ? Itried, but failed, not much of a programmer myself ๐ Thank you
I was a bit busy lately, and now the code on Pastebin is gone :’ (
I stumbled around on this a little, as I’m not an HTTP or HTTPS guru, so I wanted to make it a little more clear for those using an 8266-based board.
To get the credentials right for the HTTPS GET, you need to go to the website of the API, right click on the lock in your address line, open up the details of the certificate, and find / copy their thumbprint. Then add that into the sketch as a global variable, something like “const char thumbprint[] PROGMEM = “70 58 0e 78 0c 9d 72 75 50 61 9d 3e 4e fd b2 1d 64 d1 e9 1e”;”
Then when you begin the http session, you need to include the thumbprint:
http.begin(url, thumbprint);
Hopefully this speeds someone else up in figuring it out on an ESP8266 platform.
Hi Ben, thank you for explaining how to get HTTPS GET working on the ESP8266. For the people who want to know how to get the COVID-19 data on the ESP8266, see the blog post here:
https://oneguyoneblog.com/2020/03/28/esp8266-real-time-covoid-19-data-monitor/
It also explains how to find the certificate thumbprint and use it in a sketch.