Hi. I’ve been hacking around with some python code to create a module that can pick up a known device (via MAC address) and if detected push up a message. Having no Python experience it’s been trial and error. It’s working but only just! Two problems are that when it’s publishing the result it appears to be flicking between the MAC detected message and MAC not detected message. Also when Python is calling the ‘import bluetooth’ line it appears to be conflicting with the Pi’s WIFI (as speeds drop to next to nothing). I’m running a Pi3. Appreciate both of these are not core MagicMirror issues - but just throwing them here if of interest to anyone, or someone can help.
Here’s the module js (athomecheck.js)
Module.register("athomecheck",{
defaults: {
prependString: 'HOME: '
},
start: function() {
this.athome = 'fetching ...';
this.sendSocketNotification('CONNECT');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'ATHOMECHECK') {
this.athome = payload;
this.updateDom();
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
wrapper.innerHTML = this.config.prependString + this.athome;
return wrapper;
}
});
Here’s the node_helper.js
var NodeHelper = require("node_helper");
var PythonShell = require('python-shell');
module.exports = NodeHelper.create({
start: function function_name () {
var self = this;
setInterval(function() {
self.sendUpdate();
}, 1000);
},
sendUpdate: function() {
const self = this;
const pyshell = new PythonShell('detect.py', { mode: 'text', scriptPath: 'modules/athomecheck'});
pyshell.on('message', function(message){
console.log(message);
self.sendSocketNotification('ATHOMECHECK', message);
});
}
});
and the python script detect.py
#!/usr/bin/python
import bluetooth
import time
import sys
outputa = "x"
outputb = "y"
binder = ""
while True:
result = bluetooth.lookup_name('EX:AM:PL:E1:MA:C2', timeout=5)
if (result != None):
outputa = "Hello Mr SMITH"
else:
outputa = ""
result2 = bluetooth.lookup_name('EX:AM:PL:E1:MA:C3', timeout=5)
if(result2 != None):
outputb = "Hello Mrs SMITH"
else:
outputb = ""
if(result != None and result2 != None):
binder = ", "
print outputa + binder + outputb
break
Thanks