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

  1. Raspberry Zero WH
  2. WittyPi Mini
  3. Raspberry Pi Camera Module v2.1
  4. Huawei E3531 SurfStick
  5. SIM Card (ThingsMobile)
  6. DC DC Step Down Buck Converter
  7. DS18B20 Digital Thermometer
  8. OTG micro USB cable
  9. A micro USB cable end
  10. Window glas
  11. Sealed enclosure (G258)
  12. Cable fitting
  13. Srew Terminal Block 3-pin
  14. 4,7kOhm resistor
  15. Blank circuit board 65x30mm
  16. 40-pin staking-header
  17. 4 x M3x20 srews
  18. 8 xM3 screws
  19. Telephone cable
  20. 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.

  1. First, we need to solder the 40-pin staking-header to the the 60x35 mm blank circuit board.
  2. Put the screw terminal block and the resistor in place as shown in the picture
  3. Connect the resistor to the outer pins of the terminal block
  4. Solder the resistor and pins together
  5. Solder the cables (black, red and yellow) to the exact pins of the staking header and to the pins of the terminal block
  6. 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.
  7. 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.

  1. Solder the black wire form you speaker cable to the port labeled GND
  2. Solder the red wire to the port labeled IN+
  3. Solder the black wire from your micro-USB cable to the port labeled GND
  4. Solder the red wire from your micro-USB cable to the port labeled OUT+
  5. Cut of the circuit marked with the red arrow in the image
  6. 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:

  1. 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.
  2. Connect your surf stick to your computer via USB.
  3. A window should pop-up in your browser.
  4. Under Settings>Profile management change the APN to the one of your mobile provider. For Things Mobile it is "TM".
  5. Enable the roaming feature under Settings > Mobile Connection and 'Turn On' Mobile data.
  6. Click Apply.
  7. On the Home page you can check if a connection is established.
  8. Now you are good to go, disconnect your surf stick.
  9. 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.

  1. First create a free account on AWS: https://aws.amazon.com
  2. 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:

Raspberry Pi Downloads

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:

https://www.balena.io/etcher/

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.

  1. 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)
  2. Follow the setup steps of your Raspberry Pi [wifi, language, timezone]
  3. Enable SSH via Terminal on you PI:
    sudo raspi-config
    go to: 5 Interfacing Options
    Enable SSH
  4. 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 account

    3. 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

  1. Enable the camera in the interfacing options:
    sudo raspi-config
    go to: 5 Interfacing Options
    Enable Camera
    sudo reboot
  2. Create a new directory in your /home/pi directory
    cd /home/pi
    sudo mkdir Desktop
  3. Create a new python file in the Desktop directory
    cd Desktop
    sudo nano
    ctrl+o
    call the file qrbooftemp.py
    enter
    ctrl+x
  4. Install package pyboof (numpy and py4j are automatically installed) this can take up to 40 min.
    sudo pip3 install pyboof==0.33.1
  5. 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
  6. 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.

  1. enable 1-wire in the interfacing options
    sudo raspi-config
    go to: 5 Interfacing Options
    Enable 1-Wire
    sudo reboot
  2. 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
  3. activate 1-Wire
    sudo modprobe w1-gpio
    sudo modprobe w1-therm
    sudo reboot

  4. 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

  1. Download the Python script on your computer and open it in a texteditor
    https://github.com/gremax93/QR-Code-Water-Level
  2. Copy the complete script to your clip board (ctrl+a , ctrl+c)
  3. Go to your previously created python file
    cd /home/pi/Desktop
    sudo nano qrbooftemp.py
  4. Make a right click to insert the script
  5. Change the temperature sensor adresse to the one you previously wrote down
  6. Change the postgresql connection setup to your specific information, as indicated in the script
  7. Save and exit
    ctrl+o
    enter
    ctrl+x

Step 10: Installing the WittyPi Module

  1. Download the installation file from Witty Pi
    wget http://www.uugear.com/repo/WittyPi2/installWittyPi.sh
  2. Run the installation script
    sudo sh installWittyPi.sh
  3. 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.

  1. 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
  2. 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.

  1. 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
  2. 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

  1. Make sure your Pi is turned down, if not, shut it down
    sudo shutdown -h now
  2. Stack your WittyPi on your Raspberry Pi and the temperature module on top of it.
  3. Screw the 3 modules together
  4. Attach the camera module to the CSI port of the raspberry pi zero
  5. Attach your surfstick to your Raspberry Pi via the USB OTG cable
  6. Attach the powercable to the the WittyPi (not to the Raspberry Pi!)
  7. Put it all in the casing and screw the lid down
  8. Attach the power cable to your battery
  9. 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.