Connecting to AWS with MQTT
-
- Posts: 1
- Joined: Wed Jul 10, 2019 2:18 am
Re: Connecting to AWS with MQTT
how you generate the certs files?
-
- Posts: 1
- Joined: Fri Nov 18, 2016 6:16 am
Re: Connecting to AWS with MQTT
To be able to connect to AWS with an ESP8266 you need to convert the certificatefile and the keyfile to DER format. See https://github.com/micropython/micropyt ... -453802236
Re: Connecting to AWS with MQTT
IndexError: list index out of range
This is my code:
I am getting continuously error in umqtt/simple.py in 57 no line, where socket.getaddrinfo(hostname, port)[0][-1] unable to parse ip address of the host name and returns a empty list. so it is giving IndexError: list index out of range !
How can I solve this?
This is my code:
Code: Select all
# certificate and private key
KEY_PATH = "/619e3d582c-private.pem.key"
CERT_PATH = "/619e3d582c-certificate.pem"
with open(KEY_PATH, 'r') as f:
PVT_KEY = f.read()
with open(CERT_PATH, 'r') as f:
CERT_KEY = f.read()
client = MQTTClient(client_id="esp32_micropython_shafik",
server="xxxxxxxxxxxxxxxx-ats.iot.eu-west-1.amazonaws.com",
port = 8883,
keepalive = 10000,
ssl = True,
ssl_params = {
"cert": CERT_KEY,
"key": PVT_KEY,
"server_side":False
} )
How can I solve this?
Re: Connecting to AWS with MQTT
Has anyone used this code recently? I can make the connection but cannot publish. Has there been changes to the AWS IoT requirements?
Re: Connecting to AWS with MQTT
A couple hard days on the ide and I solved my problem. Turns out AWS IoT wouldn't post my published topic until I issued client.disconnect(). Strange since none of the code examples I found ever issued the disconnect routine. Anyway, it works.
I have working code if anyone is interested.
I have working code if anyone is interested.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Connecting to AWS with MQTT
if you need to disconnect then you don't have working code.. how is that code supposed to work when you subscribe to a message and wait for a message on that topic?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: Connecting to AWS with MQTT
Well, that is a good point and one that I have considered. I'm still working on it.
-
- Posts: 4
- Joined: Thu Jan 30, 2020 11:14 pm
Re: Connecting to AWS with MQTT
Hi,
As pointed out above this is "working" code only in the sense that the published message is received and shown in the AWS IoT dashboard console. And this is the case only after the mqtt_client.disconnect() is sent. I think this might be a problem on the AWS IoT side. Maybe with the permissions? Although I have tried many variations of the permissions nothing seems to make a difference. Except some permissions prevent connection all together.
Anyhow, here is the code I am running to connect and publish. Maybe you can help me solve this.
Regards,
Chris
As pointed out above this is "working" code only in the sense that the published message is received and shown in the AWS IoT dashboard console. And this is the case only after the mqtt_client.disconnect() is sent. I think this might be a problem on the AWS IoT side. Maybe with the permissions? Although I have tried many variations of the permissions nothing seems to make a difference. Except some permissions prevent connection all together.
Anyhow, here is the code I am running to connect and publish. Maybe you can help me solve this.
Code: Select all
from umqtt.robust import MQTTClient
import peripherals as prph
import sys
def broker_connect_aws():
# Certificates, keys and MQTT variables
certificate_file = "/xxxx1be767-certificate.pem.crt"
private_key_file = "/xxxx1be767-private.pem.key"
mqtt_client_id = "esp32"
mqtt_port = 8883
mqtt_topic = "ESP32_Temp_F"
mqtt_host = "xxxx80on2gc4gt-ats.iot.us-west-2.amazonaws.com" # Rest API Endpoint
# Open and read certificates and keys
with open(private_key_file, "r") as f:
private_key = f.read()
with open(certificate_file, "r") as f:
certificate = f.read()
# Set up MQTT client
mqtt_client = MQTTClient(client_id=mqtt_client_id,
server=mqtt_host,
port=mqtt_port,
keepalive=10000,
ssl=True,
ssl_params={"cert": certificate,
"key": private_key,
"server_side": False
})
# Connect to AWS IoT Broker and Publish
try:
print('try to connect to mqtt on aws')
mqtt_client.connect()
print('****PUBLISH TO AWS IoT****')
mqtt_client.publish(mqtt_topic, "{}F".format(str(prph.esp32_temp()))) # Read & Pub ESP32 internal temp
mqtt_client.disconnect() # <-- without this line the code connects and publishes but the message doesn't seem to be received
except Exception as e:
print('Could not connect to AWS IoT Broker {}{}'.format(type(e).__name__, e))
print('<<<Going to esp32 Deep Sleep mode>>>')
prph.esp32_dpslp(10)
sys.exit()
Chris
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Connecting to AWS with MQTT
Did you try using the AWS IOT mqtt with a CPython mqtt client like paho.mqtt?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode