Slack Notification with BLE

This program will notify on Slack when you go out and return home.

An obniz is monitoring BLE advertising from your smartphone apps. If the obniz detect a BLE advertising, a message “In Home Now” will be sent on Slack. If it comes not to detect a BLE advertising, a message “Leaving” will be sent.

Materials

  • obniz x1
  • power supply for obniz x1
  • Slack account x1
  • smartphone app which BLE advertising available x1

Steps

At First, you get Webhook URL to send messages on Slack. Please get it from here.

Choose a channel messages will be sent and click “Add Incoming WebHooks integration.”

Replace the “WEBHOOK_URL_HERE“(line50) with displayed Webhook URL. Additionally, replace the “CHANNEL_NAME_HERE” with the channel name you chose. Remember to write a channel name with “#.”

var url = 'WEBHOOK_URL_HERE';
var data = {
  "channel": 'CHANNEL_NAME_HERE',
  "username": 'obniz-bot',
  "text": msg
};

Second, download a smartphone app which emits BLE advertisement such as LightBlue Explorer (→for iOS, →for Android).

Finally, connect an obniz to a power and emit BLE advertisement from your smartphone app.

If you execute the program below, you can get notification on Slack when you go out and return home.

Program

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://unpkg.com/obniz@2.0.2/obniz.js"></script>
</head>
<body>
<div id="obniz-debug"></div>
<button id=startbutton >start</button>
<button id=stopbutton >stop</button>
<h1><div id=results >Leaving</div></h1>
<script>
var status = "undefined";
var scanTime = 1;
var obniz = new Obniz("OBNIZ_ID_HERE");
 obniz.onconnect = async function () {
    obniz.ble.scan.onfinish = ((results) => {
        var find = "Leaving";
        for (key in results){
          var beacon = results[key].iBeacon();
          if (beacon){
            if (beacon.uuid === "UUID_HERE"){
              find = "In home now";
            }
        }
     }
     
     $("#results").html(find);
     
     if(status === "undefined" || status !== find){
        var t = new Date();
        sendToSlack("" +  t.getHours() + ":" +( "00" + t.getMinutes()).slice(-2) + ":" + ("00"+t.getSeconds()).slice(-2) + "  " + find);
     }
     status = find;
     obniz.ble.scan.start({duration:scanTime});
   })
   
   $("#startbutton").click(()=>{
     $("#results").html("");
     obniz.ble.scan.start({duration:scanTime});
   });
   
   $("#stopbutton").click(()=>{
     obniz.ble.scan.end();
   });
   
 function sendToSlack(msg){
    var url = 'WEBHOOK_URL_HERE';
    var data = {
      "channel": 'CHANNEL_NAME_HERE',
      "username": 'obniz-bot',
      "text": msg
    };
	  
	var dataJson = JSON.stringify(data);
	  
    $.ajax({
      type: 'POST',
	  dataType: 'json',
      url: url,
      processData: false,
	  data: 'payload=' + dataJson
    }).then(function(data){}, function(XMLHttpRequest, textStatus, errorThrown){
		console.log("post to bot");
	});
   }
 }
</script>
</body>
</html>
 

 

 

Recommended