Raspberry Pi Server - BitKnitting/should_I_water GitHub Wiki
Overview
The code is located at this GitHub location.
Collect Moisture Readings
When the Raspberry Pi boots up, the happyday-collect.service starts running. This systemd service runs collect_readings_task.py. This python script listens for RFM69 packets from a moisture puck. When a packet comes in and it is a moisture reading, the server stores the moisture reading into the database. The moisture puck starts out by getting the current time and the time to wake up and take a reading by sending a TimeInfo packet. Events that help debug are logged in the should_i_water.log file.
Send email
Every morning at 5AM, systemd fires happyday-email.timer. Happyday-email.timer starts up happyday-email.service.
Environment Variables
Before running send_mail_task.py, systemd loads the environment variables listed in environment_siw. environment_siw is not included in the build because it sets environment variables that contain sensitive information or path information specific to this build. environment_siw contains the following environment variables:
[email protected]
[email protected]
CLIENT_SECRET_FILE=/home/pi/somedir/client_secret.json
GMAIL_STORAGE=/home/pi/somedir/gmail.storage
DATABASE_FILE=/home/pi/somedir/databases/garden_readings.db
WEATHER_URL=https://api.darksky.net/forecast/<your secret key>/47.649093,-122.199153
WEATHER_PNG=/home/pi/somedir/weather_images/daily_weather.png
LOGFILE=/home/pi/somedir/should_i_water/should_i_water.log
The environment variables are then used within a python script. For example, OUTLOOK_USERNAME = os.environ.get("OUTLOOK_USERNAME")
Building the Email Message
The email message is formatted as a multipart MIME. One part is text. The text message is created by calling thebuild_message() function of the BuildMessage class. There text can be either summary (i.e.: the second parameter to ```build_message() is True) or detailed (the second parameter is False). The Home page of this wiki shows an example of a detailed message. The detailed message contains info about the specific moisture reading and what the threshold value to determine whether the soil should be watered. The threshold for a moisture puck can be set in the database. I use Sqlite commands to add threshold information for a node. The summary message does not include details of moisture puck readings. That suits my husband just fine.
Visualize Weather Forecast
I wanted to give us a clear (or perhaps partly cloudy...just kidding...) idea of what we should expect given dark sky's forecasting. The info includes hourly reports on:
- The chance of rain (from 0 to 100%)
- The amount of water that would accumulate if it rained (in inches per hour)
- The amount of cloud cover (from 0 to 100%)
- The temperature
This way, if the moisture reading said it's the soil needs watering, but there is a high probability of getting significant rain, we can choose to put off watering and let nature take its course. Knowing the temperature might also alter our gardening activities in our quest for the perfect tomato.
The script I used - plot_weather_lib.py gets the weather forecast using the dark sky. Then it uses matplotlib to create plots and the png file. The png file is saved for use when sending email.
Sending Email
I cover sending email here. I thought it would be simply a matter of understanding how to send SMTP. Um...no...which makes sense given authentication requirements. But - the good news - I eventually got it to work sending a mail to Outlook.com and gmail.com.