AWS IoT setup for Water Level Sensing

In this post of mine, I would be taking you through the steps for setting up a system that continuously detects the water level of a water container and sends the water level data to AWS IoT and on the basis of the readings send notifications for low or high-water level using AWS SNS. The flow of the entire system will be like the Ultrasonic Sensor will be used to measure the depth of the water in the container and it will send the data to Raspberry Pi. Raspberry Pi would then prepare a payload from the data received from the sensor and send it to AWS IoT using the MQTT protocol. On the AWS IoT Rules are created which would query the incoming data for specific water level (here water-level<5 or water-level>10) and would send the data to Lambda Function which is configured to push, message to SNS topic and subscriptions to that topic will receive a message for low or high-water level along with the MAC address of Raspberry Pi sending the data. So, let’s get started.


Before going directly to the configurations and set up there are few prerequisites that need to be set up. They are as below:

1. Download and write raspberry PI OS on a micro-SD card

2. Install SD card into Raspberry PI module and set up the OS


Raspberry Pi OS – Raspberry Pi

The Raspberry Pi is a tiny and affordable computer that you can use to learn programming through fun, practical…

Create a Thing in AWS with Certificate and Policy

  1. Register a ‘Single Thing’ in IoT Core
  • Go to the IoT Core service section on the AWS console. In the left pane select Manage->Things->Register a Thing
  • Next, choose the option Create a Single Thing and it will take you to add your device to the thing registry page. Give the name of the thing of your choice, I have given the name WaterLevel. Leave all other fields as it is and click on Next.

2. Create and download ‘Certificates’

One peculiar feature with AWS IoT is that it is of the industrial standard since the communication is highly secured. So, for everything we create we have to associate it with a certificate and a policy.

In the Add, a Certificate for your Thing chooses the option of Create Certificate. Here AWS will provide the keys for the thing that we just created.

This thing can be accessed only through validating through these keys. Make sure you click on the Activate button first and then download the three key files and save them on your computer somewhere secure. Never share these keys with the public, since they can use your AWS account by launching this thing and you will be charged for it.

3. Create ‘Policy’ and attach to ‘Certificate’

Now back on the main IoT Core page in the left pane go to Secure->Policies and click on Create Policy to generate a new policy for our registered thing.

  • Create a policy, the name of the policy will be WaterLevelDetection for me, give a policy name of your choice.
  • For action enter IoT:* — indicates that we can both subscribe and publish to the things with this policy.
  • For Resources ARN enter * — indicates that the thing is accessible to all clients who have access to this certificate. Make sure the allow button is checked for allowing effect.
  • Choose the “Create” button at the end of the page to create the policy.
  • Next, go back to IoT main page and in the left pane go to Secure->Certificates where the previously created certificate would be visible, there click on Options and select Attach policy.
  • Pop-up will appear in which select the policy name created in the earlier step for me WaterLevelDetection and click on Attach.

That’s it, we have created a thing, a certificate, and a policy and have linked the policy and certificate to the thing that we have created.

Install AWS IoT Device SDK on Raspberry Pi

For using the AWS service on Raspberry Pi AWS SDK is installed. Python SDK, as well as JavaScript SDK, is available but I have used the Python SDK which will be using python programming language to write the program for setting up the entire system.

Commands to install and setup SDK

git clone

The above command have installed the AWS directory on your Pi, now navigate into it using the following command.

cd aws-iot-device-sdk-python

Inside the directory install the setup file using the line below

python install


On the hardware side, a Raspberry Pi is connected to Ultrasonic Sensor (HC-SR04) to get the values of the distance between the sensor and water surface.

Programming Raspberry Pi

Here a python program is created to read water level distance, calculate the depth on basis of distance received, get the MAC Address of raspberry pi and publish the data AWS IoT thing created in earlier steps. In the python program link to the three key files downloaded earlier is provided so you need to move the key to the raspberry pi which will give Raspberry Pi access to publish messages.

Create AWS IoT Rules, SNS Topic, and AWS Lambda Function to query data and send notifications

Rules for IoT: Rules give your devices the ability to interact with AWS services. Rules are analyzed and actions are performed based on the MQTT topic stream. We will use Rules to filter data received from a device.

Create SNS Topic and Subscription

  • Go to the AWS SNS page and there choose Create Topic.
  • In the details sections choose the Standard type and give the name of your choice, I have given water_level.
  • For display name that would appear in the message give a name like Water-Level.
  • Next, Create a Subscription, and in the details section choose the ARN of the Topic created in the previous step.
  • For Protocol choose SMS and in Endpoint enter the mobile number which should receive the notification message.

Note: If you are working in the Sandbox Environment, then the number on which messages are to be sent should be added in Mobile->Text Messaging (SMS) section.

Create a Lambda Function to receive filtered data from Rules and send formatted messages to SNS Topic

Go to Lambda Console and create a function name of your choice, choose the language which you want to work with I have chosen Python and in the IAM Role for Lambda function along with BasicLambdaExecutionRole policy also policy to publish a message to SNS.

The code in lambda is very simple, it would receive an event which will MAC address of the raspberry pi, Level of water. We will check in Lambda function if the received level is less than 5, it would send a message for High Water Level along with MAC address and if the level is greater than 10 message for Low Water Level will be sent.

Create Rules in AWS IoT

  • For creating rules go back to the IoT page there in the left pane select Act->Rules and click on Create
  • Create a Rule page is opened, give the Name for the rule, a small description.
  • In the Rule query statement, we will write the query statement to filter only the MAC, Level attributes from incoming data which has Level less than 5 or greater than 10 from the topic named WaterLevelDetection and also pass SNS topic ARN to the lambda function.
  • Next in the Set, one or more actions click on Add action and there choose to Send a message to a Lambda function and click on Configure action and choose the Lambda function created in the earlier step and click on Add action and then Create Rule.

Now that’s all, for the execution run the python file containing the code on raspberry pi and check the output.



Table of Contents

On Key

Related Posts

Doc Analyzer

AWS Textract Based Document Segregation Automating Doc’s Data Extraction from any document using Amazon Textract in Python. This project was intended to segregate the submitted