Jason from Motown Mushrooms asked me to build him an environmental controls system for his mushroom grow room. He needed to be able to monitor and graph temperature, humidity, CO2 levels, number of foggers and fan speed. He wanted a web interface that allows manual control of foggers and fan speed as well as automated control to adjust foggers and fan speed based on measurements and time of day. These requirements were met by using an Arduino to manage the sensors and control foggers and fan. The graphing and controlling is handled by a Python script running on a Raspberry Pi connected to the Arduino by serial.
- CO2 sensor
- Temperature/Humidity sensor
- Fan speed control
- Fogger control for 0 to 10 foggers
- Web interface
- Current state of the farm
- Current outside weather via scren scrape of NOAA site
- Graphs: 1 day, 1 week, 1 month and 1 year
- Manual control settings
- Automatic control settings
- Python control script for collecting, graphing and control
Arduino and Sensors:
The sensor system is handled by an Arduino UNO Rev 3 running a custom sketch. I chose to use the Arduino to handle the sensors and controls because it has more pins than a Pi and it is 5V like most of the sensors ( versus 3.3v for the Pi ). We considered a Beagle Bone but at 3.3v I didn’t want to deal with the voltage conversion for the 5v sensors.
Temp/Humidity: The first and easiest sensor is an encased AM2315 temperature/humidity sensor. I chose this one because it was encased and the I2C interface makes it dead simple. It only comes in a single I2C address model so you can’t have more than one. Jason wanted external temperature/humidity also so I just screen scrape the NOAA website from the Pi instead of using two of these expensive sensors.
CO2: For CO2 monitored I chose an MG-811 sensor module from Sandbox Electronics. This module was a lot easier than having to build up the circuit myself. I had a hard time getting this calibrated, but connecting/reading was as easy as plugging it into an analog pin. This module is protected in the farm in a case with a filter to protect it but allow air flow to the sensor.
Foggers: Jason wanted individual control of the foggers so he could adjust from 0 to 10 foggers running. He also wanted each fogger on it’s own fuse with an LED to show that the fuse wasn’t blown and another LED to show if the fogger was turned on by the Arduino. I handled turning on/off the 24V DC to the foggers using individual MOSFETs with an LED before and after to show state. Since the Arduino has a limited number of pins I didn’t want to use up 10 pins for controlling the MOSFETs so I used two 74HC595 bit-shift registers daisy chained. This allowed me to control the 10 MOSFETs with only 3 pins on the Arduino.
Fan speed: The fan for the farm is a large AC powered bathroom exhaust fan. I don’t like playing with AC so I used a nice off the shelf product called the PowerSSR Tail to “dim” the fan. To use it for dimming I also had to use a ZeroCross Tail to monitor the AC phase. These were super easy to setup and use and required only 2 pins on the Arduino.
Raspberry Pi and Python:
One of the major requirements for this project was the ability to graph the sensors and change settings via a web interface over Wi-Fi. Though the Arduino can be setup with Wi-fi and act as a web server, it is not well suited for data storage and graphing. In addition, Jason wanted to be able to hack on the automatic control logic and he doesn’t have a programming background. Though coding on an Arduino is not hard, it can be a bit cumbersome when logic gets complete and it requires being connected via USB to upload the code.
Based on the requirements and limitations of the Arduino I opted for making the Arduino a dumb “sensor device” that could be polled/set via serial. I then connected it to a Raspberry Pi via serial and a logic level convertor. The Arduino sketch expects simple commands like: “poll”, “set fan 100” and “set foggers 5” and it returns the state as a semi-colon separated list. I then used PySerial library on the Pi to send those commands.
I decided that Python was easier for Jason to hack in and it could be edited via SSH to the Pi over the Wi-Fi. The Python script uses PySerial to talk to the Arduino for polling/setting. It uses PyRRD to store the data points and generate graphs. It uses urllib2 to screen scrape the outside temperature/humidity from NOAA. Luckily the NOAA station is very close to the farm! The automatic control is very immature at this point, but it has access to all the sensor data, outside temp/humidity data, time and historical data from the databases.
Source code: PI_mushroom on GitHub
Some additional Pi configuration that might be of interest to someone attempting a similar project:
- mushControl.py is run from init with respawn. This will start up the script on boot and also make sure respawns if it crashes.
- Because I had to turn off the default serial console to use the serial pins for talking to the Arduino I needed another way into the machine should I loose Wi-Fi signal. I gave myself a “backdoor” by setting up the eth0 interface with a static IP ( 192.168.0.200 ) so I can plug an ethernet cable to a laptop, give it a static IP in the same block and connect via SSH. This doesn’t help in a complete crash, but it is better than being locked out for lack of a Wi-Fi signal.
- Created a cron job to rsync all scripts, data and images to ADrive.com nightly for backup. ADrive allows you to do key-exchange so you can do automated rsync backups very easily!
For the power supply I used a 5V, 12V and 24V unit. My original plan was to use 24V for the foggers, 12V for Arduino and 5V for the Pi, however, I got lazy about powering the Pi from it and just used a wall-wart for it. The power supply does power the Arduino using 12V through a barrel plug.