In this instructable I show how to convert a simple battery powered LED lamp into an IoT device.
This project includes:
- programming ESP8266 with Arduino IDE;
- making android application with MIT App Inventor.
The object of interest is this moon shaped lamp I bought from gearbest. But really this tutorial can be adapted to any low DC voltage device (AC powered devices require additional circuitry).
- Android smartphone (android versions 7-9 tested).
- Soldering tools.
- Prototyping PCB (protoboard).
- ESP-12E board (or other devboard with ESP8266 microcontroller).
- USB-serial converter for programming.
- Several different values of passive components (resistors and capacitors).
(Optional. See "Block Diagram" section)
- 3.3V@500mA LDO IC.
- 3.3V-5V logic level converter board.
- 5V DC power supply.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Idea
Moon lamp is powered by one Li-ION 18650 cell and it has 3 operation modes:
In manual mode lamp is controlled with push button, every press changes LED light state (blue on, orange on, both on, off), light intensity changes while holding push button pressed. In auto mode LED light states changes by tapping or shaking lamp itself.
I decided to add ESP8266 to act as a web server which listens for requests and accordingly simulates button presses. I didn't want to break original lamp functionality, just wanted to add additional control features over WiFi, so I chose ESP to simulate button presses instead of directly controlling LEDs. Also this allowed me to minimally interact with original circuitry.
When prototype was done it drove ~80mA constantly from battery in off state (~400mA on full brightness). Standby current is high because ESP8266 works as a server and always is connected to WiFi and listens for requests. Battery was depleted after one and a half day only in off state, so later I decided to use lamps USB charging port for powering all electronics from external 5V power supply and ditched battery all together (but this is optional).
Step 2: Block Diagram
In block diagram you can see what circuitry will be added and how existing circuitry will be modified. In my case I removed battery completely and shorted battery chargers IC input with output (again, this is optional). Transparent blocks in diagram indicates components which are bypassed (although push button still works as originally intended).
According to documentation ESP8266 tolerates only 3.3V, however there are plenty of examples when ESP8266 works completely fine with 5V, so logic level converter and 3.3V LDO can be left out, however I stayed with best practice and added those components.
I used 3 ESP8266 I/O pins and ADC pin. One digital output pin is for simulating button presses, two digital inputs is for detecting what color LEDs are on (from this we can figure out in which state MCU is and which state is next after button press). ADC pin measures input voltage (through a voltage divider), that's how we can monitor remaining battery charge level.
As external power supply I use old phone charger 5V@1A (don't use quick chargers).
Step 3: Programming
In a nutshell program works like this (for more information see code itself):
ESP8266 connects to your WiFi access point which credentials you must enter in the beginning of code prior programming, it gets IP address from your routers DHCP server, to find out IP which you will need later, you can check routers web interface DHCP settings or set debugging flag in code to 1 and you will see what IP ESP got in serial monitor (you should reserve that IP in your routers settings in order ESP always will get same IP on boot).
When initialized MCU always executes same routine forever:
- Check if still connected to AP, if not try to reconnect until success.
- Wait for client to make HTTP request. When request happens:
- Check input voltage.
- Check in which state LEDs are.
- Match HTTP request with known LED states (blue on, orange on, both on, off).
- Simulate so many push button presses as needed to achieve requested state.
I will briefly describe programming instructions, if its your first time programming ESP8266 MCU look for more in depth instructions.
Follow circuit diagram to connect ESP-12E module for programming. Some tips:
- use external 3.3V@500mA power supply (in most cases USB-serial power supply is not enough);
- check if your USB-serial converter is 3.3V logic level compatible;
- check if USB-serial converter drivers installed successfully (from windows device manager) also you can check if it works correctly from IDE, just short RX and TX pins, than from IDE select COM port, open serial monitor and write something, if all works you should see text that you sending appearing in the console;
- for some reason I was able to program ESP only when I first connected USB-serial converter to PC and then powered on ESP from external 3.3V source;
- after successfully programming don't forget to pull GPIO0 high at next boot.
Step 4: Schematic and Soldering
Follow schematic to solder all components to protoboard. As mentioned before some components are optional. I used KA78M33 3.3V LDO IC and this logic level converter board from sparkfun, alternatively, you can make converter yourself as shown in the schematic (you can use any N-channel mosfet instead of BSS138). In case you stick with using Li-ION battery, +5V power network will be battery positive terminal. ESP8266 ADC reference voltage is 1V, my chosen resistor divider values allows to measure input voltage as high as 5.7V.
There should be 5 connections to original lamp PCB: +5V (or +Battery), GND, push button, PWM signals from lamps MCU for controlling blue and orange LEDs. If you powering lamp from 5V source, like I did, you will want to short battery chargers IC VCC pin with OUTPUT pin, that way all electronics will be powered directly from +5V and not from battery charger OUTPUT.
Follow second image for all solder points you will need to make on lamps PCB.
- If you decided to short +5V with battery charger IC output, remove battery completely before doing so, you don't want to connect +5V directly to a battery.
- Pay attention to which push button pin you solder ESP output, because 2 pins of a push button is connected to ground and you do not want to short circuit when ESP output goes HIGH, better double check with multimeter.
Step 5: Android App
Android app was made with MIT app inventor, to download an app and/or clone project for yourself, go to this link (you will need google account to access it).
On first launch you will need to open settings and enter your ESP8266 IP address. This IP will be saved so no need to enter it again after program restarts.
App tested with several android 9 and android 7 devices.