Skip to content

Commit 7138943

Browse files
authored
[LORA] Fix message without json not being transmitted (1technophile#1875)
Fix LoRa not processing message that are not JSON Add also a non-JSON-message to the example
1 parent b2dbc2d commit 7138943

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

examples/LoraTemperature/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# OpenMQTTGateway LoRa Node Example
22
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.
3+
It also sends a constant raw string simulating a Makerfab Soil sensor payload.
34

45
## Features:
56
* Uses an SX12XX LoRa module.

examples/LoraTemperature/platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ lib_deps =
2727
monitor_speed = 115200
2828

2929
[env:heltec-wifi-lora-32] ; Heltec ESP32 Board with SSD1306 display
30-
platform = ${com.esp32_platform}
30+
platform = espressif32
3131
board = heltec_wifi_lora_32
3232
framework = arduino
3333
lib_deps =

examples/LoraTemperature/src/main.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,24 @@ void loop() {
9090
LoRa.print(msg);
9191
LoRa.endPacket();
9292

93-
Serial.println(String(msg));
93+
Serial.println(msg);
9494

9595
display.drawString(0, 15, String(NodeId));
9696
display.drawString(0, 30, "tempc: " + String(temp) + " C");
9797
display.display();
9898

99+
delay(5000);
100+
101+
// Send makerfab soil sensor example packet
102+
LoRa.beginPacket();
103+
String msg2 = "ID010770 REPLY : SOIL INEDX:5862 H:100.00 T:1.77 ADC:624 BAT:857";
104+
LoRa.print(msg2);
105+
LoRa.endPacket();
106+
107+
Serial.println(msg2);
108+
99109
counter++;
110+
100111
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
101112
delay(1000); // wait for a second
102113
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW

main/ZgatewayLORA.ino

+8-3
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,13 @@ void LORAtoMQTT() {
428428
} else {
429429
// ascii payload
430430
std::string packetStrStd = (char*)packet;
431-
auto error = deserializeJson(LORAdataBuffer, packetStrStd);
432-
if (error) {
433-
Log.error(F("LORA packet deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), LORAdataBuffer.capacity());
431+
auto result = deserializeJson(LORAdataBuffer, packetStrStd);
432+
if (result) {
433+
Log.notice(F("LORA packet deserialization failed, not a json, sending raw message" CR));
434+
LORAdata = LORAdataBuffer.to<JsonObject>();
435+
LORAdata["message"] = (char*)packet;
436+
} else {
437+
Log.trace(F("LORA packet deserialization OK" CR));
434438
}
435439
}
436440

@@ -453,6 +457,7 @@ void LORAtoMQTT() {
453457
} else {
454458
LORAdataBuffer["origin"] = subjectLORAtoMQTT;
455459
}
460+
456461
handleJsonEnqueue(LORAdata);
457462
if (repeatLORAwMQTT) {
458463
Log.trace(F("Pub LORA for rpt" CR));

0 commit comments

Comments
 (0)