-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
87 lines (67 loc) · 2.25 KB
/
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
#!/usr/bin/env python
#
# Code harvested 00:35 MDT 24 NOV 2015 and derived from these sources:
# https://github.com/sparkfun/SparkFun_MAX31855K_Thermocouple_Breakout_Arduino_Library/blob/master/examples/SparkFun_MAX31855K_Thermocouple_Breakout/SparkFun_MAX31855K_Thermocouple_Breakout.ino
# https://learn.sparkfun.com/tutorials/programming-the-pcduinoo# http://learn.linksprite.com/pcduino/quick-start/explaination-of-pcduino3-headers/
# http://tronixstuff.com/2014/01/29/tutorial-pcduino-gpio-arduino-ide/
#
# -*- coding: utf-8 -*-
"""
* (sensor.py) function:
* Reads the thermocouple breakout board SparkFun_MAX31855K_Thermocouple
* Adds the reading to a ZeroMQ queue
"""
import os
import zmq
import sys
import time
import timeit
import datetime
import serial ## Load the serial library
import logging
logging.basicConfig(filename='/var/log/sensor/sensor.log', format='%(asctime)s %(message)s', level=logging.DEBUG)
# Set up ZeroMQ
port = "5556"
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind("tcp://127.0.0.1:%s" % port)
# Sleep interval in seconds
interval = 1.0
# Write out our PID file so we can kill this process later
def writePidFile():
pid = str(os.getpid())
f = open('/var/run/sensor/sensor.pid', 'w')
f.write(pid)
f.close()
## Figure out how to open SPI
something = "something SPI"
## set up the sensor SPI bus
logging.info("******** SPI ******** ")
logging.debug("Connecting to: " + something)
## do anything needed here
logging.info("******** SPI ******** ")
logging.debug("Connected to: " + something)
## Begin infinite loop
while True:
time.sleep(interval)
start_time = timeit.default_timer()
## Read the breakout board
logging.info("******** SPI ******** ")
logging.debug("Reading: " + something)
## Wait for data to come in
s = something
elapsed = timeit.default_timer() - start_time
## Here's the elapsed reading time
logging.debug("Read cycle time: %s", str(elapsed))
## Catch any other formatting problems
logging.debug("SPI Value: %s", something)
## Capture, parse and convert value
# set value from something
value = something
## Send the data to the queue
msg = value
socket.send(msg)
logging.debug("MSG: %s", msg)
## End infinite loop
## Close the port, be a good citizen
# close something