Skip to content

Instantly share code, notes, and snippets.

@daveol
Last active January 6, 2019 18:41
Show Gist options
  • Save daveol/b1eb10170d686cb84d3fef18d46e6b8f to your computer and use it in GitHub Desktop.
Save daveol/b1eb10170d686cb84d3fef18d46e6b8f to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Net::MQTT::Simple;
use Net::Graphite;
# Setup mqtt connection
my $mqtt = Net::MQTT::Simple->new('10.42.42.1');
# Setup mqtt connection
my $graphite = Net::Graphite->new(
host => '127.0.0.1',
port => 2003,
trace => 0,
proto => 'tcp',
timeout => 1,
fire_and_forget => 1,
return_connect_error => 0
);
my %topics;
# "Connect" to graphite
$graphite->connect;
# subscribe to sensors with weird topics
$mqtt->subscribe(
"revspace/sensors/+" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
my ($type) = $topic =~ /revspace\/sensors\/(.*)/;
($message) = $message =~ m{(\d+(?:\.\d+)?)};
my $graphite_path = "revgraph.sensor.$type.general.0";
$graphite->send(
path => $graphite_path,
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# subscribe to wifi topics
$mqtt->subscribe(
"revspace/wifi/+/online" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
my ($type) = $topic =~ /revspace\/wifi\/(.*)/;
($message) = $message =~ m{(\d+(?:\.\d+)?)};
my $graphite_path = "revspace.wifi.$type.online";
$graphite->send(
path => $graphite_path,
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# subscribe to doorduino topics
$mqtt->subscribe(
"revspace/doorduino/+" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
my ($type) = $topic =~ m[revspace/doorduino/(.*)];
($message) = $message =~ m{(\d+(?:\.\d+)?)};
my $graphite_path = "revspace.doorduino.$type";
$graphite->send(
path => $graphite_path,
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# CO² clause
$mqtt->subscribe(
"revspace/sensors/co2/+" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
my ($type) = $topic =~ m[revspace/sensors/co2/(.*)];
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
$graphite->send(
path => "revgraph.sensor.co2.$type.0",
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# Netvuil
$mqtt->subscribe(
"revspace/sensors/netvuil/max_min" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
$graphite->send(
path => 'revgraph.sensor.netvuil.max',
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# subscribe to standard sensor topics
$mqtt->subscribe(
"revspace/sensors/+/+/+" => sub {
my ($topic, $message) = @_;
my $since = $topics{$topic} ? ( time() - $topics{$topic} ) : ( time() - 0 );
my ($type, $room, $id) = $topic =~ /revspace\/sensors\/([^\/]+)\/([^\/]+)\/([^\/]+)/;
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
my $graphite_path = "revgraph.sensor.$type.$room.$id";
$graphite->send(
path => $graphite_path,
value => $message,
time => time(),
) unless $since > 1800;
$topics{$topic} = time();
}
);
# subscribe to open/closed space
$mqtt->subscribe(
"revspace/state" => sub {
my ($topic, $message) = @_;
$message = $message eq 'open' ? 1 : 0;
$graphite->send(
path => "revgraph.state",
value => $message,
time => time(),
);
}
);
# revspace fan
$mqtt->subscribe(
"revspace/fan" => sub {
my ($topic, $message) = @_;
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
$graphite->send(
path => "revgraph.fan",
value => $message,
time => time(),
);
}
);
# get AC freqs
$mqtt->subscribe(
"revspace/sensors/ac/frequency" => sub {
my ($topic, $message) = @_;
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
$graphite->send(
path => "revspace.ac.frequency",
value => $message,
time => time(),
);
}
);
# fan actually on or not
$mqtt->subscribe(
"revspace/fan/state" => sub {
my ($topic, $message) = @_;
$message = $message eq 'on' ? 1 : 0;
$graphite->send(
path => "revgraph.fan.state",
value => $message,
time => time(),
);
}
);
# fox his battery
$mqtt->subscribe(
"f0x/samla" => sub {
my ($topic, $message) = @_;
($message) = $message =~ m{(\-?\d+(?:\.\d+)?)};
$graphite->send(
path => "f0x.samla",
value => $message,
time => time(),
);
}
);
# Run this thing
$mqtt->run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment