From 71389438d3ddc601d6eb04f37c3267577b368841 Mon Sep 17 00:00:00 2001 From: Florian Date: Sun, 21 Jan 2024 19:28:23 -0600 Subject: [PATCH] [LORA] Fix message without json not being transmitted (#1875) Fix LoRa not processing message that are not JSON Add also a non-JSON-message to the example --- examples/LoraTemperature/README.md | 1 + examples/LoraTemperature/platformio.ini | 2 +- examples/LoraTemperature/src/main.cpp | 13 ++++++++++++- main/ZgatewayLORA.ino | 11 ++++++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/LoraTemperature/README.md b/examples/LoraTemperature/README.md index 9efaa5c..366c372 100644 --- a/examples/LoraTemperature/README.md +++ b/examples/LoraTemperature/README.md @@ -1,5 +1,6 @@ # OpenMQTTGateway LoRa Node Example This repository contains an example of a LoRa node program designed for the ESP32 platform. The program reads the internal temperature of the ESP32, packages the data into a JSON format, and sends it over LoRa. +It also sends a constant raw string simulating a Makerfab Soil sensor payload. ## Features: * Uses an SX12XX LoRa module. diff --git a/examples/LoraTemperature/platformio.ini b/examples/LoraTemperature/platformio.ini index bba2b7f..77aa265 100644 --- a/examples/LoraTemperature/platformio.ini +++ b/examples/LoraTemperature/platformio.ini @@ -27,7 +27,7 @@ lib_deps = monitor_speed = 115200 [env:heltec-wifi-lora-32] ; Heltec ESP32 Board with SSD1306 display -platform = ${com.esp32_platform} +platform = espressif32 board = heltec_wifi_lora_32 framework = arduino lib_deps = diff --git a/examples/LoraTemperature/src/main.cpp b/examples/LoraTemperature/src/main.cpp index f8fb9d6..50bfe14 100644 --- a/examples/LoraTemperature/src/main.cpp +++ b/examples/LoraTemperature/src/main.cpp @@ -90,13 +90,24 @@ void loop() { LoRa.print(msg); LoRa.endPacket(); - Serial.println(String(msg)); + Serial.println(msg); display.drawString(0, 15, String(NodeId)); display.drawString(0, 30, "tempc: " + String(temp) + " C"); display.display(); + delay(5000); + + // Send makerfab soil sensor example packet + LoRa.beginPacket(); + String msg2 = "ID010770 REPLY : SOIL INEDX:5862 H:100.00 T:1.77 ADC:624 BAT:857"; + LoRa.print(msg2); + LoRa.endPacket(); + + Serial.println(msg2); + counter++; + digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(2, LOW); // turn the LED off by making the voltage LOW diff --git a/main/ZgatewayLORA.ino b/main/ZgatewayLORA.ino index 0edacdb..a88c3e3 100644 --- a/main/ZgatewayLORA.ino +++ b/main/ZgatewayLORA.ino @@ -428,9 +428,13 @@ void LORAtoMQTT() { } else { // ascii payload std::string packetStrStd = (char*)packet; - auto error = deserializeJson(LORAdataBuffer, packetStrStd); - if (error) { - Log.error(F("LORA packet deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), LORAdataBuffer.capacity()); + auto result = deserializeJson(LORAdataBuffer, packetStrStd); + if (result) { + Log.notice(F("LORA packet deserialization failed, not a json, sending raw message" CR)); + LORAdata = LORAdataBuffer.to(); + LORAdata["message"] = (char*)packet; + } else { + Log.trace(F("LORA packet deserialization OK" CR)); } } @@ -453,6 +457,7 @@ void LORAtoMQTT() { } else { LORAdataBuffer["origin"] = subjectLORAtoMQTT; } + handleJsonEnqueue(LORAdata); if (repeatLORAwMQTT) { Log.trace(F("Pub LORA for rpt" CR));