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

    Failed to connect Lopy with AWS and MQTT.

    Scheduled Pinned Locked Moved LoPy
    lopymqttawsamazoniot
    1 Posts 1 Posters 1.0k 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.
    • S Offline
      scg94
      last edited by

      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.

      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