-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermal_strain_sensor.py
123 lines (111 loc) · 3.74 KB
/
thermal_strain_sensor.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
#!/usr/bin/env python3
import serial
import time
import sys
sys.path.append('/home/pi/.local/lib/python3.7/site-packages')
from w1thermsensor import W1ThermSensor
import http.client
import urllib
import thingspeak
from multiprocessing import Process
port_str = '/dev/ttyACM1'
# Thermal sensors details at thingspeak
therm_channel_id = 1083150
therm_API_write = '23NNOFM7VANJ3KJA'
therm_API_read = '00IX5YL06NSI6W33'
# Smartbolt channel details at thingspeak
strain_channel_id = 1102730
strain_API_write = 'XVK2UWY0T72HDJQI'
strain_API_read = 'ISUW80PVH5LB1TJM'
def get_therm_data():
sensor_data = {}
for sensor in W1ThermSensor.get_available_sensors():
sensor_data[sensor.id] = sensor.get_temperature()
return sensor_data
def get_strain_therm_data(line):
## d = {}
print(line)
sub_str = line.split(':')
print(sub_str)
## key,value = zip(*(s.split('=') for s in sub_str))
## for k in key:
## d[k] = value
d = dict(item.split("=") for item in line.split(":"))
return d
def print_temp(sensor_data):
for key,val in sensor_data.items():
print("{:s} : {:.2f}".format(key,val))
print("-"*50)
def thingspeak_thermal_post(sensor_data):
params = ""
for idx, val in enumerate(sensor_data.values()):
params += '&' + urllib.parse.urlencode({'field{}'.format(idx+1): val})
## print(params)
params += '&' + urllib.parse.urlencode({'key':therm_API_write})
## print("Final: ")
## print(params)
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(response.status)
print(response.reason)
data = response.read()
conn.close()
except:
print("connection failed")
def thingspeak_strain_post(sensor_data):
params = ""
for idx, val in enumerate(sensor_data.values()):
params += '&' + urllib.parse.urlencode({'field{}'.format(idx+1): val})
## print(params)
params += '&' + urllib.parse.urlencode({'key':strain_API_write})
## print("Final: ")
## print(params)
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(response.status)
print(response.reason)
data = response.read()
conn.close()
except:
print("connection failed")
if __name__ == '__main__':
ser = serial.Serial(port_str,9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,timeout=5)
ser.flush()
t1 = time.time()
time.sleep(15)
t2 = 0
while (t2-t1) < 15:
print("Warming up")
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
time.sleep(1)
t2 = time.time()
ser.flush()
while(True):
#Thermal sensors:
therm_sensor_data = get_therm_data()
thingspeak_thermal_post(therm_sensor_data)
#Strain gauge:
strain_val = []
if ser.in_waiting > 0:
try:
line = ser.readline().decode('ISO-8859-1').rstrip()
except UnicodeDecodeError:
print("UnicodeDecodeError")
pass
else:
try:
strain_therm_sensor_data = get_strain_therm_data(line)
except ValueError:
print("ValueError")
pass
else:
thingspeak_strain_post(strain_therm_sensor_data)
time.sleep(15)