-
Notifications
You must be signed in to change notification settings - Fork 0
/
picellar_sensors.py
executable file
·204 lines (160 loc) · 4.54 KB
/
picellar_sensors.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import glob
import time
#import RPi.GPIO as GPIO
import Adafruit_DHT
#os.system('modprobe w1-gpio')
#os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
#device_file = device_folder + '/w1_slave'
class SensorData(object):
name = None
temperature = None
humidity = None
# def __init__(self, name, temperature, humidity):
# self.name = name
# self.temperature = temperature
# self.humidity = humidity
class DhtSensors(object):
name = None
gpio = None
def __init__(self, name, gpio):
self.name = name
self.gpio = gpio
def Read_temp_sensors():
sensorDataList = []
#### 1-WIRE (DS18B20) Temperature only ####
for dfolder in device_folder:
foldername = os.path.basename(dfolder)
sensorData = SensorData()
sensorData.name = foldername
f = open(dfolder + '/w1_slave', 'r')
rawSensorData = f.readlines()
f.close()
#while rawSensorData[0].strip()[-3:] != 'YES':
#time.sleep(0.2)
#rawSensorData = read_temp_raw()
# test = "test"
equals_pos = rawSensorData[1].find('t=')
if equals_pos != -1:
temp_string = rawSensorData[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
sensorData.temperature = round(temp_c,1)
sensorDataList.append(sensorData)
#### DTH22 Temp + Humidity ####
dhtList = []
dhtList.append(DhtSensors("DHT22-1",19))
for dhtSensor in dhtList:
sensorData = SensorData()
sensorData.name = dhtSensor.name
##### HACK POUR DESACTIVER LE CHECK DU DHT
#
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, dhtSensor.gpio)
sensorData.temperature = round(temperature,1)
sensorData.humidity = round(humidity,1)
#
#####
#
# sensorData.temperature = 12
# sensorData.humidity = 70
#
#####
sensorDataList.append(sensorData)
return sensorDataList
# lines = read_temp_raw()
# while lines[0].strip()[-3:] != 'YES':
# time.sleep(0.2)
# lines = read_temp_raw()
# equals_pos = lines[1].find('t=')
# if equals_pos != -1:
# temp_string = lines[1][equals_pos+2:]
# temp_c = float(temp_string) / 1000.0
# temp_f = temp_c * 9.0 / 5.0 + 32.0
# return round(temp_c,1)
try:
if __name__ == "__main__":
f = 1
# GPIO.setwarnings(False)
# GPIO.setmode(GPIO.BCM)
# GPIO.setup(13, GPIO.OUT)
# GPIO.setup(16, GPIO.OUT)
# GPIO.setup(20, GPIO.OUT)
# GPIO.setup(26, GPIO.OUT)
# GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# # GPIO.cleanup();
# # GPIO.setmode(GPIO.BCM)
# # GPIO.setup(20, GPIO.OUT)
# start = False;
# GPIO.setup(20, start)
# tickCount = 0
# startTime = time.time()
# p = GPIO.PWM(20, 50) # channel=12 frequency=50Hz
# p.start(100)
# while True:
# a = 1
# # for dc in range(0, 101, 5):
# # p.ChangeDutyCycle(dc)
# # time.sleep(0.1)
# # for dc in range(100, -1, -5):
# # p.ChangeDutyCycle(dc)
# # time.sleep(0.1)
# # print GPIO.input(26)
# # GPIO.output(20,True)
# # print "True"
# # time.sleep(0.3)
# # GPIO.output(20,False)
# # print "False"
# # time.sleep(0.3)
while True:
print "Start Sensor Read"
for sensor in Read_temp_sensors():
print "Sensor ID : " + sensor.name
print "Sensor Data (temperature) : " + str(sensor.temperature)
print "Sensor Data (humidity) : " + str(sensor.humidity)
print
time.sleep(1)
# # start = not start
# # print start
# # if start:
# # GPIO.setup(20, start)
# # else:
# # GPIO.setup(20, start)
# if GPIO.input(26) == False:
# tickCount = tickCount + 1
# delay = time.time() - startTime
# # print time.time()
# if delay > 1:
# rpm = tickCount
# print str(rpm) + "RPM"
# print "Delay : " + str(delay)
# tickCount = 0
# startTime = time.time()
# # time.sleep(1)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
a = 1
# GPIO.cleanup() # cleanup all GPIO
# base_dir = '/sys/bus/w1/devices/'
# device_folder = glob.glob(base_dir + '28*')[0]
# device_file = device_folder + '/w1_slave'
# def read_temp_raw():
# for SensorFolder in
# f = open(device_file, 'r')
# lines = f.readlines()
# f.close()
# return lines
# def read_temp(sensor_num):
# lines = read_temp_raw()
# while lines[0].strip()[-3:] != 'YES':
# time.sleep(0.2)
# lines = read_temp_raw()
# equals_pos = lines[1].find('t=')
# if equals_pos != -1:
# temp_string = lines[1][equals_pos+2:]
# temp_c = float(temp_string) / 1000.0
# temp_f = temp_c * 9.0 / 5.0 + 32.0
# return round(temp_c,1)
# if __name__ == "__main__":
# while True:
# print(read_temp())
# time.sleep(1)