From 27a10b395f5a7ae0e6e9884250b7a7518c441b81 Mon Sep 17 00:00:00 2001 From: arduinoGP <115203453+arduionoGP@users.noreply.github.com> Date: Sun, 4 Dec 2022 00:00:43 -0500 Subject: [PATCH 1/2] Update MQTT.cpp (First real try at writing meaningful C++ but it seems to work.) Allows sending JSON Position data from MQTT broker for broadcast to a LORA mesh via gateway device. The new type is "sendposition". Valid envelope looks like: { "sender": "someSender", "type": "sendposition", "payload": { "latitude_i": 399600000, "longitude_i": -862600000, "altitude": 100 } } This complements the "sendtext" type envelope. --- src/mqtt/MQTT.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 8af009bad..414afd7cb 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -61,7 +61,26 @@ void MQTT::onPublish(char *topic, byte *payload, unsigned int length) } else { DEBUG_MSG("JSON Ignoring downlink message we originally sent.\n"); } - } else { + } else if ((json.find("sender") != json.end()) && (json.find("payload") != json.end()) && (json.find("type") != json.end()) && json["type"]->IsString() && (json["type"]->AsString().compare("sendposition") == 0)) { + //invent the "sendposition" type for a valid envelope + if (json["payload"]->IsObject() && json["type"]->IsString() && (json["sender"]->AsString().compare(owner.id) != 0)) { + JSONObject posit; + posit=json["payload"]->AsObject(); //get nested JSON Position + Position pos =Position_init_default; + pos.latitude_i=posit["latitude_i"]->AsNumber(); + pos.longitude_i=posit["longitude_i"]->AsNumber(); + pos.altitude=posit["altitude"]->AsNumber(); + + // construct protobuf data packet using POSITION, send it to the mesh + MeshPacket *p = router->allocForSending(); + p->decoded.portnum = PortNum_POSITION_APP; + p->decoded.payload.size=pb_encode_to_bytes(p->decoded.payload.bytes,sizeof(p->decoded.payload.bytes),Position_fields, &pos); //make the Data protobuf from position + service.sendToMesh(p, RX_SRC_LOCAL); + + } else { + DEBUG_MSG("JSON Ignoring downlink message we originally sent.\n"); + } + } else{ DEBUG_MSG("JSON Received payload on MQTT but not a valid envelope\n"); } } else { From b84c7ae49bdfc1b43771ca5ef438a6778665f22e Mon Sep 17 00:00:00 2001 From: arduinoGP <115203453+arduionoGP@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:41:58 -0500 Subject: [PATCH 2/2] Oops, added time to the Pos --- src/mqtt/MQTT.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 414afd7cb..96135bc34 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -70,6 +70,7 @@ void MQTT::onPublish(char *topic, byte *payload, unsigned int length) pos.latitude_i=posit["latitude_i"]->AsNumber(); pos.longitude_i=posit["longitude_i"]->AsNumber(); pos.altitude=posit["altitude"]->AsNumber(); + pos.time=posit["time"]->AsNumber(); // construct protobuf data packet using POSITION, send it to the mesh MeshPacket *p = router->allocForSending();