Skip to content

Instantly share code, notes, and snippets.

@vooon
Created October 4, 2014 08:29
Show Gist options
  • Save vooon/307a468bdc5590b5993d to your computer and use it in GitHub Desktop.
Save vooon/307a468bdc5590b5993d to your computer and use it in GitHub Desktop.
<Plugin python>
ModulePath "/root/bin"
LogTraces true
Interactive false
Import "sb_pressure"
<Module sb_pressure>
TempOffset -12.0
PressOffset -2000.0
</Module>
</Plugin>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Weather monitoring plugin for Snowball board
Uses onboard pressure sensor.
"""
from os import path
try:
import collectd
except ImportError as ex:
if __name__ != '__main__':
raise ex
SYS_DEVICE_PATH = "/sys/bus/i2c/devices/2-005c"
def sens_enable():
ep = path.join(SYS_DEVICE_PATH, "enable")
with open(ep, "w") as f:
f.write("1");
def read_pressure():
"Get pressure in [Pa]"
pp = path.join(SYS_DEVICE_PATH, "press_data")
with open(pp, "r") as f:
return float(f.readline()) * 100
def read_temperature():
"Get temperature [C]"
tp = path.join(SYS_DEVICE_PATH, "temp_data")
with open(tp, "r") as f:
return float(f.readline())
# offsets
temp_offset = 0.0
press_offset = 0.0
def config_callback(conf):
global temp_offset, press_offset
sens_enable()
for c in conf.children:
if c.key == "TempOffset":
temp_offset = float(c.values[0])
elif c.key == "PressOffset":
press_offset = float(c.values[0])
collectd.info("sb_pressure: Pressure offset: {}".format(press_offset))
collectd.info("sb_pressure: Temperature offset: {}".format(temp_offset))
def read_callback():
global temp_offset, press_offset
press = read_pressure() + press_offset
temp = read_temperature() + temp_offset
pv = collectd.Values(plugin='sb_pressure', plugin_instance='pressure')
pv.type = 'gauge'
pv.values = [press]
pv.dispatch()
tv = collectd.Values(plugin='sb_pressure', plugin_instance='temperature')
tv.type = 'gauge'
tv.values = [temp]
tv.dispatch()
if __name__ == '__main__':
sens_enable()
print "Current air pressure: {} Pa, temperature: {} C°".format(
read_pressure(),
read_temperature()
)
else:
collectd.register_config(config_callback)
collectd.register_read(read_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment