forked from openenergymonitor/EmonGLCD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemonglcd-send.pl
executable file
·166 lines (109 loc) · 3.83 KB
/
emonglcd-send.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl -w
# Send data to emonGLCD wireless power display device
# Subscribes to MQTT topic from emonhub, listens for updates to
# the utility power and solar readings, and then sends them via
# emonhub/RF to emonGLCD units
use Net::MQTT::Simple;
use Net::MQTT::Simple::Auth;
use POSIX 'strftime';
use Config::Simple;
use File::Basename qw(dirname);
use Cwd qw(abs_path);
use lib dirname(dirname abs_path $0) . '/libs/perl';
my $mqtt ;
my $nodeId = 0 ;
my $mqttMsg = "0" ;
my %mqttData = () ;
my $utilityW = 0 ;
my $solarW = 0 ;
my $utilityKwh = 0 ;
my $solarKwh = 0 ;
my $lastSend = 0 ; #when we last sent to emonglcd
my $sendInterval = 30 ; #only send max every $sendInterval secs
my $cfg = new Config::Simple('/home/pi/EmonGLCD/emonglcd-send.cfg') ;
#MQTT params
my $baseTopic = $cfg->param('mqtt_pub_topic') ;
my $debug = $cfg->param('debug') ;
sub sendToEmonglcd {
my ($solarW, $utilityW, $solarKwh, $utilityKwh) = @_ ;
my $mqttValues = "0" ;
#Send the time
my $hour = strftime('%H', localtime) ;
my $min = strftime('%M', localtime) ;
my $sec = strftime('%S', localtime) ;
my $day = strftime('%d', localtime) ;
my $month = strftime('%m', localtime) ;
my $year = strftime('%y', localtime) ;
my $type = "10";
$mqttValues= "$type," . "$hour," . "$min," . "$sec," . "$day," . "$month," . "$year," . "$utilityW," . "$solarW," . "$utilityKwh," . "$solarKwh" ;
if ($debug) {
print "sendToemonglcd: Creating MQTT payload for emonglcd : $mqttValues \n";
}
return $mqttValues ;
}
sub mqttSend {
my ($mqtt, $topic, %mqdata) = @_ ;
foreach my $key (keys %mqdata) {
$$mqtt->publish("$topic/$key" => $mqdata{$key});
}
}
sub msgRecv {
my ($topic, $msg) = @_ ;
my $sendNow = 0 ;
my @nodeIds = ();
if ($debug) {
print "MQTT: topic $topic sent payload ($msg) \n" ;
}
if ($topic eq $cfg->param('mqtt_sub_utilityW') ) {
$utilityW = $msg * 1000;
$sendNow = 0;
} elsif ($topic eq $cfg->param('mqtt_sub_utilityKwh') ) {
#multiply by 100 so we only send ints
$utilityKwh = $msg * 100 ;
$sendNow = 0;
} elsif ($topic eq $cfg->param('mqtt_sub_solarW') ) {
$solarW = $msg ;
$sendNow = 0;
} elsif ($topic eq $cfg->param('mqtt_sub_solarKwh') ) {
#multiply by 100 so we only send ints
$solarKwh = $msg * 100 ;
$sendNow = 0;
}
if ( time() - $sendInterval > $lastSend || $sendNow) {
# Set MQTT topic to emonhub/tx/<nodeid>/values for TX by emonpi
foreach my $emonglcdNodeId ($cfg->param('emonglcd_nodeid')) {
# build the mqtt payload
$mqttData{msg} = sendToEmonglcd($solarW,$utilityW,$solarKwh,$utilityKwh) ;
$topic = $baseTopic . "/" . $emonglcdNodeId ."/values" ;
mqttSend(\$mqtt, $topic, %mqttData) ;
$lastSend = time() ;
sleep(1) ;
if ($debug) {
print "msgRecv $mqttData{msg} sent to emonglcd node $emonglcdNodeId \n" ;
}
}
}
return ;
}
#########################################################################################################################
# Main #
#########################################################################################################################
if ($debug) {
print "Emonglcd updater started. \n" ;
print "Connecting to MQTT broker..\n" ;
}
# MQTT Connect
$mqtt = Net::MQTT::Simple::Auth->new($cfg->param('mqtt_hostname'), $cfg->param('mqtt_user'), $cfg->param('mqtt_pass') );
if ( $mqtt ) {
if ($debug) {
print ("MQTT connected to broker " . $cfg->param('mqtt_hostname') . "\n") ;
}
$mqtt->subscribe($cfg->param('mqtt_sub_solarW'), \&msgRecv) ;
$mqtt->subscribe($cfg->param('mqtt_sub_utilityW'), \&msgRecv) ;
$mqtt->subscribe($cfg->param('mqtt_sub_solarKwh'), \&msgRecv) ;
$mqtt->subscribe($cfg->param('mqtt_sub_utilityKwh'), \&msgRecv) ;
$mqtt->run() ;
} else {
print ("MQTT ERROR could not connect to broker " . $cfg->param('mqtt_hostname') . "\n") ;
}
exit () ;