From 7868ca4aa2842305a103987c30ce0a5427c49af8 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 24 Jan 2024 18:03:06 -0600 Subject: [PATCH] Add telemetry --- examples/SendReceiveClient/SendReceiveClient.ino | 15 +++++++++++++++ src/Meshtastic.h | 3 +++ src/mt_protocol.cpp | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/examples/SendReceiveClient/SendReceiveClient.ino b/examples/SendReceiveClient/SendReceiveClient.ino index 3fe528a..a100240 100644 --- a/examples/SendReceiveClient/SendReceiveClient.ino +++ b/examples/SendReceiveClient/SendReceiveClient.ino @@ -47,6 +47,18 @@ void text_message_callback(uint32_t from, const char* text) { Serial.println(text); } +// This callback function will be called whenever the radio receives a text message +void telemetry_callback(uint32_t from, meshtastic_Telemetry* telemetry) { + // Do your own thing here. This example just prints the message to the serial console. + Serial.print("Received telemetry from "); + Serial.print(from); + Serial.print(": "); + if (telemetry->variant.device_metrics) { + Serial.println("Device Metrics: %fV", telemetry->variant.device_metrics->voltage); + } +} + + void setup() { // Try for up to five seconds to find a serial port; if not, the show must go on Serial.begin(9600); @@ -81,6 +93,9 @@ void setup() { // Register a callback function to be called whenever a text message is received set_text_message_callback(text_message_callback); + + // Register a callback function to be called whenever a telemetry payload is received + set_telemetry_callback(telemetry_callback); } void loop() { diff --git a/src/Meshtastic.h b/src/Meshtastic.h index 197e16b..f194bfe 100644 --- a/src/Meshtastic.h +++ b/src/Meshtastic.h @@ -75,6 +75,9 @@ bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t)); // Set the callback function that gets called when the node receives a text message. void set_text_message_callback(void (*callback)(uint32_t from, const char * text)); +// Set the callback function that gets called when the node receives a text message. +void set_telemetry_callback(void (*callback)(uint32_t from, meshtastic_Telemetry * telemetry)); + // Send a text message with *text* as payload, to a destination node (optional), on a certain channel (optional). bool mt_send_text(const char * text, uint32_t dest = BROADCAST_ADDR, uint8_t channel_index = 0); diff --git a/src/mt_protocol.cpp b/src/mt_protocol.cpp index 51051e0..67adee1 100644 --- a/src/mt_protocol.cpp +++ b/src/mt_protocol.cpp @@ -24,6 +24,7 @@ uint32_t my_node_num = 0; bool mt_debugging = false; void (*text_message_callback)(uint32_t from, const char* text) = NULL; +void (*telemetry_callback)(uint32_t from, meshtastic_Telemetry * telemetry) = NULL; void (*node_report_callback)(mt_node_t *, mt_nr_progress_t) = NULL; mt_node_t node; @@ -120,6 +121,10 @@ void set_text_message_callback(void (*callback)(uint32_t from, const char* text) text_message_callback = callback; } +void set_telemetry_callback(void (*callback)(uint32_t from, meshtastic_Telemetry * telemetry)) { + telemetry_callback = callback; +} + bool handle_my_info(meshtastic_MyNodeInfo *myNodeInfo) { my_node_num = myNodeInfo->my_node_num; return true; @@ -191,6 +196,9 @@ bool handle_mesh_packet(meshtastic_MeshPacket *meshPacket) { if (meshPacket->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) { if (text_message_callback != NULL) text_message_callback(meshPacket->from, (const char*)meshPacket->decoded.payload.bytes); + } else if (meshPacket->decoded.portnum == meshtastic_PortNum_TELEMETRY_APP) { + if (telemetry_callback != NULL) + telemetry_callback(meshPacket->from, ); } else { // TODO handle other portnums return false;