Connecting to AWS with MQTT

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
AJB2K3
Posts: 44
Joined: Wed Mar 06, 2019 5:20 pm
Location: @nd Star on the Right.
Contact:

Re: Connecting to AWS with MQTT

Post by AJB2K3 » Thu Apr 15, 2021 3:42 pm

stanely wrote:
Thu Apr 15, 2021 11:40 am
Which version of MicroPython are you using? Or are you using the trick MicroPython that comes with their UIFlow? That's actually a very cool system with many interface drivers built in. Have you seen the Core 2 with the integrated touchscreen? They bundle a lot of drivers with it that makes is very easy to use.

In my current project I need to use a HUZZAH32, and looks like it doesn't get far enough to talk to AWS.
yeh the M5MP version running on a core, core 2 and the atom lite!
the point i was making is that missing or knowing about iam and incognito setting on aws causes so many issues

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Connecting to AWS with MQTT

Post by stanely » Thu Apr 15, 2021 5:18 pm

Can you please share your code that reads your cert/key and sets up the MQTT SSL/TLS connection? Thanks.

I will watch for the IAM and COGNITO when I get that far. Thanks for the heads up. Right now, I would be overjoyed to hear AWS tell me my credentials are bad.

AJB2K3
Posts: 44
Joined: Wed Mar 06, 2019 5:20 pm
Location: @nd Star on the Right.
Contact:

Re: Connecting to AWS with MQTT

Post by AJB2K3 » Thu Apr 15, 2021 5:39 pm

Screenshot 2021-03-20 at 12.28.45.png
Screenshot 2021-03-20 at 12.28.45.png (84.62 KiB) Viewed 7209 times
stanely wrote:
Thu Apr 15, 2021 5:18 pm
Can you please share your code that reads your cert/key and sets up the MQTT SSL/TLS connection? Thanks.

I will watch for the IAM and COGNITO when I get that far. Thanks for the heads up. Right now, I would be overjoyed to hear AWS tell me my credentials are bad.
On the M5Stack?

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Connecting to AWS with MQTT

Post by stanely » Thu Apr 15, 2021 8:37 pm

Right, that's why I asked about UIFlow. Makes it easy, doesn't it? Problem is you can't re-use your code because they have no copy/paste yet. You can't open just pieces of prior programs to use in the new project. So you always start from scratch. But it's great for prototyping ideas. I have a Core2 with the built-in touchscreen. Dropping GUI switches, buttons and sliders is a breeze.

The great thing is that it gives you the MicroPython source so you can cut that and paste it into the final app. UIFlow works out a lot of the driver calls and interrupt stuff for you automatically, and that's very useful.

Can you also paste the MicroPython source it generated for the AWS stuff?

Thanks.

AJB2K3
Posts: 44
Joined: Wed Mar 06, 2019 5:20 pm
Location: @nd Star on the Right.
Contact:

Re: Connecting to AWS with MQTT

Post by AJB2K3 » Fri Apr 16, 2021 5:07 pm

stanely wrote:
Thu Apr 15, 2021 8:37 pm
Right, that's why I asked about UIFlow. Makes it easy, doesn't it? Problem is you can't re-use your code because they have no copy/paste yet. You can't open just pieces of prior programs to use in the new project. So you always start from scratch. But it's great for prototyping ideas. I have a Core2 with the built-in touchscreen. Dropping GUI switches, buttons and sliders is a breeze.

The great thing is that it gives you the MicroPython source so you can cut that and paste it into the final app. UIFlow works out a lot of the driver calls and interrupt stuff for you automatically, and that's very useful.

Can you also paste the MicroPython source it generated for the AWS stuff?

Thanks.
I don't have the code but here is a project I'm working on using the atom matrix
Screenshot 2021-04-16 at 18.05.31.png
Screenshot 2021-04-16 at 18.05.31.png (107.75 KiB) Viewed 7195 times
Here is the source

Code: Select all

from m5stack import *
from m5ui import *
from uiflow import *
from IoTcloud.AWS import AWS
import i2c_bus

rgb.set_screen([])






# Describe this function...
def White_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 1:
    rgb.set_screen([0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF])

# Describe this function...
def Forward_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 32:
    rgb.set_screen([0x00ff00,0x00ff00,0,0,0,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0,0x00ff00,0x00ff00,0,0,0])
    aws.publish(str('home'),str('message:fwdbutton'))

# Describe this function...
def Blue_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 4:
    rgb.set_screen([0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff])
    aws.publish(str('home'),str('message:bluebutton'))

# Describe this function...
def Back_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 128:
    rgb.set_screen([0,0,0,0x00ff00,0x00ff00,0,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0,0,0,0x00ff00,0x00ff00])
    aws.publish(str('home'),str('message:backbutton'))

# Describe this function...
def Red_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 8:
    rgb.set_screen([0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000,0xff0000])
    aws.publish(str('home'),str('message:redbutton'))

# Describe this function...
def Stop_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 64:
    rgb.set_screen([0,0,0,0,0,0,0xff0000,0xff0000,0xff0000,0,0,0xff0000,0xff0000,0xff0000,0,0,0xff0000,0xff0000,0xff0000,0,0,0,0,0,0])
    aws.publish(str('home'),str('message:stopbutton'))

# Describe this function...
def Yellow_button():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 1:
    rgb.set_screen([0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00])
    aws.publish(str('home'),str('message:yellowbutton'))

# Describe this function...
def Green_button2():
  #Change the value at the end of the below block for each button
  if (i2c0.read_u8(0x03)) == 16:
    rgb.set_screen([0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00])
    aws.publish(str('home'),str('message:greenbutton'))


def fun_home_helloword_(topic_data):
  # global params
  pass

def buttonA_wasPressed():
  # global params
  aws.publish(str('home'),str('message: matixtest'))
  pass
btnA.wasPressed(buttonA_wasPressed)


aws = AWS(things_name='core2', host='a7lxppddpjt7e-ats.iot.eu-west-2.amazonaws.com', port=8883, keepalive=300, cert_file_path="/flash/res/certificate.pem.crt", private_key_path="/flash/res/private.pem.key")
aws.subscribe(str('home/helloword'), fun_home_helloword_)
aws.start()
i2c0 = i2c_bus.easyI2C(i2c_bus.PORTA, 0x29, freq=400000)
i2c0.write_u8(0x00, 0x01)
i2c0.write_u8(0x72, 0xff)
while True:
  i2c0.write_u8(0x00, 0x00)
  rgb.set_screen([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
  Stop_button()
  Yellow_button()
  White_button()
  Blue_button()
  Green_button2()
  Red_button()
  Back_button()
  Forward_button()
  wait_ms(2)

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Connecting to AWS with MQTT

Post by stanely » Sat Apr 17, 2021 4:17 am

Thanks for posting that. Unfortunately, they're using their own AWS library so doesn't help me much trying to connect to AWS with MicroPython MQTT libraries.

AJB2K3
Posts: 44
Joined: Wed Mar 06, 2019 5:20 pm
Location: @nd Star on the Right.
Contact:

Re: Connecting to AWS with MQTT

Post by AJB2K3 » Sat Apr 17, 2021 6:45 am

What I noticed was that there was no error message when it failed to connect.
I had no feedback at all telling me what the issue was.
I spent 2 weeks deleting and rebuilding the AWS side until i was told about IAM and Cognito profiles needed.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Connecting to AWS with MQTT

Post by stanely » Mon Apr 19, 2021 12:08 am

I finally got it to work, and am publishing MQTT messages to an AWS IoT Core topic. The biggest trick was to use an older version of the binary. The only one I found that works is an idf3 binary image:
esp32-idf3-20191220-v1.12.bin


I am using umqtt.robust and both text and binary (.der) key and certificate work. Note that the root certificate is not used because the "ca_certs" argument is not implemented.

The other trick that was mentioned earlier is to change the policy to make it wide open. You find that out after you are able to connect without error and try to publish.

The default AWS policy has topic template titles and only work if you publish on that exact topic. You can edit the policy to use your specific topic, but then the device has to publish to the topic that's in the policy. If you set the topic to wildcard (*), then the IoT device can publish any topic and it's up to the subscriber to find it.

I did not have to mess with IAM or COGNITO. The way I set up my IoT thing, the IAM was set up automatically, and the 'thing' used the IAM sign in that I think is a combination of the device ID, private key, and certificate. AWS hands those out.

I hope this all makes sense.

Back to the binary... They say that idf4 does not work on a generic ESP32 unless you roll your own with some trick heap parameters. I am too primitive to do this. :( Binaries from idf3 beyond 1.12 also don't work. The reason I put so much time trying to get this to work is because AWS is the 800 pound gorilla in IoT. Having WiFi is great, but if you can't do AWS, you miss out on a lot of applications.

AJB2K3
Posts: 44
Joined: Wed Mar 06, 2019 5:20 pm
Location: @nd Star on the Right.
Contact:

Re: Connecting to AWS with MQTT

Post by AJB2K3 » Mon Apr 19, 2021 12:32 pm

Thanks for reporting back.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Connecting to AWS with MQTT

Post by stanely » Tue Apr 20, 2021 12:07 pm

I have great news! The brand spanking new v1.15 (idf4) MicroPython binary works with AWS Iot! YAY!!!

v1.15 does not have mqtt built in so you have to install it.

Post Reply