-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemperaturetesting
More file actions
65 lines (52 loc) · 1.79 KB
/
temperaturetesting
File metadata and controls
65 lines (52 loc) · 1.79 KB
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
import os
import glob
import time
import http.client
import urllib
API_KEY = ‘API_KEY’
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
os.chdir('/users/Sean/RaspPi')
device_folder1 = glob.glob('./28*')[0]
device_folder2 = glob.glob('./28*')[1]
device_folder3 = glob.glob('./28*')[2]
device_folder4 = glob.glob('./28*')[3]
device_file1 = device_folder1 + '/w1_slave'
device_file2 = device_folder2 + '/w1_slave'
device_file3 = device_folder3 + '/w1_slave'
device_file4 = device_folder4 + '/w1_slave'
def read_temp_probe(device_file):
f = open(device_file, 'r')
temp = f.readlines()
f.close()
return temp
def read_temp(device_file):
temp = read_temp_probe(device_file)
while temp[0].strip()[-3:] != 'YES':
time.sleep(16)
temp = read_temp_probe(device_file)
equals_pos = temp[1].find('t=')
if equals_pos != -1:
temp_string = temp[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def transmit(temp1, temp2, temp3, temp4):
params = urllib.parse.urlencode({'field1': temp1, 'field2': temp2, 'field3': temp3, 'field4': temp4, 'key’:’API_KEY’})
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = http.client.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print(temp1, temp2, temp3, temp4)
print(response.status, response.reason)
data = response.read()
conn.close()
except:
print("connection failed")
while True:
temp1 = read_temp(device_file1)
temp2 = read_temp(device_file2)
temp3 = read_temp(device_file3)
temp4 = read_temp(device_file4)
transmit(temp1, temp2, temp3, temp4)
time.sleep(16)