They said that taking care of plants relieves stress . But no one told me that I cannot automate this.
So I created this.
Its an IoT based automated plant watering system with soil moisture sensor and monitoring system. This project uses Azure IoT Hub, Azure Streaming Analytics and PowerBI for dashboard. Data is sent by the NodeMCU / ESP8266 Arduino. Data is from a collected from a Soil Moisture sensor which controls a submersible pump thru a relay module. The relay turns on the pump depending on the amount of reported soil moisture and waters the plant from a water source. Reported soil moisture is also shown in the 20×4 LCD and is sent to the Azure IoT hub over the internet.
Lets get started!
For software you would be needing an Azure Subscription and A PowerBI Subscription. Hardware components are discussed on the hardware section. You may want to jump there to see if you have the right parts for this project.
We will first configure the cloud based resources needed for this project. We would be needing just the Azure IoT hub for now and streaming analytics.
First is to create a Resource Group
Azure IoT Hub
Then inside the Azure resource group, create a new IoT Hub. IoT hub controls and receives messages from our IoT devices. Start by clicking the add icon.
Search for IoT Hub. Then click create.
Choose the newly created Resource Group and remember the IoT Hub name you specified here. You will be needing it later.
IoT hub has Freeee tier. Lets use that for now. Note that in production setup, you may want to review the scale tier and messages per day. https://azure.microsoft.com/en-us/pricing/details/iot-hub/
Once created, it would look like this:
Now lets create a device in the IoT Hub. On the explorers tab, click IoT Device then click + New icon.
On the create a device, make sure you have the auto-generate keys checked. Specify a unique device name and then click save.
After adding a device, open Azure Cloud Shell located at the top right corner of the page. We will use PowerShell.
Once open add the azure iot cli extentions by executing:
“az extension add –name azure-cli-iot-ext”
Then using the Azure IOT CLI extension, lets create a SAS Token. A Shared Access Signature (SAS) Token is used by a physical device to authenticate to Azure IoT Hub. IoT Hub then checks the token and authorizes the device to send data.
To generate a SAS token use:
“az iot generate-sas-token –d <devicename>– n <iot-hub-name>”
Replace <devicename> with the device name that you specified during device creation and the <iot-hub-name> with the name of your IoT hub. SAS Tokens looks like this:
It is important that you save the result SAS token.
Lets test the SAS Token and the IoT Hub. You may use other mock or API testing software. For now, lets use postman.
Perform an HTTP Post to this URL, replacing the <iot-hub-name> with your IoT Hub Name and the <device-name> with your newly created device.
In the header, add an Authorization key and for its value, use the Shared Access Signature that was returned during the SAS Token Creation in Azure CLI.
For the body, create a simple JSON that has some payload. In my payload I have “smc” or soil moisture content of 80 value and “idn” or device identification which I specified as “device5” again. Do not forget to make it as a “raw” – “json” request and then hit send. You should be able to get a 2XX response from Azure IoT hub.
Stream Analytics Job
Our next is to create a stream analytics job. A stream analytics “accepts” or ingests data, streams it real-time to dashboards or any data at rest storage. For our project, as highlighted here in this figure that data will be coming from IoT Devices thru the IoT Hub. Streaming analytics then delivers our data into a Power BI dashboard in real time.
Same as IoT Hub, search and create for Stream Analytics Job
Choose a name for your new Stream Analytics Job, make sure your location is as close as possible and click create.
Once created we need to configure an Input to be able to ingest some data. So on Job topology, click Add a Stream Input and choose IoT Hub.
Add an alias and select the IoT Hub from your Azure subscription, click save after.
Now lets deliver data from IoT Hub to PowerBI. Still on Job Topology, click output and select PowerBI.
Click Authorize and sign-in to your account. Few window will pop-up, follow the wizard until you are already signed-in.
This part is important, It was hard for me at first and tried to figure out how this works but for now you must choose “user token” authentication mode. This is so that the group workspace shows includes the “my workspace” in the dropdown. Add an output alias, a dataset (database) name and a table name. Remember your values here and then click Save.
Now lets tie them together. From the Job Topology, click query. Use a query that selects all inputs into your output from your input like so. Save the query.
Check your query in the overview and you may start the Streaming Analytics at this point.
Important: Using Postman, send a couple of requests for you to see if there are data coming from Azure IoT hub to Azure Streaming Analytics. Click test query to check if there are Json payload.
Login to your Power BI account with the same account used during the authorizing process in Azure Stream Analytics. Expand My workspace and you should see your datasets under the datasets. Remember the dataset (database) name and a table name used in streaming analytics? This should be the same.
Click your dataset and there fields of the table would appear on your right hand pane.
You may now be able to design your report. Add Event enqueue and the sum of SMC in the fields selection. Select an appropriate visualization such as a stacked area chart here.
Save your report.
Once saved, you may now create a new dashboard. Pin this report to a new dashboard and then click pin live.
You can also add more tiles in the dasbhoard by clicking edit and then add tile.
Select Custom Streaming Data and then click Next.
Your dataset should be available here. Select and click next.
Choose your visualization type, for this example, lets choose a gauge type.
On the tile details, use Soil Moisture Content and then click Apply.
Power BI is also available as an IoS App in the Apple Store. https://apps.apple.com/us/app/microsoft-power-bi/id929738808
Here’s my running dashboard for device5. I plan to use more devices, sensors and pumps. And that’s for another blogpost. Until then, happy coding!
To test real time integration, try and posting data using PostMan and send data to the streaming analytics. We should be able to show data in the Dashboard.
We have configured the IoT back-end and dashboards for our project as shown as in the red dashed box in this figure.
Our next is to set-up the Arduino and Hardware part of our project.
So for our bill of materials:
- ESP8266 / NodeMCU Arduino
- Node MCU Base Board
- LCD Display 20×4 I2C
- HW-080 Soil Moisture Sensor
- Jumper Wires Pack (M-F / F-F)
- Relay Module
- 5v Power Adapter
- Used USB Power Adapter and Cord
- Submersible Pump
I got most of my Arduino parts from the following stores here in Philippines.
Lets get started on assembling our hardware:
Follow this pin-out. Note that the soil moisture uses analog.
Instead of using a breadboard, I used a NodeMCU base board. This is so that I can power V5 PowerSource plus once completed, the project can already be deployable since the baseboard is more sturdy than a breadboard.
Connect the submersible pump to your relay. Use the normally closed on your relay. The relay turns connects the power or cuts off the power going to the submersible pump. Here’s my wiring:
At this point, the LCD can also be connected. Pin configuration is available in the diagram. Also attached the components with some glue stick to make it sturdy and easy to work with.
Casing is just a soap dispenser. Bought from SM Makati (they got it all for youuuu…) bathroom section, near pillows 4th floor. Drilled some holes in the soap dispenser that would fit wiring and LCD screen. Since the NodeMCU needs power, a hole was also drilled on the side.
Here’s some measurements that I used.
Arduino Software / Firmware
I have created a simple repository that contains the relevant source codes for running this. Please do check-out this github link or directly to the circtuio.io storage.
Checkout the code inside the loop.
For Wi-Fi and IoT I used ESP8266-SimpleWifiManager that is available in Github.
Integrating Arduino with Azure IoT Hub
For sending data to the cloud I used HTTP
Here’s the relevant code that sends data to Azure IoT Hub.
First is the SSL Fingerprint. As of writing, the HTTP Client that I am using seems to need the SSL fingerprint. There are a number of ways out there like from the following sites. Use that to begin your HTTP Client instance.
The server address is the one you were given in creating the IoT Hub and adding a device. In my example, I already replaced <device-name> with device5.
Also add the SAS as a value of the Authorization Header as you did in postman.
Perform an HTTP post with a formatted payload with your soil moisture and other information, mine, I added the last pump interval. I measure the amount of time the pump has been running. Why you say? Just wanted to make sure my plants are not drowning.
The Completed Project
Horay! Lets run this. Closing the lid and powering on ESP8266 and the Submersible pump. Also soil moisture is being shown in Power BI Dashboard in mobile.
When soil moisture is below the threshold, ESP8266 turns on the pump and waters the plant. Also shows in LCD like here:
So that’s it! Thats my pet project for the weekend. Hope this was informative and had much fun as I did.
In conclusion, watering plants can relive stress. Automate it, put some IoT and Azure, now fun begins!
Ahh before you leave and trying this out, make sure you have enough credits to support your Steaming Analytics Job. Would cost you a bit. So here is my bill after a while:
Second is that I am not an electrical engineer. My background is IT and Software. My last experience in electronics is back when I took Industrial Electro Mechanics in Don Bosco Canlubang. Other than that, I usually break electronics at home. Soooo.. Please check your wiring and double check sources. (As any other source in the internet, fact check people!). So I provide no warranties whatsoever and use at your own risk.
Its a prototype, a hobby build. Do not use in production.
Finally this is more of an opinionated approach. Thoughts of my own and I do not represent any institution or my employer.