2022-08-20 13:40:10 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import board
|
|
|
|
import time
|
|
|
|
import busio
|
|
|
|
import adafruit_ads1x15.ads1015 as ADS
|
|
|
|
from adafruit_ads1x15.analog_in import AnalogIn
|
|
|
|
from time import sleep, strftime
|
2022-09-04 08:59:20 +00:00
|
|
|
from rpi_lcd import LCD
|
|
|
|
|
|
|
|
try:
|
|
|
|
lcd = LCD(bus=2)
|
|
|
|
except OSError:
|
|
|
|
lcd = None
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
# Create the I2C bus
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
|
|
|
|
# Create the ADC object using the I2C bus
|
|
|
|
ads = ADS.ADS1015(i2c)
|
|
|
|
|
|
|
|
# Create single-ended input on channel 0
|
2022-09-03 15:17:20 +00:00
|
|
|
# tmp36 = AnalogIn(ads, ADS.P0)
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
# Attempting to create a single-ended input on channel 1
|
|
|
|
depthS = AnalogIn(ads, ADS.P1)
|
|
|
|
|
|
|
|
# Subtract the offset from the sensor voltage
|
|
|
|
# and convert chan.voltage * (1 degree C / 0.01V) = Degrees Celcius
|
2022-09-03 15:17:20 +00:00
|
|
|
# temperatureC = (tmp36.voltage - 0.5) / 0.01
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
# Open the file to write down the results
|
|
|
|
timestr = time.strftime("%Y-%m-%dT%H-%M-%S")
|
2022-09-04 08:59:20 +00:00
|
|
|
filename = "/home/shared/hydrophonitor/data/depth/" + timestr + "_depth_data.csv"
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
#depthM = ((depthS.voltage * 31.848) - 22.93)
|
|
|
|
|
|
|
|
#Attempting to round the figure to a more intelligible figure
|
|
|
|
#rounddepth = round(depthM, ndigits)
|
|
|
|
#psi = depthS.voltage * 104.1666667 - 75
|
|
|
|
|
|
|
|
#bar = psi * 14.503773800722
|
|
|
|
|
2022-09-03 15:17:20 +00:00
|
|
|
with open(filename, "w", 1) as f:
|
|
|
|
f.write("time and date, Voltage of depth sensor (V), Depth (m)\n")
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
while True:
|
2022-09-03 15:17:20 +00:00
|
|
|
voltage = depthS.voltage
|
|
|
|
depthM = ((voltage * 31.848) - 22.93)
|
2022-08-20 13:40:10 +00:00
|
|
|
rounddepth = round(depthM, 2)
|
2022-09-03 15:17:20 +00:00
|
|
|
# roundtemp = round(temperatureC, 2)
|
|
|
|
roundvolts = round(voltage, 3)
|
2022-08-20 13:40:10 +00:00
|
|
|
|
2022-09-03 15:17:20 +00:00
|
|
|
print((str(voltage) + " V ") + (str(depthM) + " m ") + (str(roundvolts) + " V ") + (str(rounddepth) + " m"))
|
2022-08-20 13:40:10 +00:00
|
|
|
|
2022-09-04 08:59:20 +00:00
|
|
|
if lcd:
|
|
|
|
lcd.clear()
|
|
|
|
lcd.text((str(roundvolts) + " V ") + (str(rounddepth) + " m"), 1)
|
2022-09-03 15:17:20 +00:00
|
|
|
f.write(time.strftime("%Y-%m-%dT%H:%M:%S") + ",")
|
|
|
|
f.write(str(roundvolts) + "," + str(rounddepth) + "\n")
|
2022-08-20 13:40:10 +00:00
|
|
|
|
|
|
|
time.sleep(3)
|