Introduction: Automated River Water Monitoring System
This instrucatbale is used to document the development of an automated river water monitoring system. The monitored parameters are the water level and water temperature. The goal of this project was to develop a low-cost and independent logger that is connected to a database. The water level is monitored with a special QR-level. A camera in the Pi is taking a picture every 15 min. The QR-codes in this picture get decoded by software and are indicative to the actual water level. Water temperature is measured with a DS18B20 sensor.
Supplies
- Raspberry Zero WH
- WittyPi Mini
- Raspberry Pi Camera Module v2.1
- Huawei E3531 SurfStick
- SIM Card (ThingsMobile)
- DC DC Step Down Buck Converter
- DS18B20 Digital Thermometer
- OTG micro USB cable
- A micro USB cable end
- Window glas
- Sealed enclosure (G258)
- Cable fitting
- Srew Terminal Block 3-pin
- 4,7kOhm resistor
- Blank circuit board 65x30mm
- 40-pin staking-header
- 4 x M3x20 srews
- 8 xM3 screws
- Telephone cable
- Speaker cable
Step 1: Soldering the Temperature Module
In this step we create a temperature module from scratch. It has the size of a pHAT and can be connected to the 40-pin GPIO of the Pi. This method allows us to securely mount the DS18B20 sensor to the Raspberry Pi.
- First, we need to solder the 40-pin staking-header to the the 60x35 mm blank circuit board.
- Put the screw terminal block and the resistor in place as shown in the picture
- Connect the resistor to the outer pins of the terminal block
- Solder the resistor and pins together
- Solder the cables (black, red and yellow) to the exact pins of the staking header and to the pins of the terminal block
- To drill the mounting holes, first mount the module to your raspberry pi zero, then drill with a 3mm drill bit through the existing holes from your Raspberry Pi into your new module.
- Finally, you can connect your DS18B20 sensor to your module by attaching the the cables of your sensor to the corresponding colours on your board.
Step 2: Building Our DCDC Converter Cable
To power our Raspberry Pi we need to convert the 12V we get from our battery to 5V. We use a DC DC buck converter to reduce the voltage.
- Solder the black wire form you speaker cable to the port labeled GND
- Solder the red wire to the port labeled IN+
- Solder the black wire from your micro-USB cable to the port labeled GND
- Solder the red wire from your micro-USB cable to the port labeled OUT+
- Cut of the circuit marked with the red arrow in the image
- Weld the bonding pads togehter to get a 5V fixed output (blue arrow)
Step 3: Installing Your Surfstick
To establish an internet connection, we need to connect our surf stick to our Raspberry Pi. First we need to make some configurations:
- Insert a SIM card from the moblie provider of your choice, into your surf stick. In this example we use a SIM from Things Mobile.
- Connect your surf stick to your computer via USB.
- A window should pop-up in your browser.
- Under Settings>Profile management change the APN to the one of your mobile provider. For Things Mobile it is "TM".
- Enable the roaming feature under Settings > Mobile Connection and 'Turn On' Mobile data.
- Click Apply.
- On the Home page you can check if a connection is established.
- Now you are good to go, disconnect your surf stick.
- The surf stick can be connected to your Raspberry without any further configurations.
Step 4: Create a PostgreSQL Database
In this step we set up our database. We make use of the free tier offer from Amazon Web Services.
- First create a free account on AWS: https://aws.amazon.com
- Follow this tutorial to create your PostgreSQL database and learn how to connect to it: https://aws.amazon.com/getting-started/tutorials/create-connect-postgresql-db/?nc1=h_ls
Step 5: Preparing the SD Card
First you need to download the Raspian Stretch Lite image:
Now we need to flash the image on an empty SD-card (at least 16 GB). Insert the SD-card into your SD-card reader. Download Balena Etcher and install it on your computer:
Open Balena Etcher, select the .zip file of your Raspian image you previously downloaded. Select your SD-card in Select Drive. Click Flash!
Step 6: Installing the Raspberry and Connecting to It Via SSH
Before we can use our Raspberry Pi we need to make some initial setups.
- Insert your SD-card into your Raspberry Pi and connect it to a monitor, keyboard and mouse.
(If you don't have an extra monitor, keyboard and mouse you can make a headless installation. Look it up it has been done befor) - Follow the setup steps of your Raspberry Pi [wifi, language, timezone]
- Enable SSH via Terminal on you PI:
sudo raspi-config
go to: 5 Interfacing Options
Enable SSH - On your computer download Termius. This program makes it easy to connect to our Raspberry Pi via SSH.
1. https://termius.com/
2. Make an account3. Create NEW HOST
4. Give your Raspberry a label (choose a Name)
5. Enter your PI's IP-ADRESSE from the NETWORK it is logged in (if you don't know the IP-ADRESSE you can look for it with a program called "ADVANCED IP SCANNER")
7. Enter your username (pi by default if not changed)
8. Enter your password (raspberry by default if not changed)
9. Click save
10. Double click on your new created host
11. A message pops up -> click yes
Your Raspberry should be conected to your computer via SSH
Step 7: Installing the Logger
- Enable the camera in the interfacing options:
sudo raspi-config
go to: 5 Interfacing Options
Enable Camera
sudo reboot - Create a new directory in your /home/pi directory
cd /home/pi
sudo mkdir Desktop - Create a new python file in the Desktop directory
cd Desktop
sudo nano
ctrl+o
call the file qrbooftemp.py
enter
ctrl+x - Install package pyboof (numpy and py4j are automatically installed) this can take up to 40 min.
sudo pip3 install pyboof==0.33.1 - Run an update (if you dont do this you might get problems while installing packages later on)
sudo apt-get update
sudo apt-get install libpq-dev - Install postgreSQL and psycopg2 to interact with the postgreSQL database via Python
sudo apt-get install postgresql
sudo pip3 install psycopg2
Step 8: Installing the Logger (Temperature Module)
To install the Temperature Module you need to mount the Module on your Raspberry Pi with a DS18B20 sensor attached to it. The module gets mounted via the 40-pin header.
- enable 1-wire in the interfacing options
sudo raspi-config
go to: 5 Interfacing Options
Enable 1-Wire
sudo reboot - set 1-Wire pin to pin 23 and gpu_mem=256
sudo nano /boot/config.txt append dtoverlay=w1-gpio, gpiopin=23, pullup=on to the end of the file
change gpu_mem=128 to gpu_mem=256 - activate 1-Wire
sudo modprobe w1-gpio
sudo modprobe w1-therm
sudo reboot - find out your adress of your DS18B20, it should start with 28- ...
cd /sys/bus/w1/devices
ls
write the adress down, it's later needed in the Python script
Step 9: Download and Adjust Pyhton Script
- Download the Python script on your computer and open it in a texteditor
https://github.com/gremax93/QR-Code-Water-Level - Copy the complete script to your clip board (ctrl+a , ctrl+c)
- Go to your previously created python file
cd /home/pi/Desktop
sudo nano qrbooftemp.py - Make a right click to insert the script
- Change the temperature sensor adresse to the one you previously wrote down
- Change the postgresql connection setup to your specific information, as indicated in the script
- Save and exit
ctrl+o
enter
ctrl+x
Step 10: Installing the WittyPi Module
- Download the installation file from Witty Pi
wget http://www.uugear.com/repo/WittyPi2/installWittyPi.sh - Run the installation script
sudo sh installWittyPi.sh - Reboot
sudo reboot
Step 11: Make Some Changes in the Java Virtual Machine Setup
This step is necessary to make sure that the JVM will start when the python script is executed.
- Go to the py4j directory and open jawa_gateway.py
cd /usr/local/lib/python3.5/dist-packages/py4j
sudo nano jawa_gateway.py
set DEFAULT_CALLBACK_SERVER_ACCEPT_TIMEOUT = 20 - Go to the pyboof directory and open __init__.py
cd /usr/local/lib/python3.5/dist-packages/pyboof
sudo nano __init__.py
set while time.time() - start_time < 20.0
Step 12: Set Cron Job
In this step we set a cronjob when the Raspberry boots. This way every time the Pi wakes up, our python script gets executed.
- Set cronjob and write it to a log file. This makes troubleshooting more efficent.
crontab -e
@reboot sleep 20 && sudo python3 /home/pi/Desktop/qrbooftemp.py >> /home/pi/Desktop/log.txt - Append an other line to crontab. This line makes sure, that when WittyPi failes the Raspberry Pi reboots anyway.
@reboot sleep 1500 && sudo reboot
ctrl+o
enter
ctrl+x
Step 13: Putting It All Together
- Make sure your Pi is turned down, if not, shut it down
sudo shutdown -h now - Stack your WittyPi on your Raspberry Pi and the temperature module on top of it.
- Screw the 3 modules together
- Attach the camera module to the CSI port of the raspberry pi zero
- Attach your surfstick to your Raspberry Pi via the USB OTG cable
- Attach the powercable to the the WittyPi (not to the Raspberry Pi!)
- Put it all in the casing and screw the lid down
- Attach the power cable to your battery
- Congratulations your logger should be running!
Step 14: Final Installation
These pictures show the mounted logger with the QR-code water level installed.
The loggers were installed under a bridge for optimal results. Direct sunlight should be avoided.