Pool pH Level and Temperature Wifi Monitor

June 25, 2018
5723
Views

Part 2 – Software Setup and Programming

Firstly I began with setting up the temperature sensor, and pulling data to make sure everything was working fine. To this I used the this code to initiate a read from the 1 Wire interface that the DS18B20 was connected to. This temperature is a simple 3 wire sensor that requires power and a single signal wire to send data. In order for this to work, you need to make sure that the PI has 1 Wire enabled in the interfacing settings. Use sudo raspi-config to access the PI interface settings menu.

#!/usr/bin/python

import sys
import glob

sensors = glob.glob("/sys/bus/w1/devices/(device serial)/w1_slave")

for sensor in sensors:
  tfile = open(sensor)
  text = tfile.read()
  tfile.close()
  secondline = text.split("\n")[1]
  temperaturedata = secondline.split(" ")[9]
  temperature = float(temperaturedata[2:])/1000
  print "sensor", sensor, "=", temperature, " degrees."

Once I was able to detect the temp sensor, I updated the python script with the serial number of the 1 Wire device found. 1 Wire devices are automatically detected by the PI when they are connected to the bus. To find the serial number of 1 Wire devices

cd /sys/bus/w1/devices
ls

Executing the above script gave a read out of the current temperature being read by the DS18B20 in real time

sudo python temp.py

Temperature 27 DegreesC

Now with the temperature sensor up and running, its time to get the pH probe reading data! To do this I used a pre-build code written in python for the PI on the DFRobot GitHub page. I added the script and made a few changes. I ran into a lot of problems with the script not using “Relative” file paths, So I went through and made all file paths in the script relative. At the start of each line I changed

sys.path.append('../')

To

sys.path.append('/var/scripts/ph/')

This path is the FULL path to the root directory of the script. This is the directory containing the ADS1115 python files. This way if the script if executed from any other location, it knows where its root path is and where to call the ADS1115 dependent files. I found if this was not done and I tried to execute the script from any other directory other then the root directory, I would receive an error. One this was done I was able to execute the script and get a pH reading in realtime.

sudo python DFRobot_PH_Read.py

Voltage:1.94    pH value: 6.78

I now have the temperature sensor and pH probes up and running! I will now pass the data to thingsboard. Using the following python script, I am able to pass the requested data to thingsboard for graphing and logging.

#!/usr/bin/python
import sys
sys.path.append('/scripts/ph')
import time

device = "28-00000a69b724"

transport = "http"
host = "127.0.0.1:8080"
apiKey = "mwBqmIQhuVUqaUmOx99j"

import json
import os

url= transport+"://"+host+"/api/v1/"+apiKey+"/telemetry"

ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16

from DFRobot_ADS1115 import ADS1115
from DFRobot_PH import DFRobot_PH

ads1115 = ADS1115()
ph = DFRobot_PH()

ph.begin()

tsFile = open("/sys/devices/w1_bus_master1/"+device+"/w1_slave","r")
data=tsFile.readlines()
tsFile.close()

words = data[1].split("=")
temperature = float(words[1].strip())/1000.

print "Temperature = ",temperature


ads1115.setAddr_ADS1115(0x48)

ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)

adc0 = ads1115.readVoltage(0)

PH = ph.readPH(adc0['r'],temperature)
print "PH = ",PH
time.sleep(1.0)


measurement = {}
measurement["temperature"] = "{:f}".format(temperature)
measurement["ph"] = "{:f}".format(PH)
json_data = json.dumps(measurement)

url=transport+"://"+host+"/api/v1/"+apiKey+"/telemetry"
uploadCmd="curl -d \""+json_data+"\" "+url+" --header \"Content-Type:application/json\""

uploadResult = os.system(uploadCmd)

if uploadResult == 0:
 print  "Upload completed successfully"
else:
 print "Upload failed - check cUrl output"

This script will run once when executed and not loop meaning the script will not update in realtime. I chose to do this as I didn’t want the data constantly updating. I figured a temperature and pH reading every hour was more then enough considering the temperature and pH levels will change very little in 1 hourly intervals. In this script, we need to update the API key which is created when you create the device in thingsboard, also you will need to specify the thingsboard server which in out case, is localhost (127.0.0.1) on port 8080 (127.0.0.1:8080). If we were running a server remotely for use with the thingsboard IoT platform and had a remote server, we would specify and host name or IP address here as well as port, by default 8080. We also need to update the 1 Wire device serial number for the DS18B20 to be read by the script.

device = "28-00000a69b724"

transport = "http"
host = "127.0.0.1:8080"
apiKey = "mwBqmIQhuVUqaUmOx99j"

Finally to execute the script, I created a CRON job to run the script every hour. Every time the the CRON job ran, it executed the python code above to run the script, calling for the sensor data and sending the result to thingsboard.

*/60 * * * * cd /scripts/ph/example && /usr/bin/python update.py

You can also output a log file for debugging

*/60 * * * * cd /scripts/ph/example && /usr/bin/python update.py >> /scripts/logs/cronlog.txt

In the above code, I navigate into the root directory of my script before executing. I was again getting errors when trying to execute the script with a file path and this work around worked for me. You will also notice I use a relative file path to execute python. To check the CRON job has run successfully

grep CRON /var/log/syslog

Feb 19 15:16:01 raspberrypi CRON[22267]: (pi) CMD (/usr/bin/python /scripts/ph/e                                            xample/update.py)

Now, we are all ready to test and send data to thingsboard and check the graphed data.

1 2 3

All Comments

  • Very useful and great idea. Easy to follow too. Thanks for the post!

    Edward G November 19, 2020 2:01 pm

Leave a Reply