-
-
Notifications
You must be signed in to change notification settings - Fork 651
/
Copy pathLoRaSimpleNode.ino
135 lines (103 loc) · 3.96 KB
/
LoRaSimpleNode.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
/*
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>
const long frequency = 915E6; // LoRa Frequency
const int csPin = 10; // LoRa radio chip select
const int resetPin = 9; // LoRa radio reset
const int irqPin = 2; // change for your board; must be a hardware interrupt pin
int packetReceived = 0; // keeps track of packetReceived for onReceiveCallback
bool txDone = false; // keeps track of txDone for onTxDone
void setup() {
Serial.begin(9600); // initialize serial
while (!Serial);
LoRa.setPins(csPin, resetPin, irqPin);
if (!LoRa.begin(frequency)) {
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa init succeeded.");
Serial.println();
Serial.println("LoRa Simple Node");
Serial.println("Only receive messages from gateways");
Serial.println("Tx: invertIQ disable");
Serial.println("Rx: invertIQ enable");
Serial.println();
// register the receive callback
// onReceive calls interrupt internally. should run only tiny amount of code
// any heavylifting should handled in the loop
// also, should not contain any timer based delay calls
LoRa.onReceive([](int packetLength) { packetReceived = packetLength; });
// onTxDone calls interrupt internally. should run only tiny amount of code
// any heavylifting should handled in the loop
// also, should not contain any timer based delay calls
LoRa.onTxDone([] { txDone = true; });
LoRa_rxMode();
}
void loop() {
if (txDone) onTxDone();
if (packetReceived) onReceive();
if (runEvery(1000)) { // repeat every 1000 millis
String message = "HeLoRa World! ";
message += "I'm a Node! ";
message += millis();
LoRa_sendMessage(message); // send a message
Serial.println("Send Message!");
}
}
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(String message) {
LoRa_txMode(); // set tx mode
LoRa.beginPacket(); // start packet
LoRa.print(message); // add payload
LoRa.endPacket(true); // finish packet and send it
}
void onReceive() {
String message = "";
while (LoRa.available()) {
message += (char)LoRa.read();
}
Serial.print("Node Receive: ");
Serial.println(message);
packetReceived = 0; // reset packetReceived so that
// onReceive only runs once per packet
}
void onTxDone() {
Serial.println("TxDone");
LoRa_rxMode();
txDone = false; // reset packetReceived so that
// onTxDone only runs once per tx
}
boolean runEvery(unsigned long interval)
{
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
return true;
}
return false;
}