Adafruit IO IoT LED Candle

IoT LED Candle with Adafruit IO Dashboard

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.

LED Candle with Try Me button
LED Candle with Try Me button
IoT LED Candle breadboard hookup
  1. ESP8266 based NodeMCU
  2. A USB 5v Power Bank
  3. 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.

  1. NodeMCU Ground to LED Candle Ground
  2. NodeMCU Pin D0 to LED Candle Power (The D0 pin is already connected to an On board LED and a pull up resistor)
LED Candle Try Me Components
LED Candle Try Me Components
NodeMCU LED Candle wireup
NodeMCU LED Candle wireup
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.

Adafruit MQTT
Adafruit MQTT
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:

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

 

Sample Dashboard from Adafruit IO
Sample Dashboard from Adafruit IO

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

Adafruit IO LEDCandleToggle Block
Adafruit IO LEDCandleToggle Block

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

Adafruit IO Stream Block
Adafruit IO Stream Block

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.

Adafruit IO LED Candle Dashboard
Adafruit IO LED Candle Dashboard

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 <ESP8266WiFi.h>
#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(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char LEDCandleFEED[] PROGMEM = AIO_USERNAME "/feeds/LEDCandle";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, LEDCandleFEED);

/*************************** Sketch Code ************************************/

void setup() {
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  mqtt.subscribe(&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 == &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
IoT LED Candle driven by Adafruit IO

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.

One thought on “Adafruit IO IoT LED Candle”

Leave a Reply

Your email address will not be published. Required fields are marked *