Skip to content

mpearcy13/2021IoTIntro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2021 IoT ESP32 using AWS IoT Shadows

IoT Getting Started - ESP32 with flashing LED

This project is an introduction to AWS IoT Core and using Classic Shadows to control the state of the device. The device will use the shadow to turn on and off a LED connected to PIN 21 or the onboard LED using pin 2.

https://platformio.org/

How to get started - https://bit.ly/3ctJioK

How to install PlatformIO in VS Code - https://bit.ly/354Q0NV

Sync code from Github

  1. Clone GitHub repository
git clone https://github.com/mpearcy13/2021IoTIntro.git
  1. Open a project in PIO pointing to the directory cloned.

Create Policy

  1. Connect to AWS Console and open IoT Core
  2. Open Security and select Policies
  3. Create a new Policy - Update REGION and ACCOUNTNUMBER
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "arn:aws:iot:REGION:ACCOUNTNUMBER:*"
    }
  ]
}

This policy will Allow access to All IoT services.

Setup AWS IoT Thing

  1. Connect to AWS Console.
  2. Open IoT Core
  3. Open Manager then click on Things
  4. Create a new Thing (Single Thing)
  5. Add Unnamed Shadow and remove welcome to IOT messages from Desired and Reported states.
  6. Create a certificate (One-click certificate creation)
  7. Download the certificates
    • A certificate for this thing
    • A private key
    • Root CA for AWS IoT
  8. Select your Device Policy that was created in previous step.

Activate Certificate & Attach Policy

  1. Open Thing under Manage in IoT Core console
  2. Click on Security
  3. Open the assigned certificate
  4. From the action menu, choose Activate
  5. Attach a policy to the certificate.

Update Project and load your ESP32

  1. Open the Project in Visual Studio
  2. Click PlatformIO in the left menu, then click Open under PIO Home
  3. Click on Open Project
  4. Browse to the directory containing the synced Github files.
  5. Open secrets.h in the include directory.
  6. Update the DEVICE_NAME to match the Thing created above.
  7. Update the WIFI_SSID
  8. Update the WIFI_PASSWORD
  9. Update the AWS_IOT_ENDPOINT (Available from IoT Core console under settings)
  10. Open the certificate for the device and copy & paste to certificate_pem_crt. Add *\n* to each line and \n to the last line.
  11. Open the private key and copy & paste to private_pem_key. Add *\n* to each line and \n to the last line.
  12. [OPTIONAL] in main.cpp change the LED_PIN to 2 if using the onboard ESP32 LED instead of an extneral LED.
  13. Connect the ESP32 to the USB port on your system.
  14. Click the PlatformIO Upload button ➡️ on the bottom bar.
  15. When prompted in the console "Connecting.........." press the right hand button on the ESP32 to load the software.
  16. Then client the PlatformIO Serial Monitor button 🔌 to view the serial output for the ESP32.

Testing

Open MQTT Test client and send the following messages:

To turn off the light... Topic: $aws/things/DEVICENAME/shadow/update

{
  "state": {
    "desired": {
      "lightstatus": 0
    }
  }
}

To turn on the light... Topic: $aws/things/DEVICENAME/shadow/update

{
  "state": {
    "desired": {
      "lightstatus": 1
    }
  }
}

To get the thing's Device Shadows settings... Subscribe to topics:

  1. $aws/things/DEVICENAME/shadow/get/accepted
  2. $aws/things/DEVICENAME/shadow/get/rejected

Send an empty JSON document to the following topic: $aws/things/DEVICENAME/shadow/get

{}

About

IoT Getting Started - ESP32 with flashing LED

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors