From 4fb40fc1d6f35d7dcec5ae1cd6f04a7d2ccbe1bf Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 30 May 2017 19:34:32 +0800 Subject: [PATCH] first --- .gitignore | 1 + arduino/LoRaSender.ino | 107 +++++++++++++++++++++++++++++++++++++++ nodejs/package-lock.json | 47 +++++++++++++++++ nodejs/package.json | 15 ++++++ nodejs/receiver.js | 56 ++++++++++++++++++++ 5 files changed, 226 insertions(+) create mode 100644 .gitignore create mode 100644 arduino/LoRaSender.ino create mode 100644 nodejs/package-lock.json create mode 100644 nodejs/package.json create mode 100644 nodejs/receiver.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/arduino/LoRaSender.ino b/arduino/LoRaSender.ino new file mode 100644 index 0000000..38d04f8 --- /dev/null +++ b/arduino/LoRaSender.ino @@ -0,0 +1,107 @@ +#include +#include +#include +#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); + } +} + diff --git a/nodejs/package-lock.json b/nodejs/package-lock.json new file mode 100644 index 0000000..7aec563 --- /dev/null +++ b/nodejs/package-lock.json @@ -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=" + } + } +} diff --git a/nodejs/package.json b/nodejs/package.json new file mode 100644 index 0000000..8abfda7 --- /dev/null +++ b/nodejs/package.json @@ -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" + } +} diff --git a/nodejs/receiver.js b/nodejs/receiver.js new file mode 100644 index 0000000..cdd4580 --- /dev/null +++ b/nodejs/receiver.js @@ -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(); + }); +});