Introduction
This is the last blog entry for my Experimenting with Extreme Environment challenge entry. In the first one, the project was presented together with a brief explanation of the main chemical reaction taking place in my swimming pool. It was also described how the kit will be exposed to an extreme environment and why making this project is interesting for me.
In the second one, an unboxing of the kit was presented, along with a description of each component and a summary of their datasheet.
I wish I had time to complete all the extra credit blogs, but it has been impossible. I will at least present this final blog that summarizes the project.
Project goal
Even if the project was described in the first blog entry, I will list the milestones:
- Measuring water temperature with a DS18B20 sensor.
- Drawing plots of the evolution of the water temperature with time.
- Creating a site to see the plots.
- Showing the water temperature on the LCD screen.
Software development
For the first part of the project, software was developed.
The first step was using a 32GB microSD card to burn a copy of the Raspberry Pi OS.
This task was performed with the official tool provided by the Raspberry Pi Foundation: Raspberry Pi Imager. The use of this tool is too convenient, as it allows us to choose among different OS versions. For this project I selected the 64-bit Raspberry Pi OS Lite version. This lightweight version doesn't include a desktop, but it is not needed as the prototype will be used in headless mode.
Moreover, with the Imager, we can also select different advances options like:
- Hostname: for this project, it has been defined as poolsensor.
- Enable SSH for headless operation.
- Username and password.
- Wireless LAN configuration.
- Locale settings.
An example of the configuration is shown in Figure 1.
Figure 1. Raspberry Pi Imager
After burning the OS into the microSD card, the Raspberry Pi CM 4 can be easily accesed using SSH from any device.
The second part involved writing the Python code to measure temperature with the sensor, log it, draw a plot and display the temperature on the LCD. Most of the code is based on previous work by myself on a past project called DashboardPi. The bits needed to display the temperature on the LCD are based on the great work by javagoza with the lcdrw1063 library. However, I had some issues with this part of the code and with the wiring of the LCD, so I decided not to use it, so I could be in time to post the blog.
The code is presented below:
!/usr/bin/env python #-*-coding:utf-8-*- #This program creates graphs with data obtained by DS1820b. Temperature is also presented in a MIDAS LCD Display. #Created by Iker García. import csv from ds18b20 import DS18B20 import lcdrw1063 as LCD import matplotlib matplotlib.use("Agg") #Added to plot graphs without running X server. import matplotlib.pyplot as plt import numpy as np import time from w1thermsensor import W1ThermSensor while True: try: sensor = W1ThermSensor() temp = sensor.get_temperature() file = open("/var/www/dashboard/pooltemperature.csv", "a") #Opens file to save data. writer = csv.writer(file, delimiter = ",") readfile = open("/var/www/dashboard/pooltemperature.csv", "r") #Opens file to count rows. reader = csv.reader(readfile, delimiter = ",") log = list(reader) rows = len(log) measure = 60*rows #Data is going to be added each 60 minutes, we want to reflect this in the data file. data = [measure,temp] data = [int(i) for i in data] #Transforms data list in int, in order to be read by Matplotlib. writer.writerow(data) #Writes data on the csv file. # display = LCD.Lcd() #LCD driver # templcd = str(temp) #Transforms temperature into a string # display.lcd_clear() #Clears the LCD # display.lcd_display_string(" Pool temperature sensor", 1) # display.lcd_display_string(" Last reading", 2) # display.lcd_display_string(templcd ,"C", 3) if rows >= 1: #Graph can't be plotted with only one data point, so for the first data point (rows = 0), matplotlib is not executed. x,y=np.loadtxt("/var/www/dashboard/pooltemperature.csv", unpack = True, delimiter = ",") #Opens first data set. plt.plot(x,y, label = u"Temperature (\u00B0C)") #Plots first data set. plt.legend(loc = "best") #Plots legend at the best location. plt.xlabel("Time(min)") #x axis label. plt.savefig("/var/www/dashboard/html/images/lines.png") #Saves created plot. plt.clf() #Clears figure, in order to create a tidy plot. time.sleep(3600) #Code is executed each hour. else: time.sleep(3600) except KeyboardInterrupt: #Exits program. break
Due to the issues I had with the LCD, those lines are commented out to prevent their execution.
Hardware development
As the hardware only involves the Raspberry Pi CM 4, its IO Board and a temperature sensor, the wiring is quite straightforward. I used a breadboard to connect the three wires of the sensor (Vcc, GND and data bus) to the Raspberry Pi. A 4k7 Ohm pull-up resistor is needed between Vcc and data bus.
In order to power the prototype, the wire from a DC plug I already owned were soldered to the provided IP67 connectors. The soldered wires are presented in Figure 2 and Figure 3.
Figure 2. DC plug work (1 of 2)
Figure 3. DC plug work (2 of 2)
During hardware development, I faced another issue apart from the one previously explained with the LCD: using the temperature sensor while maintaining the IP68 rating of the enclosure. I decided to drill a hole as tight as possible and place only the sensor, instead of all the prototype, inside the water. The hole for the sensor is depicted in Figure 4.
Figure 4. The hole for the temperature sensor
Website development
The first step for creating the website was installing all the required packages, as follows:
sudo apt-get update sudo apt-get install apache2 -y sudo apt-get install php libapache2-mod-php -y sudo apt-get install mariadb-server sudo mysql_secure_installation sudo apt install php-mysql sudo service apache2 restart
After this, going to http://localhost would present the Apache2 Debian Default Page.
In order to display the website with our data, which we will call the dashboard, we need to configure a Virtual Host. This is done as follows:
sudo mkdir -p /var/www/dashboard/html sudo chown -R $USER:$USER /var/www/dashboard/html sudo chmod -R 755 /var/www nano /var/www/dashboard/html/index.html
Now we modify the html file to display whatever we want:
<html> <head> <title>Poolsensor</title> </head> <body> <h1>This is a dashboard example for the Poolsensor project</h1> </body> <img src="lines.png" /> <body> <h2>Thank you element14, AVNET and Hammond Manufacturing for this opportunity</h2> </body> </html>
This html file can be further customized. Then we continue enabling the Virtual Host:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dashboard.com.conf sudo nano /etc/apache2/sites-available/dashboard.com.conf
We configure the Virtual Host:
<VirtualHost *:80> ServerAdmin "write any email you want" ServerName dashboard.com ServerAlias www.dashboard.com DocumentRoot /var/www/dashboard/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Finally we enable the Virtual Host, disable the default page and restart Apache2:
sudo a2ensite dashboard.com.conf sudo a2dissite 000-default.conf sudo systemctl restart apache2
Field tests
As it has been previously explained, only the sensor was placed inside the water. This can be seen in Figure 5.
Figure 5. Prototype during the field test
When it was tested, suddenly there was a storm. The enclosure protected the electronics without any problem, as seen in Figure 6.
Figure 6. Prototype vs storm
After running the code, we can go to http://localhost and see our dashboard with the data obtained by the Poolsensor, as presented in Figure 7:
Figure 7. Data obtained by the dashboard
Future work
These are only the first steps of a project that could be really useful:
- Measure other parameters, like pH and ORP.
- Present the data on the included LCD.
- Control all the electronics of the pool automatically.
Acknowledgments
Thank you element14 and Hammond Manufacturing for this opportunity.
John D. Hunter was the creator of the amazing Matplotlib, so this project is in memory of him.