Have you ever seen a sprinkler spraying water during a rainy day and wondering why the system can not be designed to be aware of the weather ? It turns out most of the sprinkler system are scheduled by its per-programmed controller, this means the water pump will be powered for fix amount of time at a given time of a day no matter what the weather is. Some sprinkler makers did manufacture rain sensors which can override the schedule based on rain sensors' output, but it requires extra purchase, and the installation could be an headache because rain sensor has to be install in open space to collect rain water and controller on the other end, usually installed in the utility room probably far away .
If you do not have a rain sensor came with the sprinkler system and not want to make a purchase, with the help of Thingspeak , free MATLAB app online, and NodeMCU, you can convert your existing sprinkler system into a smarter internet based sprinkler system that make its irrigation decision based on weather report.
This project demonstrates how to build a smart irrigation system by using Thingspeak and MATLAB to get weather report, analyze the weather report and make irrigation decision and storing the decision in a Thingspeak channel, on the hardware side, using cheap programmable board such as NodeMCU together with Thingspeak channel to control/override the existing sprinkler schedule.
Overall DesignBelow is the diagram for the overall design.
The 1st layer is data input layer, which is an online weather report service used for this project. The 2nd layer is data aggregation and analysis layer, here we will use the combination of 2 apps that Thingspeak offers to design a system to make irrigation decision. The 3rd layer is hardware layer , including a cheap programmable hardware, it is going to get the irrigation decision from Thingspeak and use a relay to control the real sprinkler controller.
It's easy to find a free weather API that provides precipitation forecast. In this project I choose www.weather.gov , it supports HTTP protocol. For example : provided longitude and latitude of point of interest:
http://forecast.weather.gov/MapClick.php?lon=-71.2976743&lat=42.29&FcstType=json
It will response a 2 weeks forecast in JSON format:
{
"operationalMode":"Production",
"srsName":"WGS 1984",
"creationDate":"2016-07-10T19:24:28-04:00",
"creationDateLocal":"10 Jul 22:53 pm EDT",
"productionCenter":"Taunton, MA",
"pop":[null,null,null,null,null,null,null,"30","30","40","30",null,null,null],
"credit":"http://www.weather.gov/boston",
We only interested in its "pop" filed, which stands for "percentage of precipitation". This field lists next 14 days of rain forecast. Since we plan to query this weather report daily, we only need to retrieve the first entry of this array, which is the chance of raining of next 24 hours. For this project, let's set the rule to be: if the POP is equal or bigger than 60, it means tomorrow is definitely a rainy day and we will skip the irrigation, if it is below 60, it won't be a rainy day, so do everything as scheduled.
2nd Layer Data aggregation and Analysis : Thingspeak, MATLAB Analysis and TimeControl- First create a channel at Thingspeak, and get it's API keys for read and write.
- Second create a MATLAB analysis app to pull the weather report from weather.gov using
webread
,
it converts the JSON response into MATLAB struct , and then it will calculate a variable calleddoIrrigation
, which indicates whether if the sprinkler should be open on next day, and then this variable will be inserted into the Thingspeak channel usingthingSpeakWrite
function. The code is also downloadable from MATLAB File Exchange
response = webread('http://forecast.weather.gov/MapClick.php?lon=-71.29&lat=42.29&FcstType=json');
popToday = str2double(response.data.pop{1}); % only get the first entry
if isnan(popToday)
popToday = 0 ;
doIrrigation = 1 ;
elseif popToday < 60
doIrrigation = 1 ;
else
doIrrigation = 0 ;
end
thingSpeakWrite(ChannelNumber,'Fields',[1], 'Values',doIrrigation,'WriteKey','MYTOKEN')
- Third, use TimeControl app to execute this MATLAB script at 11pm every night.
Below is a MATLAB plot of doIrrigation VS POP in several days. We can see that when POP (orange) is above 60, doIrrigation(blue) = 0.
Only the last inserted value in the channel will be useful, so the irrigation decision can also be manually overrided, by using thingSpeakWrite
function one more time. 1 means "Operate everything as scheduled ". 0 means " Disable the schedule".
Here analysis to determine whether if doIrrigation
is true
of false
is simple, but with the possibilities of running of MATLAB on Thingspeak, the complexity of analysis can be easily expanded. For example, you can query some other free weather API to get the recent amount of rain fall and store and accumulate the results in another channel, now you can incorporate the amount of rainfall , say in past 12 hours , together with the weather forecast to determine the doIrrigation
value.
In this project, I choose NodeMCU to get the analyzed data from Thingspeak and control the sprinkler, any other programmable hardware with WIFI capabilities will also be suitable. Here is a brief introductory usage of NodeMCU.
a. NodeMCU HTTP Get
In attachments, I listed complete code of using NodeMCU to get data from a specific Thingspeak channel using LUA. In summary, the code contains the following parts:
- Connects to WIFI automatically upon startup
- Queries the Thingspeak channel for instructions
- sets GPIO port accordingly , in order to control the replay output
b. NodeMCU to Replay
According to the irrigation controller manual, it's working voltage is 24V (the reason a relay is needed in this project). Here is the wiring diagram. From NodeMCU to the relay
The actual relay I used is shown below:
c. Relay to Irrigation Controller
Below is a picture of the irrigation controller used in this project.
It has four zones which are represented by four colored wires, and white is the common wire. For example when green zone is in sprinkling mode, white wire and green wire are part of a complete circuit, according to its schema on the manual shown below
I choose to let the common wire go across the relay's COM and NC in this project. NC means "Normal Close". It means unless Vin is HIGH, there is no connection between COM and NC.
Here is a state table, from the value in Thingspeak channel, to the connection status of the Hunter SRC
| Thingspeak | NodeMCU GPIO | Relay NC | Irrigation |
| Channel Value | D2 value | connection to COM | schedule |
|---------------+--------------+-------------------+------------|
| 1 | HIGH | Connected | as usual |
| 0 | LOW | Disconnected | disabled |
d. The rest of the work
The utility room has power outlet , therefore I used a USB adapter and USB cable to power the NodeMCU. so that it will keep running without needing to change the battery. I used some left over wood plate and L-shaped brackets to mount the breadboard and the relay to the wall, and some left over cable wire to connect the relay to the controller.
Comments