Yes that’s a mouth full but over the last few weeks I have been playing with an Arduino based retic controller. After looking at some off the shelf solutions from the local Bunnings I wasn’t happy with the number of channels provided nor the control or expandability. So off I went to buy an Arduino and a bunch of relays and a rough plan. The idea is to wire all my solenoids to a set of relays managed by the Arduino which is programmed to trigger them for a certain period of time on the correct watering days. Additionally the long term plan was to add in fertilizer injection and rain/moisture sensors to be able better manage my gardens.
First step, all the kit you need.
- Arduino Mega 2560 (http://www.sainsmart.com/arduino-compatibles/atmel-avr-board/sainsmart-mega2560-development-board-for-arduino.html)
- W5100 based Ethernet shield (http://www.sainsmart.com/ethernet-shield-w5100-for-arduino-2009-uno-mega-1280-2560.html)
- Sain Smart LCD Display (http://www.sainsmart.com/arduino-compatibles/module/lcd-module/sainsmart-keypad-shield-1602-lcd-module-v3-display-for-arduino-mega-2560-1280-uno-r3.html)
- 16 Channel Relay Board (http://www.sainsmart.com/16-channel-12v-relay-module-for-pic-arm-avr-dsp-arduino-msp430-ttl-logic.html)
So with all this kit you can effectively build a 16 channel system, with the capacity to expand to around 50 channels or so depending on what shields you have connected.
After getting all the gear there were a few issues to sort out. First is how do you track time on Arduino, unfortunately by default the 2560 does not have a RTC. Initially I had looked at some NTP based libraries but settled on the RTC module from Freetronics. With this wired up I started up a basic sketch and had my relays triggering at certain times without too many problems. Everything seemed ok so time to add in the Ethernet and LCD shields.
You end up with a rather tall stack like this.
Not too bad, not exactly pretty but then it’s going to be housed in a box anyway.
Getting the LCD and Ethernet working was pretty straight forward. There are plenty of libraries to deal with getting these working however where things become complicated is the fact that Arduino is really single threaded at heart so how do you still accept input whilst triggering something for a period of time? E.g. if you use a simple sleep the entire process halts till complete thus blocking any other code from running such as your LCD management or Ethernet code. The easiest way is to track the number of seconds and use that to create start and stop times, however the RTC library recommended by Freetronics doesn’t support a simple unix time stamp, and only reports in literal days, hours, seconds.
The solution after a great deal of messing around was to use the timer library which makes creating triggers and simple timers relatively easy. Once in place it was straight forward to trigger something. However yet again a new problem was found. Triggering multiple timers and mixing them in with the rest of the system made programming multiple “programs” for the retic zones complicated. Not impossible but lots of looping to figure out things resulting in it being rather slow in performance. Throw in this with the complexity of building a web interface this was fast becoming a right pain.
So the simplest way out was to drop most of the complexity from the Arduino and treat it as a simple controller accepting commands over Ethernet from a big PHP powered system with cron jobs on the back end. This makes things easy to program. So after dropping all the existing code I can now trigger an Arduino channel with a simple packet consisting of <channel>|<action>|<time>|<security code>. E.g. 1|1|10|***** would trigger channel one to turn on for 10 seconds and without the security code the trigger will be ignored. Simple and effective and means I can more easily build a web interface in PHP and spend more time designing that than messing with the Arduino side of things.
The only remaining functionality problem was how do I add things in like rain sensors or other inputs. A slight adjustment to the code now allows a return if a channel is an input which can then be acted upon. E.g. 1|2|***** would tell the controller to read an input from channel 1, and the standard 0,1 options for the action just simply switch on and off.
With the code in place I purchased a large weatherproof box and mounted the parts. The next item on the list is connectivity. I wasn’t keen on running a network cable the box, or at least I will eventually but want this working sooner rather than later. So a TP-Link WR702N was the quick fix, it’s a small credit card sized (although obviously not as thin) wireless access point that can also act as a client and bridge a device. This is the blue device in the image below. It runs off USB to you can easily hack the cable to run off the 5v supply from the Arduino making it a cheap solution compared to a WiFi shield.
So with connectivity sorted, power was the next obstacle. I needed a combination of 24v AC for the solenoids, 12v DC to drive the relay and ideally 5-12V DC for the Arduino. During testing I had been using a standard ATX power supply with the correct pins bridged to power it up. I then dug out my old M1-ATX power supply for my CarPC which wasn’t in use. This would give me a nice easy 5v and 12v regulated supply.
However I still needed to get 24v AC which was solved with a standard wall wart power pack. However I still needed to supply 12v to the DC-DC car PSU. I picked up a cheap $20 24VAC –> 12VDC adapter for security camera’s which worked well but the wiring looked messy and I had to mod the ATX plug to get it to power on.
In the end the solution was actually quite simple. It turns out the relay board has a 5v regulator on board to drop 12v down to 5v for some of the components on the board. I was able to tap into this and directly power both the Arduino and the TP-Link. This means doing away with the car PSU and it being a rather simple layout. 240VAC –> 24VAC –> 12VDC –> 5VDC with each voltage branched out to where it needs to go.
The image above shows the resulting relay board layout. To reduce any bleed or earthing the first relay needs to be switched on before power flows to any others. This also works as a master trigger for the master solenoid valve which you should always have in a retic system to prevent leaks causing large water bills.
So the end result of all of this is the following. Soon to be mounted in the cabinet outside and wired up.
An early version of the trigger code is listed below but doesn’t have the security option. I’m trying some other options at the moment to simply send a hash with a salt loaded into the Arduino to stop sniffing (just in case).