In this project, I'm using an esp32 board combined with a thermistor, plus an Amazon Web Services Lambda API plus some web charting code for temperature sensing. Source code can be found in the GitHub repository: https://github.com/AcrossTheCloud/temperature-iot
Steps
- Pick a region from the region list that supports API Gateway, Lambda, DynamoDB, and IoT, and make sure you use that region (from the console drop-down menu) when it comes to configuring AWS IoT, and also update in the serverless.yml file with the corresponding region string from the table here.
- Deploy the API per instructions below and update the web code.
- Put the web code (under web/) somewhere, e.g. an s3 bucket optionally with CloudFront.
- Configure AWS IoT.
- Build the sensor.
- Configure and transfer the code to the sensor.
API Deployment
Requirements
- node.js v6 or higher with the corresponding npm
- serverless: run npm i -g serverless to install.
Installation
Run the following commands in the shell from the top level directory:
cd api serverless deploy
note the output URL here and update the URL in the web code.
Configure AWS IoT
- Create an AWS account if you don't have one and sign in to the IoT console.
- Register a thing.
- Create the certificate and download and copy the one ending with ".cert.pem" into esp32/main/certs/certificate.pem.crt and the one ending ".private.key" into esp32/main/certs/private.pem.key".
- Create a policy and when it comes to the part to add statements, add one for "iot:Connect", replacing "replaceWithAClientId" with "my_temperature_esp32" (without quotes) and effect "allow", then another one for "iot:*", replacing "replaceWithATopic" with "topic/temperature" (without quotes, and note the word topic will appear twice as "topic/topic/temperature" on the end), and effect "allow".
- Create a rule to transfer the IoT data into the dynamoDB table created automatically as part of the API deployment step. To do this, go to "act" in the IoT console, then create a rule. On the "create a rule" page, enter a name (e.g. temperature, but can be anything you like here), under "attribute" enter "*" (without quotes), under "topic filter" enter "topic/temperature" (you guessed it, no quotes here although you will see single quotes automatically entered above), then "add an action". On the "select an action page" select "Split messages into multiple columns of a database table (DynamoDBv2)" then configure action. On the "Configure action" page select the "temperature" table under the table name dropdown. Click "create a new IAM role" and an enter a name (e.g. temperature but can be anything), then you need to select it from the dropdown (as it doesn't stay there), and then click "update role", then "add action" then "create rule".
Build the sensor
- An Adafruit Huzzah esp32 board.
- A thermistor (note if you use one with different characteristics be sure to update those values in the #define lines in the esp32 code) and corresponding 10k resistor, plus breadboard and m-f jumper wires.
Connect as per the schematic.
The following photo shows the pins on the feather board you need to connect wires to:
Sometimes you can find packages with the thermistor and resistor combined as in my setup: In that case connect from the pin marked GND on the feather board to the "-" pin on the thermistor unit, from the "A4/36" (input) pin to the "S" pin on the thermistor unit, and the pin marked "3V" (actually 3.3V) on the feather board to the remaining pin on the thermistor unit.
Configure and transfer code to the sensor
Requirements
- Set up the esp32 toolchain.
- Get the esp32 IoT development framework then setup the path to the esp32 IoT development framework. Note no need to go on to set up a project as there's one supplied here which we just need to configure and transfer.
- Grab the USB drivers from [https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers] and then connect the feather board over USB to your computer.
Configuration and transfer
Run the following commands in the shell from the top level directory:
cd esp32 make menuconfig
Be sure to set:
- the path to python2 under "SDK tool configuration",
- the USB-serial device under "Serial flasher config",
- your WiFi network and password under "Example config", and
- the AWS IoT endpoint (from aws iot describe-endpoint in the CLI / the AWS IoT console under "settings") into the field in "Component config" -> "Amazon Web Services IoT Platform". then
make flash
End result
The temperature sensor can then be used to monitor the temperature of a location. It is sending its results to the DynamoDB (NoSQL) database, as exposed by a serverless cloud API, and then these temperature results can be graphed as shown using the web graphing code, as shown in this example: You can view the current temperature graph for my study here: https://temperature.acrossthecloud.net