diff options
-rwxr-xr-x | files/common/openwrt/sensors-init.lua | 75 | ||||
-rw-r--r-- | inventory/group_vars/chaos-at-home-sensors/vars.yml | 2 | ||||
-rw-r--r-- | inventory/host_vars/ch-sensors1.yml | 25 |
3 files changed, 101 insertions, 1 deletions
diff --git a/files/common/openwrt/sensors-init.lua b/files/common/openwrt/sensors-init.lua new file mode 100755 index 00000000..f958dd2d --- /dev/null +++ b/files/common/openwrt/sensors-init.lua @@ -0,0 +1,75 @@ +#!/usr/bin/lua + +cjson = require "cjson.safe" +glob = require "posix.glob" +time = require "posix.time" +unistd = require "posix.unistd" + +-- ############# utils ########### + +function i2c_bus_path(bus) + return string.format("/sys/bus/i2c/devices/i2c-%d", bus) +end + +function i2c_device_path(bus, address) + return string.format("/sys/bus/i2c/devices/%d-%04X", bus, address) +end + +function i2c_add_device(bus, name, address) + local f, err = io.open(i2c_bus_path(bus) .. "/new_device", "w") + if not f then return nil, err end + + ret, err = f:write(string.format("%s 0x%02X", name, address)) + f:close() + return ret, err +end + +function i2c_delete_device(bus, address) + local f, err = io.open(i2c_bus_path(bus) .. "/delete_device", "w") + if not f then return nil, err end + + ret, err = f:write(string.format("0x%02X", address)) + f:close() + return ret, err +end + +function i2c_get_mux_channels(parent, address) + local channels = {} + local channel_paths, glob_result = glob.glob(i2c_device_path(parent, address) .. "/channel-*", 0) + if not channel_paths then + if glob_result == glob.GLOB_NOMATCH then return channels end + if glob_result == glob.GLOB_ABORTED then return nil, "glob(): aborted" end + if glob_result == glob.GLOB_NOSPACE then return nil, "glob(): no space" end + return nil, "glob(): unknown error" + end + for _, channel_path in pairs(channel_paths) do + local channel = string.match(channel_path, '/channel%-(%d+)$') + if not channel then return nil, "unable to parse channel number from path: " .. channel_path end + + local bus_path, err = unistd.readlink(channel_path) + if not bus_path then return nil, err end + local bus = string.match(bus_path, '/i2c%-(%d+)$') + if not bus then return nil, "unable to parse bus number from path: " .. bus_path end + + channels[channel] = bus + end + return channels +end + +-- ############# main ########### + +-- assert(i2c_delete_device(0, 0x70)) +-- time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) +assert(i2c_add_device(0, "pca9548", 0x70)) +local mux_channels, err = i2c_get_mux_channels(0, 0x70) +if not mux_channels then error(err) end +for ch, bus in pairs(mux_channels) do + print(string.format("mux channel %d -> %s", ch, i2c_bus_path(bus))) +end + +-- i2c_add_device(0, "ds2482", 0x18) +-- i2c_add_device(0, "ads1115", 0x48) +-- i2c_add_device(0, "bme280", 0x76) +-- i2c_add_device(0, "bmp280", 0x77) +-- i2c_add_device(0, "am2315", 0x5c) +-- i2c_add_device(0, "mcp3221", 0x4d) diff --git a/inventory/group_vars/chaos-at-home-sensors/vars.yml b/inventory/group_vars/chaos-at-home-sensors/vars.yml index 4086c66a..2eea9b2c 100644 --- a/inventory/group_vars/chaos-at-home-sensors/vars.yml +++ b/inventory/group_vars/chaos-at-home-sensors/vars.yml @@ -44,6 +44,8 @@ openwrt_packages_add: - usbutils - i2c-tools - iio-utils + - luaposix + - lua-cjson - prometheus-node-exporter-lua - prometheus-node-exporter-lua-netstat - prometheus-node-exporter-lua-openwrt diff --git a/inventory/host_vars/ch-sensors1.yml b/inventory/host_vars/ch-sensors1.yml index 1fab589f..52617de0 100644 --- a/inventory/host_vars/ch-sensors1.yml +++ b/inventory/host_vars/ch-sensors1.yml @@ -1,2 +1,25 @@ --- -sensornode_network_wired: yes +#sensornode_network_wired: yes + +senosrnode_sensors: + i2c: + - bus: 0 + devices: + - address: 0x70 + driver: pca9548 + channels: + - channel: 0 + devices: + - name: foo + address: 0x76 + driver: bme280 + - channel: 1 + devices: + - name: bar + address: 0x77 + driver: bmp280 + - channel: 2 + devices: + - address: 0x18 + driver: ds2482 + 1wire: {} |