NederlandsKlik deze knop voor de Nederlandstalige website

ESP8266 Real-time COVID-19 Data Monitor

If you still have to sit at home because of COVID-19, there is plenty of time to tinker. Last time we went to work with an ESP32, this time we experiment with an ESP8266 microcontroller. This ESP8266 Real-time COVID-19 data monitor is comparable to the ESP32 COVID-19 CORONA Tracker. Again we use the data from the Coronavirus Disease (COVID-19) GIS Hub and we program a small “sketch” in the Arduino IDE. In this article, I use the ESP-WROOM-02 (from VNGSystems in Gouda) but of course, you can use any other ESP8266 development board.

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

ESP8266 Real-time COVID-19 data monitor: OLED
ESP8266 Real-time COVID-19 data monitor: OLED

Arduino IDE & Arduino core for ESP8266

First of all, the Arduino IDE must be installed on your computer. If you have not already done so, read the article “Installing the Arduino IDE on Windows 10 ”. If you use Linux, then choose “Install or upgrade to the latest Arduino IDE on Linux“.

The “Arduino core for ESP8266 WiFi chip” must also be installed. See the chapter “Installing the ESP8266 Arduino core” in the blog “ESP8266 NodeMcu and Arduino IDE on Linux“. The procedure is the same for Linux and Windows.

ESP8266 Real-time COVID-19 data monitor with ESP-WROOM-02

If you also use the ESP-WROOM-02 for this project, read the article “WEMOS D1 Esp-Wroom-02 and Arduino IDE“. Here you can read how you can install the ThingPulse ESP8266 OLED SSD1306 library and the Brzo I2C library for the display in addition to the above.

ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
Tested and recommended by OneGuyOneBlog.com:

ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
VNGsystems.nl

ArduinoJson library

Finally, you need the ArduinoJson library. You can install it with the library manager in the Arduino IDE.

Coronavirus Disease (COVID-19) GIS Hub API

Then, of course, we need the data. These can be found, for example, on the Coronavirus Disease (COVID-19) GIS Hub website. Using the API Explorer on that website, you can build the correct query URL for the REST API. You can read how to do this in the blog COVID-19 CORONA Tracker: ESP32 & Arduino IDE.

ESP8266 Real-time COVOID-19 data monitor: ESP32 & Arduino IDE - REST API Query-URL
ESP8266 Real-time COVOID-19 data monitor: REST API Query-URL

The result is a URL like this:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=Country_Region%20like%20'%25SWEDEN%25'&outFields=Last_Update,Confirmed,Deaths,Recovered&returnGeometry=false&outSR=4326&f=json

This will yield the following JSON data:

{
  "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": 1585416253000,
        "Confirmed": 3069,
        "Deaths": 105,
        "Recovered": 16
      }
    }
  ]
}
ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
Tested and recommended by OneGuyOneBlog.com:

ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
VNGsystems.nl

ArduinoJson Assistant code generator

We can generate the code required to read the JSON data with the online “ArduinoJson Assistant”. Paste the JSON data we generated above into the Assistant, and the website will generate the code.

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

We take the necessary lines from that code, and adjust the line with “deserializeJson”, as indicated 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"];

This code has been incorporated in the sketch below.

ESP8266 Real-time COVID-19: Certificate fingerprint

Finally, we need the certificate fingerprint of the website to be able to establish an https connection with the ESP8288. For the Google Chrome browser on Windows 10, you can find it as follows. First, click on the lock next to the URL. Then choose “Certificate” in the dropdown menu.

ESP8266 Real time COVID 19 data monitor: get certificate fingerprint
ESP8266 Real-time COVID 19 data monitor: get certificate fingerprint

Then in the next window, click on the “Details” tab and scroll down to “Fingerprint”. In the box below you will now see the fingerprint that you can cut-paste to the sketch.

ESP8266 Real time COVID 19 data monitor: get certificate fingerprint
ESP8266 Real-time COVID 19 data monitor: get certificate fingerprint

In the sketch it looks like this:

const char fingerprint[] PROGMEM = "70580e780c9d727550619d3e4efdb21d64d1e91e"; //SHA1 finger print

You will find this rule in the sketch below.

ESP8266 Real-time COVID-19 Data Monitor Sketch

All of the above is eventually combined in the sketch below. As usual on this blog, this is just an example. Use it in your own project and make it something interesting!

You can also download this code from GitHub: https://github.com/oneguyoneblog/COVID-19-Corona-ESP8266

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include "SSD1306Brzo.h"
#include <ArduinoJson.h>

SSD1306Brzo display(0x3c, 5, 4);   // Initialize OLED display

const char* ssid = "yournetworkname";
const char* password = "yournetworkpassword";

const char* host = "services1.arcgis.com";
String request = "/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=Country_Region%20like%20'%25SWEDEN%25'&outFields=Last_Update,Confirmed,Deaths,Recovered&returnGeometry=false&outSR=4326&f=json";
const int httpsPort = 443;
const char fingerprint[] PROGMEM = "70580e780c9d727550619d3e4efdb21d64d1e91e"; //SHA1 finger print

void setup() {

  Serial.begin(115200);
  Serial.print("Connecting");

  delay(1000);
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue
  delay(1000);
  WiFi.mode(WIFI_STA);        //Station mode
  WiFi.begin(ssid, password);     //Connect to WiFi

  display.init();   // Initialise the display
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_16);
  display.drawString(0, 0, "Connecting");
  display.display();    // Write the buffer to the display

  while (WiFi.status() != WL_CONNECTED) {  // Wait for connection
    delay(500);
    Serial.print(".");
  }

  Serial.println("");  //If connection successful show IP address of ESP8266 in serial monitor
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClientSecure httpsClient;    //Declare object of class WiFiClient

  Serial.println(host);
  Serial.printf("Using fingerprint '%s'\n", fingerprint);

  httpsClient.setFingerprint(fingerprint);
  httpsClient.setTimeout(15000); // 15 Seconds
  delay(1000);

  Serial.println("HTTPS Connecting");

  int r = 0; //retry counter
  while ((!httpsClient.connect(host, httpsPort)) && (r < 30)) {
    delay(100);
    Serial.print(".");
    r++;
  }
  
  if (r == 30) {
    Serial.println("Connection failed");
  }
  else {
    Serial.println("Connected");
  }

  Serial.print("Requesting: ");
  Serial.println(host + request);

  httpsClient.print(String("GET ") + request + " HTTP/1.1\r\n" +
                    "Host: " + host + "\r\n" +
                    "Connection: close\r\n\r\n");

  Serial.println("Request sent");

  while (httpsClient.connected()) {
    String line = httpsClient.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("Headers received");
      break;
    }
  }

  Serial.println("Payload received:");

  String payload;
  while (httpsClient.available()) {
    payload = httpsClient.readStringUntil('\n');  //Read Line by Line
    Serial.println(payload); //Print response
  }

  Serial.println("Closing connection");

  char charBuf[500];
  payload.toCharArray(charBuf, 500);

  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"];

  String Confirmed = features_0_attributes["Confirmed"];
  String Deaths = features_0_attributes["Deaths"];
  String Recovered = features_0_attributes["Recovered"];
  long Last_Update = features_0_attributes["Last_Update"]; // not used yet

  display.clear();      // Clear OLED display
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_16);

  display.drawString(0, 0, "Conf: " + Confirmed);
  display.drawString(0, 24, "Dead: " + Deaths);
  display.drawString(0, 48, "Recv: " + Recovered);

  display.display();    // Write the buffer to the display

  delay(60000);
}

VNG Systems

ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
Tested and recommended by OneGuyOneBlog.com:

ESP-Wroom-02 ESP8266+0,96inch OLED+18650 Batterijhouder
VNGsystems.nl
One Comment