Error when connecting to MQTT

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Error when connecting to MQTT

Post by ajocius » Mon Feb 19, 2018 9:56 am

I am quite new to this and tried to program my ESP32 to read sensor data from DHT22 (actualy AM2302, but I was told they are the same), and post it to MQTT server running on Raspberry Pi 3. To do that I run microphyton script on ESP32, using REPL.
Here is error message that I get:
---------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "dht_publish.py", line 11, in <module>
File "umqtt/simple.py", line 99, in connect
MQTTException: 5
---------------------------------------------
I am not sure what is file "<stdin>", but other two files ("dht_publish.py", "umqtt/simple.py") are based on tutorial that I found on this subject:
https://www.youtube.com/watch?v=_vcQTyLU1WY

Line 11 in file "dht_publish.py" is : "client.connect() # Connect to MQTT broker"

So I understand it is not able to connect to MQTT broker. I know that wifi is enabled and working on ESP32 as I got IP address and I see it registered on my wifi router. I also know that MQTT broker on my Raspberry Pi is working, I tested it with command line using Putty as well as using Node-Red. So if both network and MQTT broker are fine, I think something else must be causing error message... I double checked IP address that I have in "dht_publish.py" file, not sure what else can I check.

Here are lines 97-100 from file "umqtt/simple.py" . File Simple.py is taken from: https://github.com/micropython/micropyt ... qtt.simple (umqtt folder) and I haven't done any changes to the file as per turorial.
--------------------------------------
assert resp[0] == 0x20 and resp[1] == 0x02
if resp[3] != 0:
raise MQTTException(resp[3])
return resp[2] & 1
--------------------------------------

Any thoughts on how can I proceed further?

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

Re: Error when connecting to MQTT

Post by pythoncoder » Tue Feb 20, 2018 5:55 am

What broker are you using? I recommend mosquitto as this has been widely used here.

It's worth checking that your broker is working correctly by installing and running mosquitto_pub and mosquitto_sub from a PC on your local network. If these work as expected you know your infrastructure is correct before trying to access it from other hardware.

Then connection from an ESP32 should be easy, and you can test publications from it by running mosquitto_sub on a PC.
Peter Hinch
Index to my micropython libraries.

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: Error when connecting to MQTT

Post by ajocius » Tue Feb 20, 2018 7:00 am

It is mosquito server that I am running. Will search for pc programs, haven't used those yet.

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: Error when connecting to MQTT

Post by ajocius » Tue Feb 20, 2018 9:49 am

found the reason... needed user name and password in the script to connect to mqtt broker...

TGAlbertin
Posts: 3
Joined: Thu Oct 29, 2020 4:40 pm

Re: Error when connecting to MQTT

Post by TGAlbertin » Thu Mar 24, 2022 3:26 am

I'm trying to use the AZURE iot. Could someone help me with this?

https://github.com/Azure-Samples/IoTMQT ... icroPython

connecting
Traceback (most recent call last):
File "<stdin>", line 33, in <module>
File "umqtt/robust.py", line 24, in reconnect
File "umqtt/simple.py", line 110, in connect
MQTTException: 5

I feel like it is almost working...

This is the full code:


## The file name needs to be renamed to main.py for it work on the ESP 32 board
import utime
from util import create_mqtt_client, get_telemetry_topic, get_c2d_topic, parse_connection

HOST_NAME = "HostName"
SHARED_ACCESS_KEY_NAME = "SharedAccessKeyName"
SHARED_ACCESS_KEY = "SharedAccessKey"
SHARED_ACCESS_SIGNATURE = "SharedAccessSignature"
DEVICE_ID = "DeviceId"
MODULE_ID = "ModuleId"
GATEWAY_HOST_NAME = "GatewayHostName"

## Parse the connection string into constituent parts
dict_keys = parse_connection("HostName=ESP32-ALBERTIN.azure-devices.net;DeviceId=MeuESP32;SharedAccessKey=xOESticedR2oq5EW5TbV3R9Id6ZPyrw3yvoX9kJcJC8=")
shared_access_key = dict_keys.get(SHARED_ACCESS_KEY)
shared_access_key_name = dict_keys.get(SHARED_ACCESS_KEY_NAME)
gateway_hostname = dict_keys.get(GATEWAY_HOST_NAME)
hostname = dict_keys.get(HOST_NAME)
device_id = dict_keys.get(DEVICE_ID)
module_id = dict_keys.get(MODULE_ID)

## Create you own shared access signature from the connection string that you have
## Azure IoT Explorer can be used for this purpose.
sas_token_str = "sp=racwdl&st=2022-03-24T03:11:20Z&se=2025-03-24T11:11:20Z&spr=https&sv=2020-08-04&sr=c&sig=jiN9%2FNQiHtLCARyVoTTgxkCjYTK2CLPwQcoN%2Bp1Ra%2BI%3D"

## Create username following the below format '<HOSTNAME>/<DEVICE_ID>'
username = "ESP32-ALBERTIN.azure-devices.net/MeuESP32"

## Create UMQTT ROBUST or UMQTT SIMPLE CLIENT
mqtt_client = create_mqtt_client(client_id=device_id, hostname=hostname, username=username, password=sas_token_str, port=8883, keepalive=120, ssl=True)

print("connecting")
mqtt_client.reconnect()

def callback_handler(topic, message_receive):
print("Received message")
print(message_receive)

subscribe_topic = get_c2d_topic(device_id)
mqtt_client.set_callback(callback_handler)
mqtt_client.subscribe(topic=subscribe_topic)

print("Publishing")
topic = get_telemetry_topic(device_id)

## Send telemetry
messages = ["Accio", "Aguamenti", "Alarte Ascendare", "Expecto Patronum", "Homenum Revelio", "Priori Incantato", "Revelio", "Rictusempra", "Nox" , "Stupefy", "Wingardium Leviosa"]
for i in range(0, len(messages)):
print("Sending message " + str(i))
mqtt_client.publish(topic=topic, msg=messages)
utime.sleep(2)

## Send a C2D message and wait for it to arrive at the device
print("waiting for message")
mqtt_client.wait_msg()

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

Re: Error when connecting to MQTT

Post by pythoncoder » Fri Mar 25, 2022 9:53 am

MQTT error 5 means
Connection Refused, not authorized The Client is not authorized to connect
From MQTT spec V3.1.1 section 3.2.2.3 Connect Return code
Peter Hinch
Index to my micropython libraries.

Post Reply