Skip to content

Instantly share code, notes, and snippets.

@3ng1n33r
Created July 3, 2018 17:05
Show Gist options
  • Save 3ng1n33r/b59a0ac2c26e9a5603dc4344f6e48110 to your computer and use it in GitHub Desktop.
Save 3ng1n33r/b59a0ac2c26e9a5603dc4344f6e48110 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.
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 52430,
"pin": "031-45-154"
},
"description": "Config file",
"accessories": [
{
"accessory": "Withings",
"name": "Scales",
"email": "",
"password": "",
"device_id":"",
"user_id":""
}
]
}
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.user_id = config.user_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.health.nokia.com/connectionwou/account_login?r=https%3A%2F%2Fdashboard.health.nokia.com%2F';
var measureUrl = 'https://scalews.health.nokia.com/cgi-bin/v2/measure';
var j = request.jar();
var withings = this; // Required to ensure that this can be accessed within callbacks.
var enddate = Math.round(new Date().getTime() / 1000); // time now
var startdate = enddate - (7 * 24 * 3600); // week ago
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('User ID: '+ withings.user_id);
withings.log('Attempting to retrieve temperature...');
var headersMeasure = {
'Origin': 'https://dashboard.health.nokia.com',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Content-Type': 'text/plain;charset=UTF-8',
'Accept': '*/*',
'Referer': 'https://dashboard.health.nokia.com/',
'Connection': 'keep-alive',
'DNT': '1',
'Cookie': 'session_key='+sessionKey
};
var dataMeasure = 'action=getmeashf&meastype=35%2C12&deviceid='+ withings.device_id +'&userid='+ withings.user_id +'&startdate='+ startdate +'&enddate='+ enddate +'&appname=hmw&apppfm=web&appliver=92d98b4';
request({
url: measureUrl,
method: 'POST',
gzip: true,
headers: headersMeasure,
body: dataMeasure
}, 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[1].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];
}
};
@klebermagno
Copy link

How can I get device_id ?

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