Changeset - 8df2c24d4336
[Not reviewed]
default
0 1 0
virii - 6 years ago 2018-12-28 17:05:46

MQTT support added
1 file changed with 22 insertions and 4 deletions:
dash.pl
22
4
0 comments (0 inline, 0 general)
dash.pl
Show inline comments
 
@@ -6,21 +6,35 @@ use diagnostics;
 

	
 
use feature qw(:all);
 
use WWW::Curl::Easy;
 
use Net::MQTT::Simple;
 
use JSON::Parse qw(:all);
 
use Amazon::Dash::Button;
 

	
 
my ($response_body, $info, $postfields, $space_status, $new_space_status);
 
my ($status, $site, $user, $pass, $mac, $curl, $json, $dev, $timeout);
 
my ($mqtt, $topic, $message, $mqttuser, $mqttpass, $mqttIP, $mqttonly);
 

	
 

	
 
$status  = "https://spaceapi.c3l.lu/status.json";				# SpaceAPI direct link to status json file
 
$site    = "https://spaceapi.c3l.lu/open";					# SpaceAPI direct link to change the state
 
$user    = "";		                   					# SpaceAPI user
 
$pass    = "";				         				# SpaceAPI pass
 
$mac     = 'de:ad:be:ef:0a:0a';							# MAC of Dash button.
 
$user     = "ledashbutton";                                     # SpaceAPI user
 
$pass     = "sohNgei3raiche2choo5";                             # SpaceAPI pass
 
$mqttIP   = "192.168.1.189";                                    # MQTT Server IP
 
$mqttuser = "dashbutton";                                       # MQTT User
 
$mqttpass = "oosahN0a";                                         # MQTT Pass
 
$mqttonly = "True";                                             # MQTT Only modus active = not changing Space Status by myself
 
$mac      = '78:e1:03:78:4a:0a';                                # MAC of Dash button.
 
$timeout = 60;									# Timeout in seconds.
 

	
 

	
 
$ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'} = 1;                   # Needed for the MQTT Login
 

	
 
$mqtt = Net::MQTT::Simple->new($mqttIP);
 
$mqtt->last_will("availability/dashbutton", "offline", 1);
 
$mqtt->login($mqttuser, $mqttpass);
 
$mqtt->retain("availability/dashbutton", "online");
 

	
 
say "[+] Connection to MQTT server established";
 
say "[+] Scanning network traffic";
 

	
 
Amazon::Dash::Button->new( from => {						# The author of the module says that you
 
@@ -35,8 +49,11 @@ Amazon::Dash::Button->new( from => {				
 
sub spaceapi {
 

	
 
say "[+] Button has been pushed";
 
$mqtt->publish("space/status/trigger" => 1);
 
say "[+] MQTT message sent";
 

	
 
$curl = WWW::Curl::Easy->new;       						# $curl = new WWW::Curl::Easy;
 
if ($mqttonly eq "False") {
 
    $curl = WWW::Curl::Easy->new;                        
 

	
 
$curl->setopt(CURLOPT_URL, $status);
 
$curl->setopt(CURLOPT_VERBOSE,0);
 
@@ -77,6 +94,7 @@ if ($json->{state}{open} and $json->{sta
 
$info = $curl->getinfo(CURLINFO_RESPONSE_CODE);
 
say "[-] Server returned $info" and exit unless $info == 302||200;
 
say "[+] Changed Space Status to $new_space_status";
 
}
 

	
 
}
 

	
0 comments (0 inline, 0 general)