Embedded systems are everywhere: in vehicles, in household appliances, in climate monitoring systems in urban spaces, in agribusiness, and so on.
However, one of the utilities that can be very relevant nowadays is as an emergency button for the elderly.
The reason for this is that the elderly are usually more susceptible to accidents (falls, for example), as well as they are more likely to have serious and sudden health problems (heart attacks, for example). And given the circumstances, the time taken to care for the elderly is critical to a greater chance of recovery and, sometimes, survival.
As the elderly may not always have someone taking care of them, it is important to have a way of telling family members that something is wrong. Hence the importance of a system like the one mentioned in this article.
In this line of thought, in this article you will learn how to make a system that works as an emergency button for the elderly, so that by pressing a single button, a message is sent via Telegram to a family member, containing the GPS coordinates of the elderly.
In other words, using your cell phone, from anywhere in the world, it is possible for the elderly to warn of any critical condition, which contributes to a reduction in the time of assistance and increases the chances of recovery and survival of the elderly.
In detail, you will learn:
- How to Program ESP32 with Arduino IDE;
- How to create a bot in Telegram to be used by your ESP32;
- How to get GPS module coordinates;
- How to make ESP32 read the button and, in case of activation, use the created Telegram bot and send an emergency message to a family member.
Telegram is a Russian instant messaging application/service, currently the main competitor of WhatsApp, and having versions for Android, iOS, and the late (or almost) Windows Phone.
In addition, it is an open-source tool (as far as the application is concerned), it allows the creation of bots, secret chats with self-destruct messages (with expiration date) and from the beginning a very light and efficient web interface for use on computers (something that, until a few years ago, did not exist on WhatsApp).
Developing the project Emergency button for the elderly with ESP32, GPS, and TelegramThe schematic circuit of the project can be seen in figure 1.
In terms of hardware, this project makes use of the following components:
- 01 x ESP32 (ESP32 Devkit 1)
- 01 x micro-USB cable
- 01 x GPS V KEL module, model VK2828U7G5LF TTL
- 01 x large button, for easy activation by the elderly.
- Male-male and male-female jumpers
- 01 x power bank with 5V / 2A output, for powering the project without the computer (continuous use)
In general terms, the schematic circuit of the project has three main parts: ESP32 Devkit 1, button, and GPS module. See the details of these parts in detail below.
Below is a picture of the assembled project.
The ESP32 Devkit 1 consists of a complete development board, containing good processing and memories for an embedded device, wi-fi connectivity, Bluetooth connectivity (classic and BLE), a good number of GPIOs available for development and multiple communication interfaces.
In addition, ESP32 has two processors, making it a dual-core solution for IoT projects.
In general, it is a (great) evolution, in terms of computational resources and the number of communication interfaces, of the well-known NodeMCU (ESP8266).
In addition, it has a very inviting cost, being one of the best alternatives for prototypes and products that involve Internet of Things concepts and that require good processing capacity, memories, and connectivity.
Unfortunately, the ESP32 Devkit 1 does not have a perfect shape to fit protoboards (it is a little bit wider than allowed for a perfect fit), which requires less conventional maneuvers for use with protoboards, as shown in the schematic circuit of figure 1, where only one side was connected to the protoboard.
Even so, the ESP32 Devkit 1 is a very interesting option for both professionals and hobbyists/makers who want to do compact projects at a very competitive cost.
Important: in this project, because ESP32 Devkit 1 does not have mobile Internet access natively, we will use Wi-Fi as the Internet connectivity and, as an Internet “source”, a smartphone with router/tethering mode enabled.
Thus, if the elderly person carries the smartphone along with the system, Internet connectivity is guaranteed.
Thus, it is important to leave the smartphone for the elderly with a router/tethering mode already correctly configured and enabled, since elderly people normally have difficulties in dealing with smartphones.The Button
The button is the only interface of the project with the elderly. In other words, the button makes the operation as simple as possible for the elderly: the button is pressed, a notification is sent. Since it will be used by the elderly, it is good practice for this button to be large and colorful.
In the electronic aspect, a relevant factor regarding the button is that, for the purpose of simplifying the circuit, the pull-up (resistor to prevent fluctuation in the GPIO that reads the button, thus preventing false activations) is internal. This makes assembly as simple as possible.
Overview of the GPS ModuleThe project has a GPS module to obtain the geographic location of the elderly, a key factor in their assistance.
The GPS module used is from the manufacturer V KEL, model VK2828U7G5LF TTL. It is a very affordable GPS module, very applicable in low-cost projects.
Furthermore, it is a GPS module that works in both 3.3V and 5V, with applications in a wide range of development boards and microcontrollers.
The GPS module VK2828U7G5LF TTL communicates with ESP32 via UART (UART default configuration: 9600/8 / N / 1), using the NMEA2 protocol.
In this way, it is simpler to process your data, since there are consolidated libraries and already available for free for this purpose. Of the full range of data provided, we will only use latitude and longitude.
Programming ESP32 on the Arduino IDEESP32 Devkit 1 can be programmed in the Arduino IDE so that the development of projects with it is very similar to what is done with any common Arduino.
This facilitates both the development of new projects and the port/transfer of other projects made on other hardware/platforms to the ESP32 Devkit 1.
To prepare the Arduino IDE for development with the ESP32 Devkit 1, I recommend reading the topic “Development with Arduino IDE” in this article.
Installing the library to use Telegram on ESP32You must install the UniversalTelegramBot library, the library that allows you to use Telegram on ESP32.
This library can be installed simply and quickly via Arduino IDE, in the library manager (Sketch> Include Library> Manage Libraries).
Just enter her name (UniversalTelegramBot) in the search field and install the newest versions of them.
Important: for the UniversalTelegramBot library to work correctly, you must install the ArduinoJson library on any version of 5.x. Install it via Arduino IDE with the same procedure done at UniversalTelegramBot.Installing the library to use GPS
It is necessary to install the TinyGPS ++ library, the library capable of processing the data that the GPS module sends and extracting geographic coordinates from them (the only GPS information that will be used in this project).
This library can be installed as follows:
- Download it from this link: https://github.com/mikalhart/TinyGPSPlus/archive/master.zip
- Save the library's.zip file to an easily accessible / known folder.
- In the Arduino IDE, go to Sketch> Include Library> Manage Libraries> Add.ZIP Library.
- In the window that opens, go to the folder where you saved the library's.zip file, click on the.zip file and press the Ok button.
Ready! The TinyGPS ++ library is installed and ready to use.
Creating a Telegram botAll bot management in Telegram is done from a bot master, called Bot Father (@BotFather).
- The first step is to find him on Telegram and start a conversation with him.
- To start creating your Telegram bot, type the command / newbot in the chat with @BotFather.
- You will be asked by @BotFather to provide a name for your Telegram bot. Enter a name without spaces and special characters (for example: projeto_botao_emergencia_bot).
- Then, if the name is approved by @BotFather (approval is required to ensure that there is no bot with the same name), you will be asked to provide a username for your bot. You can use the bot's own name for the username, since it ends in bot.
- If the username is approved by @BotFather (approval is required to ensure that there is no bot with the same username), your bot will be created! You will receive a token (a unique key for your bot) and the address to "chat" with your bot. Keep this token in a safe place, it will be needed in the project's source code.
Assuming that you will be the one to receive the warning if the button is pressed by the elderly, you should obtain your ChatId on Telegram.
The ChatId of your personal Telegram account is a unique code / key for your account, so it will allow the Telegram bot to send messages only to you (as if it were your contact on Telegram). To obtain this ChatId, follow the procedure below:
- Look for the Chat ID Echo bot (@chatid_echo_bot). This bot is used to provide the ChatId of your personal Telegram account
- Send the message / command / start to @chatid_echo_bot
- Then @chatid_echo_bot will return your ChatId. Keep this token in a safe place, it will be needed in the project's source code.
Next, we will present the source code of the project.
Source code of this Project
Now, we will start presenting the source code.
The way to program ESP32 Devkit 1 with this source code is very similar to programming any other common Arduino: copy and paste it into your Arduino IDE, change the contents of the ssid_wifi and password_wifi variables respectively with the name and password of the wifi network that the ESP32 Devkit 1 should connect to, compile and upload/schedule the ESP32 Devkit 1.
Also, don't forget to replace your Telegram bot's token (obtained with @BotFather) in BOT_TELEGRAM_TOKEN and to replace the ChatId of your personal Telegram account (obtained with @chatid_echo_bot) in auth_id.
Read the source code comments carefully to fully understand it.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <TinyGPS++.h>
/* Definições - UARTS */
#define BAUDRATE_SERIAL_DEBUG 115200
#define BAUDRATE_SERIAL_GPS 9600
#define GPS_TX 18
#define GPS_RX 19
/* Definição do GPIO do botão de emergência */
#define GPIO_BOTAO 13
/* Definição do token de seu Bot Telegram
(valor obtido no @BotFather) */
#define BOT_TELEGRAM_TOKEN " "
#define TEMPO_ESPERA_BOTAO 100 //ms
/* Wifi */
/* Coloque aqui o SSID (nome da rede wifi) que o
ESP32 deve se conectar
*/
char ssid_wifi[] = " ";
/* Coloque aqui o password (senha da rede wifi) que o
ESP32 deve se conectar
*/
char password_wifi[] = " ";
/* Variáveis e objetos globais para usar o GPS */
float latitude = 0.0;
float longitude = 0.0;
/* Variáveis e objetos globais para usar o wifi e Bot Telegram */
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TELEGRAM_TOKEN, client);
TinyGPSPlus gps;
/* Coloque aqui o auth_id de sua conta telegram
(auth_id da conta que o bot deve alertar)
*/
String auth_id = " ";
/* Protótipos */
void init_wifi(void);
void conecta_wifi(void);
void verifica_conexao_wifi(void);
void configura_gpio_botao(void);
/* Função: inicializa wi-fi
* Parametros: nenhum
* Retorno: nenhum
*/
void init_wifi(void)
{
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("------WI-FI -----");
Serial.print("Conectando-se a rede: ");
Serial.println(ssid_wifi);
Serial.println("Aguarde...");
conecta_wifi();
}
/* Função: conecta-se a rede wi-fi
* Parametros: nenhum
* Retorno: nenhum
*/
void conecta_wifi(void)
{
/* Se ja estiver conectado, nada é feito. */
if (WiFi.status() == WL_CONNECTED)
return;
/* refaz a conexão */
WiFi.begin(ssid_wifi, password_wifi);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Conectado com sucesso a rede wi-fi ");
Serial.println(ssid_wifi);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
/* Função: verifica se a conexao wi-fi está ativa
* (e, em caso negativo, refaz a conexao)
* Parametros: nenhum
* Retorno: nenhum
*/
void verifica_conexao_wifi(void)
{
conecta_wifi();
}
/* Função: configura GPIO para leitura do botão de emergência
* Parametros: nenhum
* Retorno: nenhum
*/
void configura_gpio_botao(void)
{
pinMode(GPIO_BOTAO, INPUT_PULLUP);
}
void setup()
{
/* Inicializa serial de debug */
Serial.begin(BAUDRATE_SERIAL_DEBUG);
/* Inicializa serial de comunicação com módulo GPS */
Serial1.begin(BAUDRATE_SERIAL_GPS, SERIAL_8N1, GPS_RX, GPS_TX);
/* Configura GPIO para leitura do botão de emergência */
configura_gpio_botao();
/* Inicialização e conexao wifi */
init_wifi();
conecta_wifi();
}
void loop()
{
char str_msg[200];
/* Verifica se conexão wifi está ok.
Caso não estiver, refaz a conexão. */
verifica_conexao_wifi();
/* Le bytes vindos do módulo GPS e os direciona para biblitoeca GPS */
while (Serial1.available() > 0)
{
gps.encode(Serial1.read());
}
/* Obtem a latitude e longitude parseada pela biblioteca GPS */
latitude = (float)gps.location.lat();
longitude = (float)gps.location.lng();
/* Verifica se há acionamento do botão de emergência */
if (digitalRead(GPIO_BOTAO) == LOW)
{
delay(200);
if (digitalRead(GPIO_BOTAO) == LOW)
{
/* Avisa, por Telegram, que houve acionamento do botão de emergência */
sprintf(str_msg,"Botão de emergência acionado! GPS: %f,%f", latitude, longitude);
bot.sendMessage(auth_id, str_msg, "");
Serial.print(str_msg);
Serial.println(". Avisando via Telegram...");
/* Aguarda botão de emergência ser desacionado
(para notificar via Telegram só uma vez) */
while(digitalRead(GPIO_BOTAO) == LOW)
{
delay(1);
}
/* Espera alguns ms para evitar que, se ocorrer
oscilações no sinal digital do botão, estes sejam
erroneamente considerados. */
delay(TEMPO_ESPERA_BOTAO);
}
}
}
So, curious to know how it works?
Project operationThe project works as follows: once connected and connected to wifi, it will already be operating as a Telegram bot.
When the button is pressed, it will send a message via Telegram directly to the contact referred to the ChatId, as shown in figure 3.
In this way, just connect the project in a location with access to the wifi network that it is programmed and ready!
Now, we'll construct a PCBGOGO Printed Circuit Board for this project.
PCBGOGO Printed Circuit BoardFor this project, we developed this PCBGOGO electronic board. It has connection pins to connect the ESP32 and 2 connectors to fit the GPS module and the button.
The electronic schematic of the PCBGOGO printed circuit board design is presented below.
All files can be downloaded from this link and you can purchase your cards directly from PCBGOGO.
ConclusionIn this article, you learned how to make your own emergency button system for the elderly, using ESP32 Devkit 1, GPS, and Telegram. This project gives the opportunity to reduce the time to help an elderly person in an emergency situation, which can be decisive for their recovery and survival.
This article can open your mind to new projects and products using IoT (Internet of Things) and instant communicators like Telegram, so that your projects can interact directly with others quickly, simply and from anywhere in the world via the Internet.
Comments