Gayathri Krishnan
Published © GPL3+

Automatic Medication Alert System Using ESP32 and Twilio API

A simple IoT-based drug reminder that sends an SMS alert at a preset time using ESP32, RTC, and Twilio API.

BeginnerWork in progress1 hour4
Automatic Medication Alert System Using ESP32 and Twilio API

Things used in this project

Hardware components

DS3231M MEMS Precise RTC
DFRobot DS3231M MEMS Precise RTC
×1
ESP32S
Espressif ESP32S
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Schematics

RTC connections

Code

Automatic Medication Alert System Using ESP32 ,RTC and Twilio API

Arduino
ESP32 sends an SMS reminder to your phone through the Twilio API, at a specified time(set using the RTC).
Before running this code:
1. Create a Twilio account and get Account SID, Auth Token, and Twilio number
2. Replace the placeholders below with your actual credentials
#include <WiFi.h>       # ESP32 WiFi library; needed to connect to the internet.
#include <HTTPClient.h> # for sending HTTP/HTTPS requests
#include <Wire.h>       # for I2C communication
#include "RTClib.h"     #  library for DS3231 RTC
#include <WiFiClientSecure.h> # handles secure HTTPS connections for Twilio.

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

const char* accountSid = "YOUR_TWILIO_ACCOUNT_SID";
const char* authToken  = "YOUR_TWILIO_AUTH_TOKEN";
const char* fromNumber = "+TWILIO_NUMBER";   // include +
const char* toNumber   = "+YOUR_PHONE_NUMBER"";          // include +
const char* messageBody = "take thyronorm on empty stomach";

RTC_DS3231 rtc;   #creates an object for communication with RTC

bool sendSMS();

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  Wire.begin();

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

   rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); #adjusting time
WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
Serial.println("\nConnected!");

}

void loop() {
  
 DateTime now = rtc.now();
  int currentHour = now.hour();
  int currentMinute = now.minute();
  int currentSecond = now.second();


  Serial.printf("Time: %02d:%02d:%02d\n", currentHour, currentMinute, currentSecond);



  if (currentHour == 6 && currentMinute == 10 && currentSecond < 10) {
    bool ok = sendSMS();        
    if (ok) {
      Serial.println("sendSMS() reported success.");
    } else {
      Serial.println("sendSMS() reported failure.");
    }
    delay(60000); #wait a minute so it doesn't spam
  }

  delay(10000); # check every 10 seconds
}

bool sendSMS() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected");
    return false;
  }

  # Use secure client for HTTPS
  WiFiClientSecure client;
  client.setInsecure(); 
  HTTPClient https;
  String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(accountSid) + "/Messages.json";

  if (!https.begin(client, url)) {
    Serial.println("HTTP begin failed");
    return false;
  }

  # Basic auth with Account SID and Auth Token
  https.setAuthorization(accountSid, authToken);
  https.addHeader("Content-Type", "application/x-www-form-urlencoded");

  String body = "To=" + String(toNumber) + "&From=" + String(fromNumber) + "&Body=" + String(messageBody);

  int code = https.POST(body);

  if (code > 0) {
    String resp = https.getString();
    Serial.printf("HTTP %d\n%s\n", code, resp.c_str());
    https.end();
    return (code == 201 || code == 200);
  } else {
    Serial.printf("POST failed, error: %d\n", code);
    https.end();
    return false;
  }
}

Credits

Gayathri Krishnan
3 projects • 0 followers
I’m an MBBS doctor who has always been interested in physics, which led me into Arduino projects. I’ve been building for about three months

Comments