-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph.py
37 lines (30 loc) · 849 Bytes
/
ph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import time
from smbus2 import SMBus
I2C_ADDRESS = 99
I2C_BUS = 1
bus = SMBus(I2C_BUS)
def read_ph_sensor():
try:
bus.write_byte(I2C_ADDRESS, 0x52)
time.sleep(1.5)
data = bus.read_i2c_block_data(I2C_ADDRESS, 0, 31)
result = ''.join([chr(i) for i in data])
return result.strip()
except Exception as e:
print("Error ")
return None
while True:
try:
reading = read_ph_sensor()
if reading:
pH_level = float(reading)
if pH_level > 12.5:
print("Very basic environment")
elif pH_level < 0 :
print("Very acidic environment")
else:
print(f"pH Level: {pH_level}")
time.sleep(1)
except Exception as e:
print(f"Error: {e}")
time.sleep(1)