使用物理设备时无法连接到 MQTT Broker
Failing to connect to MQTT Broker when using physical device
这是我正在尝试的代码 运行:
import mqtt
import net
import tls
import net.x509
import device
import encoding.json
// Unique MQTT client ID to identify each client that connects to the MQTT broker.
CLIENT_ID ::= "$device.hardware_id"
// The publicly available Mosquitto MQTT server/broker is used in this example.
HOST ::= "..."
// MQTT port
PORT ::= 8883
// MQTT topic name
TOPIC ::= "..."
main:
socket := net.open.tcp_connect HOST PORT
tls_cert := tls.Certificate CLIENT_CERT PRIVATE_KEY
tls_socket := tls.Socket.client socket
--server_name=HOST
--root_certificates=[SERVER_CERT]
--certificate=tls_cert
tls_socket.handshake
client := mqtt.Client
CLIENT_ID
mqtt.TcpTransport tls_socket
// Client is now connected.
print "connected to $HOST"
// Start subscribing to a topic.
subscribe client
print "Subscribed to client"
task:: my_task_1
// Process subscribed messages.
client.handle: | topic/string payload/ByteArray |
decoded := json.decode payload
print "Received message '$(decoded)' on '$topic'"
// Stop after first message.
//return
// Test task
my_task_1:
while true:
sleep --ms=5000
print "task 1 is running"
subscribe client/mqtt.Client:
// Subscripe to a topic
client.subscribe TOPIC --qos=1
print "Subscribed to topic '$TOPIC'"
它在模拟设备上运行良好,但 运行在 ESP32-WROOM-32UE 上运行它会抛出以下错误:
EXCEPTION error.
MALLOC_FAILED
0: tls_handshake_ <sdk>/tls/session.toit:214:3
1: Session.handshake.<block> <sdk>/tls/session.toit:99:9
2: Session.handshake <sdk>/tls/session.toit:79:3
3: Socket.handshake <sdk>/tls/socket.toit:53:5
4: main /c:/.../MQTT/tls_connect.toit:29:14
5: __entry__.<lambda> <sdk>/core/entry.toit:46:20
我已经尝试了多个相同型号的不同设备,并且我上传到这些设备的所有其他代码都按预期工作。
我已经从代码中删除了证书和个人路径
当 运行 基于 TLS 的 MQTT 与 Toit 云的主要连接同时进行时,系统有相当多的网络缓冲区在运行。这意味着内存水印比平时略高,使系统比以前更进一步。
我们正在优化这些场景中的内存使用,以及更好地将 malloc 实现与 GC 集成,以更好地处理这些情况。我怀疑 1.5(计划于本月发布)将在这里有重大改进。
这是我正在尝试的代码 运行:
import mqtt
import net
import tls
import net.x509
import device
import encoding.json
// Unique MQTT client ID to identify each client that connects to the MQTT broker.
CLIENT_ID ::= "$device.hardware_id"
// The publicly available Mosquitto MQTT server/broker is used in this example.
HOST ::= "..."
// MQTT port
PORT ::= 8883
// MQTT topic name
TOPIC ::= "..."
main:
socket := net.open.tcp_connect HOST PORT
tls_cert := tls.Certificate CLIENT_CERT PRIVATE_KEY
tls_socket := tls.Socket.client socket
--server_name=HOST
--root_certificates=[SERVER_CERT]
--certificate=tls_cert
tls_socket.handshake
client := mqtt.Client
CLIENT_ID
mqtt.TcpTransport tls_socket
// Client is now connected.
print "connected to $HOST"
// Start subscribing to a topic.
subscribe client
print "Subscribed to client"
task:: my_task_1
// Process subscribed messages.
client.handle: | topic/string payload/ByteArray |
decoded := json.decode payload
print "Received message '$(decoded)' on '$topic'"
// Stop after first message.
//return
// Test task
my_task_1:
while true:
sleep --ms=5000
print "task 1 is running"
subscribe client/mqtt.Client:
// Subscripe to a topic
client.subscribe TOPIC --qos=1
print "Subscribed to topic '$TOPIC'"
它在模拟设备上运行良好,但 运行在 ESP32-WROOM-32UE 上运行它会抛出以下错误:
EXCEPTION error.
MALLOC_FAILED
0: tls_handshake_ <sdk>/tls/session.toit:214:3
1: Session.handshake.<block> <sdk>/tls/session.toit:99:9
2: Session.handshake <sdk>/tls/session.toit:79:3
3: Socket.handshake <sdk>/tls/socket.toit:53:5
4: main /c:/.../MQTT/tls_connect.toit:29:14
5: __entry__.<lambda> <sdk>/core/entry.toit:46:20
我已经尝试了多个相同型号的不同设备,并且我上传到这些设备的所有其他代码都按预期工作。
我已经从代码中删除了证书和个人路径
当 运行 基于 TLS 的 MQTT 与 Toit 云的主要连接同时进行时,系统有相当多的网络缓冲区在运行。这意味着内存水印比平时略高,使系统比以前更进一步。
我们正在优化这些场景中的内存使用,以及更好地将 malloc 实现与 GC 集成,以更好地处理这些情况。我怀疑 1.5(计划于本月发布)将在这里有重大改进。