If you have chickens and find you're not always available to close the gate at night or check the water levels each day, then why not use an ESP8266 to automate your chicken door and receive alerts door status and low water level, as well as allow you to remotely control the door from anywhere over the internet.
- Control the door and check how much water is left through a basic website hosted on the chip itself (HTML, and RESTful)
- Prevent over-closing or over-opening
- Can recover from power cut outs
- Can recover from wifi chip reboots or network disruption
- Flash LED every few seconds to indicate that the door is closed
- Fox proof: Can’t be pushed open while closed
- Automatically operated by a script (open/close times are calculated based on sunrise / sunset times for your location)
- Email alerts for open/closes, failures, water low and water empty
- Accessible over the internet via VPN
Parts you'll need
- Arduino Uno
- Adafruit Motorshield v2
- 12v 1A power supply for the Adafruit Motorshield
- Huzzah ESP8266 breakout for wifi
- FTDI cable (Serial TTL-232 USB Cable) for uploading LUA apps to the ESP8266 chip. The Huzzah ESP8266 breakout’s RX and VCC pins handle 5v, so the FTDI cable voltage does not matter (3.6 and 5v will work).
- Magnetic Relay for sensing door closed
- 5V single channel relay to control power supply to the motor shield, as the motor accumulates tremendous heat if not released. While the Arduino app calls the release function, it also cuts power as a fail-safe.
- LED for indicating door closed, plus two transistors to split the voltage supplied to it (an Arduino output pin was enough to blow the previous LED)
- Stepper Motor – Mercury Motor SM-42BYG011-25 1812010 (12v 0.33A)
- Threaded Rod (8mm) – to be driven by the step motor. As it rotates it screws itself onto a nut attached to the door. The rod is in a fixed position so this forces the door to open or close.
- Flex Shaft Coupler (5mm to 8mm) – to connect the stepper motor’s 5mm output rod to an 8mm threaded rod
- Load Cell (10kg max) – to measure how much water is left
- SparkFun Load Cell Amplifier HX711 – used to get measurable data out from a load cell and a strain gauge (as the load cell’s analog signal is weak)
- Adafruit MotorShield v2 library – Interfacing with the Adafruit Motorshield v2
- Sparkfun HX711 code samples – For calibrating and using the load sensor
- TimerObject – Setting interval timers. Used to flash an LED briefly every few seconds and to keep the wifi chip updated every few seconds.
Full project code
- Available on GitHub – all code for the Arduino, esp8266 chip, PowerShell script, and reference libraries
How it works / key points
- Arduino and the ESP8266 have separate processors. They talk to each other through SoftwareSerial. The ESP8266 is running nodeMCU, and scripts sent to it through serial are executed on the chip.
- The ESP8266 runs a RESTful web server written in LUA (nodeMCU). “/GetStatus” returns an HTML page with links to operate the door. The links point to “/OpenDoor”, “/CloseDoor”. The chip passes these commands on to Arduino through SoftwareSerial.
- Arduino listens for commands through hardware serial (PC, debugging), or Software Serial (from the ESP8266 wifi chip)
- Arduino movies the door by enabling power to the motor shield and then moves the stepper motor a pre-determined amount of steps to open or close the door or until the magnetic sensor is tripped (door closed)
- The stepper motor is moved in chunks of 10 steps so that Arduino can check if a stop command has been received, and also keep the wifi chip updated on the status.
- When Arduino starts up (e.g. after the power cut) it checks the magnetic relay switch to see if the door is closed. If it is not closed it is assumed to be 100% open
- Arduino keeps the wifi chip up to date on the status every few seconds using TimerObjects to send a line of LUA script to the wifi chip
Setup guide (hardware)
- First, connect the Adafruit Motor Shield to the Arduino, and solder on all pins.
- Connect the hardware as shown in the wiring diagram
- If you have purchased the identical step motor, then the wire colours shown in the wiring diagram will be correct
- Use the flex shaft coupler to connect the threaded rod to the step motor
- To connect the threaded rod to the gate, drill a hole in two very small squares of wood, and glue the nut in between the two pieces, then this can be attached to the door with wire, so that when the motor operates the door will slide up or down as the threaded rot rotates.
- See the photos below:
Setup guide (software)
- Calibrate the weight sensor by connecting it to Arduino according to the wiring diagram above (through the load cell amplifier) and use the SparkFun HX711 code samples for calibration. This gives you the calibration_factor and zero_factor values which you can hard-code into the Arduino code.
- Connect the Arduino to USB and upload the Arduino code
- Connect the ESP8266 to the PC with the FTDI Serial cable and use explorer to upload the LUA application files (upload each file one by one).
NOTE: The wire colours and positions on the FTDI cable didn’t match the pins on the ESP8266 chip in my case, and needed to be switched position. To check this take apart the plastic housing on the USB connector to see the pin labels. The wires need to be connected as below. In my case, the wire colours were as below.
- The Huzzah ESP8266 (unlike the plain ESP8266) can deal with the 5v inputs coming from Arduino. If you use an ordinary ESP8266 they need to be dropped to 3.5v.
- I used a PowerShell script (running on an always-on laptop) to automatically open and close the door at set times, and send water low or empty notifications by email. The script also attaches photos collected from a spare android phone mounted in the shed (IP Camera).
- In extremely hot (e.g. 40ºC) or wet weather, the magnetic relay (door closed) wire picks up so much interference that it can trip (pull the pin to a low). This is probably because the wire is over 1m long. To deal with this, the Arduino code samples it 10 times per second and needs 10 consecutive readings the same before it assumes that the state has changed. This solved the problem.
- After about 2 months of continuous operation, Arduino can stop responding (probably memory leaks). A regular reboot is enough to deal with it (you could just use a cheap timed power socket that cuts power off briefly once per day)
- The wifi chip does not have a far-reaching range. If the chip is on the edge of reception it is susceptible to constant crashing. You could extend the range by replacing one of your router's antenna with a "Yagi Cantena" or "Yagi antenna"