Introduction: $5 IoT Pill Bottle

About: I like to learn, like to make, like to share.

Do you forget to take your medication in time? It's not a problem anymore! After reading this instructables you will be able to make your own smart pill bottle which will remind you to take your medication in time!! Cost is only $5!!!

Features:

  1. Low cost (only $5)
  2. SMS and Mobile notification
  3. Run more than 5 months with a single coin cell.

  4. The configurable Android app shows battery level, total pills intake, and remaining pills on the bottle.

Step 1: Required Tools and Materials

1. ESP8266-12E wifi module (gearbest.com)

2. 3x1K resistor

3. 3.6V Lithium Ion coin cell battery (CR2450) ( sparkfun.com)

4. Coin cell battery holder (sparkfun.com)

5. Micro Switch - SPDT (gearbest.com)

6. Perfboard and connecting wires

Tools Required

1. Soldering Station (gearbest.com)

2. Hot Glue Gun (gearbest.com)

3. Wire Cutter (gearbest.com)

4. FTDI USB to TTL Serial Adapter Module (gearbest.com)

Step 2: Circuit Diagram

The circuit diagram for this project is very simple. First, you need to complete some basic connection of ESP8266-12E such as GPIO15 of ESP module must be connected to the ground through a 1K resistor. CH_PD pin should be connected to the VCC through a 1K resistor. RST pin also should be connected to the VCC pin. If you work with ESP module before, definitely you know those are the basic connection of an ESP module.

The circuit will be powered by a coin cell. I connected the +Ve pin of the coin cell to the VCC pin of ESP module through a micro switch which will be on/off automatically at the time of opening and closing the bottle cap. The -Ve terminal of the cell is connected to the ground pin of the ESP module directly.

Working principle:

The completed circuit will be placed in the bottle cap. The micro switch will be attached to the bottle cap in such a manner that, it will be on when connecting the cap to the bottle and off when removing the cap from the bottle. When the switch is on it will power up the ESP module. ESP module will be connected to the wifi and send some data to the cloud. Then it will go into the sleep mode for an infinite time. When some one opens the bottle cap to take the medication esp module will be disconnected from the power source. When he again close the bottle cap after taking medication the switch will be close again and ESP module will be turned on again. It will send some data to the cloud and again goes to the sleep mode. So, every time someone opens and close the pill bottle, ESP module restarts, sends data to the cloud and goes to the permanent sleep.

Power consumption and battery life:

Coin cell CR2450 is an 110mAh capacity battery.

110mAh = 110 x 60 x 60 = 396000mAs.

For normal operation with wifi connected ESP module take 70mA on average.

Assuming, 10s is required to turn on and sending data to cloud. (after that esp module goes to deep-sleep mode)

Total current consumption for every cycle = 70mA x 10s = 700mAs.

If a patient takes medication 4 times in a day, then consumption/day = 700mAs x 4 = 2800mAs.

So, battery lifetime = 396000mAs / 2800mAs = 141 days. Approximately, 5 months.

If a patient takes medication 3 times in a day (more common), then consumption/day = 700mAs x 3 = 2100mAs.

Then, battery lifetime = 396000mAs / 2100mAs = 189 days. Approximately, 6.3 months.

Step 3: Soldering ESP8266-12E on Perfboard

Take a piece of perf board and cut according to the size of the bottle cap. The cap is 4cm in diameter for the bottle I was chosen. So, I cut the perf board in diameter of 3.5 cm so that it can easily adjust into the bottle cap. After cutting the perf board place the ESP module in the middle of the board. I used double sided tape to fixed the ESP module to the pcb board. Then, solder the pads of esp module to the trace of the pcb board. Use thin wire for connecting the pad to the trace. I used jumper wires from an old HDD IDE cable and found it very convenient for the job. For better understanding follow the images I attached with the step.

After soldering ESP module solder one 1K resistor between GPIO15 and GND and one 1K resistor between CH_PD and VCC pin. You may directly connect RST pin with VCC or may use a 1K resistor between them.

Step 4: Soldering Headers for Programming

For programming the ESP module 4 pins of it are required (TXD, RXD, VCC, GND) to connect to the programmer. I recommend soldering pin header into the PCB board so that you can update the program anytime. For entering the esp module into programming mode GPIO0 must be connected to ground. For normal operation, the pin should be left unconnected. So, solder 5 pin header into the PCB board. Pins of the header should be connected with TXD, RXD, VCC, GND, and GPIO0 of the ESP module as shown in the images attached.

Step 5: Adding Battery Holder

As we are going to power up the circuit from a coin cell battery, so we need to connect a coin cell battery holder to the circuit board. The -Ve pin of the battery holder should be connected to the ground pin of the ESP module. For now, the +Ve pin should be kept unconnected. We will connect a micro switch to the pin in a later step.

Step 6: Adding Cap Opening Detection Button

I connected a micro switch between VCC pin of ESP module and +Ve pin of the battery holder. When the switch is close ESP module gets power otherwise not. I put the switch in a way that when the cap is attached to the bottle the switch is closed and when the bottle is open the switch is also open and the ESP module remains disconnected from the power. So, the switch is working as a detector to sense either the bottle is open or closed. In another way, we can say that by this switch we are tracking either the patient is receiving his medication in time or not.

By the way, for tightly fixing the switch in the proper place I used hot glue after soldering the switch with PCB board.

Step 7: Setup Ubidots Cloud

Here, the cloud is the medium of communication between the pill bottle and the Android application. For opening and closing the pill bottle ESP module sent a trigger message to the cloud. It also sends the battery level. From the trigger message, Ubidots calculates total pill intakes and sends it to the Android apps. Android apps calculate total pills remains in the bottle from the intake and refill data and shows the information. If Ubidots does not receive any trigger for a predefined time (gap between pill intakes) it sends a message to the patient to remind him to take the medication.

Let's set up the Ubidots cloud as our requirements.

1. Login to your Ubidots account.

2. Create a device named as Pill Bottle. (img. 1, 2)

3. Create two default variables one for battery level and another for pill intake. (img. 3, 4, 5)

4. Create one rolling variable which makes a sum from all pill intake data to determine total pills intakes.

5. Create an event to send an SMS to the mobile phone defining phone number, message, and conditions. (img. 14, 15, 16, 17, 18)

6. Note down Device ID and Token which will be used in ESP firmware and Android program. (img. 6, 7, 8)

Step 8: Uploading Program & Testing

For programming the ESP module I used Arduino environment. If you have no experience in programming ESP module you may follow this link. You may also follow the instructables Programming the ESP8266-12E Using Arduino Software/IDE. After setting the environment, upload the following program. Don't forget to replace wifi ssid and password with your own. Also change TOKEN and VARID with your Ubidots TOKEN and Variables ID.

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "taifur&mafi";
const char* password = "University";

//Ubidots account data
#define URL    "things.ubidots.com"
#define TOKEN  "Lf5QUxD8ZvDUBN9faXo1fHlsjvRV6L"     // replace with your Ubidots token generated in your profile tab
#define VARID1 "59392ba77625421b589ad853"    // voltage level
#define VARID2 "59392bb47625421b5102f767"   //pill intake

int pill_intake = 1;
ADC_MODE(ADC_VCC); //voltage level measurement

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(500);
  send_data_to_ubidot();
  ESP.deepSleep(0); //sleep forever
}

void loop() {

}

void send_data_to_ubidot(){
   WiFiClient client;
  float voltage = ESP.getVcc()/1024.0;
  int level = 100/3.6 * voltage;
  // put your main code here, to run repeatedly:
 //String containing all the sensors data according to collection endpoint API of Ubidots
  ////Build the JSON packet according to the format needed by Ubidots
  //For more info: http://ubidots.com/docs/api/v1_6/collections/post_values.html
  String payload = "[{\"variable\":\"" VARID1 "\",\"value\":"+ String(level) + "},{\"variable\":\"" VARID2 "\",\"value\":" + String(pill_intake) + "}]";
  String le = String(payload.length());  // How long is the payload

  //For sending data to Ubidots: http://ubidots.com/docs/api/index.html
  // if you get a connection, report back via serial:
  Serial.print("Connecting to ");
  Serial.println(URL);
  if (client.connect(URL, 80))
  {
    // Build HTTP POST request
    Serial.println("connected");    
    client.print(F("POST /api/v1.6/collections/values/?token="));
    client.print(TOKEN);
    client.println(F(" HTTP/1.1"));
    client.println(F("Content-Type: application/json"));
    client.print(F("Content-Length: "));
    client.println(le);
    client.print(F("Host: "));
    client.println(URL);
    client.println(); 
    client.println(payload);
    client.println(); 
    client.println((char)26); //This terminates the JSON SEND with a carriage return
  }
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(100);
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  } 
}

Step 9: Placing Circuit Board in Bottle Cap

You are in the final stage of hardware design. I hope you already completed your circuit design and soldering. Possibly you also uploaded the sketch in the ESP module and tested. If till now all are OK then it is the high time to attach the circuit board to the bottle cap. Place the circuit board into the bottle as shown in the image. Then add some hot glue at the top of the circuit. Then close the bottle cap and wait for a couple of minutes. Your circuit board is now attached to the bottle cap at the right place. Open the bottle cap and check it.

Step 10: Developing Android Application

In this stage, we are going to develop an Android application for our device. If you don't have any practical experience in Android application development, don't panic. We are going to develop the application using App Inventor. Making Android app using App Inventor is super easy and you don't need to have any programming experience for that.

To get started login to ai2.appinventor.mit.edu with your gmail account. Download the source code (PillBottle.aia) from below and import it to your app inventor account. Go to the Block editor and replace Device ID and Token with your own.

To gather some knowledge about App Inventor you may take a look at this link.

Internet of Things Contest 2017

Second Prize in the
Internet of Things Contest 2017

Invention Challenge 2017

Participated in the
Invention Challenge 2017