I have a long-running project to make timelapse movies using a Raspberry Pi Zero with camera module. I’m capturing a countryside view and have images for two years. The uptime on the Pi is seriously impressive, but nonetheless I wanted to upgrade the Pi so that it would send me updates of how it’s going, so that I would know if it image capture had failed. Here is my solution.
To recap: the camera takes images at 10 min intervals then, at the end of the day, it transfers all of the days photos to a file storage server. Once per week the old images get deleted from the Pi. I use another computer to pull down the images from the server and generate the time lapse movie so that I get an updated view of how the project is progressing.
Due to a change which is too boring to detail, I no longer get a daily update on this process, so I wanted to set up an email-based alert scheme.
Fortunately, this is straightforward to do on a Raspberry Pi.
Setting up SSMTP on the pi
Log in to the pi using ssh and run
sudo apt-get install ssmtp
…to install ssmtp. It’s good practice to backup the existing configuration file.
sudo mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bak
Now create a new configuration file where we will store our details:
sudo nano /etc/ssmtp/ssmtp.conf
In nano, I pasted in the details below and substituted my credentials for address and password. I used a gmail account that was setup for the purpose of automated messaging. IMPORTANT: this account needs to set “Less Secure App Access” to ON. You can find this in the settings after logging in to that account (currently under Security, but it does move around).
firstname.lastname@example.org mailhub=smtp.gmail.com:587 FromLineOverride=YES AuthUseremail@example.com AuthPass=mypassword UseSTARTTLS=YES UseTLS=YES
Save and quit with ctrl+X and Y. Now, secure the configuration file:
sudo groupadd ssmtp sudo chown :ssmtp /etc/ssmtp/ssmtp.conf
Finally, I checked that I could send email from the command line using
echo "Hello, World!" | ssmtp firstname.lastname@example.org
I used another email account in place of email@example.com and confirmed that I got the message.
Currently, the bash script to upload the photos runs daily via cron. This runs using rsync and I simply wanted to send the output from rsync to myself via email. So, I just needed to add this to the bash script and I would be done!
To explain how I approached this, here is an example:
ssmtp firstname.lastname@example.org < msg.txt
As long as msg.txt is formatted correctly, this command will send an email. You can test this out by making a msg.txt file:
To: email@example.com From: firstname.lastname@example.org Subject: alert The server is down!
Note that there is a carriage return between the subject and body.
If you run the command above after creating that file, you will receive an email from your Pi!
So, my idea was to make a file called header.txt and add the output from the file transfer to it and then send the resulting txt file via ssmtp in the bash script.
Step one, in the same directory as my bash script, I made a file called header.txt that looked like this:
To: email@example.com From: firstname.lastname@example.org Subject: PiCam update Here is the output
Step two, I modified the bash script. The original script was:
#!/bin/bash sudo mount -t cifs -o user=xxx,pass=xxx,vers=1.0 //server.address/xxx/ /mnt/abc sudo rsync -trv camera/ /mnt/abc/camera/ sudo umount /mnt/abc
I changed it to
#!/bin/bash sudo mount -t cifs -o user=xxx,pass=xxx,vers=1.0 //server.address/xxx/ /mnt/abc sudo rsync -trv camera/ /mnt/abc/camera/ > output.txt sudo umount /mnt/abc # concatenate output with the correct header and email it cat header.txt output.txt > msg.txt /usr/sbin/ssmtp email@example.com <msg.txt # backup the output (in case we need to look at it) mc output.txt output.txt.bak # now create blank file in its place touch output.txt
The output from rsync goes to a file called output.txt. This gets joined with the header to make the msg.txt file.
Note that I used the full path to ssmtp since when the script runs under cron, ssmtp is not found in the path.
This works well. I am getting daily emails about the upload and the absence of an email or a blank body email will tell me that there is a problem with the Pi.
Of course, there are other ways to tackle this. For example, getting the server to send the message (and do so only in the absence of an upload), but the approach outlined here was simple to set up. I also modified the deletion script so that I get an email about that too!
The post title comes from The Bee Gees’ single “I’ve Gotta Get A Message to You”. The Raspberry Pi is undergoing a different type of execution than the person in the song’s lyrics.