Data of confirmed infections and deaths during the Covid-19 pandemic strongly illustrate the seriousness of the situation.
Many websites or smartphone apps are available to view and compare this data. Interfaces and APIs to current data sets are also available, which makes it possible to evaluate and display the data by yourself.
I was interested to visualize that data on mi band like fitness tracker. Actually I was so bored due to current lockdown situation and I had nothing to do.
Luckily I had T-Wristband around me with which I could easily get data from website because it has ESP32-PICO-D4 as main microcontroller and a 0.96 inch TFT color display.
Source of dataI was using www.coronatracker.com for tracking and I manage to get data out of that wesite using its non-public API. I used chrome browser's inspect element feature to find out the API.
I found these API
- Data of all country :- api.coronatracker.com/v2/analytics/country
- Data Related to specific country :- api.coronatracker.com/v3/stats/worldometer/country?countryCode=US
This data is encoded in JSON so we decode it in Arduino using ArduinoJson Library I used wemos-esp32 to test the code.
Parsing the dataWhen the connection to the server is established, the JSON file can be fetched. The header data should be skipped and the JSON data can be read and evaluated line by line:
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin("https://api.coronatracker.com/v2/analytics/country"); //HTTP
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
Serial.println(payload);
}
}
else
{
Serial.printf("[HTTP]GET failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
The JSON file has the following structure:
[
{
"countryCode":"US",
"countryName":"United States",
"lat":37.09024,
"lng":-95.712891,
"confirmed":164931,
"deaths":3162,
"recovered":1868,
"dateAsOf":"2020-03-31T16:35:04.000Z"
},
{
"countryCode":"IT",
"countryName":"Italy",
"lat":41.87194,
"lng":12.56738,
"confirmed":101739,
"deaths":11591,
"recovered":14620,
"dateAsOf":"2020-03-31T16:35:04.000Z"
}.....................]
So I used ArduinoJson to Decode The Data..
deserializeJson(doc, payload);
for (int i = 0; i <= 95; i++)
{
Serial.println(i);
JsonObject covid = doc[i];
const char* countryCode = covid["countryCode"];
const char* countryName = covid["countryName"]; // "United States"
float lat = covid["lat"];
float lng = covid["lng"];
long confirmed = covid["confirmed"];
int deaths = covid["deaths"];
int recovered = covid["recovered"];
const char* dateAsOf = covid["dateAsOf"];
Serial.println("countryCode : " + (String)countryCode);
Serial.println("countryName : " + (String)countryName);
Serial.println("confirmed : " + (String)confirmed);
Serial.println("recovered : " + (String)recovered);
Serial.println("deaths : " + (String)deaths);
Serial.println("dateAsOf : " + (String)dateAsOf);
Serial.println("---------------------------");
delay(50);
}
There are total 95 country in the JSON data so I looped all the data one by one
/**
BasicHTTPClient.ino
Created on: 24.05.2015
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
WiFiMulti wifiMulti;
const size_t capacity = JSON_ARRAY_SIZE(96) + 10 * JSON_OBJECT_SIZE(6) + 86 * JSON_OBJECT_SIZE(8) + 11110;
DynamicJsonDocument doc(capacity);
const char* ssid = "XXXXXXXX";
const char* password = "xxxxx";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
wifiMulti.addAP(ssid, password);
}
void loop() {
// wait for WiFi connection
if ((wifiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin("https://api.coronatracker.com/v2/analytics/country"); //HTTP
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Serial.println(payload);
deserializeJson(doc, payload);
for (int i = 0; i <= 95; i++)
{
Serial.println(i);
JsonObject covid = doc[i];
const char* countryCode = covid["countryCode"];
const char* countryName = covid["countryName"]; // "United States"
float lat = covid["lat"];
float lng = covid["lng"];
long confirmed = covid["confirmed"];
int deaths = covid["deaths"];
int recovered = covid["recovered"];
const char* dateAsOf = covid["dateAsOf"];
Serial.println("countryCode : " + (String)countryCode);
Serial.println("countryName : " + (String)countryName);
Serial.println("confirmed : " + (String)confirmed);
Serial.println("recovered : " + (String)recovered);
Serial.println("deaths : " + (String)deaths);
Serial.println("dateAsOf : " + (String)dateAsOf);
Serial.println("---------------------------");
delay(50);
}
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(10000);
}
And than I ported This Code to T-Wristband and Added Code For TFT display.
Data displayT-Wristband 0.96 Inch TFT Screen
I added some logo and Stayhome sticker from Instagram.
As you tap the button on screen it goes thrue every country in order of total confirm case of Covid-19.As if now the United States is first on the list.
It currently show total Confirmed case followed by Recovered case and than Deaths.
Final ResultHere is a video that I made with covid-19 tracker
I hope this application and the code can prove to be useful to the wider community.Feel free to message me here if you have questions or comments.
Enjoy!
Regards,
Chahil Patel
Comments