Skip to content

Instantly share code, notes, and snippets.

@frenchesco
Last active March 3, 2019 11:10
Show Gist options
  • Save frenchesco/55b63e00fd84f0a2ab67631f0375e751 to your computer and use it in GitHub Desktop.
Save frenchesco/55b63e00fd84f0a2ab67631f0375e751 to your computer and use it in GitHub Desktop.
Attempt to get temperature from Withings Smart Body Analyser (Now sold as Nokia Body+) to be displayed within Homekit. Ended up giving up as it appears the temperature information is only updated when you step on the scale, which kind of defeats the purpose.
var Service, Characteristic;
var request = require('request');
// var withingsApi = require("withings-api"); // Doesn't support Weather and CO2 levels
var temperatureService;
var url;
var humidity = 0;
var temperature = 0;
const DEF_MIN_TEMPERATURE = -100,
DEF_MAX_TEMPERATURE = 100;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-withings", "Withings", Withings);
};
function Withings(log, config) {
this.log = log;
this.email = config.email;
this.password = config.password;
this.name = config.name;
this.manufacturer = config.manufacturer || "Withings";
this.model = config.model || "Unknown";
this.serial = config.serial || "Unknown";
this.device_id = config.device_id || "";
this.minTemperature = config.min_temp || DEF_MIN_TEMPERATURE;
this.maxTemperature = config.max_temp || DEF_MAX_TEMPERATURE;
}
Withings.prototype = {
getState: function (callback) {
var authUrl = 'https://account.withings.com/connectionuser/account_login?appname=my2&appliver=8b90cb73&r=https%3A%2F%2Fhealthmate.withings.com%2F';
var measureUrl = 'https://healthmate.withings.com/index/service/v2/measure';
var j = request.jar();
var withings = this; // Required to ensure that this can be accessed within callbacks.
withings.log('Authenticating with Withings...');
withings.log('Email: ' + withings.email);
request.post(authUrl,{
form: {
email: withings.email,
password: withings.password
},
jar: j
})
.on('response', function (r) {
var cookies = j.getCookieString(authUrl);
var reg = /session_key=(.*)/;
var sessionKey = cookies.match(reg)[1];
// console.log(sessionKey);
withings.log('Device ID: '+ withings.device_id);
withings.log('Attempting to retrieve temperature...');
request({
method: 'POST',
uri: measureUrl,
formData: {
sessionid: sessionKey,
meastype: '12',
deviceid: withings.device_id,
appname: 'my2',
appliver: '6316ea52',
// appliver: '8b90cb73',
// appliver: '300f785c',
apppfm: 'web',
action: 'getmeashf'
}
}, function(error, response, body) {
withings.log('Response received...');
var temperature = null;
var json = JSON.parse(body);
if (error) {
withings.log('HTTP bad response (' + measureUrl + '): ' + error.message);
}
else if (parseInt(json.status) !== 0) {
withings.log('Body: '+ body);
withings.log('Status: ' + json.status);
try {
throw 'Reponse Unsuccessful (' + measureUrl + '):' + json.status;
} catch (respErr) {
withings.log(respErr.message);
error = respErr;
}
}
else {
try {
temperature = json.body.series[0].data[0].value;
withings.log('Temperature: ' + temperature);
if (temperature < withings.minTemperature || temperature > withings.maxTemperature || isNaN(temperature)) {
throw "Invalid value received";
}
withings.log('HTTP successful response: ' + body);
} catch (parseErr) {
withings.log('Error processing received information: ' + parseErr.message);
error = parseErr;
}
// console.log(json);
}
callback(error, temperature);
});
});
},
getServices: function () {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this))
.setProps({
minValue: this.minTemperature,
maxValue: this.maxTemperature
});
return [this.informationService, this.temperatureService];
}
};
@devilbes
Copy link

devilbes commented Mar 3, 2019

How is install this module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment