DIY Raspberry Pi: Temp / Humidity / Soil Moisture / Camera Setup

Joined
Sep 9, 2019
Messages
277
Reputation
0
Reaction score
862
Points
0
Raspberry Pi powered temp/humidity/moisture monitoring system:

TLDR: setting up a Raspberry Pi to monitor temperature, humidity, soil moisture and run a camera.


upload_2019-9-15_21-38-58.png


upload_2019-9-15_21-39-54.png


So currently running Raspbian GNU/Linux 10 (buster) on a Pi Model 3B with 1GB memory although it could probably run off Pi Zero WH ($16.58 USD). Any would do, as long as it has GPIO to connect the sensors and in my case, the camera connector.

Hooked upto this is a camera, a temp/humidity sensor and a capacitive moisture/temp sensor (most available from PiHut). I think it's not polite to link in forum so I've included model no's etc for your googling pleasure :D

Humidity/Temp:

393-00_1024x1024.jpg

AM2302 (wired DHT22) temperature-humidity sensor ADA393 [AM2302] - $17.85 USD
Connectors:
Adafruit Premium Female/Female Jumper Wires - 20 x 12" (300mm) [ADA1949] - $5.10 USD

Soil sensor:

4026-01_05c419bd-30ea-46bc-82e9-b1d62bcaafa3_1024x1024.jpg

Adafruit STEMMA Soil Sensor - I2C Capacitive Moisture Sensor [ADA4026] - $9.56 USD
Connectors:
Adafruit JST PH 4-Pin to Female Socket Cable - I2C STEMMA Cable - 200mm [ADA3950] - $1.91 USD
Adafruit Premium Female/Male 'Extension' Jumper Wires - 20 x 12" [ADA1952] - $5.10 USD

Camera:

index.jpeg

SainSmart Wide Angle Fish-Eye Camera Lenses for Raspberry Pi Arduino (approx $27 USD from amazon)
  • Specs
    • 5 MP Omnivision 5647 Camera Module
    • Still Picture Resolution: 2592 x 1944
    • Viewing angle: 160 degrees
    • Video: Supports 1080 p @ 30 fps, 720 p @ 60 fps and 640 x480 p 60/90 Recording 15-pin MIPI Camera Serial Interface
  • Mounting
    • Adafruit Raspberry Pi Camera Board Case with 1/4" Tripod Mount [ADA3253] - $3.82 USD
    • Handlebar For GoPro Hero Camera Seatpost Clamp Roll Bar Mount Adapter (approx $6 USD from ebay)
  • Connectors
    • Dorhea 6.56ft (2m) Raspberry Pi 3 B+ Camera Cable FFC FPC Ribbon 15 pin Extension Cord Flat Wire Cable Connection for Raspberry Pi 2/3 B B+ Camera Module (approx $6 USD)
Camera.jpg cam_light.jpg

Wiring:

Pi Wiring.jpg

Hooking it all up:

I'll start with the two sensors.

Quick version:
Use python script to obtain readings every (x) seconds and send/save readings somewhere.
Run python script as a service so it runs on boot ..24/7

What you do with the readings can obviously vary a lot. Save them to a text file, a database etc on the Pi SD card or send them somewhere else over wifi/network.

Personally I prefer not relying on the SD card as they can be prone to failure and I wanted to be able to monitor them using apps I felt were a bit too demanding for running on the Pi (you could run them on the Pi though).
I instead opted to send the readings over http to InfluxDB which runs on a small linux NAS box at home. This means the Pi just has to get the readings and make an http request with the values which I can then view as my fancy looking dashboard.

There are quite a few good tutorials and variations on setting this up if you search for "am2302 raspberry pi influxdb"

The script I run for both sensors.

There are a few things to install which the script will need. To install them, run the following from the Pi's terminal. [Disclaimer: following is not complete at present]

Code:
cd /home/Pi
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl git
git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT
sudo python setup.py install

NB: when installing, you will be asked to confirm Y/N.

So first, create a new python script (called mine sensor.py) and saved it in a new folder (tent) under the default Pi user's home dir (/home/Pi/tent)

Code:
[/SIZE]
#!/usr/bin/python3
import time
import sys
import datetime
import Adafruit_DHT
import requests
import logging
from board import SCL, SDA
import busio
from adafruit_seesaw.seesaw import Seesaw

# Configure InfluxDB connection variables
interval = 12 # Sample period in seconds
host = "" # InfluxDb
port = 9999 # api ingestion port
org = ""
bucket = ""
url = f'http://{host}:{port}/api/v2/write?org={org}&bucket={bucket}&precision=s'
token = "" # user Pi
headers = { 'Authorization': f'Token {token}' }

# Enter the sensor details
sensor = Adafruit_DHT.DHT22
sensor_gpio = 4

i2c_bus = busio.I2C(SCL, SDA)
ss = Seesaw(i2c_bus, addr=0x36)

# think of measurement as a SQL table, it's not...but...
measurement = "rpi-dht22"
# location will be used as a grouping tag later
location = "tent"

prevTemp = -9999
prevHum = -9999

def get_change(current, previous):
    if previous is None:
        return 0
    if current == previous:
        return 0
    try:
        return (abs(current - previous) / previous) * 100.0
    except ZeroDivisionError:
        return None

# Run until you get a ctrl^c
try:
    while True:
        
        try:
            
            # Read the sensor using the configured driver and gpio
            humidity, temperature = Adafruit_DHT.read_retry(sensor, sensor_gpio)
            moisture = ss.moisture_read()
            soilTemp = ss.get_temp()
            iso = int(time.time())
            
            # odd reading is wild, if change is greater than 20% from last value, we skip
            if (humidity is not None) and ((prevTemp == -9999 or prevHum == -9999) or (humidity < 100 and get_change(temperature, prevTemp) < 20 and get_change(humidity, prevHum) < 20)) :
                # Print for debugging, uncomment the below line
                # print("[%s] Temp: %s, Humidity: %s" % (iso, temperature, humidity))
                prevTemp = temperature
                prevHum = humidity
                
                # send the data
                dataT = f'{measurement},location={location} temperature={temperature},humidity={humidity},soilTemp={soilTemp},soilMoisture={moisture} {iso}'
                response = requests.post(url, headers=headers, data=dataT, timeout=5.00)
                
        except Exception:
            logging.exception('Failed to process DHT reading')
            pass
        
        # Wait until it's time to query again...
        time.sleep(interval)

except KeyboardInterrupt:
    pass


[SIZE=4]

Once you have saved your script. The next step is to run it as a service.

Code:
cd /lib/systemd/system/
sudo nano tent.service

Then paste the following into the file.

Code:
[Unit]
Description=Tent monitoring
After=network-online.target
[Service]
ExecStart=/home/pi/tent/sensor.py
User=pi
[Install]
WantedBy=multi-user.target

You can save and exit the nano editor using [CTRL-X], [Y] then [ENTER]

Setup the correct permissons

Code:
sudo chmod 644 /lib/systemd/system/tent.service
sudo systemctl daemon-reload
sudo systemctl enable tent.service
sudo reboot

[More to come on programming/setup, still writing it up! ;)]
 
Last edited:
Wow man thanks for all your work on this! Actually have an extra Raspberry Pi 3 laying around might half to give this a go :biggrin:.
 
Very cool!

I have a Pi 3b with a camera which is going mostly unused, I might have to look into those additional moisture and temp/humidity sensors to get a monitoring system set up like this.

Just another project to add to the list :biggrin:
 
This is a cool project. I'm trying to work out a low cost automated sub irrigation system that runs a flood pump and a drain valve. Im going to use wired moisture sensors in 30% of the crop that can hopefully log it while an IR thermometer+ambient temp+humidity sensor calculates vpd and logs it all for precision irrigation rather than timing it.

Even better would be if I could control fans+speed, humidifiers, heaters, etc and get 30 second logs

Your camera's are a great idea and I would love to mount 4 to my led frame and splice the images together. Could I possibly control dimming on a meanwell driver and light schedules as well?

I'll be watching this is so cool
 
Nice work, I have the Grove plant Kit. Which I got to going for about a minute before the pump motor fell apart. Much better to do it yourself, but my soldering skills are pants.

Grove Plant Kit
 
Back
Top