Last active
October 16, 2017 02:12
-
-
Save yoshidaken1/10bb1c601d242665e6a438b01ab4c348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wx2Beaconの測定データを取得するプログラム | |
// Programed by Kazuyuki Eguchi | |
// Ambientに測定データを定期的に送信するプログラム を追加 | |
// Kenichi Yoshoda | |
var noble = require('noble'); | |
var WX2_SERVICE_UUID = '0c4c3000770046f4aa96d5e974e32a54'; | |
var WX2_NOWDATA_UUID = '0c4c3001770046f4aa96d5e974e32a54'; | |
//Ambient接続インスタンス | |
var ambient = require('ambient-lib'); | |
//Ambiet接続 引数(チャネルId, ライトキー) | |
ambient.connect(nnnn, "xxxxxxxxxxxxxxxx"); | |
//タイムスタンプ用 | |
require('date-utils'); | |
noble.on('stateChange', function(state) { | |
console.log('on -> stateChange: ' + state); | |
if (state === 'poweredOn') { | |
noble.startScanning(); | |
} else { | |
noble.stopScanning(); | |
} | |
}); | |
noble.on('scanStart', function() { | |
console.log('on -> scanStart'); | |
}); | |
noble.on('scanStop', function() { | |
console.log('on -> scanStop'); | |
}); | |
noble.on('discover', function(peripheral) { | |
console.log('on -> discover: ' + peripheral.advertisement.localName); | |
if(peripheral.advertisement.localName == 'Env') | |
{ | |
console.log(peripheral.address); | |
noble.stopScanning(); | |
peripheral.on('connect', function() { | |
console.log('on -> connect'); | |
this.discoverServices(); | |
}); | |
//Ambient送信データ用 | |
var ambidata; | |
peripheral.on('disconnect', function() { | |
console.log('on -> disconnect'); | |
//Ambient送信 | |
ambient.send(ambidata, function(err, res, body) { | |
if (err) { | |
console.log(err); | |
} | |
//時間表示 | |
var now = new Date(); | |
console.log(now.toFormat('DDD MMM DD YYYY HH24:MI:SS')); | |
console.log(res.statusCode); | |
//接続後終了 | |
process.exit(); | |
}); | |
//process.exit(); | |
}); | |
peripheral.on('servicesDiscover', function(services) { | |
for(var i = 0; i < services.length; i++) { | |
// console.log(services[i]['uuid']); | |
if(services[i]['uuid'] == WX2_SERVICE_UUID) | |
{ | |
services[i].on('includedServicesDiscover', function(includedServiceUuids) { | |
this.discoverCharacteristics(); | |
}); | |
services[i].on('characteristicsDiscover', function(characteristics) { | |
for(var j = 0; j < characteristics.length; j++) { | |
// console.log(characteristics[j].uuid); | |
if(characteristics[j].uuid == WX2_NOWDATA_UUID) | |
{ | |
var WX2_NOWDATA = characteristics[j]; | |
WX2_NOWDATA.read(function(error, data) { | |
if (data) { | |
// 温度 | |
var temp = ((data[2] & 0xff) << 8) + (data[1] & 0xff); | |
temp = temp * 0.01; | |
// 湿度 | |
var hum = ((data[4] & 0xff) << 8) + (data[3] & 0xff); | |
hum = hum * 0.01; | |
// 照度 | |
var lum = ((data[6] & 0xff) << 8) + (data[5] & 0xff); | |
// UVインデックス | |
var uv = ((data[8] & 0xff) << 8) + (data[7] & 0xff); | |
uv = uv * 0.01; | |
// 気圧 | |
var atom = ((data[10] & 0xff) << 8) + (data[9] & 0xff); | |
atom = atom * 0.1; | |
// 騒音 | |
var noise = ((data[12] & 0xff) << 8) + (data[11] & 0xff); | |
noise = noise * 0.01; | |
// 不快指数 | |
var disco = ((data[14] & 0xff) << 8) + (data[13] & 0xff); | |
disco = disco * 0.01; | |
// 熱中症危険度 | |
var heat = ((data[16] & 0xff) << 8) + (data[15] & 0xff); | |
heat = heat * 0.01; | |
// バッテリー電圧 | |
var batt = ((data[18] & 0xff) << 8) + (data[17] & 0xff); | |
batt = batt * 0.001; | |
console.log('No=' + data[0]); | |
console.log('Temp=' + temp + ' ℃'); | |
console.log('Humidity=' + hum + ' %') | |
console.log('Luminosity=' + lum + ' lx'); | |
console.log('UV Index=' + uv); | |
console.log('Atom=' + atom + ' hPa'); | |
console.log('Noise=' + noise + ' dB'); | |
console.log('Discomfort index=' + disco); | |
console.log('Heat=' + heat + ' ℃'); | |
console.log('Battery=' + batt + ' mv'); | |
//Ambient送信d1〜d8のJSONデータ準備 | |
ambidata = { | |
d1: temp, | |
d2: hum, | |
d3: lum, | |
d4: uv, | |
d5: atom, | |
d6: noise, | |
d7: disco, | |
d8: heat | |
}; | |
} | |
peripheral.disconnect(); | |
}); | |
} | |
} | |
}); | |
services[i].discoverIncludedServices(); | |
} | |
} | |
}); | |
peripheral.connect(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment