Hi,
I've a test code below which connects to a AWS server but it doesn't loop every 10s.
What do I've to do so it loops? Also, I get a warning for line 31 "Coroutine 'create_task' is not awaited".
Appreciate some direction on this issue.
I've a test code below which connects to a AWS server but it doesn't loop every 10s.
What do I've to do so it loops? Also, I get a warning for line 31 "Coroutine 'create_task' is not awaited".
Appreciate some direction on this issue.
import ssl
import paho.mqtt.client as mqtt
import json
import asyncio
server = 'amazonaws.com'
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
async def send_message(client):
while True:
data = {"led": "off"}
client.publish("picow/ctr", json.dumps(data))
print("Message sent")
await asyncio.sleep(10) # Send message every 10 seconds
async def setup_mqtt():
client = mqtt.Client()
client.on_connect = on_connect
client.tls_set(ca_certs = "C:\SharedFiles\Micropython\PICO\PicoW_AWS\AmazonRootCA1.pem",
certfile = "C:\SharedFiles\Micropython\PICO\PicoW_AWS\certificate.pem.crt",
keyfile = "C:\SharedFiles\Micropython\PICO\PicoW_AWS\private.pem.key",
cert_reqs = ssl.CERT_REQUIRED,
tls_version = ssl.PROTOCOL_TLS,
ciphers = None)
client.connect(server, 8883)
asyncio.create_task(send_message(client))
client.loop_start()
asyncio.run(setup_mqtt())
