These scripts allow you to monitor the temperature and humidity in a set of dryboxes. This code allows you to monitor your dryboxes in a number of ways:
- logs the readings to a database
- serve a website showing the readings in charts
- display the last readings on an LCD screen
- display color-coded LEDs based on the last readings
I use the following hardware for this project:
- One Snapware Airtight 40-cup Rectangular Food Storage Container per drybox
- One DHT22 sensor per drybox
- One dehumidifier per drybox
- Raspberry Pi 3B+
- 20x4 I2C LCD module
- WS2812b LED strip
- Arduino Nano
- 5V power supply with enough current to power the LED strip
- 5V, 2A (or more) Micro-USB power supply for the Pi
- USB to Mini-USB cable (to connect the Pi with the Arduino)
- Wires and Dupont connectors to connect everything up
- Raspbian Stretch Lite
- nginx web server
- PHP for the website
- MariaDB for the database
- Google Charts for interactive charts
- FastLED.io library to control LEDs
- Adafruit DHT library
- I2C LCD Driver by Denis Pleic
- Serial communication demo by user robin2 at Arduino Forums
Thingiverse listing with STL files and printing instructions
Load a Pi with Raspbian Stretch Lite and get SSH going
- Load Raspbian Stretch Lite on a Micro SD card
- Set up image for SSH and WiFi.
- Insert the Micro SD card into a Pi and boot it up.
- Establish SSH connection.
- Change password, locale, timezone, etc. using
sudo raspi-config
- Enable I2C using
sudo raspi-config
andsudo reboot
Update packages
sudo apt update
sudo apt upgrade
Install new packages needed
sudo apt install python-dev python-pip mariadb-server python-mysqldb nginx php-fpm php-mbstring php-mysql raspberry-ui-mods smbus arduino
sudo pip install Adafruit_DHT pyserial
Set up the database
- Secure the database:
sudo mysql_secure_installation
sudo mysql -u root -p
create database drybox;
create user 'sensor'@'localhost' IDENTIFIED BY 'sensorpass'
;grant all privileges on drybox.* to 'sensor'@'localhost';
- Exit from mysql and now login as the user "sensor":
mysql -u sensor -p
use drybox;
CREATE TABLE readings ( id int(11) NOT NULL AUTO_INCREMENT, stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, humidity_1 float DEFAULT NULL, temperature_1 float DEFAULT NULL, humidity_2 float DEFAULT NULL, temperature_2 float DEFAULT NULL, humidity_3 float DEFAULT NULL, temperature_3 float DEFAULT NULL, humidity_4 float DEFAULT NULL, temperature_4 float DEFAULT NULL, humidity_5 float DEFAULT NULL, temperature_5 float DEFAULT NULL, PRIMARY KEY (id) );
Configure php-fpm
sudo nano /etc/php/7.0/fpm/php.ini
, change #cgi.fix_pathinfo=1
to cgi.fix_pathinfo=0
Configure nginx
sudo nano /etc/nginx/sites-available/default
, edit these lines to look like:
index index.html index.htm index.php index.nginx-debian.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
Download and set up the scripts
- Download the code from this repository using
wget
orgit clone
- Create folder drybox:
mkdir /home/pi/drybox
- Copy the python files into this new folder
- Copy the
sketch_drybox_v4
folder into/home/pi/drybox
- Copy the contents of the html folder to
/var/www/html
Connect everything up
- Setup the dryboxes with DHT22 sensors inside them.
- Connect the DHT22 sensors to the Pi over GPIO pins. If you use different pins from what is shown in the picture below, edit
sensor.py
to update the pin numbers - Connect the LCD to the Pi using I2C pins.
- Connect the Arduino Nano to the Pi using a USB-to-Mini-USB cable.
- Connect the LED strip to the Arduino Nano over Pin 2. If you use a different pin, you need to update the sketch_drybox_v4.ino file with the number of the pin you are using
Load Arduino sketch to control LEDs
- Establish a GUI session with the Pi, either using Putty and Xming, or using an HDMI monitor plus USB keyboard and mouse
- Start the Arduino IDE:
arduino
- Load the sketch called
sketch_drybox_v4.ino
- Compile and upload sketch to the Arduino Nano
Test that things are working
- Run the sensor script with
python /home/pi/drybox/sensor.py
- The LCD should light up and within a minute or so, display the readings
- Login to the database with the
mysql
command above and check for recorded data:select * from drybox.results;
- From your computer or phone browser, try the IP address of your Pi, it should show you the website like in the screenshots
- The script will turn the LEDs above a drybox to red if the humidity reading from that DHT22 sensor is higher than a threshold. Based on your layout, you will need to specify which LEDs correspond to which drybox. You can do this in
sensor.py
by updatingHIGH_HUMIDITY
,fil_starts
andfil_ends
- Press Ctrl-C to kill the python script; the LCD should turn off
Install the service
- Copy the
dhtlogger.service
file:sudo cp /home/pi/drybox/dhtlogger.service /etc/systemd/system/dhtlogger.service
- Attempt to start the service:
sudo systemctl start dhtlogger.service
- Check that there are no errors, and check steps 2-4 from the "Test that things are working" section above
- If everything is good, enable the service:
sudo systemctl enable dhtlogger.service
- Reboot the Pi with
sudo reboot
and check that it starts and check steps 2-4 from the "Test that things are working" section again