Discover the transformative capabilities of seamlessly transmitting data from the Raspberry Pico W to AWS DynamoDB. Unleash the potential to elevate your IoT applications within the expansive AWS ecosystem, propelling them to new heights of scalability. This integration not only empowers you to harness the full potential of your data but also paves the way for advanced automation, unlocking a realm of possibilities for dynamic and efficient data-driven applications. Mastering this connection between the Pico W and DynamoDB is not just a learning experience; it is a gateway to unparalleled prowess in the realm of data and IoT innovation.
Before reading the remainder, be sure to subscribe and support the channel if you have not!
Subscribe:
Youtube
Support:
Step 1-) Architecture Overview
Select create a table:
Select create function. We want to Author from scratch.
The provided Lambda function is designed to handle events triggered by AWS IoT Core, specifically from an MPU6050 sensor on a Raspberry Pi Pico W. Here's a comprehensive explanation:
The code begins by importing necessary modules: boto3 for AWS interactions, Decimal for precise numeric representation, and logging for generating log messages.
The logging module is configured to use the INFO log level for informational messages.
The DynamoDB resource is initialized to interact with the 'MPU6050' table in AWS DynamoDB.
The lambda_handler function serves as the entry point for the Lambda function, automatically called when an event triggers it.
Informational logging indicates the start of event processing.
Event data, including 'value', 'topic', and 'point_number', is extracted from the incoming event.
The 'point_number' value is logged for reference.
The extracted data is written to DynamoDB using the put_item method.
A final log message signals the completion of the data input process.
In summary, this Lambda function processes IoT Core events, extracts MPU6050 sensor data, and writes it to the 'MPU6050' DynamoDB table, with logging statements aiding in monitoring and debugging.
Note that the names of the fields are dependent on how you pass the event to the Lambda function, this will make more sense once you see the next couple steps, but in todays example we keep it simple, we will only be processing one stream of acceleration data from our sensor. Mind you that this can be made to fit your use case very easily with some simple changes.
Every time you make a change make sure you Deploy your Lambda!
Step 4-) IAM Permissions
Amazon automatically generates a role for your Lambda function. In order to upload to your table that you created, you will have to increase the permissions on this role.
Search IAM in the console and select the appropriate service.
Select add permissions and add the AdministratorAccess policy so that the user has access to anything on AWS. Be cautious with this role in production!
To create a policy scroll down on the left side menu and select Policies under Security.
You can name it however you like! For further customization of policies, you can look into their documentation. In general, policies are designed to limit/extend access of a device to the channels of your IoT network.
Once you have your policy setup you can create a Thing, go to All Devices in the left bar and select Things > Create Things.
We can auto generate the cert for our purposes:
Now you have a Thing, you will need to download 4 files, shown below.
Device Cert, Public and Private Key, and Root CA 2048 file.
You will need to upload these to your Pico W for your MQTT client to work!
Keep this information as private as possible!
Now that you have a Thing with a policy, we want to create an IoT Rule, that will tell AWS to send data from an MQTT channel to the Lambda function, completing the tunneling of information on the AWS side.
Go to the left panel in Message routing select Rules
Next we define the selection of the rule, in this case we select all values on the MPU6050/ax topic for our IoT Core. This is telling AWS to take the whole message/attributes from every message on that channel.
If you got up to this point congrats, you finished connecting the AWS infra together! Now the MicroPython code and you are done.
Step 6-) MicroPython and Library
Before we start coding the main file we will run, we need to import the 4 files we downloading onto the Pico W and also download the library for MQTT in MicroPython.
Open your MicroPython editor, in this case we are using Thonny to manage files on the Pico W. Upload the 4 files accordingly to the Pico W:
You can copy the contents of this file and substitute it in a file in your lib folder. Create a simple.py file and just copy the code into it. This is all you need for the library.
If everything we smoothly and the code ran it is likely you see results in the table:
And if you want to go over this in detail you can book a consulting slot with me on
https://www.buymeacoffee.com/mmshilleh
Let me know what you think; I hope you got it to work and stay tuned for more. Also do not forget you can buy more sensors at ShillehTek on Amazon.
SOCIAL SHARE CARD GENERATOR