Listing Program - RaspberryPi - SenzorPH - SenzorPH Reza - SenzorPH - tempAPA
Listing Program - RaspberryPi - SenzorPH - SenzorPH Reza - SenzorPH - tempAPA
/usr/bin/python
2
3 # Distributed with a free-will license.
4 # Use it any way you want, profit or free, provided it fits in the licenses of its
associated works.
5 # TMP100
6 # This code is designed to work with the pH I2C Dormant Module available from:
7 # http://rezaalihussain.blogspot.com/2014/04/measuring-ph-using-dormant-labs-ph.html
8 # Code Vers1 19.08.2018 George IPATE
9
10 import uuid
11 import os, fnmatch
12 import smbus
13 import time
14 import datetime
15 from picamera import PiCamera
16
17
18 # Get I2C bus
19 bus = smbus.SMBus(1)
20
21 camera = PiCamera(resolution=(640, 480))
22 camera.rotation = 180
23 camera.start_preview()
24 time.sleep(1)
25 camera.capture('/home/pi/SenzorPH/SenzorPH Reza/image%s.jpg')
26 camera.stop_preview()
27
28
29 # Create a text file
30 filename = "//home/pi/SenzorPH/SenzorPH Reza/DateExp/ExpPH_"+str(uuid.uuid4())+".txt"
31 f = open(filename,"w+")
32
33 #Add in the w1_gpio and w1_therm modules
34 os.system('modprobe w1-gpio')
35 os.system('modprobe w1-therm')
36
37
38 volt4 = 0.95
39 volt7 = 0.67
40 calibrationTempC = 20
41
42 print('==== Masurarea pH-ului si temperaturii apei , lab ICM
====',datetime.datetime.now())
43 print('==== Curs INGINERIA si CALITATEA MEDIULUI ====')
44
45 def getRoomTemperatureC():
46 # TMP100 address, 0x4B(79)
47 # Read data back from 0x00(00), 2 bytes
48 # temp MSB, temp LSB
49 data = bus.read_i2c_block_data(0x4B, 0x00, 2)
50 # SetRoomTemperataureResolutionBits(12) # 12 bits room temp resolution in celsius
51 # Convert the data to 12-bits
52 temp = (data[0] * 256 + (data[1] & 0xF0)) / 16
53 temp = temp * 0.0625
54 # Output data to screen
55 #print("Temperature in Celsius is : %.2f C" %temp)
56 return round(temp,2)
57
58 def getPHVolts():
59 # PHADDRESS 0x4D
60 # Read data back from 0x00(00), 2 bytes
61 # pH MSB, pH LSB
62
63 dataPH = bus.read_i2c_block_data(0x4D, 0x00, 2)
64 units = (dataPH[0] * 256) + dataPH[1]
65 volts = (units /4096)*3
66 return round(volts,2)
67
68 def adjustPHBasedOnTemp(PH, temp):
69 #// http://www.omega.com/Green/pdf/pHbasics_REF.pdf
70 #// When the temperature is other than 25degC and the ph is other than 7
71 #// the temperature error is 0.03ph error/ph unit/10degC
72 #// which means error = 0.03*(ph away from 7)*(tempdiffC/10)
73
74 phDifference = abs(PH-7)
75 tempDifferenceC = abs(temp-25.0)
76 phAdjust = (0.03*phDifference)*(tempDifferenceC/10)
77
78 if PH>7:
79 if temp<25:
80 phAdjust=phAdjust
81 elif temp>25:
82 phAdjust=phAdjust*-1
83 if PH<7:
84 if temp>25:
85 phAdjust=phAdjust
86 elif temp<25:
87 phAdjust=phAdjust*-1
88
89 tempAdjustedPH = PH + phAdjust
90 return round(tempAdjustedPH,2)
91
92
93 try:
94 while True:
95 sampleSize = 500
96 avgMeasuredPH = 0
97 avgRoomTempC = 0
98 avgPHVolts = 0
99 avgRoomTemperatureCompensatedMeasuredPH = 0
100
101 for i in range(1, sampleSize):
102 phVolt = getPHVolts()
103 tempAdjusted4 = adjustPHBasedOnTemp(4,calibrationTempC)
104 voltsPerPH = (abs(volt7-volt4)) / (7-tempAdjusted4)
105 realPHVolt = (volt7 - phVolt)
106 phUnits = realPHVolt / voltsPerPH
107 measuredPH = 7 + phUnits
108 roomTempC = getRoomTemperatureC()
109 roomTempCompensatedMeasuredPH = adjustPHBasedOnTemp(measuredPH,roomTempC)
110 avgMeasuredPH+=measuredPH
111 avgRoomTemperatureCompensatedMeasuredPH+=roomTempCompensatedMeasuredPH
112 avgRoomTempC+=roomTempC
113 avgPHVolts += phVolt
114
115 avgMeasuredPH/=sampleSize
116 avgRoomTemperatureCompensatedMeasuredPH/=sampleSize
117 avgRoomTempC/=sampleSize
118 avgPHVolts/=sampleSize
119 dateArray = []
120 for filename in os.listdir("/sys/bus/w1/devices"):
121 if fnmatch.fnmatch(filename, '28-*'):
122 with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as fileobj:
123 lines = fileobj.readlines()
124 #print lines
125 if lines[0].find("YES"):
126 pok = lines[1].find('=')
127 dateArray.append(float(lines[1][pok+1:pok+6])/1000)
128 tempWater = dateArray[0]
129 else:
130 print("Error reading sensor DS18B20 with ID: %s" % (filename))
131
132 acum = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S")
133 f.write(str(acum) + "," + str(avgMeasuredPH) + "," + str(avgPHVolts)+ "," +
str(avgRoomTempC)+ "," + str(tempWater) + "\r\n")
134 f.flush() #This allows us to just see the last 50 data
135
136 print("Data-", acum, "valPH-medie ", avgMeasuredPH," valPHcomp- ",
avgRoomTemperatureCompensatedMeasuredPH
137 , " valPHVolts- ", avgPHVolts, " TempC- ", avgRoomTempC," TempApa- ",
tempWater)
138
139
140
141 time.sleep(30)
142
143 except KeyboardInterrupt:
144 print ("Program intrerupt de la tastatura Ctrl-C")
145 #// which means error = 0
146
147