lora-project/gateway/receiver.js
2017-06-01 18:16:31 +08:00

57 lines
1.2 KiB
JavaScript

var SX127x = require('sx127x'); // or require('sx127x')
var sx127x = new SX127x({
frequency: 866e6,
resetPin: 17,
dio0Pin: 4,
syncWord: 0x22,
crc: true
});
var count = 0;
// open the device
sx127x.open(function(err) {
console.log('open', err ? err : 'success');
if (err) {
throw err;
}
// add a event listener for data events
sx127x.on('data', function(data, rssi) {
//console.log('Origin Data: ', data, data.length);
console.log('data:', '\'' + data.toString() + '\'', rssi);
let json = {};
try{
json = JSON.parse(data.toString());
if(json.count % 5 == 0){
setTimeout(function(){
sx127x.write(new Buffer('Count is ' + json.count), err => {
console.log('write data', new Buffer('Count is ' + json.count));
if(err) console.log('send error' , err);
setRX()
});
}, 1000);
}
}catch(e){}
});
setRX();
});
function setRX(){
// enable receive mode
sx127x.receive(function(err) {
console.log('receive', err ? err : 'success');
});
}
process.on('SIGINT', function() {
// close the device
sx127x.close(function(err) {
console.log('close', err ? err : 'success');
process.exit();
});
});