ESP32 Covid19 Tracker Project

Kirill Yatsenko
5 min readOct 17, 2020

Intro

I recently started learning embedded systems and already have something to share with you. In this project, I’m using the esp-idf which is the framework by espressif. The chip supports the Arduino IDE. But I believe that Arduino libraries are more suitable for fast prototyping. You will open the whole potential of the board by using the official ESP32 framework.

I decided it won’t be a regular tutorial and more like an explanation of some tricky parts of the project. So you won’t waste your time reading.

Note: This tutorial is not for the complete beginners though. So please start with the blinking led project

By completing this project you will learn how to:

  1. Connect to the local Wi-Fi access point
  2. Synchronize time by using SNTP
  3. Make external HTTP rest call
  4. Parse JSON response with cJSON library
  5. Present data on the i2c display

Prerequisites:

  1. ESP32 dev board of course :) I have one of these
  2. i2c monochrome display. For example
  3. esp-idf installed. Installation guide
  4. Downloaded project: https://github.com/KirillYatsenko/covid-tracker

For getting coronavirus cases https://covid19api.com/ is used. It’s free API and you don’t need to sign up.

Running the demo

I recommend first to try to run the application and then try to understand how it works.

The first step would be to wire up the display. Since it’s working on a simple i2c protocol you will only need 2 data lines connected to it. You should check your ESP32's datasheet but in general SCL = 22GPIO and SDA = 21GPIO

i2c display wiring

Configuring project

Inside the project run:

idf.py menuconfig

Navigate to the COVID19 menu and fill in SSID and Wi-Fi password. For the country please use https://api.covid19api.com/countries. You will get the list of countries supported by the API. You need the “Slug” value.

COVID19 menu

Now you can run the app.

Connect esp32 to the computer and type:

idf.py flash monitor

If everything goes good you will see covid19 status in your country

Ukraine Covid19 statistic

Digging into the code

Note: Before reading further please try to figure out how the code works by yourself it’s really simple and straightforward. I will explain a few things in this section.

connect.c

event_handler is a callback function inside connect.c for the events that’s happened with the esp32 chip. You can read more here.

When the function receives the SYSTEM_EVENT_STA_GOT_IP event it’s “giving” to the connectionSemaphore meaning it’s producing the event to this semaphore.

main.c

On the other side inside the main.c file in OnConnected function the code is waiting for semaphore event. The timeout for this operation is 10 seconds.

The good thing about semaphore that it won’t actually block the thread and it’s implemented by using interrupters.

sntp-time.c

When synchronizing time with the SNTP server we should wait for time to be set inside while loop and delaying for 2 seconds if SNTP sync status is still Reset.

Once we have time synchronized we can call covid19 API. A new task for calling external API should be created because sometimes you can get a stack overflow error when the HTTP headers are too big and the chip can panic and will reboot itself. I’ve set the stack to be 10kb in size for the new task.

fetch.c

clientEventHandler is the callback function inside fetch.c file for the HTTP client. It’s going to be called several times while making get a request so we are getting data in chunks and save them into the buffer. Once we’ve got HTTP_EVENT_ON_FINISH status we are calling the OnGotData callback function with buffer as a formal.

Once we’ve got a buffer we can parse it as a JSON by using cJSON library which is included inside the esp-idf framework.

For communication with the display, we are using u8g2 library which is specifically designed for monochrome displays. Binaries are already included in the project so you don’t need to download them separately. But in order to use this library, we should add u8g2_esp32_hal(Hardware Access Layer) file for the esp32 board. The use of this file is to basically map esp32 hardware to the library functions. You can read more here.

Summary

Follow-ups: You can add live updates for covid statistics so you don’t need to reboot chip in order to get new data.The API: https://covid19api.com supports this functionality.

Credits: https://www.learnesp32.com I’ve really learned a lot from this course and recommend you to buy it. It’s not advertising(sort of) but no one asked me :) Half of the project’s code is taken from this course.

Source code: https://github.com/KirillYatsenko/covid-tracker

If you have any questions please email me: kiriyatsenko@gmail.com

--

--