The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial


raspberrypi.org/magpi The Official Raspberry Pi Projects Book 107


WINE SAVER: ROOM TEMPERATURE MONITOR


Language
>C AND BASH

DOWNLOAD:
magpi.cc/2uBzlR4

of the configuration file with sudo nano
/etc/ssmtp/ssmtp.conf.


Wine Room Monitor settings – for gmail


hostname=wineroom
[email protected]
mailhub=smtp.gmail.com:587
FromLineOverride=YES
AuthUser=your gmail name (leave out the @
and stuff after that)
AuthPass=your gmail password (no quotes
needed here)
UseSTARTTLS=YES
UseTLS=YES
AuthUser=LOGIN


Now save the file and exit. As we will run the
monitor program with crontab, we must also change
the /etc/ssmtp/revaliases file or authentication errors
will occur. So, sudo nano /etc/ssmtp/revaliases
and add the following line:


root:[email protected]:smtp.gmail.com:587


Now reboot.


The program
Log back into the Pi. First, create a directory called
wineroom and move into it:


mkdir wineroom
cd wineroom


Download the ds18b20.c file from GitHub
(magpi.cc/2uBzlR4) and customise it for your email
address and your alarm temperature levels. To compile:


gcc ds18b20.c –o ds18b20 –Wall -std=gnu99


Hardware setup
Take the wine bottle cork and cut it to half its length
with a hacksaw. Carefully clamp it and use a ¼′′ bit
to drill a hole lengthwise through the centre of the
cork. Next, push the DS18B20 sensor's stainless steel
through the cork so that it is protruding from the other
end – this is why we cut it in half. Wetting the sensor
may make insertion easier. Now rinse the wine bottle
with hot water and fill it nearly full of water. Wet the
cork and push it carefully into the bottle, then put the
bottle on its side to check for leaks. You may need to
add a bit of silicone to seal around the sensor.
On the stacking header, solder a 4.7 kΩ resistor to
the 3V3 and GPIO 4 pins. Solder the sensor red wire to
3V3; the white wire to GPIO 4; the black wire to GND.
Now plug the header onto the Raspberry Pi (make
sure it’s off!), being careful to get the location of pin 1
(3V3) correct. Boot the Pi up, log in, and test it with:


cd wineroom
sudo ./ds18b20 &

Look at the file in your /home/pi/wineroom directory
called wine.log. There should be one entry. You are way
over the temperature range and should get an email
showing an alarm or alert. This will test whether you’ve
got the email settings correct. If you don’t get an email
in a few minutes, take a look at /etc/ssmtp/ssmtp.conf
again to see whether there are any errors.
Now we create a short script to rename the log file and
add the year and month to the log file name at the end
of every month. Of course, since we run it on the first
of the month, we need last month’s date. Download
logupdate.sh from GitHub, edit it to incorporate your
email address, then make it executable:

chmod +x logupdate.sh

Next, we automate things with crontab by editing
with sudo crontab -e and adding two lines to the
end of the file. The first line runs the code at five
minutes after the hour, every hour of every day, and
the log file renaming happens at four minutes after
midnight on the first of the month.

5 * * * * /home/pi/wineroom/ds18b20
4 0 1 * * /home/pi/wineroom/logupdate.sh

Save the file, then to get the new crontab
entries working:

sudo service cron start
sudo update-rc.d cron defaults

Use some Velcro on the bottom of the Raspberry
Pi Zero W case to attach it to the cooler unit. If there
is no power plug inside the wine cooler, remove a
bit of the sealant around the cooler and run a micro
USB power cable through. Then replace the sealant to
close the system back up, power it up, and let it run!

USE BITVISE


Use Bitvise
instead of
PuTTY (same
developers) to
check on the
temperature at
any time.

Place the wine bottle sensor below the top of the rack

CREATE A
UNIQUE GMAIL
Create a
brand new
Gmail account
just for this
purpose. A
unique Gmail
account for
the wine room
prevents a lot
of spam.
Free download pdf