Project One Howest MCT: IoT Self Regulating Well

by korneelvanmullem in Circuits > Raspberry Pi

729 Views, 4 Favorites, 0 Comments

Project One Howest MCT: IoT Self Regulating Well

project one poster.png
IMG20230615142030.jpg

This is my self-regulating digital water well prototype for raspberry pi. It has a website to control the well and view historical data. Please do keep in mind that this is a prototype so it will be applied to a plastic box rather than an actual well. Feel free to use/modify any part of the components or code for your own needs. Feedback and suggestions are appreciated.

Supplies

IMG20230610103214.jpg

github:

  • https://github.com/howest-mct/2022-2023-projectone-Korneel-Van-Mullem

electronics:

  • raspberry pi (i used raspberry pi os)
  • sd-card for your raspberry pi
  • 2 * standard breadboard
  • wires
  • water level sensor
  • 2 * tip120 transistor
  • jsr04t waterproof ultrasonic distance sensor
  • 2 * yf-s201 water flow sensors
  • 2 * standard closed solenoid valves (note: the valves I used in this example don't let enough flow through to get a flow measurement at normal water pressures, please make sure your valves are big enough)
  • mcp3008
  • pcf8574
  • resistors
  • breadboard power supply
  • 9v adapter
  • 12v adapter
  • lcd display 2*16

materials:

  • pvc well lid 40*40cm
  • adapters for valves (see image)
  • for the two outer adapters, use adapters that fit with your tubing/hole size
  • rubber for the well-side outer adapter
  • building grade silicone, preferably seethrough
  • plastic box 39*39*40 (for well prototype)
  • plastic box 20*40
  • electrical tape
  • teflon tape
  • three small bolts and nuts
  • a distribution plug with at least 3 sockets

tools used:

  • kitchen knife with serrated edge
  • drill
  • one regular small plastic drill head
  • 25mm drill head for plastic (or other size if you use a differently size well-side adapter)
  • 18 mm drill head for plastic

api keys:

  • free openweather api subscription

Getting Ready

Make sure you have all the needed materials.

Start by getting an openweather api key if you haven't already, the free subscription should more than suffice for this project. It may take a while to get activated so be sure to do this first. https://openweathermap.org/api

If your pi doesn't have wireless internet yet, you can configure it for easy access by executing the following commands. You can plug a standard ethernet cable (UTP) into your pi to access it or connect a monitor using one of the HDMI ports. If you're using a network cable, you can use putty https://www.putty.org/ or any other SSH service of your choice. You will be needing root access to your pi for this to work.

First:

wpa_passphrase <your_wifiname> <your_wifi-password> >> /etc/wpa_supplicant/wpa_supplicant.conf

Second:

wpa_cli -i wlan0 reconfigure

Update your packages to ensure compatibility

First:

sudo apt update

Second:

sudo apt upgrade

Configure Code

Next, we will download the code from github into a directory of our choice.

Start by installing git if you haven't already.

sudo apt install git

Change to a directory of your choice, in this tutorial I will use /home/pi.

cd /home/pi

Clone the code from github.

git clone <insert_url_here>

Create a virtual environment, note: you must still be in your directory when doing this.

python -m venv <venv_name>

Now we will install all the necessary packages

pip install -r requirements.txt

Next up, we will configure the settings for our well. Start by copying the .env_example file to a new file named .env.

cp .env_example .env

Then, use any text editor of your choice (use nano like me if you're not familiar with any) to open the .env file.

nano .env

Change the values to fit with your well, do not forget to insert your openweather api key here. Ctrl+x, y, enter to save and exit if you're using nano.

Configure Database

Now we will configure our database.

The database dump file should already be present in the github repo we downloaded previously.

Now we can start installing MariaDb.

sudo apt install mariadb-server mariadb-client -y

Then install security.

mysql_secure_installation

Press enter at the first prompt, since the root password is just empty for now.

At the second prompt, pick a secure password of your choice.

Press y at the next four prompts.

Now we will be configuring a user on our database.

First:

mysql -u root -p

Second:

grant all on *.* to '<username>'@'localhost' identified by '<password>'

Third:

grant grant option on *.* to '<username>'@'localhost'

Fourth:

flush privileges

Now we can install our database on our pi, change the path to your directory if you put the code elsewhere.

source /home/pi/2022-2023-projectone-Korneel-Van-Mullem/well-grounded.sql

We can now exit our database by using the exit command.

exit

Now we will connect our code to our database, we again start by copying an example file.

cp config_example.py config.py

And again edit the file to have the correct info, change the user and password to those that you configured on your database.

nano config.py

Configure Code to Run on Startup

We want our code to run whenever our pi is plugged in. So that's what we will be doing next.

Start by creating a service file that we will run on startup. I will again be using nano for this tutorial.

nano wellgrounded.service

and fill it with the following text, make sure to enter the name of the directory and virtual environment with all our code where needed, you may need to change the directory paths if you decided to put the code in a different place.

Don't forget to enter your pi user as well.

[Unit]
Description=ProjectOne Project WellGrounded Service
After=network.target
[Service]
ExecStart=/home/pi/<directory_name>/<venv_name>/bin/python -u /home/pi/<directory_name>/backend/app.py
WorkingDirectory=/home/pi/<directory_name>/backend
StandardOutput=inherit
StandardError=inherit
Restart=always
User=<user>
[Install]
WantedBy=multi-user.target

We will now copy this file to the right directory.

sudo cp wellgrounded.service /etc/systemd/system/wellgrounded.service

And enable the service.

sudo systemctl enable wellgrounded.service

You can check if the service is running correctly if you want to

sudo service wellgrounded status

Configure Apache

Install apache2 onto your pi if you haven't already.

sudo apt install apache2

We will now edit the apache2 configuration

nano /etc/apache2/apache2.conf

Navigate down until you encounter this part

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Require all denied
</Directory>

And change it to look like below. Save again with ctrl+x, y, enter

<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

Now restart apache2

service apache2 restart

If you want to, you can check if you did it correctly with the following command

service apache2 status

The Well Lid

IMG20230615090612.jpg
IMG20230615090617.jpg
IMG20230615090622.jpg
IMG20230615090601.jpg
IMG20230615090555.jpg
IMG20230615090607.jpg

We will now start the process of physically assembling our well.

Start by putting smaller the plastic box in the center of the well lid. Drill at least three holes through the box and lid. Put the bolts inside of these and screw the nuts on on the other side to secure the box to the lid.

Use the 18mm drill head to drill a hole as much in the center of the lid as possible, this is where we will later attach our distance sensor.

Drill a small hole into the lid from somewhere around the center. Then use the knife to widen and lengthen it enough to make watersensor fit tightly, you can always secure it more with silicone later but try to avoid it as much as possible.


Cut a small sleeve in the upper left corner of the box, it should be just large enough to fit the power cable of your distribution plug. Drill a small hole in the lower half of the opposite side with your 7.5 mm drill. We will use this to fit the cables of our valves and flow measurement devices.

The Well Itself

IMG20230610103214.jpg
IMG20230610103436.jpg
IMG20230615091206.jpg
IMG20230615091210.jpg
IMG20230615091213.jpg
IMG20230615091201.jpg

Keep in mind that this step will be different depending on what kind of well or well-standin you are using.

Start by assembling all the adapter parts together as shown in the images above except the big bolt. Wrap the parts with teflon tape in the opposite direction of the thread to ensure a watertight seal.

Drill a hole into your well container using the 25mm drill head. Push the adapter through, then put a rubber over it, wrap it in teflon tape and screw the bolt on as hard as you can to make sure no water can leak out.

Electronics

model_elektrischschema-3.jpg
model_breadboard-3.jpg

Next up we will assemble our electronics as seen in the images above. I have attached the pdf files if you want to take a closer look.

Assemble your electronics inside the box, fit the cables through the hole we made earlier if necessary.

The water sensor needs to get inserted into the sleeve we made for it earlier, the ultrasonic distance sensor needs to have it's cable pulled through the hole and get pushed into it so it fits tightly.

Verify that all your parts have been assembled correctly before plugging the power in. Make sure that you're ready to pull the power the moment you hear strange noises, smell smoke or anything similar.

If this happens, dissassemble and start over.

Use

image.png

If you did everything correctly, you should now see the pi's ip-address on your local network pop up on the lcd screen.

Go to your browser and enter it, this should bring you to the main screen. Enjoy!


tip: You can set the location in the control panel so you can see live weather updates on the main page.