2019-07-17 08:43:12 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
# Replacement for powerraw.shack pollin box
|
|
|
|
# Requires usb-serial device on host
|
|
|
|
# Requires mqtt available at mqtt.shack
|
|
|
|
# Requires hostname powerraw.shack
|
|
|
|
let
|
2019-07-17 11:55:33 +00:00
|
|
|
influx-url = "http://influx.shack:8086";
|
2019-07-17 08:43:12 +00:00
|
|
|
pkg = pkgs.python3.pkgs.callPackage (
|
2021-09-05 17:51:31 +00:00
|
|
|
pkgs.fetchFromGitHub {
|
|
|
|
owner = "shackspace";
|
|
|
|
repo = "powermeter";
|
2020-12-08 20:41:33 +00:00
|
|
|
rev = "438b08f";
|
|
|
|
sha256 = "0c5czmrwlw985b7ia6077mfrvbf2fq51iajb481pgqbywgxqis5m";
|
2019-07-17 08:43:12 +00:00
|
|
|
}) {};
|
|
|
|
in {
|
|
|
|
# receive response from light.shack / standby.shack
|
|
|
|
networking.firewall.allowedUDPPorts = [ 11111 ];
|
2021-09-09 18:40:13 +00:00
|
|
|
networking.firewall.allowedTCPPorts = [ 11111 ];
|
2021-06-05 17:13:25 +00:00
|
|
|
users.users.powermeter = {
|
|
|
|
extraGroups = [ "dialout" ];
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2019-07-17 08:43:12 +00:00
|
|
|
|
2020-12-08 20:41:33 +00:00
|
|
|
# we make sure that usb-ttl has the correct permissions
|
|
|
|
# creates /dev/powerraw
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="powerraw", MODE="0660", GROUP="dialout"
|
|
|
|
'';
|
2019-07-17 08:43:12 +00:00
|
|
|
systemd.services.powermeter-serial2mqtt = {
|
|
|
|
description = "powerraw Serial -> mqtt";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
User = "powermeter";
|
2020-12-08 20:41:33 +00:00
|
|
|
ExecStart = "${pkg}/bin/powermeter-serial2mqtt /dev/powerraw";
|
2019-07-17 08:43:12 +00:00
|
|
|
PrivateTmp = true;
|
2019-09-26 19:46:14 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "15";
|
2019-07-17 08:43:12 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.powermeter-mqtt2socket = {
|
|
|
|
description = "powerraw mqtt -> raw socket 11111";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
User = "powermeter";
|
|
|
|
ExecStart = "${pkg}/bin/powermeter-mqtt2socket";
|
|
|
|
PrivateTmp = true;
|
2019-09-26 19:46:14 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "15";
|
2019-07-17 08:43:12 +00:00
|
|
|
};
|
|
|
|
};
|
2019-07-17 11:55:33 +00:00
|
|
|
|
|
|
|
services.telegraf = {
|
|
|
|
enable = true;
|
|
|
|
extraConfig = {
|
|
|
|
agent.debug = false;
|
|
|
|
outputs = {
|
|
|
|
influxdb = [{
|
|
|
|
urls = [ influx-url ];
|
|
|
|
database = "telegraf";
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.telegraf.extraConfig.inputs.mqtt_consumer = let
|
|
|
|
genTopic = name: topic: tags: {
|
|
|
|
servers = [ "tcp://mqtt.shack:1883" ];
|
|
|
|
qos = 0;
|
|
|
|
connection_timeout = "30s";
|
|
|
|
topics = [ topic ];
|
|
|
|
inherit tags;
|
|
|
|
persistent_session = false;
|
|
|
|
name_override = name;
|
|
|
|
data_format = "value";
|
|
|
|
data_type = "float";
|
|
|
|
};
|
|
|
|
sensor = "total";
|
|
|
|
types = [ "Voltage" "Current" "Power" ];
|
2020-12-01 22:20:05 +00:00
|
|
|
phases = [ "1" "2" "3" ];
|
2019-07-17 11:55:33 +00:00
|
|
|
in
|
|
|
|
[ (genTopic "Power consumed" "/power/${sensor}/consumed" { inherit sensor; }) ] ++
|
|
|
|
(lib.flatten (map (type: (map (phase: (genTopic "Power" "/power/${sensor}/L${toString phase}/${type}" { inherit sensor phase type; }) ) phases)) types));
|
2019-07-17 08:43:12 +00:00
|
|
|
}
|