not able to blink led on esp8266 with MQTT App & Raspberry Pi

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Posts: 51
Joined: Wed May 08, 2019 10:54 am

not able to blink led on esp8266 with MQTT App & Raspberry Pi

Post by RajaRamesh » Sun May 19, 2019 1:13 pm

After googling i build below code to blink in-built led on ESP8266 with MQTT App (Android) and mosquitto broker installed on Raspberry Pi. But i am not able to make the led blink by publishing from MQTT App. can someone suggest me where i do mistake. Also,i have file on ESP8266. code:-

Code: Select all

import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import micropython
import network
import esp
import gc

#mqtt_server = ''
client_id = ubinascii.hexlify(machine.unique_id())
topic_sub = b'led'

station = network.WLAN(network.STA_IF)
station.connect(ssid, password)

while station.isconnected() == False:

print('Connection successful')
print(station.ifconfig()) code:-

Code: Select all

from machine import Pin 
def sub_cb(topic, msg):
  print((topic, msg))
  if topic == b'led' and msg == b'off':
    print('ESP received '+msg+' message')
  elif topic==b'led' and msg==b'on':
    print('ESP received '+msg+' message') 

def connect_and_subscribe():
    global client_id, mqtt_server, topic_sub
    client = MQTTClient(client_id, mqtt_server)
    print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
    return client
  except OSError as e:
    print('Failed to connect to MQTT broker. debug why MQTT not connected')


User avatar
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: not able to blink led on esp8266 with MQTT App & Raspberry Pi

Post by jimmo » Sun May 19, 2019 2:32 pm


Btw, you use the </> button to format your code, so it keeps the correct indenting.

What do you see when you run the code? Importantly, does it say that it's connected successfully? Do you see the prints in sub_cb?

If you can tell us more then we can provide more help -- sorry I can't tell whether the issue is with the actual LED blinking or the MQTT bits or something else?

Does the LED turn on/off if you just write a simple program that only turns the pin on/off ?

Posts: 11
Joined: Mon Apr 09, 2018 12:41 pm

Re: not able to blink led on esp8266 with MQTT App & Raspberry Pi

Post by Jonwalter » Tue Jul 09, 2019 12:59 pm

I've set up my Pi as an MQTT app broker (was getting information from Mova Company), and my node MCU in charge of dimming an AC light. I want to control it with my phone. Currently, I'm using an app called mqtt dash and it does the job, just about, but it's not ideal. There are two main problems:

1) The first is that I can't make a widget for these mqtt topics so that I could see the dimness level of the light and change it from my home screen. At the moment, I have to open the app to change it.

2) It's a light dimmer, and I have a slider in MQTT dash, but it doesn't update (publish) until I hit the "Set" button. This is super irritating! I want to slide and see the light change brightness.

Does anyone know any other Android apps that do the above? Thanks

Post Reply