Skip to content

Instantly share code, notes, and snippets.

@dave-malone
Last active June 22, 2023 21:07
Show Gist options
  • Save dave-malone/00f98142dc1d8b64a56318cf26f6457c to your computer and use it in GitHub Desktop.
Save dave-malone/00f98142dc1d8b64a56318cf26f6457c to your computer and use it in GitHub Desktop.
AWS FreeRTOS to Greengrass Core setup

Requirements

This project was built with the following requirements in mind:

  • RaspberryPi 3
  • Espressif ESP32 DevKitC
  • AWS account with access to AWS IoT
  • Windows or OSX based machine
  • Python 2.7
  • Wifi connection (egress to AWS available)

Install Greengrass Core on RaspberryPi

These instructions assume that you have already downloaded and installed Rasbpian on your RaspberryPi.

RaspberryPi setup

SSH into your RaspberryPi and execute the following commands:

sudo su -

adduser --system ggc_user
addgroup --system ggc_group

apt-get install rpi-update
rpi-update b81a11258fc911170b40a0b09bbd63c84bc5ad59

echo 'fs.protected_hardlinks = 1' >> /etc/sysctl.d/98-rpi.conf
echo 'fs.protected_symlinks = 1' >> /etc/sysctl.d/98-rpi.conf

reboot

Download the Greengrass dependency checker

cd /home/pi/Downloads
git clone https://github.com/aws-samples/aws-greengrass-samples.git
cd aws-greengrass-samples
cd greengrass-dependency-checker-GGCv1.5.0
sudo modprobe configs
sudo ./check_ggc_dependencies | more

Configure AWS Greengrass on AWS IoT

Follow these instructions: https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-config.html

AWS IoT Greengrass Core Settings

In order for your IoT Things to know how to connect to your Greengrass Core, you must set the following:

IoT Core > Greengrass > Groups > YourGreengrassGroup > Cores > YourGreengrassGroup_Core > Connectivity

Core endpoints:

  • Your RaspberryPi's IP address
  • Port 8883

Start AWS Greengrass on the Core Device

Follow these instructions: https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-device-start.html

Disable IPv6 on RaspberryPi

sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

Resources

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

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

Run the configuration script to provision an AWS IoT thing:

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

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

Update amazon-freertos/demos/esp32_devkitc_esp_wrover_kit/common/config_files/amazon_demo_config.h

Find democonfigGREENGRASS_DISCOVERY_TASK_STACK_SIZE and make the following change:

  /* original value was configMINIMAL_STACK_SIZE * 8 */
  #define democonfigGREENGRASS_DISCOVERY_TASK_STACK_SIZE    ( configMINIMAL_STACK_SIZE * 16 )

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 /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

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 Topic = freertos/demos/ggd

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}:*"
    }
  ]
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment