Added LoRaWAN auto-reconnect

This commit is contained in:
Filip Znachor 2022-12-14 15:18:41 +01:00
parent 4061c62fd5
commit 10089fce6d

View file

@ -9,17 +9,17 @@
M5_LoRaWAN LoRaWAN; M5_LoRaWAN LoRaWAN;
bool connected = false;
int lastMessage = 0;
void setup() { void setupLoRaWAN() {
M5.begin(); connected = false;
LoRaWAN.Init(&Serial2, 13, 14); LoRaWAN.Init(&Serial2, 13, 14);
delay(100); delay(100);
M5.Lcd.begin(); M5.Lcd.clear();
M5.Lcd.setTextSize(1); Serial.println("Connecting...");
M5.Lcd.setFreeFont(&Ubuntu_24px); M5.Lcd.drawString("Connecting...", 0, 0);
M5.Lcd.setTextPadding(0);
M5.Lcd.drawString("Connecting to LoRaWAN...", 0, 0);
while (!LoRaWAN.checkDeviceConnect()); while (!LoRaWAN.checkDeviceConnect());
LoRaWAN.writeCMD("AT?\r\n"); LoRaWAN.writeCMD("AT?\r\n");
delay(100); delay(100);
@ -62,11 +62,25 @@ void setup() {
if(recvMsg == "+CJOIN:OK") { if(recvMsg == "+CJOIN:OK") {
Serial.println("Connected!"); Serial.println("Connected!");
M5.Lcd.drawString("Connected!", 0, 30); M5.Lcd.drawString("Connected!", 0, 30);
delay(500); delay(500);
connected = true;
lastMessage = time(NULL);
break; break;
} }
} }
}
void setup() {
M5.begin();
M5.Lcd.begin();
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&Ubuntu_24px);
M5.Lcd.setTextPadding(0);
setupLoRaWAN();
xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, 1, NULL, 1); xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, 1, NULL, 1);
} }
@ -137,7 +151,7 @@ int departureIndex = 0;
// Global variables // Global variables
time_t seconds; time_t seconds;
int last_update = 0; int lastUpdate = 0;
int unixtime = 1; int unixtime = 1;
@ -166,6 +180,7 @@ void receiveMsg(int wait) {
String recvMsg = LoRaWAN.waitMsg(wait); String recvMsg = LoRaWAN.waitMsg(wait);
recvMsg.trim(); recvMsg.trim();
if(recvMsg.length() != 0 && recvMsg.substring(0, 8) == "OK+RECV:") { if(recvMsg.length() != 0 && recvMsg.substring(0, 8) == "OK+RECV:") {
lastMessage = seconds;
String decodedMsg = decodeMsg(split(recvMsg, ',', 3)); String decodedMsg = decodeMsg(split(recvMsg, ',', 3));
Serial.println(decodedMsg); Serial.println(decodedMsg);
if(decodedMsg.substring(0,5) == "CLEAR") { if(decodedMsg.substring(0,5) == "CLEAR") {
@ -347,8 +362,11 @@ void addDeparture(String s) {
void loop() { void loop() {
seconds = time(NULL); seconds = time(NULL);
if(seconds / 6 > last_update) { if(seconds - lastMessage > 60) {
last_update++; setupLoRaWAN();
}
if(seconds / 6 > lastUpdate) {
lastUpdate++;
sortDepartures(); sortDepartures();
displayDepartures(); displayDepartures();
displayTimeDate(); displayTimeDate();
@ -358,8 +376,9 @@ void loop() {
// Additional task for receiving LoRaWAN messages // Additional task for receiving LoRaWAN messages
void task1(void* pvParameters) { void task1(void* params) {
while(true) { while(true) {
receiveMsg(1); if(connected) receiveMsg(1);
else delay(1000);
} }
} }