This commit is contained in:
Jay 2017-05-30 19:34:32 +08:00
commit 4fb40fc1d6
5 changed files with 226 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

107
arduino/LoRaSender.ino Normal file
View File

@ -0,0 +1,107 @@
#include <ArduinoJson.h>
#include <SPI.h>
#include <LoRa.h>
#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int lightPin = A0;
int counter = 0;
int LEDPin = 4;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(866E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
dht.begin();
LoRa.crc();
LoRa.setSyncWord(0x22);
// register the receive callback
LoRa.onReceive(onReceive);
// put the radio into receive mode
LoRa.receive();
// attachInterrupt(digitalPinToInterrupt(2), lora, HIGH);
pinMode(LEDPin, OUTPUT);
}
void loop() {
StaticJsonBuffer<200> jsonBuf;
JsonObject& json = jsonBuf.createObject();
Serial.print("Sending packet: ");
Serial.println(counter);
float h = dht.readHumidity();
float t = dht.readTemperature();
json["count"] = counter;
json["temperature"] = t;
json["humidity"] = h;
json["light"] = analogRead(lightPin);
// send packet
LoRa.beginPacket();
json.printTo(LoRa);
LoRa.endPacket();
counter++;
LoRa.onReceive(onReceive);
LoRa.receive();
delay(1000);
}
void lora() {
Serial.println("Get Pin High");
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
void onReceive(int packetSize) {
// received a packet
Serial.print("Received packet '");
uint8_t d[packetSize];
// read packet
for (int i = 0; i < packetSize; i++) {
// Serial.print((char)LoRa.read());
d[i] = LoRa.read();
Serial.print(d[i], HEX);
Serial.print(" ");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
if (int(d[packetSize - 1]) % 2 == 0) {
Serial.println("LED on");
digitalWrite(LEDPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(LEDPin, LOW);
}
}

47
nodejs/package-lock.json generated Normal file
View File

@ -0,0 +1,47 @@
{
"name": "nodejs",
"version": "1.0.0",
"lockfileVersion": 1,
"dependencies": {
"async": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/async/-/async-2.4.1.tgz",
"integrity": "sha1-YqVrJ5yYoR0JhwlqAcw+6463u9c="
},
"bindings": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz",
"integrity": "sha1-FK1hE4EtLTfXLme0ystLtyZQXxE="
},
"epoll": {
"version": "0.1.21",
"resolved": "https://registry.npmjs.org/epoll/-/epoll-0.1.21.tgz",
"integrity": "sha1-OCoIzZNOy7g2k+v0JfS2tPYXTZc="
},
"lodash": {
"version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
},
"nan": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.5.1.tgz",
"integrity": "sha1-1bAWkSUzJql6K77p5hxV2NYDUeI="
},
"onoff": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/onoff/-/onoff-1.1.2.tgz",
"integrity": "sha1-3NOdP9VZ2y0N9bzVTYzJ3x13R6I="
},
"spi-device": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/spi-device/-/spi-device-0.2.6.tgz",
"integrity": "sha1-tmosN1gAsqgFQXZUIuFU4Ln8bE8="
},
"sx127x": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/sx127x/-/sx127x-0.0.0.tgz",
"integrity": "sha1-0/Ui6hAv39yUA2DxbxRWKPBabO4="
}
}
}

15
nodejs/package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "receiver.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"sx127x": "0.0.0"
}
}

56
nodejs/receiver.js Normal file
View File

@ -0,0 +1,56 @@
var SX127x = require('../index'); // 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();
});
});