-
Notifications
You must be signed in to change notification settings - Fork 0
/
489-1-2(로라노드).txt
87 lines (69 loc) · 2.16 KB
/
489-1-2(로라노드).txt
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
#include <SPI.h> // include libraries
#include <LoRa.h>
//920.9Mhz
const long frequency = 9209E5;
void setup() {
Serial.begin(115200); // initialize serial
LoRa.setPins(D8, D1, D2);
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();
LoRa.onReceive(onReceive);
LoRa.onTxDone(onTxDone);
LoRa_rxMode();
}
void loop() {
//node쪽은 1초에 한번 뭔가 전송하려고 한다~
if (runEvery(2000)) { // 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(int packetSize) {
String message = "";
while (LoRa.available()) {
message += (char)LoRa.read();
}
Serial.print("Node Receive: ");
Serial.println(message);
}
void onTxDone() {
Serial.println("TxDone");
LoRa_rxMode();
}
boolean runEvery(unsigned long interval)
{
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
return true;
}
return false;
}