3 min read

Feed Blecon sensor data to Amazon DynamoDB

Blecon + DynamoDBIntroduction

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.

 

Architecture Overview

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.

  1. Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.

  2. Click Create Table.

  3. In the Create DynamoDB table screen:

    1. In the Table name box, enter SensorData

    2. For the Primary key, in the Partition key box, enter DeviceID. Set the data type to String.

    3. For the Sort Key, enter Timestamp

  4. 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.

  1. From the AWS Console, click Lambda, and then Create function.
  2. In the Create Lambda screen:
    1. Choose Author from scratch.
    2. Choose a name for the function such as BleconToDynamo.
    3. Choose a runtime environment. For this example we will be using Python 3.9.
    4. Click Advanced Settings and then select Function URL with auth type ‘NONE’.
    5. Click Create function.
  3. In the editor window, paste the following code:
import boto3, json
client = boto3.client('dynamodb')
def lambda_handler(event, context):
    body = json.loads(event['body'])
    for record in body['request_data'].get('records', []):
        dynamo_response = client.put_item(
            TableName='SensorData',
            Item={
                'DeviceID': {
                  'S': str(body['network_headers']['device_id'])
                },
                'Timestamp': {
                  'S': record['timestamp']
                },
                'event_type': {
                  'S': record['event_type']
                }
            }
        )
    response = {'statusCode': 200}
    return response
 

Grant permissions to your Lambda function

We’ll need to grant permissions so that your Lambda function can write to your database table. 

  1. Go to Configuration in your Lambda function and open the IAM role shown under Execution Role.
  2. Click Add Permissions, then choose Create inline policy.
  3. Choose the DynamoDB service, and tick Write in actions.
  4. Click Resources, and click Add ARN under next to Table.
  5. Tick Any Region, and then enter your DynamoDB table name.
  6. Click Add, then Review Policy.
  7. 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.

  1. Still in the AWS console, open the Lambda function you created, and copy the Function URL to your clipboard.
  2. Open the Blecon console at https://console.blecon.net
  3. Create a Blecon Network by clicking Create Network
  4. Go to the Handlers tab and click Edit next to Request Handler
  5. 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.

 

Conclusion

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! 

 




Embedded World Nuremberg 2024: Highlights and Trends

Embedded World Nuremberg 2024: Highlights and Trends

Earlier this month, the Blecon team had the opportunity to attend Embedded World in Nuremberg. This annual event showcases the latest trends and...

Read More
Blecon Joins Cambridge Wireless as a Founder Member

Blecon Joins Cambridge Wireless as a Founder Member

We are excited to announce that Blecon has recently joined Cambridge Wireless (CW) as a founder member. This membership signifies our commitment to...

Read More
Bluetooth Low Energy (BLE) Characterisation with Cambridge Consultants

Bluetooth Low Energy (BLE) Characterisation with Cambridge Consultants

This week we started working with Cambridge Consultants, focusing on the Bluetooth Low Energy (BLE) performance characterisation for Blecon Hotspots...

Read More