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.
How to get started - https://bit.ly/3ctJioK
How to install PlatformIO in VS Code - https://bit.ly/354Q0NV
- Clone GitHub repository
git clone https://github.com/mpearcy13/2021IoTIntro.git
- Open a project in PIO pointing to the directory cloned.
- Connect to AWS Console and open IoT Core
- Open Security and select Policies
- 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.
- Connect to AWS Console.
- Open IoT Core
- Open Manager then click on Things
- Create a new Thing (Single Thing)
- Add Unnamed Shadow and remove welcome to IOT messages from Desired and Reported states.
- Create a certificate (One-click certificate creation)
- Download the certificates
- A certificate for this thing
- A private key
- Root CA for AWS IoT
- Select your Device Policy that was created in previous step.
- Open Thing under Manage in IoT Core console
- Click on Security
- Open the assigned certificate
- From the action menu, choose Activate
- Attach a policy to the certificate.
- Open the Project in Visual Studio
- Click PlatformIO in the left menu, then click Open under PIO Home
- Click on Open Project
- Browse to the directory containing the synced Github files.
- Open secrets.h in the include directory.
- Update the DEVICE_NAME to match the Thing created above.
- Update the WIFI_SSID
- Update the WIFI_PASSWORD
- Update the AWS_IOT_ENDPOINT (Available from IoT Core console under settings)
- Open the certificate for the device and copy & paste to certificate_pem_crt. Add *\n* to each line and \n to the last line.
- Open the private key and copy & paste to private_pem_key. Add *\n* to each line and \n to the last line.
- [OPTIONAL] in main.cpp change the LED_PIN to 2 if using the onboard ESP32 LED instead of an extneral LED.
- Connect the ESP32 to the USB port on your system.
- Click the PlatformIO Upload button ➡️ on the bottom bar.
- When prompted in the console "Connecting.........." press the right hand button on the ESP32 to load the software.
- Then client the PlatformIO Serial Monitor button 🔌 to view the serial output for the ESP32.
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:
- $aws/things/DEVICENAME/shadow/get/accepted
- $aws/things/DEVICENAME/shadow/get/rejected
Send an empty JSON document to the following topic: $aws/things/DEVICENAME/shadow/get
{}