Micropython-MQTT

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Micropython-MQTT

Post by Shravan N Agni » Wed Jul 19, 2017 11:10 am

Hi,
I am using NodeMCU running micropython firmware for my IoT project. I am new to micropython. I wanted to know what modules or libraries to use for running MQTT protocol on my board. Presently I am using my Windows-10 machine with mosquitto installed as the broker. I want to communicate between my NodeMCU board and my machine through MQTT with my machine as broker/server.

Best Regards

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Micropython-MQTT

Post by pythoncoder » Thu Jul 20, 2017 7:38 am

Look at the MicroPython library https://github.com/micropython/micropython-lib in particular umqtt.simple and umqtt.robust.
Peter Hinch
Index to my micropython libraries.

Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Re: Micropython-MQTT

Post by Shravan N Agni » Thu Jul 20, 2017 10:20 am

Thank you

Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Re: Micropython-MQTT

Post by Shravan N Agni » Thu Jul 20, 2017 11:44 am

How do I install micropython packages onto my widows-10 machine. Is it done using pip install? When I do it, it shows " "python setup.py egg_info" failed with error code 1". How do I use upip tool to install packages?

Best Regards

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Micropython-MQTT

Post by pythoncoder » Fri Jul 21, 2017 6:01 am

You could try running upip on the NodeMCU to install it directly from PyPI.

Failing that - alas I have no Windows experience. One approach would to cheat by using a Linux VM or a Raspberry Pi to enable you to run the Unix build. But hopefully a Windows guru will pop up with a more direct approach.
Peter Hinch
Index to my micropython libraries.

Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Re: Micropython-MQTT

Post by Shravan N Agni » Fri Jul 21, 2017 6:45 am

Thank you

Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Re: Micropython-MQTT

Post by Shravan N Agni » Fri Jul 21, 2017 6:54 am

I have an Orange Pi lite board. I am confused with what OS to use with that to run micropython. Should I use Raspbian or Armbian?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Micropython-MQTT

Post by pythoncoder » Fri Jul 21, 2017 7:03 am

The Unix build should run on any Linux. As to which is best for your board I'd consult appropriate forums. I use Raspbian on Raspberry Pi's and it seems mature and well supported but I've no knowledge of its suitability for your hardware.
Peter Hinch
Index to my micropython libraries.

Shravan N Agni
Posts: 13
Joined: Wed Jul 19, 2017 10:53 am

Re: Micropython-MQTT

Post by Shravan N Agni » Fri Jul 28, 2017 7:09 am

Hi,
I am implementing MQTT on NodeMCU. I have a doubt. Is it possible to subscribe and publish at the same time from my NodeMCU to and from the server(In my case, Mosquitto installed on windows 10 is the server)?
Below is the code i have written. Here I am able to publish the temperature value to my system(subscribed to same topic using mosquitto), but when the control goes to subscribe part, I get the this error: timeout waiting for first EOF reception. When I run the program for the second time, it says Bytes index out of range.

Code:
from machine import Pin
from umqtt_simple import MQTTClient
import dht
import machine
import time

led = Pin(4, Pin.OUT)

SERVER = "192.168.0.5"
TOPIC = b"led"

c = MQTTClient("esp", SERVER)
c.connect()
led.on()
c.publish(b"led", b"on")
p=dht.DHT11(machine.Pin(13))
p.measure()
t=p.temperature()
c.publish("temp",str(t))
def sub_cb(topic, msg):
print((topic, msg))
c.set_callback(sub_cb)
c.subscribe(TOPIC)
print("Connected to %s, subscribed to %s topic" % (SERVER, TOPIC))
while 1:
c.wait_msg()
time.sleep(300)

Best Regards,

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Micropython-MQTT

Post by pythoncoder » Fri Jul 28, 2017 1:45 pm

With minimal mods it works here on the reference Feather Huzzah board. (I haven't got a DHT so I substituted a fixed value for t. Aside from that I merely changed the IP address.) On my board pin 4 isn't actually connected to an LED but presumably you've checked this.

I'm puzzled by "timeout waiting for first EOF reception". The source code to the "simple" driver has no such message so it's not clear where this is coming from. Or does your server produce this message?

However you are publishing and subscribing to the same "led" topic - it might be worth avoiding this. Regarding subsequent runs, you need to issue a soft reset (ctrl-D) between runs. In short I can't see much wrong; if it persists in failing it could be an issue related to the DHT sensor, although this seems unlikely as the measurement is complete before you subscribe.
Peter Hinch
Index to my micropython libraries.

Post Reply