Skip to content

Commit

Permalink
Add telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern committed Jan 25, 2024
1 parent fc6d261 commit 7868ca4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/SendReceiveClient/SendReceiveClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions src/Meshtastic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/mt_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7868ca4

Please sign in to comment.