-
Notifications
You must be signed in to change notification settings - Fork 3
/
scale.py
executable file
·278 lines (222 loc) · 13.1 KB
/
scale.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
import datetime
import time
import yaml
import shelve
import logging
import scaleConfig
import argparse
import scalePlotly
import scaleTwitter
import scaleEmail
import scaleAlerts
import os
import RPi.GPIO as GPIO
import mattdaemon
'''
This is mostly from:
https://learn.adafruit.com/
reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/
script
(git://gist.github.com/3151375.git)
With some modifications by me that should have been other
commits, but weren't, so sorry, you'll have to diff it yourself
if you want to see what changed.
I have no idea what the license is on the original, so I have no
idea what the license on this is. Personally, I don't care, but
Adafruit might.
'''
class Scale(mattdaemon.daemon):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(self,adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
def initialize(self):
configFile = './scaleConfig.yaml'
parser = argparse.ArgumentParser()
parser.add_argument ("-d", "--debug", type=int, choices=[0,1],
nargs='?', const=1,
help="turn debugging on (1) or off (0); this overrides"+
"the value in the configuration file ")
parser.add_argument("-c","--config", action="store", dest="configFile",
help="specify a configuration file")
parser.add_argument("-r","--requires-root", action="store_true", dest="configFile",
help="for Matt Daemon")
args = parser.parse_args()
if args.configFile:
configFile = args.configFile
self.logger = logging.getLogger("beanscale")
self.logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/home/pi/scale/scaledaemon.log")
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.info("Starting")
cfg = scaleConfig.readConfig(self,configFile)
if args.debug == None:
DEBUG = cfg['raspberryPiConfig']['debug']
else:
DEBUG = args.debug
# if DEBUG:
# logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s')
# else:
# logging.basicConfig(filename="scale.log",level=logging.DEBUG,
# format='%(asctime)s %(message)s')
logger.debug("Initializing.")
try:
f = open(cfg['raspberryPiConfig']['plotlyCredsFile'])
args.plotlyConfig = yaml.safe_load(f)
f.close()
except:
logger.error("I couldn't open the file %s to read the plot.ly settings, so I can't make a plot and am giving up. I am %s/%s",
cfg['raspberryPiConfig']['plotlyCredsFile'], os.path.abspath(os.path.dirname(sys.argv[0])),sys.argv[0])
exit (1)
if not cfg['raspberryPiConfig']['alertChannels']:
cfg['raspberryPiConfig']['alertChannels'] = ''
if not cfg['raspberryPiConfig']['updateChannels']:
cfg['raspberryPiConfig']['updateChannels'] = ''
global GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) # to stop the "This channel is already in use" warning
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
# ADCPort Cobbler
# -------- ----------
args.SPICLK = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICLK']
args.SPIMISO = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMISO']
args.SPIMOSI = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMOSI']
args.SPICS = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICS']
# Diagram of ADC (MCP3008) is at:
# https://learn.adafruit.com/
# reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/
# connecting-the-cobbler-to-a-mcp3008
# set up the SPI interface pins
GPIO.setup(args.SPIMOSI, GPIO.OUT)
GPIO.setup(args.SPIMISO, GPIO.IN)
GPIO.setup(args.SPICLK, GPIO.OUT)
GPIO.setup(args.SPICS, GPIO.OUT)
# FSR connected to adc #0
args.fsr_adc = cfg['raspberryPiConfig']['adcPortWithFSR']
# to keep from being jittery we'll only change when the FSR has moved
# more than this many 'counts'
args.tolerance = cfg['raspberryPiConfig']['tolerance']
# not ideal, but at least it's just in one place now
args.logString = '{0} {1}\tfsr: {2:4d}\tlast_value: {3:4d}\tchange: {4:4d}\tbeans: {5}'
# If we will need Twitter credentials, read them now
#
if 'twitter' in cfg['raspberryPiConfig']['alertChannels'] or 'twitter' in cfg['raspberryPiConfig']['updateChannels']:
logger.debug ("Reading Twitter credentials from {}".format(cfg['twitterConfiguration']['twitterCredsFile']))
try:
f = open(cfg['twitterConfiguration']['twitterCredsFile'])
twitterCredentials = yaml.safe_load(f)
f.close()
except:
logger.error("I couldn't open the file %s to read the twitter credentials, so I can't tweet. I am: %s/%s",
cfg['twitterConfiguration']['twitterCredsFile'],
os.path.abspath(os.path.dirname(sys.argv[0])),
sys.argv[0])
cfg['raspberryPiConfig']['alertChannels'].remove('twitter')
cfg['raspberryPiConfig']['updateChannels'].remove('twitter')
oTime = cfg['raspberryPiConfig']['updateTime']
while cfg['raspberryPiConfig']['updateTime'] % cfg['raspberryPiConfig']['checkTime']:
cfg['raspberryPiConfig']['updateTime'] += 1
if oTime != cfg['raspberryPiConfig']['updateTime']:
logger.debug ("\"updateTime\" changed from %s to %s so it would divide evenly by \"checkTime\".",
oTime, cfg['raspberryPiConfig']['updateTime'])
# This totally isn't a clock, it's a counter. But we use it sort of
# like a clock.
args.scaleClock = 0
# Python shelves let us keep some state between runs; in this case, we
# are keeping the state of the alerts we have generated, so we don't
# re-send emails or tweets if one has already been sent for a given
# state
args.alertState = shelve.open("scaleState.db", writeback=True)
for alert in cfg['raspberryPiConfig']['alertChannels']:
if alert not in args.alertState:
args.alertState[alert] = 0
logger.debug ("Ready.")
return args,cfg
def mainloop(self,args,cfg):
print "in 'mainloop'..."
# self.initialize()
last_read = 0
while True:
# read the analog pin
fsr = self.readadc(args.fsr_adc, args.SPICLK, args.SPIMOSI, args.SPIMISO, args.SPICS)
# how much has it changed since the last read?
fsr_change = (fsr - last_read)
currentTime = str(datetime.datetime.now()).split('.')[0]
if fsr_change > cfg['raspberryPiConfig']['maxChange'] and ( abs(fsr_change) > args.tolerance ):
last_read = fsr
beans = int((fsr/1024.)*100)
logger.debug (args.logString.format(currentTime, "CHANGE",
fsr, last_read,
fsr_change, beans))
scalePlotly.updatePlot (currentTime,
beans,
args.plotlyConfig['username'],
args.plotlyConfig['apikey'])
# I think we only need to check the alert status after
# changes, within tolerance and maxChange
scaleAlerts.processLowBeanAlerts (fsr, args.alertState, cfg, currentTime)
if args.scaleClock % cfg['raspberryPiConfig']['updateTime'] == 0:
last_read = fsr
beans = int((fsr/1024.)*100)
logger.debug (args.logString.format(currentTime, "UPDATE",
fsr, last_read,
fsr_change, beans))
if 'plotly' in cfg['raspberryPiConfig']['updateChannels']:
scalePlotly.updatePlot (currentTime,
beans,
args.plotlyConfig['username'],
args.plotlyConfig['apikey'])
if 'twitter' in cfg['raspberryPiConfig']['updateChannels']:
hashTags=" ".join(["#"+m for m in cfg['twitterConfiguration']['twitterUpdateHashtags']])
tweet = cfg['twitterConfiguration']['twitterUpdateMessage']+" "+ hashTags
tweet = tweet.format(beans,currentTime)
scaleTwitter.tweetStatus(cfg['twitterConfiguration']['twitterCredsFile'],
tweet)
if 'email' in cfg['raspberryPiConfig']['updateChannels']:
subject = cfg['emailConfiguration']['emailUpdateSubject']
body = cfg['emailConfiguration']['emailUpdateMessage'].format(beans)
scaleEmail.sendEmail (cfg['emailConfiguration']['smtpServer'],
cfg['emailConfiguration']['gmailCredsFile'],
cfg['emailConfiguration']['fromAddr'],
cfg['emailConfiguration']['toAddr'],
subject, body)
args.scaleClock += cfg['raspberryPiConfig']['checkTime']
time.sleep(cfg['raspberryPiConfig']['checkTime'])
def run(self):
args,cfg = self.initialize()
print "Args",args
print "Cfg",cfg
self.mainLoop(args,cfg)
if __name__ == "__main__":
pidfile = "./scale.pid"
myS = Scale(pidfile, root=True, daemonize=False)
myS.start()