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