From e38fbd54f8832a665edaf4aace8975f087d74eed Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Mon, 12 Dec 2022 20:47:25 +0100 Subject: [PATCH] Moved LoRaWAN to separated task & improved reliability --- client/client.ino | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/client/client.ino b/client/client.ino index 7efbff5..585cda9 100644 --- a/client/client.ino +++ b/client/client.ino @@ -18,7 +18,7 @@ void setup() { M5.Lcd.begin(); M5.Lcd.setTextSize(1); M5.Lcd.setFreeFont(&Ubuntu_24px); - M5.Lcd.drawString("Connecting to LoraWAN...", 0, 0); + M5.Lcd.drawString("Connecting to LoRaWAN...", 0, 0); while (!LoRaWAN.checkDeviceConnect()); LoRaWAN.writeCMD("AT?\r\n"); delay(100); @@ -29,17 +29,14 @@ void setup() { // Enable Log Information LoRaWAN.writeCMD("AT+CSAVE\r\n"); LoRaWAN.writeCMD("AT+IREBOOT=0\r\n"); - Serial.println("LoraWan Rebooting"); delay(1000); - - Serial.println("LoraWan config"); // Set Join Mode OTAA. LoRaWAN.configOTTA("00bdea85badeedf1", // Device EUI "00bdea85badeed01", // APP EUI "00bdea85badeed0100bdea85badeed01", // APP KEY "2" // Upload Download Mode ); - Serial.println("MESSAGE: " + LoRaWAN.waitMsg(1000)); + LoRaWAN.waitMsg(1000); // Set ClassC mode LoRaWAN.setClass("2"); @@ -68,11 +65,13 @@ void setup() { if(recvMsg == "+CJOIN:OK") { Serial.println("Connected!"); M5.Lcd.drawString("Connected!", 0, 30); - delay(500); + delay(500); break; } } + xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, 1, NULL, 1); + } @@ -167,7 +166,7 @@ String decodeMsg(String hexEncoded) { } void receiveMsg() { - String recvMsg = LoRaWAN.waitMsg(500); + String recvMsg = LoRaWAN.waitMsg(100); recvMsg.trim(); if(recvMsg.length() != 0 && recvMsg.substring(0, 8) == "OK+RECV:") { String decodedMsg = decodeMsg(split(recvMsg, ',', 3)); @@ -294,9 +293,7 @@ void displayDepartures(int lines) { // Main loop with time counting void loop() { - receiveMsg(); seconds = time(NULL); - // TODO: Not sure if time is ticking correctly (check by adding real date & time and keep it running) if(seconds / 6 > last_update) { last_update++; int lines = 7; @@ -304,5 +301,14 @@ void loop() { displayDepartures(lines); displayTimeDate(lines); } - delay(5); + delay(500); +} + +// Additional task for receiving LoRaWAN messages + +void task1(void* pvParameters) { + while(true) { + receiveMsg(); + delay(1); + } }