Simple MQTT Tutorial
-
Dummy question,
I have the code on my LoPy working :
from simple import MQTTClient import time def settimeout(duration): pass client = MQTTClient("MainSheet", "broker.hivemq.com", port=8000) client.settimeout = settimeout client.connect() while True: print("Sending ON") client.publish("/lights", "ON") time.sleep(1) print("Sending OFF") client.publish("/lights", "OFF") time.sleep(1)output is this
connected with goofy, channel 1 Sending ON Sending OFF Sending ON Sending OFF Sending ON Sending OFF Sending ON Sending OFF Sending ON Sending OFFConnecting to http://www.hivemq.com/demos/websocket-client/? with Chrome
Host = broker.hivemq.com
ClientID = MainSheet
Port = 8000. ( I tried setting the port to 1883 but then I can't connect to hivemq. I also tried to set the code above to connect to port 8000, however, got an out of range error)
subscribed to /lightshowever, the messages never get through to HiveMQ
Steve
-
@MainSheet said in Simple MQTT Tutorial:
Try client.disconnect(), then client.connect(). Try on firefox, it works fine in my tests, and try another topic (without slash), ie.: "mylopytopic"
-
@vruizvil
For Windows, I use MQTTFX Client. Here the link
http://www.jensd.de/apps/mqttfx/ -
@MainSheet I'am not able to connect.
What do I wrong?In my main.py I setup the wifi connection successfully.
I checked that by doing an http get call and I was able to get HTML data from a website. So internet connection is working.I went to: http://www.hivemq.com/demos/websocket-client/
And created a connection with the following details:
Host: broker.hivemq.com
Port: 8000
ClientID: MyLoPyI created a subscription and tested some messages. All works.
Then I typed the following commands on my LoPy:
MicroPython v1.8.6-40-gd10463e on 2016-11-25; LoPy with ESP32
Type "help()" for more information.from umqtt import MQTTClient
def settimeout(duration): pass
...
client = MQTTClient("MyLoPy", "broker.hivemq.com", port=8000)
client.settimeout = settimeout
client.connect()
0xc b'10:12:00:04:4d:51:54:54:04:02:00:00'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "umqtt.py", line 86, in connect
IndexError: bytes index out of range -
@gertjanvanhethof Hi!, try with machine.reset(), then try your code one more time, you need only one client socket, when you try few times the code, I think the socket doesn't work fine. That is the way to solve the same problem in my tests, but I don't know the reason.
-
My code is as following:
import time
import machine
from umqtt import MQTTClientdef settimeout(duration): pass
def connect():
if machine.reset_cause() != machine.SOFT_RESET:
client = MQTTClient("MyLoPy", "broker.mqttdashboard.com", port=8000)
client.settimeout = settimeout
client.connect()
print('Connected MQTT Broker.')
return clientdef send(client):
while True:
print("Sending ON")
client.publish("/lopy", "ON")
time.sleep(1)
print("Sending OFF")
client.publish("/lopy", "OFF")
time.sleep(1)I do a connect() and the result is as following:
client.connect()
0xc b'10:12:00:04:4d:51:54:54:04:02:00:00'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "umqtt.py", line 86, in connect
IndexError: bytes index out of rangeStill going wrong. What could be the solution?
-
@MrPy Are you sure those are you messages? I checked and somebody else was publishing these messages.
-
I found the error. The port is wrong.
It should be: 1883.
Then connect is working. -
@vruizvil Hi
I started a mosquitto on my laptop (vm debian) and it is working with the code as it is i updated it a little to make it configurable via config param file (wifi and mqtt)
what error you are getting ? -
@gas It's working now. The used port was wrong. Thxs for your help.
-
MQTTClient is missing in the latest firmware, using the image release on 02/Dec/2016.
Could someone confirm this.

-
@RSK
umqttis not part of Pycom's firmware or standard micropython. It is an external Python library, so to be able to use it you need to upload it to your device.The easiest way to do that is using Pymakr. Another way is using plain FTP (https://docs.pycom.io/pycom_esp32/pycom_esp32/general.html#local-file-system-and-ftp-access).
Please read step 2 again and try to follow the instructions. If you have any problems let us know.
-
@sakis Thanks!
I have downloaded umqtt.simple.py from https://github.com/micropython/micropython-lib and placed it in /flash/simple.py of the LoPy using FTP.
And trying to connect from my LoPy application:

With Hive settings:

But connect fails with error "bytes index out of range":

Please help to resolve.
Thanks & Regards,
Satish Kumar. R. -
Sorry the link i provided is incorrect, downloaded from the below link:
https://pypi.python.org/pypi/micropython-umqtt.simpleAnd the changes done to line number 57 "self.sock.settimeout(self.timeout)"
Thanks & Regards,
Satish Kumar. R. -
@RSK Have you tried using port 1883 instead of 8000?
-
I am trying in Windows, by default http://www.hivemq.com/demos/websocket-client/
give port number 8000.If i try to change in the browser to 1883 then socket connection fails in websocket client.
-
@RSK , Pretty sure you have a port issue. 1883 should work. For some reason Hive MQ isn't accepting requests on 1883 currently. I will search for another public test broker..
-
-
What version of the micropython-umqtt.simple library are you using? I'm trying the latest 1.3.2. It doesn't have the same bug on line 57 anymore, in fact it doesn't seem to use the same timeout mechanism.
I'm not able to connect successfully to the HiveMQ public broker:
Traceback (most recent call last):
File "main.py", line 10, in <module>
File "/flash/lib/umqtt.py", line 74, in connect
OSError: [Errno 113] EHOSTUNREACHUsing:
client = MQTTClient("MQTT_lcg_Client", "broker.hivemq.com", port=1883)
client.settimeout = settimeout
client.connect()I can pub / sub using the MQTT.fx client on my laptop, and the WiPy is on the same network (I can ping and ftp to WiPy from laptop).
Any ideas?
-
I'm also using the same apparently current 1.3.2 simple.py file, and having the exact issue described by lcgeldenhuys 3 months ago. Any suggestions on how to get past this?
-Phil
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login