MQTT config for AWS IoT Core - SOLVED



  • Does anyone have a working configuration for MQTT and AWS IoT core? Micropython code would be really helpful. I have my certificate and private key uploaded to the device but am not sure where it's failing. Here is how my code looks:

    while True:
      m5mqtt = M5mqtt('m5stack', 'iot.us-west-2.amazonaws.com', 1883, '', '', 300, ssl = True, ssl_params = {'key': "/flash/res/m5.key", 'cert': "/flash/res/m5.crt"})
      m5mqtt.start()
      m5mqtt.publish(str('test'),str('hello world'))
      wait(1)
      wait_ms(2)
      wait(1)
      wait_ms(2)
    

    Re: Connecting to AWS IoT Core



  • This took me way too long to figure out so here is a simple step by step guide for anyone else who's struggling with this:

    AWS Setup

    1. Log onto your AWS account and pick a supported region for your IoT device. For example:
      https://us-west-2.console.aws.amazon.com/iot/home?region=us-west-2#/thinghub
    2. Click on Secure > Policies.
    3. Create a new policy and edit it in advanced mode. We're going to create a simple, open policy that is appropriate for development and experimenting. You can name your policy m5stack. Fill the policy contents with this JSON:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "iot:*",
          "Resource": "*"
        }
      ]
    }
    
    1. Click on 'Things' and then click 'Create'.
    2. Click on 'Create a Single Thing'.
    3. Give your thing a name. Example: m5stick
    4. You can leave the type, group, and attributes settings blank for now. Click Next.
    5. Click on the top 'Create Certificate' button.
    6. Download the cert file and the private key file. You do not need the public key file. Rename these as simply cert.pem and priv.key. The shorter names are important as they can't be longer than 10 characters.
    7. Click the 'Activate' button.
    8. Click 'Attach a Policy'. Attach the m5stack policy you created in the previous step. Click 'Register Thing'.

    AWS Monitoring

    Now you're ready to monitor for published messages.

    1. Click on the 'Test' link on the left side of the page.
    2. Enter # in the Subscription topic. This is a wildcard that will receive all messages for all topics. Click 'Subscribe to Topic'.
    3. View your endpoint. You can find it in the pulldown menu on the upper right:

    0_1609660966279_view_endpoint.png

    UIFlow configuration

    Finally you can upload your cert and key onto your device, and create some UIFlow code.

    1. Connect your device to UIFlow. Create a new MQTT block that looks like this, using your own URL:

    0_1609661085404_example.png

    1. Change the port to 8883. This is important, don't forget it. AWS runs MQTT on a non-standard port.
    2. Click on the plus icon to upload your cert.pem and priv.key files onto your device. They will then be available in the pulldown menus.

    0_1609661203820_example2.png

    1. Finally, run the code on your device. You will now begin to see "hello world" messages appearing in the queue every second:

    0_1609661277903_b0bd34a6-fc22-48f9-9eab-2d3331e803a3-image.png

    Here's one last screenshot that includes an example of how to subscribe to messages on your device. In this example you'd Publish a test message to the 'receive' topic. Just make sure you have a label0 somewhere on the screen.

    0_1609664735105_6227262f-c431-4a03-b840-2822e018980d-image.png



  • @greenleaf This is an excellent tutorial, I was able to follow through with it really well. I can subscribe to the "test" topic on AWS and it is coming through regularly, but I am not able to receive a published topic "receive" on the device. It could be the internet service. ATT has been sketchy here in Georgia the past couple days.

    I was able to get the device to receive a couple times, but it seeks to be intermittent Again, thinking it's the sketch internet. Need to feed the squirrels in the router again or something.

    Thanks for posting this. I can see how this can be useful.



  • @greenleaf I've played around with it more tonight and the receipt of the publish on the device seems really random. I turned it on and sent 3 messages back to back and then nothing. I wonder if it has something to do with the port 8883.



  • Trying out the new AWS blocks but only getting
    MQTTException -> Server close socket link