forked from arminstr/reremeter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensorInterface.py
More file actions
138 lines (112 loc) · 4.91 KB
/
sensorInterface.py
File metadata and controls
138 lines (112 loc) · 4.91 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
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
import serial
import struct
import re
import numpy
import csv
import time
import os
import pyrebase
import tensorflow as tf
PLASTICTYPES = ["PET","HDPE","PVC","LDPE","PP","PS"]
wavelength_correction = [10887, 162684, 224906, 134547, 113060, 135121, 101445, 78442]
config = {
#insert your firebase config here
}
#id of your device within the firebase
device_id = "1234567"
firebase = pyrebase.initialize_app(config)
db = firebase.database()
measure = False
#stream handler listening to firebase changes
def stream_handler(callback):
global measure
if callback["path"] == "/":
if callback["data"] == {'measure': 'true'}:
measure = True
if callback["data"] == 'true':
measure = True
controlStream = db.child("devices").child(device_id).child("control").stream(stream_handler)
#open the serial to the device
ser = serial.Serial('/dev/tty.usbmodem205231794B411')
#print(ser.name)
measurements = []
while 1:
if ser.is_open == False:
ser.open()
ser.flushInput()
ser.flush()
#wait for firebase command to enable a single measurement
if measure == True:
start = time.time()
ser.write(b'CMD=1')
#byte_array = ser.readline()
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("CMD=OK"):
transmission_started = True
#print('Measurement started...')
for x in range(0, 3):
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("MEASURE=PRE"):
measurement_type = "PRE"
if string_array.startswith("MEASURE=VALUE"):
measurement_type = "VALUE"
if string_array.startswith("MEASURE=POST"):
measurement_type = "POST"
num_array = re.findall('\d+', string_array) #find all integers
measurements.append([0,0,0,num_array[0], num_array[1], num_array[2], num_array[3], num_array[4], num_array[5], num_array[6], num_array[7]]) #store an array to write to the csv file
if len(measurements) > 3:
measurements = measurements[3:]
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("MEASURE=FINISH"):
end = time.time()
wavelengths = []
for y in range(0, 8):
j = y+3
wavelengths.insert(y, int(round(int(measurements[1][j]) - (int(measurements[0][j]) + int(measurements[2][j]))/2))- wavelength_correction[y])
measurements_corrected = [
wavelengths[0],
wavelengths[1],
wavelengths[2],
wavelengths[3],
wavelengths[4],
wavelengths[5],
wavelengths[6],
wavelengths[7],
(wavelengths[2] - wavelengths[1])/(940-850),
(wavelengths[3] - wavelengths[2])/(1200-940),
(wavelengths[4] - wavelengths[3])/(1300-1200),
(wavelengths[5] - wavelengths[4])/(1450-1300),
(wavelengths[6] - wavelengths[5])/(1550-1450),
(wavelengths[7] - wavelengths[6])/(1650-1550)
]
measurements_upload = [
wavelengths[0],
wavelengths[1],
wavelengths[2],
wavelengths[3],
wavelengths[4],
wavelengths[5],
wavelengths[6],
wavelengths[7]
]
#calculate measurement time
time_ms = int(round((end - start)*1000))
data = {"white": str(measurements_upload[0]),
"nm850": str(measurements_upload[1]),
"nm940": str(measurements_upload[2]),
"nm1200": str(measurements_upload[3]),
"nm1300": str(measurements_upload[4]),
"nm1450": str(measurements_upload[5]),
"nm1550": str(measurements_upload[6]),
"nm1650": str(measurements_upload[7])
}
#upload new measurement to firebase
itemid = db.child("devices").child(device_id).child("detection").child("measurements").push(data)
data = {"predictID": itemid}
db.child("devices").child(device_id).child("control").update(data)
data = {"measure": "false"}
db.child("devices").child(device_id).child("control").update(data)
print(str(time_ms) + ' ms')
#print('Measurement successfully stored!')
measure = False
ser.close()