Introduction Blecon is enabling a new class of physical data sources and sensors based on Bluetooth...
Feed Blecon sensor data to Amazon DynamoDB
Blecon sensors are a new type of low-cost Bluetooth-based sensor designed for developers and application builders to feed real-world data into their products and applications.
In this article, I will show you an example of how you can integrate Blecon with AWS and DynamoDB to create a fully serverless real-world data ingestion pipeline.
This tutorial assumes an intermediate-level knowledge of AWS.
The architectural diagram below shows a high level overview of the solution.
Blecon feeds your integration with HTTP requests containing a JSON representation of a real-world events from any sensors that have been connected to it.
To log the data, I will create a Lambda function which creates records in a DynamoDB table. Lambda and DynamoDB are serverless, so this is a cost-effective and scalable way to record Blecon sensor data.
Creating your DynamoDB table
Let’s start with creating our database table. To do so, you’ll need an AWS account. This AWS getting started tutorial may be useful if you've never used AWS before.
Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
Click Create Table.
In the Create DynamoDB table screen:
In the Table name box, enter SensorData
For the Primary key, in the Partition key box, enter DeviceID. Set the data type to String.
For the Sort Key, enter Timestamp
- Click Create
Creating your Lambda Function
When your Blecon network receives readings from your sensors, it will forward it to an HTTP endpoint that you specify. To prepare to accept those sensor readings, let’s create a lambda function that will accept the HTTP request and then write the readings to our DynamoDB table.
- From the AWS Console, click Lambda, and then Create function.
- In the Create Lambda screen:
- Choose Author from scratch.
- Choose a name for the function such as BleconToDynamo.
- Choose a runtime environment. For this example we will be using Python 3.9.
- Click Advanced Settings and then select Function URL with auth type ‘NONE’.
- Click Create function.
- In the editor window, paste the following code:
import boto3, json
Grant permissions to your Lambda function
We’ll need to grant permissions so that your Lambda function can write to your database table.
- Go to Configuration in your Lambda function and open the IAM role shown under Execution Role.
- Click Add Permissions, then choose Create inline policy.
- Choose the DynamoDB service, and tick Write in actions.
- Click Resources, and click Add ARN under next to Table.
- Tick Any Region, and then enter your DynamoDB table name.
- Click Add, then Review Policy.
- Choose a name for your policy such as WriteToSensorData, then click Create Policy.
You should now see the new policy attached to your Lambda role.
Configure your Blecon Network
Your pipeline is now ready to use. The last steps are to tell Blecon the URL where to send sensor data.
- Still in the AWS console, open the Lambda function you created, and copy the Function URL to your clipboard.
- Open the Blecon console at https://console.blecon.net
- Create a Blecon Network by clicking Create Network
- Go to the Handlers tab and click Edit next to Request Handler
- Paste the Function URL you copied and click Save. Note that it’s best practice to require a specific header code in your handler. We have left that step out of this tutorial for clarity.
Your Blecon Network is now forwarding a stream of sensor data to your AWS environment and storing it in your DynamoDB table.
Connecting your sensors
This is the easy part! Connecting Blecon sensors and starting the data stream can be done very quickly and without any coding or configuration. All Blecon Enabled sensors work the same way, regardless of manufacturer.
For this example, we are using the Blecon B1 analytics tag.
To get your sensor connected, scan the Network QR code from the network you just created, then tap on the device to use NFC. For more information, see the quickstarts in the Blecon Documentation.
This quick tutorial shows one way that Blecon helps you add low-cost Bluetooth sensors to your application.
There are limitless ways to integrate sensor data into various cloud environments, from data warehouses to no-code platforms. We'll be exploring some of these possibilities in future articles.
Blecon is currently in limited preview. To apply for early access and receive a free device, get in contact and tell us what you want to build!