So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By sunildm4u
#65657 Hi Everyone

I am a newbie for IOT. Was trying to implement SMART Home Automation, stuck at last step where my relay needs to toggle based on GPIO pin state. Details Below:

Hard ware : Node Mcu + 5V Relay (Image Below)


Issue :

I have flashed standard ADAFRUIT MQTT LIBRARY. And was able to successfully toggle a LED Bulb based on the data feed sent from

The problem started when I replaced the LED bulb with 5V Relay. As soon connected to VCC and GND of node MCU to vcc and gnd of the relay. Red light turned on fine. But connecting IN pin of Relay to D2 pin of mcu. Green light came up fine. But it never turned off an data feed change to OFF. Connecting IN pin of relay to many other pins of mcu kept the light on.


I am completely confused on this behavior any help is highly appreciated,

Flashed mqtt code below :

Code: Select all/***************************************************
  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:

  Works great with Adafruit's Huzzah ESP board & Feather

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Tony DiCola for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

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

#define WLAN_SSID       "Sunilwifi"
#define WLAN_PASS       "14311431"

/************************* Setup *********************************/

#define AIO_SERVER      ""
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "sunildm4u"
#define AIO_KEY         "xxxx dummy api code"

/************ 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.

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

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

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

/*************************** 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() {
   pinMode(D3, OUTPUT);

  Serial.println(F("Adafruit MQTT demo"));

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

  while (WiFi.status() != WL_CONNECTED) {

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

  // Setup MQTT subscription for onoff feed.

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.

  // 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: "));
      Serial.println((char *)onoffbutton.lastread);
      if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
        digitalWrite(D3, LOW);
      if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
        digitalWrite(D3, HIGH);

  // Now we can publish stuff!
  Serial.print(F("\nSending photocell val "));
  if (! photocell.publish(x++)) {
  } else {

  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  if(! {

// 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()) {

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

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println("Retrying MQTT connection in 5 seconds...");
       delay(5000);  // wait 5 seconds
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
  Serial.println("MQTT Connected!");
Last edited by sunildm4u on Mon May 08, 2017 1:05 am, edited 1 time in total.
User avatar
By sunildm4u
#65693 SOLVED - i was able to do some more research and find the actual issue and solution. The problem was I was using 5v of Node MCU to power the relay board (VCC) and used GPIO of node MCU as IN1 for the relay which was only 3.3 volt.

In my case, for relay to work properly it requires the both VCC and IN pin should have input voltage. So instead of using 5V pin of Node MCU I used 3.3v pin of Node MCU and everything started working as expected.

Hope it helps someone :)
User avatar
By Jon Chen
#70305 I don't know if this is relevant for you but I'm currently running a NodeMCU board and it allows for 5-12V input. If I connect a regulated 5V to usb and then use the Vin as a power source for a relay I get a regulated 5V, and since the relay I'm using uses transistors to switch I can control it with my 3.3v GPIO pins while the driving current comes from 5V or my Vin.