Smart irrigation system part 1: Obtaining weather data via Azure Functions
Santiago Merchán Casado
Presales Manager | Solutions Engineer | Data Architect | Business Developer | BigData | AI | Cloud | Partners
Good morning again,?
I am going to translate the previous article to English so you can see how I am updating the existing developments to maximize the use of the cloud. This is one of the most complete I have done. Both at the level of programming and IoT and DIY and has been running locally for more than three years.
I use postgreSQL to store the data and then the python program intelligently handles the data to activate the sprinklers via setbacks and solenoid valves.
The initial idea was to automatically water the lawn, something simple with a programmer that is sold in any hardware store and some solenoid valves to turn on the irrigation areas but what if I could:
As I have had the system in operation for several years, you can see how using a visualization program like metabase and saving the data in postgreSQL, you can see how the irrigation time varies with sunlight (more in summer and less in winter), how there are irrigation peaks (which usually coincide with heat waves) and how there are "gaps" that refer to rain and after some rain starts to irrigate less until it doesn't work at all.
We are going to focus in this first article on point 1, this way it does not become so long and it is more enjoyable.
We are going to approach it as an ETL (Extraction, Transformation and Loading of data).
DATA EXTRACTION:
To retrieve the data, I use the open data provided by AEMET, where maybe the only downside is that they are about 2 hours late. It is very simple:
DATA TRANSFORMATION
Once the data are in JSON, we pass them to a "dataframe" structure of the PANDAS library so that we can work better with them.
It is necessary to keep in mind that sometimes, they give data that still do not have or have not taken (they show them as "NaN") for what it is necessary to revise it and to put them with a "" so that when saving them in the database it does not give error.
The formatted data in a data frame would look like this:DATA LOADING
领英推荐
LOAD DATA
Finally, we save them in the database using a library to connect to PostgreSQL called psycopg2
AZURE FUNCTION
To adapt the code to Azure Functions I have followed this guide from Microsoft, where you have to take into account several aspects:
The data must be displayed as if they were a web page, otherwise they will not be seen.
You have to enter the code inside the function that is created with the command "function init <project_name> --python -m V2".
In total there are about 5 calls that come in the guide for:
Then we can see the function in Azure Functions
And inside the function we have programmed, where we can copy the complete URL to call it.
The important thing is the result, which as you can see is as follows:
Now it only remains to schedule it to run every X minutes, and one of the things I like the most is to add functionality to send an email when it is executed, and thus know how it has gone without having to look at Azure.
Finally to close this first part, I copy the Azure function files on GitHub and I paste a screenshot of how the data looks like if you show them in a BI tool like metabase.
And soon for the second part to see how to get other data from the AEMET as the time of sunset and sunrise, or rain predictions.
Best regards to all of you:
Santiago