Xiao Xu
Published © MIT

Thingspeak and Internet Sprinkler

Have you ever seen a sprinkler spraying during a rainy day and wondering why the system can't be designed to be aware of the weather ?

EasyFull instructions provided5 hours2,168
Thingspeak and Internet Sprinkler

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
NodeMCU
×1
Songle Relay
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Wire Cable - By the Foot
OpenBuilds Wire Cable - By the Foot
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
MATLAB
MATLAB

Story

Read more

Schematics

NodeMCU and relay wiring

NodeMCU -> Relay
D2 -> In
3.3V -> Vin
GND -> GND
Wiring

Relay and Hunter Controller Wiring

Let the irrigation controller's common wire to go through relay's common and normal close ports.
so when Relay 's input is HIGH, the replay's two port is connected. The controller is enabled/powered.

Code

Thingspeak MATLAB code

MATLAB
* Use Thingspeak MATLAB Analysis app to pull weather report from weather.gov
* analyze the percentage of precipitation from weather.gov response
* calculate the irrigation decision and store it in a Thingspeak channel
* use Thingspeak TimeControl app to execute the MATLAB code daily
response = webread('http://forecast.weather.gov/MapClick.php?lon=-71.29767435395624&lat=42.29233601995253&FcstType=json');
popToday = str2double(response.data.pop{1});

if isnan(popToday) 
    popToday     = 0 ;
    doIrrigation = 1 ;
elseif popToday < 60
    doIrrigation = 1 ;
else
    doIrrigation = 0 ;
end

popToday
doIrrigation

thingSpeakWrite(128219,'Fields',[1], 'Values',doIrrigation,'WriteKey','MYTOKEN')

NodeMCU pull commands from Thingspeak

Lua
* NodeMCU connects to WIFI automatically upon startup
* NodeMCU querys the Thingspeak channel for instructions
* NodeMCU sets GPIO port accordingly to control the replay
---------------------------
---- connect to  wifi -----
---------------------------
function setupWifi()
         wifi.setmode(wifi.STATION)
	       wifi.sta.config("YourRouterName","YourPassWord")
         wifi.sta.connect();
          wifi.sta.setip({ip="192.168.1.11",netmask="255.255.255.0",gateway="192.168.1.1"})
	 tmr.alarm(1, 1000, 1, function()
	      if wifi.sta.getip()== nil then
  	        print("IP unavaiable, Waiting...")
	      else
	        tmr.stop(1)
          print("ESP8266 mode is: " .. wifi.getmode())
          print("The module MAC address is: " .. wifi.ap.getmac())
          print("Config done, IP is "..wifi.sta.getip())
        end
        end)
end

--------------------------
-- pull from thingspeak---
--------------------------
function pull_from_thingspeak()
   conn=net.createConnection(net.TCP, 0)  
   conn:connect(80,'api.thingspeak.com')
   
   conn:on("connection",function(conn, payload)
        -- replace the channel number here 	     
	      cmd ="GET  /channels/999999/fields/1.json?results=2"
		 .. " HTTP/1.1\r\n"
		 .. "Host: api.thingspeak.com\r\n"
		 .. "Connection: close\r\n"
		 .. "Accept: */*\r\n"
		 .. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
		 .. "\r\n";
	      print(cmd);
	      conn:send(cmd); 
    end)
   
   conn:on("receive", function(conn, payload)
	      -- the last 7th character is the  last entry of the channel
	      -- which is the latest direction from Thingspeak 
	      result = string.sub(payload,-7,-7) ;
	      if(result) then		
		 if  (result =='0') then
		    print("0,false,cut off");
		    gpio.write(2,gpio.LOW);      -- CLOSE the relay
		 elseif  (result =='1') then
		    print("1,true,connected");
		    gpio.write(2,gpio.HIGH);    -- OPEN the relay 
		 else
         -- if result is not 1 or 0
		     -- something is wrong, pull again
		    pull_from_thingspeak();	     
		 end
	      end    
	      conn:close();
   end)   
end



------------
-- main-----
------------
setupWifi()
-- pull every two hours here
tmr.alarm(1,7200000,1,function()
      	 pull_from_thingspeak()		    
end)

Credits

Xiao Xu

Xiao Xu

2 projects • 8 followers
Thanks to Hans Scharler, Robert Mawrey.

Comments