forked from unprovable/LoRaChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoRaChat.ino
184 lines (168 loc) · 4.96 KB
/
LoRaChat.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
// Includes
//#include <Wire.h>
#include <SSD1306Wire.h> // you need to install the ESP8266 oled driver for SSD1306
// by Daniel Eichorn and Fabrice Weinberg to get this file!
// It's in the arduino library manager :-D
#include <SPI.h>
#include <LoRa.h> // this is the one by Sandeep Mistry,
// (also in the Arduino Library manager :D )
// display descriptor
SSD1306Wire display(0x3c, 4, 15);
// definitions
//SPI defs for LoRa radio
#define SS 33
#define RST -1
#define DI0 32
// #define BAND 429E6
// LoRa Settings
#define BAND 434500000.00
#define spreadingFactor 9
// #define SignalBandwidth 62.5E3
#define SignalBandwidth 31.25E3
#define codingRateDenominator 8
#define preambleLength 8
// we also need the following config data:
// GPIO47 — SX1278’s SCK
// GPIO45 — SX1278’s MISO
// GPIO48 — SX1278’s MOSI
// GPIO21 — SX1278’s CS
// GPIO14 — SX1278’s RESET
// GPIO26 — SX1278’s IRQ(Interrupt Request)
// misc vars
String msg;
String displayName;
String sendMsg;
char chr;
int i = 0;
// Helpers func.s for LoRa
String mac2str(int mac)
{
String s;
byte *arr = (byte*) &mac;
for (byte i = 0; i < 6; ++i)
{
char buff[3];
// yea, this is a sprintf... fite me...
sprintf(buff, "%2X", arr[i]);
s += buff;
if (i < 5) s += ':';
}
return s;
}
// let's set stuff up!
void setup() {
// reset the screen
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
Serial.begin(115200);
while (!Serial); //If just the the basic function, must connect to a computer
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(5, 5, "LoRa Chat Node");
display.display();
SPI.begin(0, 36, 26, 33);
LoRa.setPins(SS, RST, DI0);
Serial.println("LoRa Chat Node");
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.print("LoRa Spreading Factor: ");
Serial.println(spreadingFactor);
LoRa.setSpreadingFactor(spreadingFactor);
Serial.print("LoRa Signal Bandwidth: ");
Serial.println(SignalBandwidth);
LoRa.setSignalBandwidth(SignalBandwidth);
LoRa.setCodingRate4(codingRateDenominator);
LoRa.setPreambleLength(preambleLength);
Serial.println("LoRa Initial OK!");
display.drawString(5, 20, "LoRaChat is running!");
display.display();
delay(2000);
Serial.println("Welcome to LoRaCHAT!");
// get MAC as initial Nick
int MAC = ESP.getEfuseMac();
displayName = mac2str(MAC);
//displayName.trim(); // remove the newline
Serial.print("Initial nick is "); Serial.println(displayName);
Serial.println("Use /? for command help...");
Serial.println(": ");
display.clear();
display.drawString(5, 20, "Nickname set:");
display.drawString(20, 30, displayName);
display.display();
delay(1000);
}
void loop() {
// Receive a message first...
int packetSize = LoRa.parsePacket();
if (packetSize) {
display.clear();
display.drawString(3, 0, "Received Message!");
display.display();
while (LoRa.available()) {
String data = LoRa.readString();
display.drawString(20, 22, data);
display.display();
Serial.println(data);
}
} // once we're done there, we read bytes from Serial
if (Serial.available()) {
chr = Serial.read();
Serial.print(chr); // so the user can see what they're doing :P
if (chr == '\n' || chr == '\r') {
msg += chr; //msg+='\0'; // should maybe terminate my strings properly....
if (msg.startsWith("/")) {
// clean up msg string...
msg.trim(); msg.remove(0, 1);
// process command...
char cmd[1]; msg.substring(0, 1).toCharArray(cmd, 2);
switch (cmd[0]){
case '?':
Serial.println("Supported Commands:");
Serial.println("h - this message...");
Serial.println("n - change Tx nickname...");
Serial.println("d - print Tx nickname...");
break;
case 'n':
displayName = msg.substring(2);
Serial.print("Display name set to: "); Serial.println(displayName);
break;
case 'd':
Serial.print("Your display name is: "); Serial.println(displayName);
break;
default:
Serial.println("command not known... use 'h' for help...");
}
msg = "";
}
else {
// ssshhhhhhh ;)
Serial.print("Me: "); Serial.println(msg);
// assemble message
sendMsg += displayName;
sendMsg += "> ";
sendMsg += msg;
// send message
LoRa.beginPacket();
LoRa.print(sendMsg);
LoRa.endPacket();
display.clear();
display.drawString(1, 0, sendMsg);
display.display();
// clear the strings and start again :D
msg = "";
sendMsg = "";
Serial.print(": ");
}
}
else {
msg += chr;
}
}
}