obniz developer team
Published

Hands-Free Slack Notification Using Bluetooth

Detecting bluetooth advertising, a message "Home now" is sent to Slack!

BeginnerFull instructions provided597
Hands-Free Slack Notification Using Bluetooth

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
power supply for obniz
×1

Software apps and online services

Bluetooth advertising app

Story

Read more

Code

Untitled file

HTML
<!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>

Credits

obniz developer team

obniz developer team

80 projects • 32 followers
Development board "obniz" is controlled via the internet.

Comments