LoPy4 fails to connect to AWS with MQTT



  • Hello,
    I've been trying to connect LoPy4 to AWS through MQTT unsuccessfully. The library in use (AWSIoTPythonSDK) is the one downloaded from AWS whilst the main.py is a slightly modified example from Pycom GitHub, as follows:

    from MQTTLib import AWSIoTMQTTClient
    from network import WLAN
    import time
    import config
    
    # Connect to wifi
    wlan = WLAN(mode=WLAN.STA)
    #wlan.connect(config.WIFI_SSID, auth=(None, config.WIFI_PASS), timeout=5000)
    while not wlan.isconnected():
        print("Waiting for WLAN...")
        time.sleep(0.5)
    print('WLAN connection succeeded!')
    
    # user specified callback function
    def customCallback(client, userdata, message):
    	print("Received a new message: ")
    	print(message.payload)
    	print("from topic: ")
    	print(message.topic)
    	print("--------------\n\n")
    
    # configure the MQTT client
    pycomAwsMQTTClient = AWSIoTMQTTClient(config.CLIENT_ID)
    pycomAwsMQTTClient.configureEndpoint(config.AWS_HOST, config.AWS_PORT)
    pycomAwsMQTTClient.configureCredentials(config.AWS_ROOT_CA, config.AWS_PRIVATE_KEY, config.AWS_CLIENT_CERT)
    
    pycomAwsMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)                 ### added line
    pycomAwsMQTTClient.configureOfflinePublishQueueing(config.OFFLINE_QUEUE_SIZE)
    pycomAwsMQTTClient.configureDrainingFrequency(config.DRAINING_FREQ)
    pycomAwsMQTTClient.configureConnectDisconnectTimeout(config.CONN_DISCONN_TIMEOUT)
    pycomAwsMQTTClient.configureMQTTOperationTimeout(config.MQTT_OPER_TIMEOUT)
    #pycomAwsMQTTClient.configureLastWill(config.LAST_WILL_TOPIC, config.LAST_WILL_MSG, 1)
    
    #Connect to MQTT Host
    #if pycomAwsMQTTClient.connect():
    #    print('AWS connection succeeded')
    
    while not pycomAwsMQTTClient.connect():
        print("Waiting for AWS...")
        time.sleep(2)
    print('AWS connection succeeded')
    

    Point is that LoPy4 connects smoothly to WiFi, but not to AWS. The output as follows keeps running indefinitely:

    rst:0x7 (TG0WDT_SYS_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff8028,len:8
    load:0x3fff8030,len:1728
    load:0x4009fa00,len:0
    load:0x4009fa00,len:14584
    entry 0x400a059c
    Scanning for known wifi networks...
    
    Connected to CheckidTestZone with IP address: 192.168.0.190
    
    WLAN connection succeeded!
    Packet sent. (Length: 32)
    Waiting for AWS...
    Packet sent. (Length: 32)
    Waiting for AWS...
    

    The certificates and the correspondent config.py seems to be working correctly, as a test performed by running the code from my PC was successful. Config.py is anyway posted here (AWS host replaced):

    # AWS general configuration
    AWS_PORT = 8883
    AWS_HOST = 'blablabla-ats.iot.us-west-2.amazonaws.com'
    AWS_ROOT_CA = '/flash/cert/root-CA.crt'
    AWS_CLIENT_CERT = '/flash/cert/NG1.cert.pem'
    AWS_PRIVATE_KEY = '/flash/cert/NG1.private.key'
    
    ################## Subscribe / Publish client #################
    CLIENT_ID = 'PycomPublishClient'
    TOPIC = 'PublishTopic'
    OFFLINE_QUEUE_SIZE = -1
    DRAINING_FREQ = 2
    CONN_DISCONN_TIMEOUT = 10
    MQTT_OPER_TIMEOUT = 5
    #LAST_WILL_TOPIC = 'PublishTopic'
    #LAST_WILL_MSG = 'To All: Last will message'
    

    Any help on this is more than welcome.

    Thank you very much!



  • Hello again,

    Have found that the entire code is correct and works perfectly. Issue was at configuration AWS Thing, Policy, Certificate side.

    The tutorial followed to fix the problem is this:
    https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html


Log in to reply
 

Pycom on Twitter