NederlandsKlik deze knop voor de Nederlandstalige website

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 COVOID-19 Data Monitor“.

If you want to know how to retrieve the data with PHP, read the blog post “COVID-19 CORONA Get data with PHP

COVID-19 CORONA Tracker ESP32 & Arduino IDE - M5StickC
COVID-19 CORONA Tracker ESP32 & Arduino IDE – M5StickC

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.

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Tested and recommended by OneGuyOneBlog.com:

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Coronavirus COVID-19 Global Cases
Coronavirus COVID-19 Global Cases

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.

COVID-19 CORONA Tracker: ESP32 & Arduino IDE - REST API Query-URL
COVID-19 CORONA Tracker: ESP32 & Arduino IDE – REST API 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
         }
      }
   ]
}
COVID-19 CORONA Tracker: ESP32 & Arduino IDE - JSON data
COVID-19 CORONA Tracker: ESP32 & Arduino IDE – JSON data

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.

COVID-19 CORONA Tracker: ESP32 & Arduino IDE - ArduinoJson Assistant
COVID-19 CORONA Tracker: ESP32 & Arduino IDE – ArduinoJson Assistant

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!

VNG Systems

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Tested and recommended by OneGuyOneBlog.com:

M5StickC ESP32 PICO Color LCD Mini IoT Development Board

9 Comments

Add a Comment

Your email address will not be published. Required fields are marked *