Introduction: $5 IoT Pill Bottle

Picture of $5 IoT Pill Bottle

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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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.

Comments

sahrudaym (author)2017-10-16

it is showing not connected to the server in the app can u help me

arduino-raspi (author)2017-06-11

This is the thing I am looking for. Thanks for sharing.

taifur (author)arduino-raspi2017-06-12

Thank you and let me know if you need any help.

shrivini16 (author)taifur2017-08-17

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

error: espcomm_upload_mem failed

i am getting this error when i try to upload the code through cp2102 to esp8266.

can u help me?

Jesper MartinS (author)2017-07-26

Hi

I had everything running, but waited for a lithium battery. Now i installed the Android app again, then i set up in config.

Then i come back on the front screen, it starts with showing zero in all.

i press Update - then the first 3 are filled with HUGE numbers, the first are out of screen (ending on 020000), but the next is 150101999, also the third is 150101999. Hours and battery seems to work.

What am I doing wrong??

PS: Why dosn't it update automatically when i open the app?

Hope you have time to help

hi Jasper..can you tell me how you connect the blue wire?? I just can't understand..you can inbox me, I really need your help.thanj you

You should look at the circuit diagram.

i think the blue one, are connected from ESP plus to the micro sw.

i really need your help..can you tell me the blue wire is connected between what? I can't understand about it. Hope you reply me message

Koreanaaaa (author)2017-07-07

Hi,can you tell me all the hardware's name?? I know that it's not complete in here,i hope you can reply my comment.thank you

zposner (author)2017-07-01

I really enjoy seeing cheap IOT. It is so often very expensive.

mjrovai (author)2017-07-01

clever project! Thanks. I only have a trouble with turning off the project when the cap is open. Why do it? You will stay a small time with the cap open when compared with it closed.

Jesper MartinS (author)2017-06-19

Hi again

I found the error

URL things.ubidots.com/api/v1.6/datasources/594163297655421a04898e64/variables/?token=YEbtl5DDIHZgDCLDcRHQs51eZ7Or5B

594163297655421a04898e64 this is the Device ID and is found by pressing the Device (Pill Bottle) showed in the left window

YEbtl5DDIHZgDCLDcRHQs51eZ7Or5B this is the token

NEW ERROR
Select list item: Attempt to get item number 3 of a list of length 2:........

i forgot to add the rolling SUM, now everything works

BUT When i measure the power consumption it fall to 0.07mA, but after 5 min it draw 8mA , Does anyone knows why, i HAVEN'T Connected RST to GPIO16 and IF i had it should boot and draw 70-140mA.????

taifur (author)Jesper MartinS2017-06-19

Just replace the code: ESP.deepSleep(5 * 60 * 1000000); //5 minute sleep

With: ESP.deepSleep(0); // esp will be going to sleep forever.

Jesper MartinS (author)taifur2017-06-19

Hi

Thanks, but that will demand a change in construction, as it wont wake by power cycle, only by reset.

So maybe I can give it instant supply, and use the SW to reset the ESP through a 100 nF capacitor or so. This is my next attempt :-)

Jesper MartinS (author)2017-06-18

H

Thanks for a need project.

I have a working ubidots, but the App won't Connect.

I have only added TOKEN, i Can't see what Device ID is and where to put it.

Can you help??

taifur (author)Jesper MartinS2017-06-19

Log in your Ubidots account and click on the device you created. From the left panel, you will get the device id as like the image.

KaylaM43 (author)2017-06-14

This is awesome but does it work for iPhone or soley android

taifur (author)KaylaM432017-06-19

You have to develop your own app for iPhone.

curtiseggen (author)2017-06-19

Very impressive and instructive 'ible. Thanks! I'm thinking about adapting it to mount in a small, weatherproof box on my mailbox so I can get a text when the letter carrier opens the box (and presumably new mail has arrived).

Vidmo (author)2017-06-14

how long does it take to discharge that battery?

taifur (author)Vidmo2017-06-14

Approximately 6 months. I explain the facts in step 2.

Vidmo (author)taifur2017-06-14

Didn't see that, sorry for question.

JeromeS29 (author)2017-06-13

what about different pills? IE different pills in same bottle.. or is this more for one kind of pills per bottle?

taifur (author)JeromeS292017-06-14

It may be in both way, in a same bottle or different bottle.

javadbacker (author)2017-06-13

Very detailed and very good instructable. Great job.

One suggestion is to upload a demo video if instructables has that feature.

taifur (author)javadbacker2017-06-14

Thank you for your sugestion.

ScienceDiscoverer (author)2017-06-13

Of course, this is a good show of skills that you have, but I think its not that practical device. The problem is - it 100% relays on the internet connection.

Imagine if person will need to go in the place where there is not internet, but still take pills? Or if electricity will shut off? I think its not reliable solution.

In fact, simple timer mobile app would be much more reliable and cheaper, and easier to make!

taifur (author)ScienceDiscoverer2017-06-14

Yes, you are right. If you have internet related issue then it is batter a standalone mobile app.

itsmescotty (author)2017-06-13

Yeah, but. 5 pills in the morning and 4 night - not really a working scenario.

taifur (author)itsmescotty2017-06-13

Customize your Android application and cloud accordingly.

jkimball (author)2017-06-13

I hope you didn't include your real token and wifi password here.

taifur (author)jkimball2017-06-13

This is for testing purpose only.

JoeM314 (author)2017-06-13

Thank you I made two for my grandmother with a hard time remembering to take her heart medication. This has made it easier for us to check times open education versus pill count dead on and grandmother says it makes it easier for to remember too so thank you so very much for your gift of security for our love one

taifur (author)JoeM3142017-06-13

Thank you.

AncientWire (author)2017-06-13

Wonderful! Even if I didn't need a medication reminder, you have shown electronics, programming, and android app creation. Thank you!

taifur (author)AncientWire2017-06-13

Thank you too.

keebie81 (author)2017-06-13

This is technically the 8$ IoT pill bottle. The parts besides battery, perf board, resistors and wires are $4.74. But the battery is $2.95

taifur (author)keebie812017-06-13

Thank you for your research. Price may very from cuntry to country.

keebie81 (author)taifur2017-06-13

Its prices based on the links you provided

About This Instructable

8,798views

159favorites

License:

Bio: I like to learn, like to make, like to share.
More by taifur:Smart PlugArduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2) Solar Powered Bluetooth Headphone (Version 2)
Add instructable to: