Failed to connect Lopy with AWS and MQTT.



  • Hello, good day to all.

    I am trying to connect my LoPy device with AWS through the MQTT client, when I connect through MQTTfx with the certificates I have no problem, however when I do the tests with the device I get errors.

    import time #Importamos la libreria para los delay
    from mqttclient import MQTTClient #Importamos la libreria para la solicitud
    
    #MQTT Configuration
    DISCONNECTED = 0
    CONNECTING = 1
    CONNECTED = 2
    DEVICE_ID = "12345"
    HOST = "a1uva9z7kasg52.iot.us-west-2.amazonaws.com"
    TOPIC_DOWNLOAD = "Download"
    TOPIC_UPLOAD = "my/topic"
    
    state = DISCONNECTED
    connection = None
    
    def _recv_msg_callback(topic, msg):
        print("Received: {} from Topic: {}".format(msg, topic))
    
    def _send_msg(msg):
        global connection
        connection.publish(TOPIC_UPLOAD, msg)
    
    def run():
        global state
        global connection
    
        while True:
            # Wait for connection
            while state != CONNECTED:
                try:
                    state = CONNECTING
                    connection = MQTTClient(DEVICE_ID, server=HOST, port=8883)
                    connection.connect(ssl=True, certfile='/flash/cert/certificate.pem.crt', keyfile='/flash/cert/private.pem.key', ca_certs='/flash/cert/certificate.pem.ca.crt')
                    state = CONNECTED
                except:
                    print('Error connecting to the server')
                    time.sleep(0.5)
                    continue
    
            print('Connected!')
    
            # Subscribe for messages
            connection.set_callback(_recv_msg_callback)
            connection.subscribe(TOPIC_DOWNLOAD)
    
            while state == CONNECTED:
                connection.check_msg()
                msg = '{"Name":"Pycom", "Data":"Test"}'
                print('Sending: ' + msg)
                _send_msg(msg)
                time.sleep(2.0)
    

    The error goes here:

    connection.connect(ssl=True, certfile='/flash/cert/certificate.pem.crt', keyfile='/flash/cert/private.pem.key', ca_certs='/flash/cert/certificate.pem.ca.crt')
    

    It is as follows:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/flash/lib/mqttclient.py", line 79, in connect
    OSError: -1
    

    I hope you can help me. Thank you.



Pycom on Twitter