Happy Plants: by Libre Router + Esp 8266 relay
Continued from %jUHsj4g...
Been dreaming to automate the agroforest irrigation for a long time, so I finally was able to do it using a 4 channel relay Esp 8266 module. By following this video on how to use it I was able to connected it to my home WiFi using the ESP8266 SmartConfig app and to control it with the ESP2Relay Controller app.
But I wanna run it on a cronjob, so that different channels turn on and off at set times. I found out that I can control it using
netcat, like this:
echo -e '\xA0\x04\x01\xA5' | nc -q 1 10.147.244.91 8080
I can control each channel sending commands according to the table below:
Open relay 1 :A0 01 01 A2 Close relay 1 :A0 01 00 A1 Open relay 2 :A0 02 01 A3 Close relay 2:A0 02 00 A2 Open relay 3 :A0 03 01 A4 Close relay 3 :A0 03 00 A3 Open relay 4 :A0 04 01 A5 Close relay 4 :A0 04 00 A4
Since the ESP connected to my home #librerouter, I can set crons on it to run at set times, taking advantage of the LRs GPS to make sure time is accurate. OpenWRT comes with timezone set to UTC. By checking the documentation I found out that we can set the right timezone by changing the
/etc/config/system, file and checking this table to get the right timezone code (in my case
<-03>3). I had to reboot for the changes to take effect, and now the
date +"%T" gives me the right time.
Now I can
crontab -e and add the commands:
00 04 * * * echo -e '\xA0\x04\x01\xA5' | nc 10.147.244.91 8080 25 04 * * * echo -e '\xA0\x04\x00\xA4' | nc 10.147.244.91 8080
So this will turn channel 4 ON at 4:00am and turn it back OFF at 4:25am 🎉 💧 🌱
But there's still one problem. The ESP keeps changing it's ip, and ideally I don't want to deal with firmware tweaking. So I had to write a little bash script to get the ESP ip from dhcp.leases and simplify the whole packet sending. Final version of the script can be found on this gist.
Finally, this is how crontabs ended up looking like:
0 4 * * * control_irrigation.sh start 4 25 4 * * * control_irrigation.sh stop 4
Now it's easy to add cron jobs for other channels 🥧