Ever since I saw these LED Candles I wanted to pick them up and connect them to a WiFi enabled micro controller to make an IoT LED Candle. Recently while browsing through the local Walmart I noticed one of these with the “Try Me” button. I had no choice but to pick it up and get my IoT itch over with. In the end it was pretty straight forward with the help of the awesome Adafruit IO.

IoT LED Candle breadboard hookup

ESP8266 based NodeMCU A USB 5v Power Bank Couple of hook up wires

Since the candle had a standard Micro JST connector for the “Try Me” function, I just had to plug in the two hookup wires. The candle was operating at 4.5 volts. I figure it won’t have any problems driven by 3.5v NodeMCU pins.

NodeMCU Ground to LED Candle Ground NodeMCU Pin D0 to LED Candle Power (The D0 pin is already connected to an On board LED and a pull up resistor)

Arduino IDE and Adafruit MQTT Library setup

I had got my NodeMCU ESP8266 boards installed on my Arduino IDE earlier. First thing I did was to run the Blink example from the ESP8266 menu to check out if my basic bread boarded set up is working.

Everything ran as expected and now my NodeMCU was driving the LED Candle.

Since I wanted to connect to Adafruit IO, I had to install the Adafruit’s MQTT library. With the latest version of Arduino IDE getting the libraries installed was a breeze through the Manage Libraries menu.

Skecth > Include Libraries > Manage Libraries

Search for MQTT and choose the Adafruit MQTT library and Install.

About Adafruit IO

Adafruit has been experimenting with its own IoT service at https://io.adafruit.com for quite some time now. Turns out that its much simpler to use this service to get basic IoT prototypes done. So I signed up and got cracking.

Adafruit IO seems to be based on 3 basic building blocks:

Feeds: Data streams that can be updated and read using Blocks and APIs Blocks: Little UI widgets like a button, slider, chart etc. These blocks can be associated with feeds to update or read the feeds. Dashboard: Made up of one or more blocks and becomes your main UI to drive or monitor your IoT thing.

There are “Groups” and “Triggers” as well. Groups seem to be a simple way for grouping related feeds together. “Triggers” seem to be Adafruit’s attempt at an IFTTT like job tasker/scheduler.

Adafruit IO for IoT LED Candle

For the IoT LED Candle, I kept everything simple and put together the following.

1. Created a new private feed named LEDCandle

2. Created a new Dashboard

3. Added a Toggle Button Block, associated it with LEDCandle Feed and let the toggle text be ON and OFF

4. Then Added a “Stream” Block and associated it with LEDCandle Feed. Just updated the configuration to show the timestamp.

And the Dashboard looked like this on my phone. Simple. No Gimmicks. No need to install any software or App. I can simply add it to my phone using the “Add to Home Screen” option on my iPhone if I want to get to the dashboard directly.

When I turned the toggle ON and OFF, I could see that my FEED was updating. Now that the Adafruit IO Dashboard is ready, I had to access this feed over MQTT on my NodeMCU.

Code for the IoT LED Candle

Again there wasn’t much I had to do. Adfruit MQTT library had an excellent example for ESP8266. I just had to tweak it a little bit to ensure the right IO Pin is turned on and off. I also had to provide my WiFi Credentials, Adafruit IO Key and the Feed name in the right places. That’s all to it. Here is the code.

/*************************************************** IOT LED Candle Uses ESP8266 Arduino from: https://github.com/esp8266/Arduino With a NodeMCU Dev Board Put together by Mohan Palanisamy (http://mohanp.com) With large Parts of MQTT and Adafruit IO code from the MQTT Example by Tony DiCola for Adafruit Industries. http://mohanp.com/iot-led-candle/ ****************************************************/ #include &amp;amp;amp;amp;amp;lt;ESP8266WiFi.h&amp;amp;amp;amp;amp;gt; #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" /************************* WiFi Access Point *********************************/ #define WLAN_SSID "YourWiFiSSID" #define WLAN_PASS "YourWiFiPassword" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "YourAIOUsername" #define AIO_KEY "YourAIOKey" /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // Store the MQTT server, username, and password in flash memory. // This is required for using the Adafruit MQTT library. const char MQTT_SERVER[] PROGMEM = AIO_SERVER; const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&amp;amp;amp;amp;amp;amp;client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); /****************************** Feeds ***************************************/ // Notice MQTT paths for AIO follow the form: &amp;amp;amp;amp;amp;lt;username&amp;amp;amp;amp;amp;gt;/feeds/&amp;amp;amp;amp;amp;lt;feedname&amp;amp;amp;amp;amp;gt; const char LEDCandleFEED[] PROGMEM = AIO_USERNAME "/feeds/LEDCandle"; Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&amp;amp;amp;amp;amp;amp;mqtt, LEDCandleFEED); /*************************** Sketch Code ************************************/ void setup() { WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); } mqtt.subscribe(&amp;amp;amp;amp;amp;amp;onoffbutton); //GPIO 16 on NodeMCU Board pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected mqtt.disconnect(); delay(5000); // wait 5 seconds } } void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(1000))) { if (subscription == &amp;amp;amp;amp;amp;amp;onoffbutton) { String lastStatus=(char *)onoffbutton.lastread; if(lastStatus=="ON") { digitalWrite(BUILTIN_LED, HIGH); }else { if(lastStatus=="OFF") { digitalWrite(BUILTIN_LED, LOW); } } } } // ping the server to keep the mqtt connection alive if(! mqtt.ping()) { mqtt.disconnect(); } delay(1000); }

Testing

After flashing the sketch to the NodeMCU, I could turn the LED Candle ON and OFF from the Adafruit IO dashboard. It was very surprising to me how easy and simple it was to get this going. Not sure why Adafruit is still keeping the Adfruit IO in beta. Simplicity is its great strength.

IoT LED Candle with Adafruit IO Dashboard

With the battery power bank connected it worked nicely. All I had to do now is to find a way to open the candle up and pack the NoduMCU and the battery inside.