You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importos#import ipaddressimportwifiimportsocketpoolimportssl#import adafruit_requestsimporttimeimportboardimportdigitalioimportadafruit_minimqtt.adafruit_minimqttasMQTTimportmicrocontrollerWIFI_SSID="mySSID"WIFI_PW="myPW"MQTT_SUB_TOPIC="mySubTopic"MQTT_PUB_TOPIC="myPubTopic"HA_MQTT_BROKER="myBrokerIpAdd"defrestart(mqttClient):
print("\n\nResetting in 10 sec...\n\n")
time.sleep(1)
mqttClient.disconnect()
time.sleep(1)
mqttClient.reconnect()
time.sleep(1)
## Set Up MQTT## Define callback methods which are called when events occur# pylint: disable=unused-argument, redefined-outer-namedefconnect(mqtt_client, userdata, flags, rc):
# This function will be called when the mqtt_client is connected# successfully to the broker.print("Connected to MQTT Broker!")
print("Flags: {0}\n RC: {1}".format(flags, rc))
defdisconnect(mqtt_client, userdata, rc):
# This method is called when the mqtt_client disconnects# from the broker.print("Disconnected from MQTT Broker!")
defsubscribe(mqtt_client, userdata, topic, granted_qos):
# This method is called when the mqtt_client subscribes to a new feed.print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
defunsubscribe(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client unsubscribes from a feed.print("Unsubscribed from {0} with PID {1}".format(topic, pid))
defpublish(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client publishes data to a feed.print("Published to {0} with PID {1}".format(topic, pid))
defmessage(client, topic, message):
print("New message on topic {0}: {1}".format(topic, message))
defsetUpMqtt(pool):
sslContext=ssl.create_default_context()
# read cert into memorywithopen('ca-root-cert.crt','r') asf:
fileData=f.read()
f.closesslContext.load_verify_locations(cadata=fileData)
# Set up a MiniMQTT Clientmqtt_client=MQTT.MQTT(
broker=HA_MQTT_BROKER,
username="username",
password="password",
port=8883,
is_ssl=True,
socket_pool=pool,
ssl_context=sslContext,
)
# Connect callback handlers to mqtt_clientmqtt_client.on_connect=connectmqtt_client.on_disconnect=disconnectmqtt_client.on_subscribe=subscribemqtt_client.on_unsubscribe=unsubscribemqtt_client.on_publish=publishmqtt_client.on_message=messageprint("Attempting to connect to %s"%mqtt_client.broker)
mqtt_client.connect()
returnmqtt_client# NOTE watchdog not used currentlydefconnectToWifi(watchdog):
print()
print("Connecting to WiFi...")
ifwatchdog!=None:
wd_feed(watchdog)
wifi.radio.connect(WIFI_SSID,WIFI_PW)
ifwatchdog!=None:
wd_feed(watchdog)
pool=socketpool.SocketPool(wifi.radio)
print("My MAC addr: ",[hex(i) foriinwifi.radio.mac_address])
print("My IP address: ",wifi.radio.ipv4_address)
returnpooldefmain():
print("\n\nmqttTest Starting up...\n\n")
try:
pool=connectToWifi(None)
except:
print("\n\n---> ERROR Cannot Connect to WiFi.\n\n")
else:
print("Connected to WiFi.\n\n")
print("Setting up MQTT...")
client=setUpMqtt(pool)
client.publish(MQTT_PUB_TOPIC,"hello from pico")
print("Subscribing to %s"%MQTT_SUB_TOPIC)
client.subscribe(MQTT_SUB_TOPIC)
whileTrue:
try:
response=client.loop()
except:
print("\n\n---> ERROR in Mqtt Loop \n\n")
ifclient.is_connected():
print("CLIENT STILL CONNECTED")
else:
print("CLIENT NOT CONNECTED")
restart(client)
client.subscribe(MQTT_SUB_TOPIC)
else:
ifresponse!=None:
print("client loop response = ",response)
time.sleep(1)
print("Disconnecting from %s"%client.broker)
client.disconnect()
print("\n\nmqttTest Shutting down...\n\n")
main()
Behavior
pico successfully connects to WiFi, and then pico successfully connects to mqtt broker without fail.
pico then subscribes to mqtt topic, but when LOOP() is executed, it ALWAYS throws an exception.
NOTE: If TLS is disabled, then it ALWAYS works.
Description
This behavior seems similar to issue #7606 which has been closed.
I'm using circuitpython 8.2 with associated minimqtt library.
Any help would be appreciated.
Thanks.
Additional information
No response
The text was updated successfully, but these errors were encountered:
CircuitPython version
Code/REPL
Behavior
pico successfully connects to WiFi, and then pico successfully connects to mqtt broker without fail.
pico then subscribes to mqtt topic, but when LOOP() is executed, it ALWAYS throws an exception.
NOTE: If TLS is disabled, then it ALWAYS works.
Description
This behavior seems similar to issue #7606 which has been closed.
I'm using circuitpython 8.2 with associated minimqtt library.
Any help would be appreciated.
Thanks.
Additional information
No response
The text was updated successfully, but these errors were encountered: