- The motivation
- Description
- Typical Application
- Board of Materials
- Setup Procedure
- Arisen issues during development and solutions
- Gallery
It was in 2020, a year that all people on Earth won't forget about the global pandemic.
In Canada, beside other physical methods such as social distancing and sanitization, the only popular technological methods to slow the virus spread was COVID Alert App
. COVID Alert App
is a mobile app on both iOS and Android platforms. It can only work when the user enables Bluetooth on their smartphone. The smartphone will interact with nearby smartphone by exchanging encrypted bluetooth messages and store all the connection in the smartphone only. When an app user got infected by virus, he/she has to declare this to the app and the app will send alert from its database to other app users who have close contacted with that person.
Click here to learn more about the COVID Alert App.
However, there were many issues that this application cannot tackle:
Many people were concerned about how their private information will be stored and used and who could access them.
It could be that people left their phone at home when they go out, the WiFi connection is terrible to get alert in time, or simply they don't have a compatible smartphone
Smartphone users usually turn off the Bluetooth to increase battery life. Therefore, many app users may coincidentially leave the app running without Bluetooth which are useless and ineffective.
Not everyone can afford a compatible smartphone, and not everyone can have regular WiFi access. This is the biggest challenge if the app is applied in remote areas and developing countries.
Therefore, this project arises to overcome all those challenges. Additionally, this is my first project with both ESP32 microcontroller
and Raspberry Pi 4 single-board computer
.
The entire project consists of:
- One single
Raspberry Pi 4 single-board computer
as the database server, placed in a secure place with continuous power supply (cooling solutions such as heat sink, fan are recommended for long-term usage) - Multiple
Dr.ESP32
devices which contain aESP32
microcontroller and aLiFePO4 3.2V 1200mAh
battery as client device, attached to or come along with the user. Each device can run continuously for 14-15 hours under each full charge.
For example, this system is implemented in a supermarket. Each Dr.ESP32
is attached to the buggy or cart. The device should be disabled (under waiting state) in the buggy or cart station area. Whenever a new customer enters the supermarket and takes the buggy or cart out of the station, the Dr.ESP32
is activated (under working state). In this state,Dr.ESP32
continuously searches for any nearby Dr.ESP32
by scanning for any Bluetooth Low Energy (BLE) signal. The Dr.ESP32
will record that Dr.ESP32
in the contact list with the date and sent them to the database server (or store in EEPROM if it cannot connect to the server) if that Dr.ESP32 transmits a BLE signal that:
- is strong enough ( indicating that a nearby customer is standing closer than 2 meters from the original user )
- has correct BLE Address ( a list of BLE Address of each
Dr.ESP32
is already stored in the sketch to identify if that is really from aDr.ESP32
or from smartphone, headphones,... and whichDr.ESP32
it is )
When the customer checks out, the security guards will ask for his/her email and enter the device number to the database so the server can link the customer's email and the Dr.ESP32
device number together. In the future, this process will be automated by scanning customer member card (such as More Reward card, Air Miles card,..) and scan the NFT of the cart to get the Dr.ESP32
device number.
In the future, if one of the past customers is infected, he/she can send the email to the supermarket with his/her virus test result for verification purposes. Once a staff reviews his/her email, they will enter the infected email into the database server. The server will automatically send an alert email to all people who had close contact with him/her.
Below table is for a project with 1000 Dr.ESP32
devices. Change is neccessary accordingly to the number of Dr.ESP32
devices in the real application. Price is not including enclosure.
Device | Digi-Key Part Number | Number | Price |
---|---|---|---|
Raspberry Pi 4 Model B 4GB | 1690-RASPBERRYPI4B/4GB-ND | 1 | 87 |
ESP32-WROOM-32D(4MB) | 1965-ESP32-WROOM-32D(4MB)CT-ND | 1000 | 5601.40 |
1200mAh 3.2V LiFePO4 battery | - | 1000 | 3250 |
Linear Voltage Regulator IC 1.5A | 296-12602-1-ND | 1000 | 362.50 |
BJT PNP 65V 6A 100mA 100Mhz 200mW SMD | BC856AW,135 | 1000 | 25.206 |
Shunt Voltage Reference IC 36V 1% SMD | TL431ACDBZT | 1000 | 672.00 |
2.5 Ohm 1% 1/8W SMD 0805 | CRCW08052R50FKTA | 2000 | 45.2 |
470 Ohm 1% 0.1W SMD 0603 | CR0603-FX-4700ELF | 1000 | 3.91 |
110 Ohm 1% 0.1W SMD 0603 | RC0603FR-07110RL | 1000 | 5.59 |
1 kOhm 1%, 0.1W SMD 0603 | ERJ-3EKF1001V | 1000 | 8.59 |
2.49 kOhm 1% 0.1W SMD 0603 | ERJ-3EKF2491V | 1000 | 8.59 |
Total cost is 10069.986. An average of 10.07 for each device.
Unit of currency: CAD
Still under review but here is a sense of it. Update: Below circuit is ineffective anymore
In this project, I use Arduino IDE for compiling and uploading the program. It is a free software and most tinkerer will be familiar with.
- First of all, if this is the first time you use ESP32 in Arduino IDE, you should install ESP32 Add-on for Arduino IDE
- Secondly, you may want to install the library PubSubClient.h library
- Thirdly the ESP32 can only store 1,310,720 bytes of sketch under default, which is not enough for our program (~ 1.5 KB). You should extend the sketch storage of the ESP32 by repartition the ESP32:
-
Change
default.csv
file- Open file
C/Users/”YourName”/AppData/Local/Arduino15/packages/esp32/hardware/esp32/1.0.4/tools/partitions/default.csv
- Change it into
#Name Type SubType Offset Size Flags nvs data nvs 0x9000 0x5000 otadata data ota 0xe000 0x2000 app0 app ota_0 0x10000 0x340000 eeprom data 0x99 0x350000 0x1000 spiffs data spiffs 0x351000 0xAF000 - Open file
-
Change
boards.txt
file- Open file
C/Users/”YourName”/AppData/Local/Arduino15/packages/esp32/hardware/esp32/1.0.4/boards.txt
- Change the first block into
esp32.name=ESP32 Max esp32.upload.tool=esptool_py esp32.upload.maximum_size=3407872 esp32.upload.maximum_data_size=327680 esp32.upload.wait_for_upload_port=true esp32.serial.disableDTR=true esp32.serial.disableRTS=true esp32.build.mcu=esp32 esp32.build.core=esp32 esp32.build.variant=esp32 esp32.build.board=ESP32_DEV esp32.build.f_cpu=240000000L esp32.build.flash_size=4MB esp32.build.flash_freq=40m esp32.build.flash_mode=dio esp32.build.boot=dio esp32.build.partitions=default esp32.build.defines=
- Open file
-
- Now, you are ready to compile and upload the program code onto the ESP32 microcontroller. There are 3 files in total:
- Remember to modify the
ssid
andpass
variables in the ESP32.ino file corresponding to your WiFi name and WiFi password.
Follow this tutorial to install the OS. I recommend to install the Raspberry Pi OS (32-bit) Lite.
Notice: In step sudo nano /etc/dhcpcd.conf, use these lines instead:
interface wlan0
static ip_address = A/24
static routers = B
static domain_name_servers = B
where A is the Raspberry Pi IP Address (first 8-digit address from the command hostname -I
) and B is A with last digit replaced by 1
Follow this tutorial to install the LAMP Server on Raspberry Pi 4.
- Preparing Your MySQL Database:
- Open your browser and type
http://A/phpmyadmin
where A is the Raspberry Pi IP Address - Login by the account you setup before
- Select the
Databases
menu at the top, complete theCreate database
fields by the drop down menu:- esp_data
- utf8mb4_general_ci
- Press the
Create
button - save your database name as esp_data
- Open your browser and type
- Create SQL tables:
- Create SQL table to store contact between devices:
- In the left sidebar select your database name
esp_data
- Open the
SQL
tab, Copy this SQL query into the SQL query field then press theGo
button to create your table
- In the left sidebar select your database name
- Create SQL table to store customer information:
- In the left sidebar select your database name
esp_data
- Open the
SQL
tab, Copy this SQL query into the SQL query field then press theGo
button to create your table
- In the left sidebar select your database name
- Create SQL table to identify the infected customer :
- In the left sidebar select your database name
esp_data
- Open the
SQL
tab, Copy this SQL query into the SQL query field then press theGo
button to create your table
- In the left sidebar select your database name
- Create SQL table to store contact between devices:
- Create PHP files in
Raspberry Pi 4
:- PHP Script HTTP POST - Insert contact data into COVID 19 Database
- Connecting to Raspberry Pi with an SSH connection, then type
sudo nano /var/www/html/post-esp-data.php
- Copy this PHP script to the newly created file
- Remember to modify the $dbname, $username and $password variables with your own ones
- Connecting to Raspberry Pi with an SSH connection, then type
- PHP Script - Display COVID 19 Database Content
- Type
sudo nano /var/www/html/esp-data.php
- Copy this PHP script to the newly created file
- Remember to modify the $dbname, $username and $password variables with your own ones
- Type
- PHP Script - Insert customer information into CustomerInfo Database
- Type
sudo nano /var/www/html/checkOutCustomer.php
- Copy this PHP script to the newly created file
- Remember to modify the $dbname, $username and $password variables with your own ones
- Type
- PHP Script - Temporarily store infected customers from the original customer
- Type
sudo nano /var/www/html/trackInfection.php
- Copy this PHP script to the newly created file
- Remember to modify the $dbname, $username and $password variables with your own ones
- Type
- PHP Script HTTP POST - Insert contact data into COVID 19 Database
- Create Python file to send email upon request by LAMP Server
- Connecting to Raspberry Pi with an SSH connection, then type
sudo nano /var/www/html/newmailing.py
- Copy this Python script to the newly created file
- Remember to modify the
username
andpassword
variables. This project will use a Gmail account to send out alert email.
- Connecting to Raspberry Pi with an SSH connection, then type
- Too big sketch size
- As mentioned in Programming for Raspberry Pi 4, the default available sketch size is 1,310,720 bytes meanwhile the program sketch size is ~1.5 KB so we have to change the partition setting of ESP32
- Constantly rebooting when being powered by battery
- There is a brownout sensor in ESP32 that makes the ESP32 reboot if it detects the supply voltage drops significantly. As our
Dr.ESP32
is powered by battery, during setup, it has to draw huge current to initialize BLE and WiFi, leading to the significant supply voltage drop. Therefore, ourDr.ESP32
keeps rebooting and cannot work. To overcome this, we have to disable the brownout sensor during starting BLE and WiFi. - The solution is by adding these lines of code to disable brownout sensor at the beginning of setup and enable brownout sensor at the ending of setup.
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
to disableWRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1);
to enable
- There is a brownout sensor in ESP32 that makes the ESP32 reboot if it detects the supply voltage drops significantly. As our
- BLE connection is waiting for too long
- I used to make the
Dr.ESP32
exchange device number to recognize each other. However, there are some moments the BLE connection cannot connect and it just waits for really long time for the connection ( ~50 days in default) until it proceeds to the next instructions. - The solution is by changing the waiting time in the
FreeRTO.cpp
file:- Go to
C/Users/”YourName”/AppData/Local/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/FreeRTO.cpp
- Change
xSemaphoreTake(m_semaphore, portMAX_DELAY)
intoxSemaphoreTake(m_semaphore, 3000)
- Go to
- I used to make the
Click on the image to play the demonstration video