Skip to content

Instantly share code, notes, and snippets.

@dave-malone
Last active February 2, 2023 11:01
Show Gist options
  • Save dave-malone/204f95fc5849b6010af93df80cc68069 to your computer and use it in GitHub Desktop.
Save dave-malone/204f95fc5849b6010af93df80cc68069 to your computer and use it in GitHub Desktop.
Install Amazon FreeRTOS on an ESP32 and make it a "thing"

Install Amazon FreeRTOS on your ESP32 and make it a "thing"

These instructions are derived directly from https://docs.aws.amazon.com/freertos/latest/userguide/getting_started_espressif.html

Local Machine setup

Install and configure the AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/installing.html

Install the boto3 library:

  • On Windows in the mingw32 environment: easy_install boto3
  • On MacOS or Linux: pip install boto3

Install the ESP32 drivers on your machine and test your serial connection:

Get Amazon FreeRTOS and use it to register your Thing with AWS

Clone the Amazon FreeRTOS Github repo to your machine:

git clone https://github.com/aws/amazon-freertos

Add your thing name and wifi connection configuration:

vim amazon-freertos/demos/common/tools/aws_config_quick_start/configure.json

The security type for your Wi-Fi network. Valid security types are:

  • eWiFiSecurityOpen: Open, no security
  • eWiFiSecurityWEP: WEP security
  • eWiFiSecurityWPA: WPA security
  • eWiFiSecurityWPA2: WPA2 security

Run the configuration script to provision an AWS IoT thing:

cd amazon-freertos/demos/common/tools/aws_config_quick_start/
python SetupAWS.py setup

Add your Thing to the Greengrass Group

IoT Core > Greengrass > Groups > YourGreengrassGroup > Cores > YourGreengrassGroup_Core > Devices Click Add Device Click Select an IoT Thing Under the Devices screen, click the ... for your newly added Thing, and click 'Sync to the Cloud'

Add a Subscription for your Thing in your Greengrass Group

IoT Core > Greengrass > Groups > YourGreengrassGroup > Cores > YourGreengrassGroup_Core > Subscriptions Click Add Subscription

  • Source = Devices > your thing
  • Target = Services > IoT Cloud
  • Optional Topic Filter = freertos/demos/ggd

After adding your thing to your Greengrass Group, and after adding the subscription, under your Greengrass Group choose Actions > Deploy

AWS IoT ESP32 Thing Policy

Add greengrass:Disover to the Thing policy generated by the configuration script

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:us-east-1:{account-id}:*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:us-east-1:{account-id}:*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:us-east-1:{account-id}:*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:us-east-1:{account-id}:*"
    },
    {
      "Effect": "Allow",
      "Action": "greengrass:Discover",
      "Resource": "arn:aws:iot:us-east-1:{account-id}:*"
    }
  ]
}

Change project source to run the Greengrass demo instead of the default MQTT echo demo

Update amazon-freertos/demos/common/demo_runner/aws_demo_runner.c:

Uncomment the lines containing vStartGreenGrassDiscoveryTask

Comment the lines containing vStartMQTTEchoDemo

Flash the demo app to your esp32

Run the menuconfig program to set your Serial flash config's default serial port. On Windows, this will be something like COM1. On OSX, this will be something like /dev/cu.*

cd amazon-freertos/demos/espressif/esp32_devkitc_esp_wrover_kit/make
make menuconfig

Next, run the flash command to flash the firmare to your ESP32:

make flash

In a separate terminal window, run the monitor command to view logs for the firmware running on the ESP32:

make monitor

Resources

Troubleshooting

A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet

When attempting to run make flash, you receive the error A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header. This may indicate that you have a bad USB cable, or some other issue connecting to the ESB32 via your serial port. Often times, retrying the make flash command will work, as well as unplugging your USB cable to/from the ESP32 and/or your computer.

make monitor fails to run

When running make monitor, if you see a Traceback similar to this, you are not running Python 2.7:

Traceback (most recent call last):
  File "/Users/dmmal/Desktop/esp32/amazon-freertos/lib/third_party/mcu_vendor/espressif/esp-idf/tools/idf_monitor.py", line 607, in <module>
    main()
  File "/Users/dmmal/Desktop/esp32/amazon-freertos/lib/third_party/mcu_vendor/espressif/esp-idf/tools/idf_monitor.py", line 533, in main
    monitor.main_loop()
  File "/Users/dmmal/Desktop/esp32/amazon-freertos/lib/third_party/mcu_vendor/espressif/esp-idf/tools/idf_monitor.py", line 272, in main_loop
    self.handle_serial_input(data)
  File "/Users/dmmal/Desktop/esp32/amazon-freertos/lib/third_party/mcu_vendor/espressif/esp-idf/tools/idf_monitor.py", line 306, in handle_serial_input
    self.console.write_bytes(b)
  File "/anaconda3/lib/python3.6/site-packages/serial/tools/miniterm.py", line 63, in write_bytes
    self.byte_output.write(byte_string)
TypeError: a bytes-like object is required, not 'int'
make: *** [monitor] Error 1

[IoT_GGD] GGD - Can't connect to greengrass Core

When your esp32 is initializing, your device is unable to connect to the Greengrass Core running on RaspberryPi. Verify that your Greengrass Core Connection settings do not include the Loopback address or IPv6 addresses. You may also need to reboot the RaspberryPi or restart the greegrassd process on the RaspberryPi.

11 2098 [IoT_GGD] GGD - Can't connect to greengrass Core
12 2098 [IoT_GGD] Auto-connect: Failed to retrieve Greengrass address and certificate.

[IoT_GGD] ERROR: Could not connect to the Broker.

The Broker running on the Greengrass Core device may be unresponsive. Rebooting the Greengrass Core device may resolve this issue.

11 1691 [IoT_GGD] Greengrass device discovered.
12 1691 [IoT_GGD] Establishing MQTT communication to Greengrass...
13 1691 [IoT_GGD] Sending command to MQTT task.
14 1692 [MQTT] Received message 10000 from queue.
15 1692 [IP-task] Socket sending wakeup to MQTT task.
16 2193 [MQTT] About to close socket.
17 2293 [MQTT] Socket closed.
18 2293 [MQTT] Stack high watermark for MQTT task: 4688
19 2293 [MQTT] Notifying task.
20 2293 [MQTT] Received message 0 from queue.
21 2293 [IoT_GGD] Command sent to MQTT task failed.
22 2293 [IoT_GGD] ERROR: Could not connect to the Broker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment