IoT Chicken Shed (Arduino, Wifi)

 

Features

  • 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

Hardware

  • 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 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 strain gauge (as the load cell’s analog signal is weak)

Software

Referenced libraries

Full solution code

  • Available on gitbub – all code for the arduino, esp8266 chip, powershell script, and referenced libraries

 Wiring diagram

wiring-diagram
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 motorshield 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 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)

Setup guide (software)

  • Calibrate the weight sensor by connecting it to Arduino according to the wire 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 ESPlorer 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 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. In my case this is how it was connected:

PIN   FTDI Wire   Target pin on Huzzah ESP8266       Voltmeter test
RTS   yellow      0 (flow control not used by chip)  V+
RX    white       TX
TX    green       RX                                 V+
VCC   red         V+                                 V+
CTS   blue        4 (flow control not used by chip)
CND   black       GND
  • 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).

Other comments

  • In extemely 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 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)

 

Advertisements

4 thoughts on “IoT Chicken Shed (Arduino, Wifi)

      1. Thank you for the link and the code! Over the weekend I managed to hack/merge this into my current coop sketch, and get a webpage up and running perfectly. Only difference is I am using Serial2 on my mega instead of Softwareserial – it works great. My only missing feature is my SMS messages for door actions. It used to work ok (75% of the time) with my CC3000, but became frustratingly flaky causing my sketch to freeze, needing a hard reset. Not fun when you are away from home. I don’t know if it’s possible to use the ESP8266 for this, seeing as it is using NodeMCU, but I have reached out on a couple of forums, and if not, I may add an ATCWINC1500 breakout and see how that works in place of the CC3000. Someday I will figure out how to get my stuff on github — Thank you again – you saved me a ton of headache!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s