After over a decade of service my original coop controller finally died. This was no fault of it’s own the old 12V AGM battery died and when I hooked up the new one I did so with the incorrect polarity. This was rapidly followed but a “pop” and the dreaded magic smoke escaping from it. The flyback diode took the brunt of the damage it followed by the speed controller FET. Because of this I felt it in my best interest, instead of reviving the old board I would start anew with more goodness. The original controller suffered from a few problems:
- It was difficult to tell if the door was actually up or down from the house
- When it got an error (i.e. chicken poop prevented the door from closing) it was hard to reset
- It utilized a CdS sensor I pulled from a smoke detector which wasn’t temperature stable year round
- It was a custom arduino board and the chip needed to be pulled and put in a programmer to update the firmware (whch is lost to time)
Instead of tying to re-write the firmware for something that truly didn’t meet my needs I decided to start anew with a platform that would offer more flexibility and the possibility to more easily add more features in the future. I settled on the Node MCU 1.1 ESP-32. Being able to control, update and monitor this from the web weighed heavily in making this decision. I got to work and designed it with the following requirements:
- Easy to update/modify – including the addition of other sensors and hardware if I wanted to.
- Easy to monitor with multiple options for triggering the door with the primary mechanism being dusk/dawn
- Off-the shelf parts with somewhat rapid delivery (During the 2020 COVID-19 pandemic) for reasonable prices
- Interface with existing limit switch wiring and lift motor (In this case a Monster Guts wiper motor)
- A weather proof enclosure
The basic design of the controller is the same as the original. A high-power H-bridge to run the winch motor and some switch inputs to detect when the door is in it’s open and closed position. Because I didn’t want to rely on a sensor (or find another one) I decided that I would leverage the wireless connectivity on the ESP-32 to set the time from a network time server and compute the sunrise and sunset time based on NOAA’s calculations. I didn’t want to rely on fetching these from a site or API that might change so I set about computing them directly on the ESP-32 (its just math and at 80-240 MHz I may as well leverage some of that power). Note calculating Sunrise and Sunset is not as easy as it sounds on the surface. Once can find more information on Wikipedia.
Software
I setup the Ardiono IDE to compile ESP-32 binaries following the Espresif instructions online and got to work. I decided to partition the code up into the major components of the main ESP-32 WiFi config and webserver, Sunrise/Sunset calculations, and door controls. I heavily leaveraged some of the examples in the IDE to get started. I leveraged one external library (commented in the code) to do easy Wifi configuration. The code is all working tested and reliable after several days of testing but is missing a couple features that need to be implemented still. Namely automatic daylight savings compensation and some ability to set/store/retrieve some configuration options (aside from WiFi SSID/Password) in the EEPROM. I’d also like to spiffy up the web page and add some additional sensors and possibly logging to SD. There are plenty of pins left on the device to do this in the future. The software code can be found here on GitHub.
Hardware
I prototyped and tested this on my desk with breadboard but needed something that would last far longer and look a little cleaner. The original design did not include an “enable” FET (Q1) or flyback diode (D1) and suffered from random reboots/lockups and intermitent WiFi connectivity issues after the door ran up or down once or twice. My best guess is the back-emf from the motor was causing ground rise sufficient enough to upset the buck converter causing a brown-out on the ESP-32. Adding the FET and flyback diode gives some speed control as well as it could be pulse-width modulated as well. To assemble the final version I utilized the following parts:
- R3. R4, R5 – 1KΩ Axial Resistor (on-hand) R3,R4,R5
- D1 – FR-301 Fast Acting Diode (on-hand)
- Q1 – HUF75345G3 – UltraFET – This was on-hand but is overkill – Rated @ 55V/75A I could run a full-on utility winch
- Any low Rds on FET will do the trick here as long as the Vgs is <= 3 volt
- U2, U3 – Opto Isolated 3V Relay module – Purchased from Amazon
- U1 – ESP-32S Development Board – Purchased from Amazon
- U4 – DC-DC Buck Converter – Purchased from Amazon
- 12V-5A DC Power Supply – Purchased from Amazon
- Solderable Breadboard – Purchased from Amazon
- Pin Headers – Purchased from Amazon
- Small Watertight box – I used this outdoor products one from Wal-mart.
- 3D printed relay module holder, FET holder and Box Mount – Files on Thingiverse
- Wire, M3 screws and nuts, shrink tube, Kapton tape and screw terminals
- Monster Guts wiper motor (on-hand)
- OPEN_SW, CLOSE_SW – 2 Sets of magnetic reed switches (normally open) on-hand
- 12V supply circuit breaker – on hand
(TODO) SHOW 3D PRINTER PARTS AND RELAY MOUNTS ON BACK OF BOARD
Configuration
On first boot it will try to join a stored SSID if one has been defined. Otherwise it boots into captive portal with a name of ChickenCoop_Config. It will scan the local WiFi and allow you to select one an set a password. Once complete it reboots and joins the WiFi and fetches the time. A user can then join the same WiFi and head over to http://chickencoop.local/ where they will be greeted with a webpage showing the door status as well as the current calculated Sunrise/Sunset times and a button to manually open/close/stop the door (depending on state) and a reset button which will reboot the ESP. Currently my door moves intentionally slowly and I have a coded timeout of 10 seconds (the door takes about 8 seconds to open and close) – this prevents some cases when chicken poop or a chicken prevent the door from closing and it starts winding on the winch the other direction. When this happens or for some reason both limit switches are triggered the door goes into an error state and will not run again until the door is reset.
At this time I have my latitude and longitude hard-coded in the software as well as the time-zone information. This is a big to-do for someone who has more time on their hands – for me it’s completely adequate.
Here’s a little clip fo the door in action:
Please consider ordering through my links if you decide to build one as it helps to offset the cost of web hosting.