A (human) index that likes to code
Also drinks way too much coffee
Published Sep 05, 2019 12:30
Finally, the long-awaited (2 months) part II is here!
You must have completed Part I to continue with this tutorial, as this tutorial builds on the previous tutorial.
In the previous part, the ESP32 was linked to DynamoDB via MQTT, and it was possible to control the LED of the ESP32 by publishing a message via the AWS IoT testing console to the subscribed IoT topic. However, IoT is much more than just controlling devices from the cloud. There are some devices, known as “IoT Sensors”, which are able to report sensor data to the internet. Combined with the ability to control devices and with the massive power of Cloud Computing, this allows more powerful machines to make decisions based on the data collected from the sensors, then returning those results as commands to control devices based on inferences made from aforementioned data.
We can then integrate advanced computing techniques like Big Data and Machine Learning with IoT to better improve lives based around IoT products in a household. This vaguely composites into what is known as “Smart Home”; using a cluster of IoT sensors and actuators to: (i) cut utility bills, (ii) make living more convenient, and (iii) make living more entertaining (AndroidAuthority, 2019). Scale that to the size of a city, and the term becomes “Smart City”, making payments, transport, security, et. cetera more convenient.
Therefore, in this section, you will be modifying your handiwork in Part I to make it such that the ESP32 would publish a JSON object indicating that a button on the ESP32 has been pressed. This JSON object will then be mapped as columns on a DynamoDB table.
Looking at the ESP32 schematics:
The schematics for the 'BOOT' button | Source
It appears that the ‘BOOT’ active-low button can be used as a typical button, which is perfect for the current use case.
DynamoDB | Source: Me
Click this if you don't already have a table | Source: Me
Click this instead if you already have tables | Source: Me
Table Creation Wizard | Source: Me
Click on IAM | Source: Me
Roles | Source: Me
Create a role | Source: Me
Choosing the service to use the role | Source: Me
Next: Tags | Source: Me
Next: Review | Source: Me
Create the role with a name | Source: Me
Click the role you have just created | Source: Me
Attach Policies | Source: Me
Search and select AmazonDynamoDBFullAccess | Source: Me
Click on IoT Core | Source: Me
IoT Core | Source: Me
Act | Source: Me
Create a rule (new user) | Source: Me
Create a rule (existing user) | Source: Me
Name the rule whatever you want | Source: Me
SELECT newuuid() AS uid, timestamp() AS timestamp, * FROM 'another/topic/hello'
Put the SQL statement into the Rule query statement field | Source: Me
Click on the Add Action button | Source: Me
Configure the action | Source: Me
Table name, and IAM role | Source: Me
Create the rule | Source: Me
Now that the AWS services are setup properly, it is time to update the Arduino code. Replace (or selectively replace, if you know what you’re doing) the old code with new code from this gist. Here is the summary of the changes made since the previous Arduino program:
Ensure that the ssid
, password
, aws_iot_hostname
, aws_iot_sub_topic
, aws_iot_pub_topic
, ca_certificate
, iot_certificate
and iot_privatekey
are filled in correctly, as described in the previous tutorial’s Step Dos.
Plug in the ESP32, select the port, and upload. Open the serial console to see debugging information, if desired.
DynamoDB | Source: Me
DynamoDB sidebar | Source: Me
View items in the table | Source: Me
Press this button once | Source: Me
The refresh button | Source: Me
Now, you have a means to control a device, and a means to get device information from the ESP32. If all you need is to store the state of IoT things, including the state history, AWS IoT provides a feature to do just that - it is known as “Shadow Document”, which allows an IoT device to be stateless while reporting information to AWS IoT, which can keep track of state. You can learn more about that here.
However, for our application, we want to process data as it comes in; computing something from the device information so that we can control the said device. Hence, we don’t have a need for the Shadow Document; instead, we opted to use a database like DynamoDB. You can learn more about DynamoDB here.
Hence, we still have a missing step; the part where we process the data. (Part III? wink wink)
IoT is an important topic, and should be accessible to everyone. Studying IoT not only includes learning how to actually implement it, but includes other important aspects, such as security (our solution is not very secure, to make it easier to achieve certain steps), connectivity, and scaling.
Do look out for Part III!
Happy Coding,
CodingIndex