Introduction: IoT Based Medicine Reminder System With Email Alert
In our busy and hectic life, we sometimes forget to take medicines on time. In hospitals, it becomes difficult for doctors to remind every patient to take the medicines on time. The perfect solution for this problem should be an Automatic Medicine Reminder Alarm System which will alert the patient either by sending email/SMS or by triggering some alarm. In this tutorial, we will build an IoT Medicine Reminder Project using ESP8266-01 board which will get the time from the internet (NTP server) and send an email as a reminder to take medicines according to the schedule of medication.
Step 1: Components Required
Step 2: Utomatic Medicine Reminder Alarm System Circuit Diagram
ESP8266-01 Wi-Fi Module:
ESP8266-01 is a Wi-Fi module and has an onboard microcontroller manufactured by Espressif. This module help microcontrollers to get connect to the Wi-Fi Network using AT commands through UART communication. There is a range of Wi-Fi modules from ESP-01 to ESP-12 by Espressif Systems, but ESP-01 is very low cost and easily available in the market. It has 8 pins -Two UART Pins (RX and TX), two GPIO pins, one Reset and one CH_PD, and two for 3.3 V and Ground.
IR sensor is an electronic device that is used to detect objects by sensing infrared radiations reflected from the objects. It mainly consists of a transmitter IR LED and a receiver photodiode. It detects infrared radiations that have a wavelength from 700nm to 1mm. When a specific positive voltage is applied across the transmitter LED it transmits the IR rays. If these rays fall on some object then that object reflects back the IR rays which are received by the receiver photodiode. The receiver diode generates a voltage across its terminals which depend on the intensity of light reflected by the object. Generally, the IR receiver LED is darker (black) whereas the transmitter is transparent in color. In this project, one IR sensor will be kept inside the medicine box to detect the opening and closing of the box.
Step 3: Programming ESP-01
ESP8266-01 can be programmed in many ways but in this project, we will be programming it with FTDI Serial Adapter Module. You can also use a USB to TTL converter to program it. ESP8266 is not breadboard-friendly, so here I have soldered all the components on a perf board as per the above circuit diagram. This board can be used to program ESP8266 in future projects. Check the detailed tutorial on how to program ESP-01 using the FTDI module.
Here a Reset button with a pull-up resistor is used to reset the Wi-Fi module and a jumper switch is used to switch the module in programmable mode. During programming mode, GPIO0 PIN (Flash pin) is made grounded with the help of a Jumper cap, and when left open it can be used as a GPIO pin.
Network Time Protocol (NTP)
NTP is a very old networking Internet Protocol (IP) used for synchronization of the time between systems and Data networks. To synchronize computer clock times with extreme precision NTP uses Coordinated Universal Time, offering greater accuracy on smaller networks -- down to a single millisecond in a local area network.. NTP uses UTC as the time reference and provides accurate and synchronized time across the Internet. NTP works on client-server mode, where NTP is the server, and requesting devices like ESP8266-01 acts as a client. The client sends a request to the NTP server and the NTP server responds with a data packet consisting of timestamp and time zone related information.
Step 4: Configure SMTP2GO to Send Email Alert
SMTP (Simple Mail Transfer Protocol) is a platform used to send and receive a large number of emails from remote locations automatically. Due to its fast and reliable service, it is mostly used by developers and marketers to save their time in sending emails in a secure way. Its servers and data centers are all around the world which helps it to select the nearest server and hence provides the fastest connection in sending and receiving emails. It can be used in IoT projects to send emails automatically when a particular task occurs. In this project, we will be using SMTP2GO to send emails alerts when the fire is detected by the flame sensor.
Setting up SMTP2Go:
- Go to https://www.smtp2go.com/ and click on Try SMTP2GO Free.
- Fill the details with your name, email id, and password and click on Submit. Then it will redirect to a page where it will ask you to activate SMTP2GO.
- Go to your mailbox and click on the mail received by SMTP2GO. Click on Activate Account.
- Now enter the username which is your email id and password. A new page will open with your username. Save these usernames and passwords in a notepad file which will be needed later in the Arduino IDE code.
- Now click on Settings and then click on Users. A new page will open with your user name and SMTP server details. Save the SMTP server and SMTP Port as these will be used in the Arduino code to connect with the SMTP server.
Step 5: Encoding to Base64 Value
Before sending the username and password to the SMTPTOGO server, we need to encode them into Base64. To encode them, we will use https://www.base64encode.org/.
Here just enter the username and password you want to encode into base64 and Click on Encode to generate the encoded values. Copy and save the encoded values.
For instance, if your user name is “email@example.com”, then enter this username in the given text area and then click on encode. The encoded base64 value is “cGFzc3dvcmQ=”. Similarly, do it for a password.
Step 6: Code Explanation
First, we have to download and install the NTP library into ESP8266. There are many libraries available for NTP Client. You can install any of them from Arduino IDE. In this tutorial, I have installed the NTPClient library by Taranais because it is easy to use and has functions to get date and time from NTP servers. ESP8266 NodeMCU can be easily programmed using Arduino IDE.
To install the NTP library, first, download the library using the above link and then install it using Arduino IDE. To install it, go to Sketch > Include Library > Add .ZIP Library, then open the Zip folder by going to the location where you have downloaded the zip folder and restart the Arduino IDE.
NTPClient library comes with examples. Open Arduino IDE and Go to Examples > NTPClient > Advanced. The code given in this sketch displays the time from the NTP server on the serial monitor.
Copy the Code for Medicine Reminder System given below and paste it into the Arduino IDE. here we are explaining the complete program to understand the working of the project.
- Start with including all the necessary libraries.
- To request date and time we have to initialize the time client with the address of NTP servers. For better accuracy choose the address of NTP servers which are closer to your geographical area. Here we use “pool.ntp.org” which gives servers from worldwide. If you wish to choose servers from Asia you can use “asia.pool.ntp.org”. timeClient also takes the UTC time offset of your timezone in milliseconds.
- For instance, UTC offset for India is +5:30 so convert this offset in milliseconds which is equal to 5*60*60+30*60 = 19800.
- In the loop function, time is updated every time. Variable “obs” stores the digital value from the IR sensor and “tt” stores the time. A counter “cnt” is created which will count the number of times the box opened before the time.
- If the counter “cnt” is 0, then it means the box is not opened before the time of sending the mail and if someone opens the box before the time, then it simply displays the message on the serial monitor that “Medicine already taken”.
- Client.connect() function which takes SMTP Server and SMTP Port to connect to SMTP server. It gives “1” if the connection establishes successfully and acknowledges the server with the EHLO command.
- Send the encoded user name and password to the server. The user name and the password from the SMTP2GO which we pasted in notepad are encoded to base64 value. The procedure to encode into base64 value is shown above.
- After successful authentication, here the complete email is formed with fields like “To”, “From”, “Subject”, “Body”.
- After sending the data QUIT command is sent to complete the email.
Step 7: Working of IoT Medicine Reminder Alarm
Connections for this Medicine Reminder Project are already shown in the circuit diagram above. Here The RX pin of FTDI is connected to the TX pin of ESP-01 and the TX pin of FTDI is connected to the RX pin of ESP-01. Since the output voltage we get from FTDI is around 4 volts therefore it can’t be used to power the ESP-01 as it will damage the ESP. So we will use a 3.3V DC from a breadboard power supply which takes input power from an adapter and provides a dual output of 5 volts and 3.3 volts. This breadboard power supply module is created by following this tutorial on CircuitDigest.
Make sure to connect the grounds of FTDI and ESP-01 to the ground of the power supply module. GPIO 2 pin of the ESP-01 is connected with the data pin of IR Sensor and it is placed inside the medicine box to detect the opening and closing of the box.
Now copy the complete code and paste in the Arduino IDE. Replace the Wi-Fi credentials with your SSID and password. Change the username and password of SMTP2GO to Base 64 value as shown above. Connect the GPIO0 of the flash pin to the ground for programming mode and connect the FTDI with the PC using the USB port. Click on upload and press the reset button 2-3 times. After successfully uploading the code, disconnect the GPIO0 pin from the ground and press the Reset button once, and open the serial monitor to see the status messages.
The serial monitor will show the Time, No. of times box opened to take medicines, and box status (Closed/open). Suppose time to take medicine is 12 PM and if you have already taken the medicines before 12 PM then at 12 PM it will show the message “Medicines Taken Already”. And if you haven’t taken medicines before 12 PM then the system will work normally like an IR sensor will detect whether the box is not opened and ESP-01 will send a mail regarding the reminder to take medicines using SMTP2GO.
This is how an IoT Medicine Alarm system can be made easily using the ESP-01 module. I hope you enjoyed the tutorial and learned something useful. Share your thoughts on this and If you have any doubts, please leave them in the comment section below.
Also, follow us on Instructables for more such interesting projects.