I want to connect ESP32 to demo thingsboard using MQTT and I've used umqtt.simple to connect to it but there's a problem. Whenever I try to connect it gives me either this (File "umqtt/simple.py", line 99, in connect), OR (File "umqtt/simple.py", line 57, in connect IndexError: list index out of range), which I believe they are the same. Although when I tried to connect ESP32 to thingspeak (with some edits) it connected, sent and received data very well. So what is the problem?
My code:
import machine
import time
from machine import Pin, PWM
from umqtt.simple import MQTTClient
from time import sleep
import random
import json
import network
#################MQTT###################
def connect():
username="USER_NAME"
broker= "demo.thngsboard.io"
topic = "v1/devices/me/telemetry"
client = MQTTClient(username,broker)
try:
print("uuuuuuuuuu")
client.connect()
except OSError:
print('Connection failed')
sys.exit()
data = dict()
data["see"] = 15
data2=json.dumps(data)#convert it to json
print('connection finished')
client.publish(topic,data2)
print("kkkkkkkkkkkkkkkkkk")
time.sleep(5)
#print("Sending OFF")
connect()
Please note that I've seen that Question on micropython but it didn't fix my problem: https://forum.micropython.org/viewtopic.php?t=4412 also I've seen that question in which he had A similar problem but he solved it without providing the solution: Device not connecting to Thingsboard using MQTT
AND for sure I'm connecting it to the internet.
The problem seems to be, that umqtt from micropython only supports no authentication or user AND password authentication. So I've edited my code as follows:
import machine
import time
from machine import Pin, PWM
from umqtt.simple import MQTTClient
from time import sleep
import random
import json
import network
#################MQTT###################
def connect():
username="Your_Token"
broker= "demo.thingsboard.io"
topic = "v1/devices/me/telemetry"
Mqtt_CLIENT_ID = "Client_ID" # Max. Number is 23 due to MQTT specs
PASSWORD=""
client = MQTTClient(client_id=Mqtt_CLIENT_ID, server=broker, port=1883, user=username, password=PASSWORD, keepalive=10000)
try:
client.connect()
except OSError:
print('Connection failed')
sys.exit()
data = dict()
data["see"] = 15
data2=json.dumps(data)#convert it to json
print('connection finished')
client.publish(topic,data2)
print("Data_Published")
time.sleep(5)
#print("Sending OFF")
connect()