-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevse2mqtt.pl
executable file
·57 lines (48 loc) · 1.54 KB
/
evse2mqtt.pl
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
#!/usr/bin/perl
#
# Copyright (c) 2020 David M Brooke, [email protected]
#
#
use strict;
use warnings;
use XML::LibXML;
use LWP::UserAgent;
use AnyEvent::MQTT;
use AnyEvent::DateTime::Cron;
# File-scope variables
our $mqtt;
sub evse_callback {
my $useragent;
my $response;
my $meterReading;
# Construct the UserAgent object
$useragent = LWP::UserAgent->new( );
# Get the Charge Info summary
$response = $useragent->get( 'http://192.168.0.1/services/chargePointsInterface/chargeInfo.xml' );
if ( $response->is_error ) { print $response->error_as_HTML }
if ( $response->is_success ) {
my $dom = XML::LibXML->load_xml( string => $response->content );
foreach my $node ( $dom->findnodes( '//activeEnergy' ) ) {
my @fields = split( /\./, $node->to_literal( ) );
$meterReading = $fields[0];
if ( defined $meterReading ) {
my $topic = 'raw/evse/plug1/activeEnergy';
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic, message => $meterReading );
}
}
}
}
# Connect to the MQTT Broker
$mqtt = AnyEvent::MQTT->new( host => 'mqtt',
client_id => 'evse2mqtt',
user_name => 'USERNAME',
password => 'PASSWORD',
on_error => sub { print "MQTT Error\n" } );
# Create the cron object
my $cron = AnyEvent::DateTime::Cron->new( );
# Add the cron timer
$cron->add( '*/5 * * * *', \&evse_callback );
# Start cron and run the event loop
my $cv = $cron->start( );
$cv->recv;