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
SSID="Enter your SSID here"PASSWORD="Enter your WiFi password here"HIVEMQ_USERNAME="sgbaird"HIVEMQ_PASSWORD="D.Pq5gYtejYbU#L"HIVEMQ_HOST="248cc294c37642359297f75b7b023374.s2.eu.hivemq.cloud"
mqtt_led.py
frommqtt_asimportMQTTClient, configfrommachineimportPin, ADCimportasynciofromnetmanimportconnectWiFiimportsslimportntptimefromtimeimporttimefrommy_secretsimport (
HIVEMQ_HOST,
HIVEMQ_PASSWORD,
HIVEMQ_USERNAME,
PASSWORD,
SSID,
)
connectWiFi(SSID, PASSWORD, country="US")
# usually would be a device-specific ID, but using course ID for nowCOURSE_ID="<your_id_here>"# UPDATE THIS TO YOUR ID# To validate certificates, a valid time is requiredntptime.timeout=30# type: ignorentptime.host="pool.ntp.org"ntptime.settime()
print("Obtaining CA Certificate")
# generated via https://colab.research.google.com/github/sparks-baird/self-driving-lab-demo/blob/main/notebooks/7.2.1-hivemq-openssl-certificate.ipynb # noqa: E501withopen("hivemq-com-chain.der", "rb") asf:
cacert=f.read()
f.close()
# Local configurationconfig.update(
{
"ssid": SSID,
"wifi_pw": PASSWORD,
"server": HIVEMQ_HOST,
"user": HIVEMQ_USERNAME,
"password": HIVEMQ_PASSWORD,
"ssl": True,
"ssl_params": {
"server_side": False,
"key": None,
"cert": None,
"cert_reqs": ssl.CERT_REQUIRED,
"cadata": cacert,
"server_hostname": HIVEMQ_HOST,
},
"keepalive": 3600,
}
)
onboard_led=Pin("LED", Pin.OUT) # Pico W is slightly different than Picocommand_topic=f"{COURSE_ID}/onboard_led"sensor_data_topic=f"{COURSE_ID}/onboard_temp"adcpin=4sensor=ADC(adcpin)
defReadTemperature():
adc_value=sensor.read_u16()
volt= (3.3/65535) *adc_valuetemperature=27- (volt-0.706) /0.001721# internal temp sensor has low precision, so round to 1 decimal placereturnround(temperature, 1)
asyncdefmessages(client): # Respond to incoming messagesasyncfortopic, msg, retainedinclient.queue:
try:
topic=topic.decode()
msg=msg.decode()
retained=str(retained)
print((topic, msg, retained))
iftopic==command_topic:
ifmsg=="on":
onboard_led.on()
elifmsg=="off":
onboard_led.off()
elifmsg=="toggle":
onboard_led.toggle()
temperature=ReadTemperature()
print(f"Publish {temperature} to {sensor_data_topic}")
# If WiFi is down the following will pause for the duration.awaitclient.publish(sensor_data_topic, f"{temperature}", qos=1)
exceptExceptionase:
print(e)
asyncdefup(client): # Respond to connectivity being (re)establishedwhileTrue:
awaitclient.up.wait() # Wait on an Eventclient.up.clear()
awaitclient.subscribe(command_topic, 1) # renew subscriptionsasyncdefmain(client):
awaitclient.connect()
forcoroutinein (up, messages):
asyncio.create_task(coroutine(client))
start_time=time()
# must have the while True loop to keep the program runningwhileTrue:
awaitasyncio.sleep(5)
elapsed_time=round(time() -start_time)
print(f"Elapsed: {elapsed_time}s")
config["queue_len"] =2# Use event interface with specified queue lengthMQTTClient.DEBUG=True# Optional: print diagnostic messagesclient=MQTTClient(config)
delcacert# to free memorytry:
asyncio.run(main(client))
finally:
client.close() # Prevent LmacRxBlk:1 errors
Expected output:
MPY: soft reboot
MAC address: <...>
connected
ip = <...>
Obtaining CA Certificate
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Connected to broker.
Elapsed: 10s
Elapsed: 20s
RAM free 119776 alloc 57504
Elapsed: 30s
...
They mentioned they also tried with another WiFi (phone hotspot).
This is indicative of marginal signal strength. When mqtt_as initially connects to WiFi it repeatedly verifies connectivity for about five seconds. If connectivity is lost in this period, the reported error is thrown. If the test passes, the module goes on to establish a link with the broker. From that point reconnections are handled automatically.
The main purpose of the test is to cater for clients which can move around. If a client moves out of range the link fails. If it moves back to a point where connectivity is marginal, the reconnection attempt is aborted and re-tried. The aim is to delay reconnection until WiFi is reasonably stable.
In the case of initial connection the policy is to abandon if any error occurs. This is because errors on initial connection (rather than reconnection) usually require user intervention (password errors and suchlike). It is, of course, possible for the application to trap the exception.
The case of initialising with a marginal WiFi network is something of a special case. It is possible to prevent the test from taking place (see docs) but the purpose of this is to save time when the WiFi is known good. I don't recommend this in your situation.
Someone reported the following to me after running the example from https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/1.4-hardware-software-communication.html on a Raspberry Pi Pico W.
my_secrets.py
mqtt_led.py
Expected output:
They mentioned they also tried with another WiFi (phone hotspot).
Any ideas or hints here? I don't think I've seen this one before. Interestingly, mqtt.simple was working fine for them in a separate example (https://github.com/sparks-baird/self-driving-lab-demo/blob/cadf5cd8f5ccaff3394afa1791b9eac55fed94d0/src/public_mqtt_sdl_demo/main.py#L278).
The error seems to be raised here: https://github.com/peterhinch/micropython-mqtt/blob/94e4814aace5a1ea1fa4f500c71133fa03342ae2/mqtt_as/__init__.py#L779-788
The text was updated successfully, but these errors were encountered: