I finally got around to continue on my project.
I managed to solder a reed relay to monitor a window to pin 18. For the window monitoring I use the setWatch()-function as recommended on the Espruino pages. Temperature, air pressure and humidity I am currently sending once per minute and the battery temperature as well as the window state (in addition to the setWatch(), just to make sure the status gets updated in case a status change message gets lost). This is the code I use on the Ruuvitag:
var Ruuvitag = require("Ruuvitag");
Ruuvitag.setEnvOn(true);
pinMode(18, 'input_pulldown');
setWatch(function(e) {
console.log("Window closed");
var door = 1;
NRF.setAdvertising({
0x2a3f : [door]
});
}, 18, { repeat: true,debounce:50, edge: 'rising' });
setWatch(function(e) {
console.log("Window open");
var door = 0;
NRF.setAdvertising({
0x2a3f : [door]
});
}, 18, { repeat: true,debounce:50, edge: 'falling' });
setInterval(function(fn){
var temperature = [Math.round(Ruuvitag.getEnvData().temp*100)/100];
var pressure = [Math.round(Ruuvitag.getEnvData().pressure*10)/10];
var humid = [Math.round(Ruuvitag.getEnvData().humidity*10)/10];
NRF.setAdvertising({
0x1809 : [encodeFloat100(temperature)],
0x2A6D : [encodeFloat10(pressure)],
0x2A6F : [encodeFloat10(humid)],
});
},60000);
setInterval(function(fn){
var battery = [Math.round(NRF.getBattery()*100)/100];
var door = digitalRead(18);
NRF.setAdvertising({
0xfffe : [encodeFloat100(battery)],
0x2a3f : [door]
});
},600000);
function encodeFloat10(num) {
var d = Math.round(num*10);
return [ d&255, d >> 8 ];
}
function encodeFloat100(num) {
var d = Math.round(num*100);
return [ d&255, d >> 8 ];
}
The code on EspruinoHub that I posted above works. The problem with my number is that it is signed. So I changed the part for the temperature a little. Here is how the changed part in attribute.js looks now:
exports.names = {
"1809" : "Temperature",
"180a" : "Device Information",
"180f" : "Battery Percentage",
"181c" : "User Data",
"2a6f" : "Humidity",
"2a6d" : "Pressure",
"2aef" : "Door",
"fffe" : "Battery Voltage",
"feaa" : "Eddystone",
"6e400001b5a3f393e0a9e50e24dcca9e" : "nus",
"6e400002b5a3f393e0a9e50e24dcca9e" : "nus_tx",
"6e400003b5a3f393e0a9e50e24dcca9e" : "nus_rx",
};
exports.handlers = {
"2a3f" : function(a) { // Door/Window status
return {
door : a[0]
}
},
"fffe" : function(a) { // battery
return {
batt : (a.length==2) ? (((a[1]<<8)+a[0])/100) : a[0]/100
}
},
"2a6d" : function(a) { // Pressure
return {
pressure : (a.length==2) ? (((a[1]<<8)+a[0])/10) : a[0]/10
}
},
"2a6f" : function(a) { // Humidity
return {
humid : (a.length==2) ? (((a[1]<<8)+a[0])/10) : a[0]/10
}
},
"1809" : function(a) { // Temperature
var temp;
if (a.length==2) {
temp = (((a[1]<<8)+a[0])/100);
if (temp > 327.67){
temp = (Math.round((655.35-temp)*100)/100);
}
}else{
temp = a[0]/10;
if (temp > 127) {
temp = -(255 - temp);
}
}
return {
//temp : (a.length==2) ? (((a[1]<<8)+a[0])/100) : a[0]
temp : temp
}
},
"180f" : function(a) { // Battery percent
return {
battery : a[0]
}
},
"feaa" : function(d) { // Eddystone
if (d[0]==0x10) { // URL
var rssi = d[1];
if (rssi&128) rssi-=256; // signed number
var urlType = d[2];
var URL_TYPES = [
"http://www.",
"https://www.",
"http://",
"https://"];
var url = URL_TYPES[urlType] || "";
for (var i=3;i<d.length;i++)
url += String.fromCharCode(d[i]);
return { url : url, "rssi@1m":rssi };
}
}
};
I just realized that the problem with signed number is the same as for the rssi - there it is solved much more elegantly.
I hope this helps someone. It took me a long time to figure it all out. I am not a developer so the code is a raw hack and everything is still running only as a prototype. But it seems to do what I want.
I am reading the data via mqtt into OpenHAB. There I am using InfluxDB and Grafana to visualize the data but also assign it to variables to trigger events. For example, I have a ventilator in the bathroom that gets started when the humidity is getting too high and also the temperature is controlled via OpenHAB (I have electric heating so this is very easy to do )
Thanks to everyone that shared information, in particular to mrwhale (https://f.ruuvi.com/t/ruuvitag-basic-espruino-and-openhab/335) and the Ruuvi-team who got me looking into the right direction.
42