Skip to main content
Updated date:

Switch On And Off Led Remotely Using Adafruit IO Platform

Tim is currently doing research in Internet of Things (IoT). His desire to spread the concept, ideas, and experience of IoT.

Adafruit IO (AIO) platform provides an easy way to develop Internet of Things (IoT) applications. Since AIO supports MQTT protocol, it makes communication faster. AIO also provides graphical interface to create widgets for your devices.

Components Required

  • NodeMCU Development Board X 1
  • LED X 1
  • Resistor (220 Ohm) X 1
  • Male-To-Male Jumper Wires (Optional) X 2
  • Breadboard (Optional) X 1

Circuit Diagram

Connect LED to NodeMCU pin 'D6' using 220 Ohm resistor as shown in following figure.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Preparation

Please follow the instruction given under 'Environment Setup' section in my another tutorial by clicking this link to set up the environment for the tutorial.


Arduino Code

You need to upload following code to NodeMCU using Arduino IDE. Please remember to update your WiFi SSID and password on lines 7 and 8 and Adafruit IO username and API key in the lines 14 and 15. Please follow the instructions below to retrieve your Adafruit IO Key.

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "YOUR_WIFI_SSID"
#define WLAN_PASS       "YOUR_WIFI_PASSWORD"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "YOUR_AIO_USERNAME"
#define AIO_KEY         "YOUR_AIO_KEY"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

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

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(D6,OUTPUT);
  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

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
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &onoffbutton) {
      Serial.print(F("Got: "));
      String status = (char *)onoffbutton.lastread;
      Serial.println(status);
      if(status.equals("ON")){digitalWrite(D6,HIGH);}
      else if(status.equals("OFF")){digitalWrite(D6,LOW);}
    }
  }
}

// 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;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

Building Application

Create an account on AIO and sign in using your credentials.

switch-on-and-off-led-remotely-using-adafruit-io-platform

After login get the AIO Key by clicking the link provided at top right to retrieve username and key for Arduino code as shown in following figure.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Use this key in your Arduino code and upload the code to NodeMCU.

Now let's build the user interface on AIO platform. First you need to create the dashboard. If you have already built a dashboard from my tutorial Control Led Brightness using Adafruit IO Platrorm, then you can use same dashboard otherwise create a new one. To build the dashboard, Click on Actions->Create New Dashboard and name your dashboard as show below.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Your dashboard named 'AIO_Controller' will be created.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Click on dashboard name to open following screen where you can create widget to control your LED.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Click the '+' button to create a new block. This block will be your widget, an Of/Off button in this tutorial.

From the 'create new block' dialog box as in the following figure, choose 'Toggle'.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Now create a feed named 'onoff' as following. This feed is used in your arduino code line 33, therefore they must be the same. Actually is the MQTT topic.

switch-on-and-off-led-remotely-using-adafruit-io-platform

After creating the feed, select the feed for the block you are creating and click next.

switch-on-and-off-led-remotely-using-adafruit-io-platform

It will open 'Block setting' dialog box. Provide settings as following.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Please note that I left the button on text as 'ON' and button off text as 'OFF' because I am using this text in my code. You can use anything or even 1 and 0, but you have to modify your Arduino code accordingly. This the the message that will be published on MQTT topic.

Now click create block button at bottom.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Your widget will be created as following which you can use to control the LED.

switch-on-and-off-led-remotely-using-adafruit-io-platform

Comments

Timothy Malche (author) from Jabalpur, M.P., India on July 31, 2020:

Thanks

pdlwaste solutionsllc from Edgerton,USA on July 28, 2020:

VERY HELPFULL

Related Articles