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
RajaRamesh
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

Hi,
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 umqttsimple.py file on ESP8266.

boot.py code:-

Code: Select all

import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import micropython
import network
import esp
esp.osdebug(None)
import gc
gc.collect()

ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'
mqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP'
#EXAMPLE IP ADDRESS
#mqtt_server = '192.168.1.144'
client_id = ubinascii.hexlify(machine.unique_id())
topic_sub = b'led'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

Main.py code:-

Code: Select all

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

def connect_and_subscribe():
  try:
    global client_id, mqtt_server, topic_sub
    client = MQTTClient(client_id, mqtt_server)
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(topic_sub)
    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')

connect_and_subscribe()

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

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

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

Hi,

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 ?

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

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

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

Hey,
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