-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProMini-LoRa-Wilddetector.ino
248 lines (204 loc) · 6.62 KB
/
ProMini-LoRa-Wilddetector.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
LoRa Simple Gateway/Node Exemple
This code uses InvertIQ function to create a simple Gateway/Node logic.
Gateway - Sends messages with enableInvertIQ()
- Receives messages with disableInvertIQ()
Node - Sends messages with disableInvertIQ()
- Receives messages with enableInvertIQ()
With this arrangement a Gateway never receive messages from another Gateway
and a Node never receive message from another Node.
Only Gateway to Node and vice versa.
This code receives messages and sends a message every second.
InvertIQ function basically invert the LoRa I and Q signals.
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
for more on InvertIQ register 0x33.
created 05 August 2018
by Luiz H. Cassettari
*/
#include <SPI.h> // include libraries
#include <LoRa.h>
#include "LowPower.h"
#define DEBUG
#define ACCEPT_REMOTE_COMMANDS
#define SERIAL_BAUD 57600
#define NODE_ID 3 // NodeId of this LoRa Node
#define MAX_PACKET_SIZE 10
#define MSG_ID_NODE_STARTUP 1 // Node startup notification
#define MSG_ID_STILL_ALIVE 2 // Node still alive
#define MSG_ID_CMND_REQUEST 3 // Node wakeup/cmnd request
#define MSG_ID_PIR_MOVEMENT 4 // PIR detected movement
#define SEND_CMND_REQUEST_MSG_EVERY 228 // 0.5 hour
#define SEND_STILL_ALIVE_MSG_EVERY 1824 // 4 hours
//#define SEND_MSG_EVERY 22 // Watchdog is a timerTick on a avg 8,0 sec timebase
// SEND_MSG_EVERY=8 -> +- 1min
// SEND_MSG_EVERY=14 -> +- 2min
// SEND_MSG_EVERY=23 -> +- 3min
// SEND_MSG_EVERY=30 -> +- 4min
// SEND_MSG_EVERY=38 -> +- 5min
volatile word sendMsgTimer = SEND_CMND_REQUEST_MSG_EVERY - 1;
volatile bool movementDetected = false;
//Message max 30 bytes
struct Payload {
byte nodeId;
byte msg [MAX_PACKET_SIZE - 1];
} txPayload;
const long loRaFrequency = 866E6; // LoRa loRaFrequency
const int loRaCsPin = 15; // LoRa radio chip select
const int loRaResetPin = 14; // LoRa radio reset
const int loRaIrqPin = 2; // change for your board; must be a hardware interrupt pin
const byte pirIrqPin = 3;
const byte pirEnablePin = 4;
volatile byte state = LOW;
volatile boolean pirEnable = true;
void movementDetectedInt() {
movementDetected = true;
#ifdef DEBUG
Serial.println("Movement detected");
#endif
}
void LoRa_rxMode(){
LoRa.enableInvertIQ(); // active invert I and Q signals
LoRa.receive(); // set receive mode
}
void LoRa_txMode(){
LoRa.idle(); // set standby mode
LoRa.disableInvertIQ(); // normal mode
}
void LoRa_sendMessage(Payload payload, byte payloadLen) {
LoRa_txMode(); // set tx mode
LoRa.beginPacket(); // start packet
LoRa.write((byte*) &payload, payloadLen); // add payload
LoRa.endPacket(true); // finish packet and send it
}
void onReceive(int packetSize) {
byte rxPayload [MAX_PACKET_SIZE];
byte i = 0, rxByte;
while (LoRa.available()) {
rxByte = (byte)LoRa.read();
if (i < MAX_PACKET_SIZE) {
rxPayload[i] = rxByte;
i++;
}
}
// Only accept messages with our NodeId
if (rxPayload[0] == NODE_ID) {
#ifdef DEBUG
Serial.print("Rx packet OK "); // Start received message
for (char i = 0; i < packetSize; i++) {
Serial.print(rxPayload[i], DEC);
Serial.print(' ');
}
#endif
if (rxPayload[1] == MSG_ID_CMND_REQUEST) {
if (rxPayload[2] == 0) {
#ifdef DEBUG
Serial.println("WildDetector=0");
#endif
pirEnable = false;
} else {
#ifdef DEBUG
Serial.println("WildDetector=1");
#endif
pirEnable = true;
}
digitalWrite(pirEnablePin, pirEnable);
}
#ifdef DEBUG
delay(100); // Time to output the serial data when debugging
Serial.println();
#endif
}
}
void onTxDone() {
// Serial.println("TxDone");
LoRa_rxMode();
}
void sendMovementDetectionMsg() {
txPayload.nodeId = NODE_ID;
txPayload.msg[0] = MSG_ID_PIR_MOVEMENT;
LoRa_sendMessage(txPayload, 2); // send a message
}
void setup() {
#ifdef DEBUG
Serial.begin(SERIAL_BAUD); // initialize serial
while (!Serial);
Serial.println();
Serial.print("[LORA-NODE.");
Serial.print(NODE_ID);
Serial.println("]");
#endif
LoRa.setPins(loRaCsPin, loRaResetPin, loRaIrqPin);
if (!LoRa.begin(loRaFrequency)) {
#ifdef DEBUG
Serial.println("LoRa init failed. Check your connections.");
#endif
while (true); // if failed, do nothing
}
//LoRa.setTxPower(20);
LoRa.enableCrc();
LoRa.onReceive(onReceive);
LoRa.onTxDone(onTxDone);
LoRa_rxMode();
// Send Node startup msg
txPayload.nodeId = NODE_ID;
txPayload.msg[0] = MSG_ID_NODE_STARTUP;
LoRa_sendMessage(txPayload, 2); // send a message
delay(40); // [ms] Give RFM95W time to send the message
// PIR power pin
pinMode(pirEnablePin, OUTPUT);
digitalWrite(pirEnablePin, pirEnable);
// PIR movement sensor external interrupt
pinMode(pirIrqPin, INPUT_PULLUP);
delay(100); // Short delay to suppress a false PIR movement detection
}
void loop() {
attachInterrupt(digitalPinToInterrupt(pirIrqPin), movementDetectedInt, RISING);
// Enter power down state with ADC and BOD module disabled. Wake up when wake up pin is low
//Serial.println("Sleep for 8s....");
//delay(100);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
// Disable external pin interrupt on wake up pin
detachInterrupt(digitalPinToInterrupt(pirIrqPin));
// Waked up! From timer or movement (external interrupt)
if (movementDetected) {
movementDetected = false;
// Waked up from external interrupt
// delay(100); //Litle time to startup serial channel
// Serial.println("Beweging gezien!");
sendMovementDetectionMsg();
delay(40); // [ms] Give RFM95W time to send the message
LoRa.sleep(); // Put RFM95W in sleep mode
#ifndef ACCEPT_REMOTE_COMMANDS
sendMsgTimer = 0; // No need to send still alive message
#endif
} else {
// Waked up by periodic wakeup timer (8s)
sendMsgTimer++;
#ifdef ACCEPT_REMOTE_COMMANDS
if (sendMsgTimer >= SEND_CMND_REQUEST_MSG_EVERY) {
sendMsgTimer = 0;
#ifdef DEBUG
Serial.println("Send Command Request message");
#endif
txPayload.nodeId = NODE_ID;
txPayload.msg[0] = MSG_ID_CMND_REQUEST;
LoRa_sendMessage(txPayload, 2); // send a message
delay(80); // [ms] Wait a while for the server response
LoRa.sleep(); // Put RFM95W in sleep mode
}
#endif
#ifndef ACCEPT_REMOTE_COMMANDS
if (sendMsgTimer >= SEND_STILL_ALIVE_MSG_EVERY) {
sendMsgTimer = 0;
#ifdef DEBUG
Serial.println("Send Still Alive message");
#endif
txPayload.nodeId = NODE_ID;
txPayload.msg[0] = MSG_ID_STILL_ALIVE;
LoRa_sendMessage(txPayload, 2); // send a message
delay(40); // [ms] Give RFM95W time to send the message
LoRa.sleep(); // Put RFM95W in sleep mode
}
#endif
}
}