IoT Keychain Finder Using ESP8266-01

by ashvnv in Circuits > Electronics

7615 Views, 25 Favorites, 0 Comments

IoT Keychain Finder Using ESP8266-01

main.png
buzz site.png
desk.png
IoT keychain finder using ESP8266-01 preview

Are you like me always forgetting where you kept your keys? I can never find my keys on time! And because of this habit of mine, I have been late for my college, that limited edition star wars goodies sale (still fretting!), a date (she never picked my call again!)

So what exactly is this IoT Keychain

Well let me give you an abstract idea, imagine you planned a dinner with your parents at a posh restaurant. You were just about to hit the road suddenly the keys are missing, ouch! You know the key is somewhere in the house. Then you remember, hey I attached a IoT keychain which I made referring Ashwin's Instructable, Thank God! You take out your phone and open Chrome then type keychain IP (eg- 192.168.43.193/) or mycarkey.local/ (this works because of mDNS) and hit search. Wow!, a site appears in your phone (imagine your keychain is the server, so weird!). You click on Buz My Key button and in moments you hear a beep coming from your work shoes (jeez these cats). Well you found the keys and hit the road in no time, voila!

A brief idea on how it works

Well the ESP-01 in the Keychain connects to any WiFi that you have mentioned in the program (you can mention multiple WiFi names along with their pass-codes and ESP-01 will connect to the strongest available WiFi network at that point). If you take the keychain outside your WiFi range, ESP-01 will probably disconnect and try to connect to the available mentioned WiFi (so if you misplaced your key at your friend's house you can find it easily by just turning on the your phone's hotspot (no data required) and ESP-01 will connect to your hotspot automatically and then you can buz the keychain and find it easily).

Before starting I would recommend all the first time ESP users to read A Beginner's Guide to the ESP8266 by Pieter P. Click here. This guide has been very helpful for me as a beginner to ESP8266 chip.

What is the relation between ESP8266 and ESP-01

When I started working with ESP I got quite confused. There were a lot of information about ESP chips on the internet. I used to think ESP8266, ESP-01, ESP-12E etc were all different and can't use program written in ESP-01 on ESP-12E but that not the case. Let me clarify your doubts! ESP8266 is a chip which is used in all the ESP module (like ESP-12E and ESP-01). There are many more ESP modules available in the market and they all use ESP8266 chip. The only difference between them is the functionality that the ESP module is providing. Say ESP-01 has quite less GPIO pins while ESP-12E has a lot of GPIO pins. ESP-01 may not have different sleep modes like ESP-12E while ESP-01 is more cheaper and small in size.

Keep in mind as they all use the same ESP8266 chip, we can use the same ESP8266 program on all the ESP modules without any problems as long as you are not using a program which can work on one specific chip only (say you are trying to turn on GPIO pin 6 on ESP-01 which it does not have. No worries and programs I gave in this tutorial is compatible with all ESP modules. In fact I did all the coding on ESP-12E NodeMCU as it was more easy to work and debug errors on development board. After being convinced with my work I then tried those programs on ESP-01 which worked like charm without any modifications!

Some key points:

  • My goal is to help you understand how we can embed IoT anywhere.
  • The main takeaway from this Instructable is the knowledge of embedding ESP-01 inside a keychain which seems bizarre but hey, engineering is full of challenges! I recommend everyone to come up with different keychain designs and try to make IoT keychain idea perfect.
  • The IoT keychain which I have made is not much battery efficient (6 Hours with 500mAH 3.7v Li-Po battery) and is a little bulky. But I know, you guys can make it perfect if not better and make your own Instructable (don't forget to mention me!)

Enough bla bla bla! Lets get started

How my Instructable flows

  1. Required Materials and Components [Step 1]
  2. ESP-01 Getting Started [Step 2]
  3. Lets Ready the Buzzer for ESP-01 [Step 3]
  4. Getting Ready for Programming [Step 4]
  5. Personalizing the program [Step 5]
  6. Lets program ESP-01 [Step 6]
  7. IP and mDNS for controlling buzzer [Step 7]
  8. Selecting a suitable battery [Step 8]
  9. Placing all the components [Step 9]
  10. Preparing outer cover for placement of the keychain circuit and battery [Step 10]
  11. Finishing thoughts [Step 11]

Required Materials and Components

Components.png
additional components.png

So you are ready, great!

I have mentioned all the components that is used in this Instructable in the pic above (a picture is worth a thousand words)

ESP-01 Getting Started

esp8266_esp01_horizontal-01.png

I have used many ESP modules but I have to say ESP-01 is my favourite ESP8266 module as it is smallest and cheap.

There are total 8 pins on ESP-01. I have provided the pin diagram image above.

We will be using Arduino UNO board and Arduino IDE for programming the ESP-01 as many of you must be having Arduino at home.

There are two modes in the ESP-01:

  • Programming mode
  • Normal boot mode

To change the modes we only require to toggle RST and GPIO 0 pins.

ESP8266 will check at boot to which mode it should boot into. It does this by checking the GPIO 0 pin. If the pin is grounded 0V ESP will boot into programming mode. If the pin is kept floating or connected to 3.3V ESP boots normally.

RST pin is active low so 0V at RST pin will reset the chip (just touch the RST pin to ground for a second)

For normal boot mode: GPIO 0 should be either floating or connected to 3.3V after resetting or booting the chip for the first time

For programming mode: GPIO 0 should be grounded after resetting or booting the chip for the first time and stay grounded till the programming is over. To come out of this mode just remove the GPIO 0 pin from the ground and keep it either floating or connect to 3V then ground the RST pin for a second. ESP boots back into normal mode.

ESP-01 has 1MB flash memory.

Warning! ESP-01 works with 3.3V, if you give more than 3.6V to any of the pins you will fry the chip (I have already fried two ESP-01). We can use it between 3V - 3.6V, now this is helpful because we will be using 3.7V LiPo battery. I will explain how we can use this battery with ESP-01 in the upcoming steps.

Lets Ready the Buzzer for ESP-01

buzzer connection (1).png

There are two types of Buzzer:

  • Active buzzer
  • Passive buzzer

Active buzzers work directly by giving some voltage. You will immediately hear the buzzing sound.

Passive buzzers require PWM. So if you apply a constant voltage, the buzzer won't make any sound.

Select an Active 3V buzzer.

ESP-01 pins can only give up-to 12mA which is quite less considering the power requirement for a 3V buzzer. So we will use an NPN transistor (I have used 2N3904) as a switch for controlling the buzzer.

Follow the connection diagram by referring the images uploaded above. Make the connections on a breadboard. In the upcoming stages you can test your circuit and make sure everything is working before soldering all the components on a PCB.

Getting Ready for Programming

ide configure.png
programming connections.png

Now lets set the Arduino IDE for programming ESP-01

  • First we will add ESP8266 board on Arduino IDE. Open the Arduino IDE and go to File > Preferences. You will see Additional Boards Manager URL. Paste this link: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  • Now go to Tools > Board > Boards Manager
  • Search esp8266. You should see esp8266 by ESP8266 community. Install it.
  • Now go to Tools > Board > ESP8266 Boards. Select Generic ESP8266 module.
  • Done! You have set the Arduino IDE

Connections

Connect your ESP-01 to the Arduino UNO board referring the connections diagram in the images above.

We are not going to use Atmega328p chip (Yes that long big chip on the Arduino board). We are just using the Arduino UNO board for programming ESP-01, that is the reason we have connected RESET pin of Atmega to 5V port.

GPIO0 and RST pin are used for controlling ESP-01 boot. More on step 6

RED LED is used to check if the uploaded program is working or not.

Okay now that the connections are made, download my Keychain code from below. In the next step I will explain how to make some changes to my code and how to upload the program.

.

Some extra information (Skip if you want)

You may have noticed Rx goes to Rx and Tx goes to Tx. That is not right!. If a device is Transmitting then the other device is Receiving (Tx to Rx) and vice versa (Rx to Tx). So why this connection?

Well the Arduino UNO board was made like that. Let me make myself clear, The Rx and Tx of the USB cable connecting to the Arduino UNO board is connected to Atmega328p. The connection is made like this: Rx of the USB goes to Tx of Atmega and Tx of the USB goes to Rx of Atmega. Now the Port Pin 0 and 1 given as Rx and Tx respectively is connected directly to Atmega (Rx of Atmega is the Rx at Port Pin 0 and Tx of the Atmega is the Tx of the Port Pin 1) and as we are not going to use Atmega for programming and only need USB connections directly, you can see Tx of USB is the Rx of the Arduino UNO board Pin 0 and Rx of the USB is a Tx of the Arduino UNO board Pin 1

Phew! Now you know Rx Tx connections.

You must have noticed a Resistor between Rx - Rx connection. Well that is important for preventing the ESP-01 chip from frying due to TTL 5V. We have used a voltage divided connection which basically reduces the 5V at Rx to 3.3V so that ESP-01 won't fry. If you want to know how Voltage divider works go to this link: https://www.electrical4u.com/voltage-divider/

Personalising the Program

IDE_1.png
IDE_2.png

When you open my program you may get intimidated by all the jargon and codes. Don't worry. If you want to know how the program is working refer the Beginners Guide link that I have stated in the beginning of this Instructable.

All the area in the code where you can make changes are present between single line comments like this

//-----------------------------------

make your changes here;

//----------------------------------

Please read the comments I have provided in the program to better understand the code

.......

You can add multiple WiFi names and their respective pass-codes in the program. The ESP-01 will connect to the one that is the strongest at the time of scan. Upon disconnection, it will constantly scan for the available WiFi it can connect to and then connects automatically. I would recommend you to add your Home WiFi and your Mobile Hotspot in the program.

Syntax for adding WiFi: wifiMulti.addAP("Hall_WiFi", "12345678");

First string is the name of the WiFi and the second string is the password.

.......

If you want to change the pin on which the buzzer is connected you can mention it in the variable

const int buz_pin = pin_no;

pin_no should be a valid value according to the ESP module you are using.

LED_BUILTIN value is the GPIO 2 pin for ESP-01;

.......

Extra [Skip if you want]

As our ESP-01 will act like a server, there is a basic HTML website code which I have already added in the program you downloaded before. I won't go much into details but if you want to explore the source HTML you can download it from below. [RENAME THE FILE FROM html code.html.txt to html code.html]

Downloads

Lets Program ESP-01

upload log.png
Serial Monitor (1).png

1)

  • Connect the Arduino UNO board to your computer.
  • Make sure under Tools these options are selected
    • Board: "Generic ESP8266 Module"
    • Upload Speed: "115200"
    • Let the other options remain default
  • Not go to Tools > Port
  • Select Arduino UNO COM Port (My PC was showing COM3. Yours may vary.

2) That's it. Now before clicking on Upload, we have to boot ESP-01 into programming mode. For that ground 0V the ESP-01 pin. Then ground the RST pin for a second. Now ESP-01 has booted into programming mode.

3) Now click on Upload in your Arduino IDE. It takes some time to compile the sketch. Monitor the Command status windows below the Arduino IDE.

4) Once the compiling is done, you should see Connecting......._......._......... This is when your PC is trying to connect to your ESP-01. If you get Connecting....... for a long time or if the connection fails (it happens with me a lot) just reset the ESP-01 again (I tap the RST on ESP-01 to ground 0V 2 - 3 times to make sure it has booted into programming mode).

Sometimes even after doing this the connection fails, what I do is after I get Connecting......_...... I reset the ESP-01 again and usually that works. Keep in mind the GPIO 0 pin should be grounded during the whole programming period.

5) After uploading is done you will get:

Leaving.......

Hard Resetting via RTS pin....

This indicates the code was successfully uploaded. Now remove the GPIO 0 pin from ground then reset the ESP-01 again. Now your ESP will boot into Normal Mode and try to connect to the WiFi network you mentioned in the program.

You can monitor the ESP-01 program from the Arduino Serial Monitor.

6) Open the Serial Monitor, on the bottom right corner Select Both NL and CR and baud rate as 115200. Reset the ESP-01 (keep GPIO 0 floating or connected to 3.3V as we are trying to run the uploaded program) and then you will see all the messages returned by ESP-01. Initially you may see some garbage values which is normal in all ESP8266 chips. After the connection is successful you will see an IP address printed on the screen. Keep a note of it.

I have added some emoticons in the serial.print() which looks good in the Serial Monitor as it gives some expressions. Who says we can't be more creative!

IP and MDNS for Controlling the Buzzer

IP address.png
mdns.png
Ip num.png

Before I go into details on how the server is working leys try turning the buzzer on. The device you try accessing the ESP-01 server should be connected to the same network as ESP-01 or should be connected to your device hotspot. Now open your favourite browser and type in the IP address you got in the previous step and search. It should open a page. Click on Toggle buzz and the RED LED should start blinking!

What is IP address?

IP is an address which each device gets after connecting to a WiFi network. IP address is like a unique identifier which helps to find a particular device. No two devices can have same IP address under the same network. When ESP-01 connects to the WiFi or hotspot, it is assigned an IP address which it prints in the Serial Monitor.

So what is mDNS?

Lets understand DNS. It stands for Domain Name System. It is a special server which returns the IP address of the domain you have searched. Say for example you searched instructables.com. The browser queries the DNS server and the server returns the IP address of instructables.com. At the time of writing this Instructable I got the IP address of instructables.com as 151.101.193.105. Now if I put 151.101.193.105 on the browser address bar and search I will get the same Instructables.com site, neat! There is one more advantage of DNS, the IP address of the devices keep changing say your routers IP today was 92.16.52.18 then tomorrow it maybe 52.46.59.190. The IP changes each time your device reconnects to a network. As DNS automatically updates the IP of all the devices, we are always routed to the proper destination server.

But we can't make a DNS server for our ESP-01 which would query it's IP. In that case we will use mDNS. It works on local devices. In the serial monitor you may have noticed esp01.local/ this is the name we assigned to our ESP-01 which would automatically respond to esp01.local/ (try searching esp01.local/ in your browser). So you can now access the ESP-01 directly just as searching instructables.com without knowing their IP address. But there is a problem, mDNS does not work on Android yet means you can't access your ESP using mDNS on Android devices rather you have to type the IP address on the search bar. mDNS works great across iOS, macOS, ipadOS and for Windows you have to install Bonjour while on Linux you have to install Avahi.

To change the name of ESP-01 mDNS find mdns.begin("esp01"); in my program and replace "esp01" string with any preferred string you want.

If you don't want to use mDNS there is another thing you can do. Go to your router's settings after your ESP-01 is connected to your router and set a static IP address for the ESP-01. Static IP does not change over time. You can search the internet on how to configure router to set static IP to any device. You will get many helpful sites. So once you assign the static IP just keep a note of it or make a bookmark in the browser so that next time you can search directly from the bookmark.

Now for mobile hotspots, the IP does not change (didn't change for me like ever!). You can get the IP addresses of the device connected to your hotspot from going to the Android hotspot settings. Just make a bookmark of the ESP-01 IP in the browser and that's it, you can access the site anytime and buzz your keychain.

IP ADDRESS ASSIGNED TO ESP-01 WHEN CONNECTING TO MOBILE HOTSPOT AND WIFI MAY BE DIFFERENT.

Note: To access the ESP-01 you have to be on the same network as your ESP module. So you can't control it over the internet but only over the local network.

Selecting a Suitable Battery

battery connection (1).png

Lets understand mAh first

Say you have a 3.7V battery which has capacity of 200mAh. The battery is connected to a circuit which consumes 100mA. So how long will the battery be able to power the circuit?

just divide

200mAh/100mA = 2h

Yes, 2 Hours!

mAh is a rating which states how much power a source can give for an hour. If the battery has 200mAh, it give 200mA power continuously for 1 hour before dying out.

I have selected 3.7V 500mAh battery (go for more mAh >1000mAh (preferred). I couldn't get a better mAh battery at any store).

ESP-01 consumes 80mA current roughly.

Roughly our circuit should consume 100mA without buzzer buzzing. So our battery should be able to power the circuit for more than 5 hours (for 500mAh battery) considering the buzzer is off most of the time. A 1000mAh battery should give more than 10 hours battery backup. So choose a battery according to your requirement.

Okay, so now can we connect the battery directly to our circuit? NO. The battery voltage is 3.7V. Any voltage above 3.6V will kill our ESP8266 chip. Then what to do? You can boost the voltage to 5V and then step it down to 3.3V using a switching regulator, but hey! those circuits will take a lot of space. And also we are forgetting the 3.7V battery will give 4.2V at full charge. This bothered me a lot initially!

Then I remembered we can use a diode to drop the voltage. If you remember, silicon diode drops roughly 0.7V when forward biased. You can connect your ESP-01 to the diode which was connected to the 3.7V battery. The diode should drop 0.7V so which should get 3V (3.7 - 0.7). And at full charge we should get 3.5 (4.2 - 0.7) which is a good range for powering ESP-01. Go for 1N400x series diode.

Refer the connections in the pictures above.

Okay. Now that we have finalized the battery lets see how to make a charging mount for our keychain.


Placing All the Components

Circuit diagram.png
component assembly.png

We have almost finished our keychain!

Only thing left is to make a keychain and place all the components inside.

The circuit diagram is given above. Make sure you plan out how your components will fit together.

You may have noticed a capacitor in the circuit diagram. It is necessary for removing voltage fluctuations in the circuit as ESP8266 is sensitive to voltage changes.

You can use JST connector for connecting the battery to your circuit as it will become easy to replace the battery in future.

I am using female header pins soldered onto the PCB for connecting ESP-01. It becomes easy to remove and insert ESP-01 to the circuit.

Make sure to make your circuit as small as possible!

Preparing Outer Cover for Placement of the Keychain Circuit and Battery

keychain cardboard.png
keychain paint.png

This is where I want you guys to come up with different ideas for the keychain.

I am using cardboard cutouts for making a cube inside which the battery and circuit is placed. It is a little bulky but fine for carrying in the pocket.

Brainstorm and come up with amazing ideas for the keychains!

Finishing Up!

see you soon.png

Congratulations! You have made the IoT keychain!

There is a lot of scope for improvement in this project like we can have better battery life, making the keychain even smaller etc. I will keep updating this Instructable with better features which we can add to the keychain.

Till then keep building, keep breaking, keep rebuilding!

Do subscribe me to get notified about my next Instructable.

Any query feel free to post it in the comment section. See you in the next Instructable.