2

I am using micropython on my esp32. I want to send data from esp32 to AWS IoT MQTT Broker Endpoint. But in micropython socket module, getaddrinfo(hostname, port) method return empty list every time. How can I solve it?

It is working when I use IP address instead of host name. But AWS MQTT broker endpoint has no static IP for its broker endpoint.

# My code:
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("wifiname", "password")
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="xxxxxxxxxxx.iot.eu-west-1.amazonaws.com",
                   port = 8883,
                   keepalive = 10000,
                   ssl = True,
                   ssl_params = {
                     "cert": CERT_KEY,
                     "key": PVT_KEY,
                     "server_side":False
                   } )
def checkwifi():
    while not sta_if.isconnected():
        time.sleep_ms(500)
        print(".")
        sta_if.connect()
def publish():
    while True:
    checkwifi()
    msg = b'hello shafik'
    client.publish(b"weather", msg)
    time.sleep(1)
print("type", type(PVT_KEY))
client.connect()
publish()`

I am getting continuously this error: umqtt/simple.py in 57 line, IndexError: list index out of range.

How can I solve it?

MSI Shafik
  • 139
  • 6
  • I think you create the `client` before you check if the wifi is connected (and wait for it to connect). Also in the `checkwifi()` function why don't you have no arguments (username, password) in the `sta_if.connect()`; – George Bou Oct 09 '19 at 19:11

2 Answers2

2

I have solved this problem just changing my wifi network. Actually this problem occurred for my ISP network issue, that's why socket module didn't work properly for fetching IP address properly.

MSI Shafik
  • 139
  • 6
0

I had a similar error in a similar case. I solved it by creating a new Thing and policy for it in AWS IoT core. This time I selected '*' in both "Action" and "Resource" while creating the policy for the new Thing. The default policy created first time around probably does not allow connections.

Please find my reference and further details here: https://forum.micropython.org/viewtopic.php?t=5166