Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy4 fails to connect to AWS with MQTT

    Scheduled Pinned Locked Moved LoPy
    aws mqtt
    2 Posts 1 Posters 421 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cirobr
      last edited by cirobr

      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!

      1 Reply Last reply Reply Quote 0
      • C Offline
        cirobr
        last edited by

        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

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom