diff --git a/bundles/stromzaehler/files/stromzaehler b/bundles/stromzaehler/files/stromzaehler new file mode 100644 index 0000000..8766f5b --- /dev/null +++ b/bundles/stromzaehler/files/stromzaehler @@ -0,0 +1,37 @@ +#!/bin/bash + +#set -x + +gpio=$(expr 458 + 3) + +# prepare +if ! [[ -e "/sys/class/gpio/gpio$gpio" ]]; then + echo $gpio > "/sys/class/gpio/export" +fi + +echo in > "/sys/class/gpio/gpio$gpio/direction" + +# loop +previous_state=false +while sleep 0.001; do + state=$(cat "/sys/class/gpio/gpio$gpio/value") + if ! [[ "$state" = "$previous_state" ]] && [[ "$state" = 0 ]] + then + if ! [ -z $previous_impulse ] # skip first iteration + then + delay=$(expr $(date --utc +%s%N) - $previous_impulse) + impulse_per_hour=$(expr 3600000000000 / $delay) + watt=$(expr $impulse_per_hour / $(expr 2000 / 1000)) + echo $watt > /tmp/watt + echo $delay $impulse_per_hour $watt + # curl "https://${influxdb_domain}/ping" ${'\\'} + # -X POST ${'\\'} + # -H "Authentication: Token ${influxdb_token}" ${'\\'} + # -d "bucket=${influxdb_bucket}" ${'\\'} + # -d "ord=${influxdb_org}" ${'\\'} + # -d "data=powermeter,host=${node_name} watts=$watt" + fi + previous_impulse=$(date --utc +%s%N) + fi + previous_state=$state +done diff --git a/bundles/stromzaehler/items.py b/bundles/stromzaehler/items.py new file mode 100644 index 0000000..b861f1e --- /dev/null +++ b/bundles/stromzaehler/items.py @@ -0,0 +1,25 @@ +influxdb_metadata = repo.get_node(node.metadata.get('stromzaehler/influxdb_node')).metadata.get('influxdb') + +files = { + '/opt/stromzaehler': { + 'content_type': 'mako', + 'mode': '550', + 'context': { + 'node_name': node.name, + 'influxdb_domain': influxdb_metadata['hostname'], + 'influxdb_bucket': influxdb_metadata['bucket'], + 'influxdb_org': influxdb_metadata['org'], + 'influxdb_token': influxdb_metadata['admin_token'], + }, + }, +} + +svc_systemd = { + 'stromzaehler': { + 'enabled': False, + 'running': False, + 'needs': [ + 'file:/opt/stromzaehler', + ], + } +} diff --git a/bundles/stromzaehler/metadata.py b/bundles/stromzaehler/metadata.py new file mode 100644 index 0000000..f5ba9a8 --- /dev/null +++ b/bundles/stromzaehler/metadata.py @@ -0,0 +1,23 @@ +defaults = { + 'apt': { + 'packages': { + 'gpiod': {}, + }, + }, + 'systemd': { + 'units': { + 'stromzaehler.service': { + 'Unit': { + 'Description': 'stromzaehler', + 'After': 'network.target', + }, + 'Service': { + 'ExecStart': '/opt/stromzaehler', + }, + 'Install': { + 'WantedBy': {'multi-user.target'}, + }, + }, + }, + }, +} diff --git a/nodes/home.stromzaehler.py b/nodes/home.stromzaehler.py new file mode 100644 index 0000000..e21e725 --- /dev/null +++ b/nodes/home.stromzaehler.py @@ -0,0 +1,24 @@ +{ + 'hostname': '10.0.0.15', + 'groups': [ + 'debian-11', + 'raspberry-pi', + 'monitored', + ], + 'bundles': [ + 'stromzaehler', + ], + 'metadata': { + 'id': 'dd521b8a-dc03-43f5-b29f-068f948ba3b8', + 'network': { + 'internal': { + 'interface': 'eth0', + 'ipv4': '10.0.0.15/24', + 'gateway4': '10.0.0.1', + }, + }, + 'stromzaehler': { + 'influxdb_node': 'home.server', + }, + }, +}