Commit fa3406c6c2b83d50449d1a172c2e9e144b024938

Authored by Greg Sandstrom
1 parent 6d91bc14cd
Exists in main

write tc08 device serial as tag to measurements

Showing 1 changed file with 3 additions and 2 deletions Inline Diff

temperature_logger.py View file @ fa3406c
# inspired by example at 1 1 # inspired by example at
# https://github.com/picotech/picosdk-python-wrappers/blob/master/usbtc08Examples/tc08StreamingModeExample.py 2 2 # https://github.com/picotech/picosdk-python-wrappers/blob/master/usbtc08Examples/tc08StreamingModeExample.py
# 3 3 #
# API docs 4 4 # API docs
# https://www.picotech.com/download/manuals/usb-tc08-thermocouple-data-logger-programmers-guide.pdf 5 5 # https://www.picotech.com/download/manuals/usb-tc08-thermocouple-data-logger-programmers-guide.pdf
6 6
import signal 7 7 import signal
from threading import Timer 8 8 from threading import Timer
from tc08_device import TC08 9 9 from tc08_device import TC08
from influxdb_bridge import InfluxDBBridge 10 10 from influxdb_bridge import InfluxDBBridge
11 11
# how often does Monitor check for new data from the TC08 driver 12 12 # how often does Monitor check for new data from the TC08 driver
LOG_INTERVAL_SEC = 5 13 13 LOG_INTERVAL_SEC = 5
14 14
DEBUG = False 15 15 DEBUG = False
16 16
17 17
def dprint(arg): 18 18 def dprint(arg):
if DEBUG: 19 19 if DEBUG:
print(arg) 20 20 print(arg)
21 21
22 22
class Monitor(Timer): 23 23 class Monitor(Timer):
def __init__(self, interval): 24 24 def __init__(self, interval):
# Timer constructer expects a callback function, but we don't care 25 25 # Timer constructer expects a callback function, but we don't care
# about that, so just pass None 26 26 # about that, so just pass None
super(Monitor, self).__init__(interval, None) 27 27 super(Monitor, self).__init__(interval, None)
self.idb = None 28 28 self.idb = None
self.tc08s = list() 29 29 self.tc08s = list()
30 30
# override Timer's run method to loop on our injest_data function 31 31 # override Timer's run method to loop on our injest_data function
# wait will block while loop for timeout interval 32 32 # wait will block while loop for timeout interval
# calling timer.cancel() sets the finish flag and exits the loop 33 33 # calling timer.cancel() sets the finish flag and exits the loop
def run(self): 34 34 def run(self):
dprint("in timer.run method") 35 35 dprint("in timer.run method")
while not self.finished.wait(timeout=self.interval): 36 36 while not self.finished.wait(timeout=self.interval):
self.do_work() 37 37 self.do_work()
38 38
# connect as many tc08's as we can find 39 39 # connect as many tc08's as we can find
def setup(self): 40 40 def setup(self):
searching = True 41 41 searching = True
while searching: 42 42 while searching:
device = TC08.get_any_tc08() 43 43 device = TC08.get_any_tc08()
if device is None: 44 44 if device is None:
dprint(f"No more TC08s to dicsover") 45 45 dprint(f"No more TC08s to dicsover")
searching = False 46 46 searching = False
else: 47 47 else:
dprint(f"Found TC08: {device}") 48 48 dprint(f"Found TC08: {device}")
self.tc08s.append(device) 49 49 self.tc08s.append(device)
50 50
# setup and run 51 51 # setup and run
for device in self.tc08s: 52 52 for device in self.tc08s:
device.setup() 53 53 device.setup()
device.run(LOG_INTERVAL_SEC * 1000) 54 54 device.run(LOG_INTERVAL_SEC * 1000)
55 55
# open connection to influxdb 56 56 # open connection to influxdb
self.idb = InfluxDBBridge() 57 57 self.idb = InfluxDBBridge()
self.idb.connect() 58 58 self.idb.connect()
59 59
dprint("end setup") 60 60 dprint("end setup")
61 61
def cleaup(self): 62 62 def cleaup(self):
for device in self.tc08s: 63 63 for device in self.tc08s:
device.stop() 64 64 device.stop()
device.close() 65 65 device.close()
66 66
def do_work(self): 67 67 def do_work(self):
points = [] 68 68 points = []
for device in self.tc08s: 69 69 for device in self.tc08s:
serial = device.get_serial() 70 70 serial = device.get_serial()
data = device.get_streaming_data() 71 71 data = device.get_streaming_data()
for channel in range(0, 9): 72 72 for channel in range(0, 9):
for reading in data[channel]: 73 73 for reading in data[channel]:
(temperature, time_ms, overflow) = reading 74 74 (temperature, time_ms, overflow) = reading
point = self.make_point( 75 75 point = self.make_point(
76 serial,
channel, 76 77 channel,
temperature, 77 78 temperature,
time_ms, 78 79 time_ms,
overflow, 79 80 overflow,
) 80 81 )
points.append(point) 81 82 points.append(point)
self.idb.commit(points) 82 83 self.idb.commit(points)
83 84
def make_point(self, channel, temperature, time_ms, overflow): 84 85 def make_point(self, serial, channel, temperature, time_ms, overflow):
json_body = { 85 86 json_body = {
"measurement": "temperature", 86 87 "measurement": "temperature",
"tags": {"channel": channel}, 87 88 "tags": {"channel": channel, "tc08-serial": serial},
"time": time_ms, 88 89 "time": time_ms,
"fields": {"temperature": temperature, "overflow": overflow}, 89 90 "fields": {"temperature": temperature, "overflow": overflow},
} 90 91 }
return json_body 91 92 return json_body
92 93
def start_monitor(self): 93 94 def start_monitor(self):
self.setup() 94 95 self.setup()