thingsboard 使用 MQTT 连接到 esp32
thingsboard connection to esp32 using MQTT
我想使用 MQTT 将 ESP32 连接到演示 thingsboard,我已经使用 umqtt.simple 连接到它,但出现问题。每当我尝试连接时,它都会给我这个(文件 "umqtt/simple.py",第 99 行,连接中),或(文件 "umqtt/simple.py",第 57 行,连接中
IndexError: list index out of range),我相信它们是一样的。虽然当我尝试将 ESP32 连接到 thingspeak(进行一些编辑)时,它连接、发送和接收数据非常好。那么问题是什么?
我的代码:
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()
请注意,我在 micropython 上看到了这个问题,但它没有解决我的问题:
https://forum.micropython.org/viewtopic.php?t=4412
我也看到了他遇到类似问题的问题,但他没有提供解决方案就解决了:
Device not connecting to Thingsboard using MQTT
并且确定我正在将其连接到互联网。
问题似乎是,micropython 的 umqtt 仅支持无身份验证或用户和密码身份验证。
所以我编辑了我的代码如下:
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()
我想使用 MQTT 将 ESP32 连接到演示 thingsboard,我已经使用 umqtt.simple 连接到它,但出现问题。每当我尝试连接时,它都会给我这个(文件 "umqtt/simple.py",第 99 行,连接中),或(文件 "umqtt/simple.py",第 57 行,连接中 IndexError: list index out of range),我相信它们是一样的。虽然当我尝试将 ESP32 连接到 thingspeak(进行一些编辑)时,它连接、发送和接收数据非常好。那么问题是什么?
我的代码:
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()
请注意,我在 micropython 上看到了这个问题,但它没有解决我的问题: https://forum.micropython.org/viewtopic.php?t=4412 我也看到了他遇到类似问题的问题,但他没有提供解决方案就解决了: Device not connecting to Thingsboard using MQTT
并且确定我正在将其连接到互联网。
问题似乎是,micropython 的 umqtt 仅支持无身份验证或用户和密码身份验证。 所以我编辑了我的代码如下:
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()