summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/shack/powerraw.nix
blob: 79ba567b6353a2ac2a70818bc92e8780d0e09c07 (plain)
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
{ 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
  influx-url = "http://influx.shack:8086";
  pkg = pkgs.python3.pkgs.callPackage (
    pkgs.fetchFromGitHub {
      owner = "shackspace";
      repo = "powermeter";
      rev = "438b08f";
      sha256 = "0c5czmrwlw985b7ia6077mfrvbf2fq51iajb481pgqbywgxqis5m";
    }) {};
in {
  # receive response from light.shack / standby.shack
  networking.firewall.allowedUDPPorts = [ 11111 ];
  networking.firewall.allowedTCPPorts = [ 11111 ];
  users.users.powermeter = {
    extraGroups = [ "dialout" ];
    isSystemUser = true;
    group = "powermeter";
  };
  users.groups.powermeter = {};

  # 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"
  '';
  systemd.services.powermeter-serial2mqtt = {
    description = "powerraw Serial -> mqtt";
    wantedBy = [ "multi-user.target" ];
    serviceConfig = {
      User = "powermeter";
      ExecStart = "${pkg}/bin/powermeter-serial2mqtt /dev/powerraw";
      PrivateTmp = true;
      Restart = "always";
      RestartSec = "15";
    };
  };

  systemd.services.powermeter-mqtt2socket = {
    description = "powerraw mqtt -> raw socket 11111";
    wantedBy = [ "multi-user.target" ];
    serviceConfig = {
      User = "powermeter";
      ExecStart = "${pkg}/bin/powermeter-mqtt2socket";
      PrivateTmp = true;
      Restart = "always";
      RestartSec = "15";
    };
  };

  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" ];
    phases = [ "1" "2" "3" ];
  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));
}