I’ve been looking for a project for a bunch of ESP-8266 ESP-01 boards I’ve had kicking around for a while.
The following describes a simple button that when pushed publishes a MQTT message that I can subscribe to with Node-RED to control different tasks.
It’s been done many times before, but I wanted to have a go at building my own IoT button.
Software
The code is pretty simple:
- The MQTT PubSubClient (Thank’s Nick)
- Some hard coded WiFi and MQTT Broker details
- The setup function which connects to the network
- The reconnect function that connects to the MQTT broker and publishes the message
- The loop function which flashes the LED to show success then go into deep sleep
In order to get the best battery life you want the ESP8266 to be in deep sleep mode for as much as possible, so for this reason the loop function sends the message to signify the button has been pushed then indefinitely enters the deepest sleep state possible. This means the loop function will only run once on start up.
#include <ESP8266WiFi.h> #include <PubSubClient.h> #define ESP8266_LED 1 const char* ssid = "WifiName"; const char* passwd = "GoodPassword"; const char* broker = "192.168.1.114"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); delay(10); pinMode(ESP8266_LED, OUTPUT); WiFi.hostname("Button1"); WiFi.begin(ssid, passwd); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); client.setServer(broker, 1883); reconnect(); } void reconnect() { while(!client.connected()) { if (client.connect("button1")){ client.publish("button1", "press"); } else { delay(5000); } } } void loop() { if (client.connected()) { reconnect(); } client.loop(); // put your main code here, to run repeatedly: digitalWrite(ESP8266_LED, HIGH); delay(1000); digitalWrite(ESP8266_LED, LOW); delay(1000); ESP.deepSleep(0); }
Hardware
By using a momentary push button wake the ESP-01 I bridged reset pin to ground the chip resets each time it’s pushed, this wakes it from the deep sleep state and runs the all the code, then drops back into the sleep state.
- The green line is the Chip enable
- The blue line is the the links reset to ground via the push button
Now I had the basic circuit and code working I needed to pick a power supply. The ESP-01 needs a 3.3v supply and most people seem to opt for using a small LiPo cell. A single cell has a nominal fully charged voltage of 3.7v which is probably close enough to use directly with the ESP-01, but the problem is you normally need to add a circuit to cut them out before the voltage gets too low as they discharge to prevent permanently damage them. You would normally add a charging circuit to allow recharging from a USB source.
This wasn’t what I was looking for, I wanted to use off the shelf batteries so I went looking for a solution using AAA batteries. The board will run directly from a fresh set of 2 AAA batteries, but the voltage can quickly drop too low. To help with this I found a part from Pololu that would take an input between 0.5v and 5v and generate a constant 3.3v. This meant that even as the batteries discharged I should be able to continue to run the device.
At first things didn’t look to work, because converter was not supplying enough current for the ESP-01 at start up, to get round this I added a 100uF capacitor across the outputs of the regulator. I don’t really know how properly to size this capacitor so I basically made a guess.
The final step was to use the soldering iron to remove the red power LED from the board as this was consuming way more power than the rest of the system.
Next Steps
- Make the MQTT topic based on the unique id of the ESP-01 so it isn’t hard coded
- Look at adding Access Point mode to allow configuration of the WiFi details if the device can not connect to the existing configuration
- Design a circuit board to hold the voltage converter, capacitor, button and the ESP-01
- Create a case to hold it all
- Work out just how long a set of batteries will last