MQTT & bluetooth

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

MQTT & bluetooth

H Plato
Just got to give kudos to Neil around the MQTT support. The more I use it the more I like it.  Using a raspberry PI 3 (with bluetooth), it’s almost trivial to create a presence object:

This script running on the RPI:

#!/bin/sh
BTADDR=<BTADDR>
STATUS=away
TIMEOUT_NEAR=60
TIMEOUT_AWAY=60

while true; do
#(device near state - add commands)
    while sudo /usr/bin/l2ping -t 1 -c 1 $BTADDR >/dev/null 2>/dev/null; do
        #echo found status = $STATUS
        if [ "$STATUS" = "away" ]
          then
            STATUS=home
            echo "Apple Watch is now $STATUS"
            /usr/local/bin/mqtt_upload "presence/apple_watch" $STATUS /var/log/monitor_apple_watch.log
        fi
        sleep $TIMEOUT_NEAR
    done
#(device away state - add commands)
   until sudo /usr/bin/l2ping -t 1 -c 1 $BTADDR >/dev/null 2>/dev/null; do

        if [ "$STATUS" = "home" ]
          then
            STATUS=away
            echo "Apple Watch is now $STATUS"
            /usr/local/bin/mqtt_upload "presence/apple_watch" $STATUS /var/log/monitor_apple_watch.log
        fi
        sleep $TIMEOUT_AWAY
    done
done

using this upload script:

#!/usr/bin/perl

#use Data::Dumper;
use Net::MQTT::Simple;

my $attr = $ARGV[0];
my $value = $ARGV[1];
my $file = $ARGV[2];
my %data =();
my $exit = 1;
my $host = “host";

my $mqtt1 = Net::MQTT::Simple->new($host);

unless ($attr) {

        print "Error, Attribute name missing from command line arguments\n";

} else {
      if ($attr and $value) {
         $mqtt1->publish("$attr",$value);
         print "publish \"$attr\" => $value\n";
         $exit = 0;
      }
   unless ($exit) {
      open FILE, '>>', $file or warn "error $!";
      print FILE time . ",$attr,$value\n";
      close FILE;
   }
   print "Mqtt upload complete ($exit)\n"
}
exit $exit;


the the MH usercode:

require mqtt; #noloop
$mqtt1 = new mqtt('mqtt', '127.0.0.1', 1883, 'presence/#', "", "", 121);
       
$presence_apple_watch = new mqtt_Item($mqtt1, "presence/apple_watch");
$presence_apple_watch -> xAP_enable;;
 
if (state_changed $presence_apple_watch) {
        print_log "[mqtt_presence]: apple watch location changed to " . state $presence_apple_watch;
}


using this framework, it’s really simple to add in more bluetooth devices to monitor
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Reply | Threaded
Open this post in threaded view
|

Re: MQTT & bluetooth

Neil Cherry-3
On 01/01/2017 01:44 PM, H Plato wrote:
> Just got to give kudos to Neil around the MQTT support. The more I use it the more I
> like it.  Using a raspberry PI 3 (with bluetooth), it’s almost trivial to create a
> presence object:

If that's me (I don't think I'm the only Neil on the list) then thanks. But I'm not
the only one to blame ^H^H^H^H^^H thank. :-) Lieven helped a lot and fixed several
things. If I ever get a calm schedule I'll add the rest of the mqtt code. I'm
not sure how well it communicates with cloud mqtts.

Having said that, I am abusing it by treating each topic like a device. That allows
me to used a couple of local airport weather stations for the weather. Plus a lot of
other sensors (Air Quality, etc). Now if I can only get the fire reports.

--
Linux Home Automation         Neil Cherry       [hidden email]
http://www.linuxha.com/                         Main site
http://linuxha.blogspot.com/                    My HA Blog
Author of:     Linux Smart Homes For Dummies

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users