cannot connect to aws iot using umqtt.simple
My code
import time
import machine
import network
import ujson
from simple1 import MQTTClient
SSID = b'Exxxye'
PASS = b'8xxx766'
CLIENT_ID = b'ixxxxxxxxxxxx6eb63'
AWS_ENDPOINT = b'xxxxx-1.amazonaws.com'
PUB_TOPIC = b'temperature'
SUB_TOPIC = b'temperature'
with open('/pri.der', 'rb') as f:
DEV_KEY = f.read()
with open('/cert.der', 'rb') as f:
DEV_CRT = f.read()
light = machine.Pin("LED", machine.Pin.OUT)
light.off()
def wifi_connect():
print('Connecting to wifi...')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASS)
while wlan.isconnected() == False:
light.on()
print('Waiting for connection...')
time.sleep(0.5)
light.off()
time.sleep(0.5)
print('Connection details: %s' % str(wlan.ifconfig()))
def mqtt_subscribe_callback(topic, msg):
print("Received topic: %s message: %s" % (topic, msg))
if topic == SUB_TOPIC:
mesg = ujson.loads(msg)
if 'state' in mesg.keys():
if mesg['state'] == 'on' or mesg['state'] == 'ON' or mesg['state'] == 'On':
light.on()
print('Light is ON')
else:
light.off()
print('Light is OFF')
def get_rpi_temperature():
sensor = machine.ADC(4)
voltage = sensor.read_u16() * (3.3 / 65535)
temperature = 27 - (voltage - 0.706) / 0.001721
return temperature
wifi_connect()
mqtt = MQTTClient(
client_id=CLIENT_ID,
server=AWS_ENDPOINT,
port=8883,
keepalive=120,
ssl=True,
ssl_params={'key':DEV_KEY, 'cert':DEV_CRT, 'server_side':False})
mqtt.connect()
mqtt.set_callback(mqtt_subscribe_callback)
mqtt.subscribe(SUB_TOPIC)
while True:
message = b'{"temperature":%s, "temperature_unit":"Degrees Celsius"}' % get_rpi_temperature()
print('Publishing topic %s message %s' % (PUB_TOPIC, message))
mqtt.publish(topic=PUB_TOPIC, msg=message, qos=1)
# Check subscriptions for message
mqtt.check_msg()
time.sleep(5)
My error code:
Connecting to wifi...
Connection details: ('192.168.x.xx', '255.255.255.0', '192.168.xx.xx', '192.168.xx.xx')
dbug-msg: 0x9 b'04:4d:51:54:54:04:02:00:78'
dbug-resp: b''
Traceback (most recent call last):
File "<stdin>", line 81, in <module>
File "simple1.py", line 92, in connect
IndexError: bytes index out of range
please someone help me on this..i tried everything
Unable to Publish Message to AWS IoT Cloud
MCU: ESP32-S3
Version: GENERIC_S3-20220618-v1.19.1
With umqtt.robust2 unable to publish messages to AWS IoT Cloud and there is no error too. While, with umqtt.robust it works properly for below script with ESP32-C3 for version v1.17 and message is received successfully in AWS.
Have used micropython version - v.1.19 and his error is common with both ESP32-C3 and ESP32-S3.
And, below is my code snippet:
#AWS MQTT client cert example for esp8266 or esp32 running MicroPython 1.9
from umqtt.robust2 import MQTTClient
import time
import network
import machine
from machine import Pin, ADC
from time import sleep
import usocket as socket
import ujson
#Certs for ESP8266
CERT_FILE = "/flash/ESP32-Core.cert.pem"
KEY_FILE = "/flash/ESP32-Core.private.key"
#if you change the ClientId make sure update AWS policy
MQTT_CLIENT_ID = "basicPubSub"
MQTT_PORT = 8883
MQTT_KEEPALIVE_INTERVAL = 45
THING_NAME = "ESP32-Core1"
SHADOW_UPDATE_TOPIC = "/sdk/test/Python"
RESPONSE_RECEIVED = False
#if you change the topic make sure update AWS policy
MQTT_TOPIC = "esp8266-topic"
#Change the following three settings to match your environment
MQTT_HOST = "*******************-ats.iot.us-east-1.amazonaws.com"
WIFI_SSID = "*************"
WIFI_PW = "************"
mqtt_client = None
def sub_cb(topic, msg, retained, duplicate):
print((topic, msg, retained, duplicate))
def connect_mqtt():
try:
with open(KEY_FILE, "r") as f:
key = f.read()
print("MQTT received KEY")
with open(CERT_FILE, "r") as f:
cert = f.read()
print("MQTT received CERTIFICATE")
mqtt_client = MQTTClient(client_id=MQTT_CLIENT_ID, server=MQTT_HOST, port=MQTT_PORT, keepalive=5000, ssl=True, ssl_params={"cert":cert, "key":key, "server_side":False})
mqtt_client.connect()
mqtt_client.set_callback(sub_cb)
print("MQTT is connected")
a = {"message": "Hello from IoT console"}
try:
mqtt_client.publish(SHADOW_UPDATE_TOPIC, ujson.dumps(a))
print("Publish Message")
except Exception as e:
print('Cannot Publish' + str(e))
except Exception as e:
print('Cannot connect to MQTT ' + str(e))
raise
def connect_wifi(ssid, pw):
wlan = network.WLAN(network.STA_IF)
if(wlan.isconnected()):
wlan.disconnect()
nets = wlan.scan()
if not wlan.isconnected():
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PW)
while not wlan.isconnected():
pass
print("Connected as:", wlan.ifconfig())
try:
print("Connecting to WIFI...")
connect_wifi(WIFI_SSID, WIFI_PW)
print("Connecting to MQTT...")
connect_mqtt()
print("OK")
except Exception as e:
print(str(e))
And, below is the output of the script:
>>> %Run -c $EDITOR_CONTENT
Connecting to WIFI...
Connected as: ('192.***.*.*', '255.255.255.0', '192.***.*.*', '192.***.*.*')
Connecting to MQTT...
MQTT received KEY
MQTT received CERTIFICATE
MQTT is connected
Publish Message
OK
The publish event is successful, however, still don't see the message in AWS. Please advise, what is wrong here? Why the publish event is successful when the message is not sent?
Below is the snippet of the ESP32-S3 filesystem:
<img width="164" alt="image" src="https://user-images.githubusercontent.com/112946408/200180620-b7591706-686a-4536-b614-aa6435cdbaf9.png">
try first time MicroPython
MQTT TLS ( ssl=True)
and can not connect
env: MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040
also test: MicroPython v1.24.0-preview.335.gb08ddbba5 on 2024-09-20; Raspberry Pi Pico W with RP2040
from umqtt.simple import MQTTClient # Metadata-Version: 2.1 Name: umqtt.simple Version: 1.4.0
error:
Traceback (most recent call last):
File "<stdin>", line 47, in <module>
File "/lib/umqtt/simple.py", line 68, in connect
AttributeError: 'bool' object has no attribute 'wrap_socket'
do i need a other /lib/ ?
no, a better code did it:
