-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths0logger.py
436 lines (363 loc) · 15 KB
/
s0logger.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/python
# Using port of Adafruit GPIO library
# for Raspberry or C.H.I.P. microcontroller
#
# Author: Michael Eitelwein (michael@eitelwein.net)
# https://gitlab.eitelwein.net/MEitelwein/S0-Logger
#
# S0 enabled electricity meters send a S0 signal every tickPerkWh
# The S0 tick is detected at the GPIO pin of the microcontroller
# as a rising edge on s0Pin
# The GPIO library triggers S0Triger() based on raising edge
# and exports values via REST API as a JSON structure
#
# energy is counted in [Wh]
# power is calculated in [W]
# ticksKWH contains S0 ticks per 1 kWh
# (typically 1000 to 2000, check with your meter device)
#
# Processing of the s0 signal is indicated by the C.H.I.P.
# status LED going on and off during GPIO processing
# Configs in file /etc/s0logger (will be generated if not existing)
# hw = CHIP | RASPI select microcontroller type
# debug = True | False Print DEBUG output
# simulate = True | False Do not use GPIO HW but simulate S0 signals
# pidfile = <path/pidfile> Where to store pid
#
# htmlfile = <path/file> HTML file to be generated
# s0pin = <GPIO_PIN> Which PIN to poll for s0
# LEDpin = <GOPI_PIN> Pin with status LED on Raspi
# ticksperkwh = <number of ticks> See manual of S0 signal source
# s0blink = True | False Blink status LED with S0 signal
# port = <port-numer> Port used by built-in http-server
# ip = <ip address> IP to listen to, 0.0.0.0 means all interfaces
# -*- coding: UTF-8 -*-
import os
import time
import datetime
import sys
import syslog
import configparser
import json
import atexit
from bottle import Bottle, route, run, template, request
### Set bottle app up
### ------------------------------------------------
app = Bottle()
# Define default settings
settings = {
'HW' : 'CHIP',
'DEBUG' : True,
'SIMULATE' : True,
'configFileName' : 'config/s0logger.conf',
'ticksKWH' : 1000,
'port' : 8080,
'ip' : '0.0.0.0',
's0Pin' : 'XIO-P1',
'LEDPin' : 13,
's0Blink' : False,
'triggerActive' : False,
'url' : '/s0',
'urlPath' : '/s0'
}
# Apache2 defines urlPath in its own config
if __name__ != '__main__':
settings['urlPath'] = ''
### Reset GPIO when exiting
### ------------------------------------------------
def cleanup():
logMsg("Cleaning up S0-Logger on " + settings['s0Pin'])
if not settings['SIMULATE']:
if ( settings['HW'] == 'CHIP' ):
import CHIP.GPIO as GPIO
GPIO.remove_event_detect(settings['s0Pin'])
GPIO.cleanup(settings['s0Pin'])
elif ( settings['HW'] == 'RASPI' ):
import RPi.GPIO as GPIO
GPIO.remove_event_detect(int(settings['s0Pin']))
GPIO.cleanup(int(settings['s0Pin']))
GPIO.output(settings['LEDPin'], GPIO.LOW)
GPIO.cleanup(settings['LEDPin'])
else:
lsgMsg("Unknown hardware " + settings['HW'])
saveConfig(settings['configFileName'])
### Log msg to syslog or console
### ------------------------------------------------
def logMsg (msg):
if settings['SIMULATE']:
msg = 'Simulating: ' + msg
if settings['DEBUG']:
print (msg, file=sys.stderr)
else:
syslog.syslog(syslog.LOG_INFO, msg)
### Return string with date and time
### ------------------------------------------------
def strDateTime():
now = datetime.datetime.now()
return now.strftime("%d.%m.%Y %H:%M:%S")
### REST API /electricity to get S0 log
### ------------------------------------------------
@app.route(settings['urlPath']+'/electricity', method='GET')
def apiElectricity():
return s0Log
### REST API /trigger to trigger S0 when simulating
### ------------------------------------------------
@app.route(settings['urlPath']+'/trigger', method='GET')
def apiTrigger():
if settings['SIMULATE']:
S0Trigger(settings['s0Pin'])
return 'Triggered!'
else:
return 'Not in simulation mode!'
### REST API /version to get version info
### ------------------------------------------------
@app.route(settings['urlPath']+'/version', method='GET')
def apiVersion():
msg = {}
msg['application'] = 'S0-Logger'
msg['version'] = s0Log['data']['version']
return msg
### REST API /config to adjust configuration
### ------------------------------------------------
@app.route(settings['urlPath']+'/config', method='GET')
def apiSetConfig():
old = s0Log['data']['energy']
if request.GET.save:
oldSim = settings['SIMULATE']
# Will need to add checks here that API is used correctly
new = request.GET.energy.strip()
new = new.replace(',', '.')
s0Log['data']['energy'] = float(new)
settings['DEBUG'] = request.GET.debug.strip().lower() == 'true'
settings['HW'] = request.GET.hw.strip()
settings['SIMULATE'] = request.GET.simulate.strip().lower() == 'true'
settings['s0Blink'] = request.GET.blink.strip().lower() == 'true'
msg = '<h3>Configuration was updated</h3>'
msg += '<ul>'
if old == s0Log['data']['energy']:
msg += '<li>Energy: unchanged</li>'
else:
msg += '<li>Energy changed from '
msg += '%s Wh to %s Wh</li>' % (old, s0Log['data']['energy'])
msg += '<li>Debug: ' + str(settings['DEBUG']) + '</li>'
msg += '<li>Hardware: ' + str(settings['HW']) + '</li>'
msg += '<li>Simulation: ' + str(settings['SIMULATE']) + '</li>'
msg += '<li>s0Blink: ' + str(settings['s0Blink']) + '</li>'
msg += '</ul>'
if oldSim and not settings['SIMULATE']:
msg += '<h2>Simulation mode disabled - restart of webserver needed!'
saveConfig(settings['configFileName'])
return msg
else:
return template('config.tpl', energy=old, path=settings['url'], debug=settings['DEBUG'], hw=settings['HW'], simulate=settings['SIMULATE'], blink=settings['s0Blink'])
### Start built-in server for dev/debug
### ------------------------------------------------
def apiServer(ip, p, dbg):
run(app, host=ip, port=p, debug=dbg, quiet=dbg)
### Control C.H.I.P. status LED
### mode=1 will switch on, 0 will switch off
### ------------------------------------------------
def statusLED(mode):
if settings['s0Blink']:
if ( mode == 1 ):
if ( settings['HW'] == 'CHIP' ):
# Switch C.H.I.P. status LED on
if not settings['SIMULATE']:
os.system('/usr/sbin/i2cset -f -y 0 0x34 0x93 0x1')
elif ( settings['HW'] == 'RASPI' ):
# LED is connected to GPIO27 on pin 13
import RPi.GPIO as GPIO
GPIO.output(settings['LEDPin'], GPIO.HIGH)
else:
logMsg("Unknown hardware platform " + settings['HW'])
else:
if ( settings['HW'] == 'CHIP' ):
# Switch C.H.I.P. status LED off
if not settings['SIMULATE']:
os.system('/usr/sbin/i2cset -f -y 0 0x34 0x93 0x0')
elif ( settings['HW'] == 'RASPI' ):
import RPi.GPIO as GPIO
GPIO.output(settings['LEDPin'], GPIO.LOW)
else:
logMsg("Unknown hardware platform " + settings['HW'])
### Function being called by GPIO edge detection
### edge_handler is sending GPIO port as argument
### ------------------------------------------------
def S0Trigger(channel):
statusLED(1)
triggerTime = time.time()
s0Log['data']['time'] = strDateTime()
s0Log['data']['S0-ticks'] += 1
# dEnergy in [Wh]
# dTime in [s]
dEnergy = 1000 / settings['ticksKWH'];
s0Log['data']['dtime'] = triggerTime - settings['lastTrigger']
s0Log['data']['energy'] += dEnergy
s0Log['data']['power'] = dEnergy * 3600 / s0Log['data']['dtime']
if settings['DEBUG']:
msg = 'Trigger at ' + s0Log['data']['time']
msg += ' after ' + str(s0Log['data']['dtime']) + ' seconds,'
msg += ' at ' + str(s0Log['data']['energy']/1000) + ' kWh, '
msg += ' consuming ' + str(s0Log['data']['power']) + ' W'
logMsg(msg)
settings['lastTrigger'] = triggerTime
# write cache info to config file every 1 kWh
# to still have energy reading in case of power loss
if (s0Log['data']['energy'] % 1000) == 0:
updateConfig(settings['configFileName'])
statusLED(0)
### Initialize GPIO system
### ------------------------------------------------
def configGPIO(s0pin, LEDPin):
if not settings['triggerActive']:
if not settings['SIMULATE']:
if ( settings['HW'] == 'CHIP' ):
import CHIP_IO.GPIO as GPIO
# Config GPIO pin for pull down
# and detection of rising edge
GPIO.cleanup(s0pin)
GPIO.setup(s0pin, GPIO.IN, GPIO.PUD_DOWN)
GPIO.add_event_detect(s0pin, GPIO.RISING)
GPIO.add_event_callback(s0pin, S0Trigger)
elif ( settings['HW'] == 'RASPI' ):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
# Raspi accepts only integer values for pin
s0pin = int(s0pin)
# Status LED is connected to LEDPin
GPIO.cleanup(LEDPin)
GPIO.setup(LEDPin, GPIO.OUT)
GPIO.output(LEDPin, GPIO.LOW)
# Config GPIO pin for pull down
# and detection of rising edge
GPIO.cleanup(s0pin)
GPIO.setup(s0pin, GPIO.IN, GPIO.PUD_DOWN)
# The edge detection of RasPi sometimes bounces at about 330 ms
GPIO.add_event_detect(s0pin, GPIO.RISING, bouncetime=500)
GPIO.add_event_callback(s0pin, S0Trigger)
else:
logMsg("Unknown hardware platform " + settings['HW'])
settings['triggerActive'] = True
logMsg("Setting up S0-Logger on " + str(s0pin))
else:
logMsg("Trigger already active on " + str(s0pin))
### Create config file if not existing
### ------------------------------------------------
def createConfig(configFileName):
if not config.has_section('Config'):
config.add_section('Config')
if not config.has_section('Cache'):
config.add_section('Cache')
with open(configFileName, 'w') as configFile:
config.write(configFile)
### Load config file and create if not existing
### ------------------------------------------------
def loadConfig(configFileName):
# Create configFile if not exisiting
if not os.path.isfile(configFileName):
createConfig(configFileName)
# Try to read in config
config.read(configFileName)
# Check for sections in config
if not (config.has_section('Config') and config.has_section('Cache')):
logMsg('Config file misses section "Config" or "Cache" - will create new configuration')
createConfig(configFileName)
if config.has_option('Config', 'DEBUG'):
settings['DEBUG'] = config.get('Config', 'DEBUG').lower() == 'true'
if config.has_option('Config', 'HW'):
settings['HW'] = config.get('Config', 'HW')
if config.has_option('Config', 'SIMULATE'):
settings['SIMULATE'] = config.get('Config', 'SIMULATE').lower() == 'true'
if config.has_option('Config', 'port'):
settings['port'] = int(config.get('Config', 'port'))
if config.has_option('Config', 'ip'):
settings['ip'] = config.get('Config', 'ip')
if config.has_option('Cache', 'energy'):
s0Log['data']['energy'] = float(config.get('Cache', 'energy'))
if config.has_option('Config', 'ticksPerkWh'):
settings['ticksKWH'] = int(config.get('Config', 'ticksPerkWh'))
if config.has_option('Config', 'S0Pin'):
settings['s0Pin'] = config.get('Config', 's0Pin')
if config.has_option('Config', 'LEDPin'):
settings['LEDPin'] = int(config.get('Config', 'LEDPin'))
if config.has_option('Config', 's0Blink'):
settings['s0Blink'] = config.get('Config', 's0Blink').lower() == 'true'
if settings['DEBUG']:
logMsg('Config loaded')
### Save config file
### ------------------------------------------------
def saveConfig(configFileName):
# re-read in case it had been manually edited
config.read(configFileName)
if not config.has_section('Config'):
config.add_section('Config')
if not config.has_section('Cache'):
config.add_section('Cache')
config.set('Config', 'DEBUG', str(settings['DEBUG']))
config.set('Config', 'HW', str(settings['HW']))
config.set('Config', 'SIMULATE', str(settings['SIMULATE']))
config.set('Config', 's0Blink', str(settings['s0Blink']))
config.set('Config', 'port', str(settings['port']))
config.set('Config', 'ticksPerkWh', str(settings['ticksKWH']))
config.set('Config', 'ip', settings['ip'])
config.set('Config', 's0Pin', settings['s0Pin'])
config.set('Config', 'LEDPin', str(settings['LEDPin']))
config.set('Cache', 'energy', str(s0Log['data']['energy']))
with open(configFileName, 'w') as configFile:
config.write(configFile)
if settings['DEBUG']:
logMsg('Config saved')
### Update Cache section in config file before exiting
### --------------------------------------------------
def updateConfig(configFileName):
# re-read in case it had been manually edited
config.read(configFileName)
if not config.has_section('Cache'):
config.add_section('Cache')
config.set('Cache', 'energy', s0Log['data']['energy'])
with open(configFileName, 'w') as configFile:
config.write(configFile)
if settings['DEBUG']:
logMsg('Config updated')
### ===============================================
### MAIN
### ===============================================
# Data structure for S0 log
s0Log = {
'data': {
'energy' : 0.0,
'power' : 0,
'time' : 0,
'dtime' : 0,
'S0-ticks': 0,
'version' : 2.0
},
'units': {
'energy' : 'Wh',
'power' : 'W',
'time' : 'dd.mm.yyyy hh:mm:ss',
'dtime' : 's',
'S0-ticks': '',
'version' : ''
}
}
s0Log['data']['time'] = strDateTime()
settings['lastTrigger'] = time.time()
# Open syslog
syslog.openlog(ident="S0-Logger",logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL0)
# Read config in ConfigFileName
config = configparser.ConfigParser()
loadConfig(settings['configFileName'])
saveConfig(settings['configFileName'])
if settings['DEBUG']:
logMsg('S0-Logger starting')
# Config GPIO
configGPIO(settings['s0Pin'], settings['LEDPin'])
# Switch status LED off
statusLED(0)
# Register handle at process exit
atexit.register(cleanup)
# Start HTTP server for REST API
# start only if not called by apache-wsgi
if __name__ == '__main__':
apiServer(settings['ip'], settings['port'], settings['DEBUG'])