Skip to content

Commit

Permalink
Merge branch 'meshtastic:master' into fix-reboot-mt
Browse files Browse the repository at this point in the history
  • Loading branch information
pdxlocations authored Dec 14, 2024
2 parents 9a94dbd + edfaa9d commit 694e027
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
6 changes: 4 additions & 2 deletions examples/SendReceiveClient/SendReceiveClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ void connected_callback(mt_node_t *node, mt_nr_progress_t progress) {
}

// This callback function will be called whenever the radio receives a text message
void text_message_callback(uint32_t from, uint32_t to, const char* text) {
void text_message_callback(uint32_t from, uint32_t to, uint8_t channel, const char* text) {
// Do your own thing here. This example just prints the message to the serial console.
Serial.print("Received a text message from: ");
Serial.print("Received a text message on channel: ");
Serial.print(channel);
Serial.print(" from: ");
Serial.print(from);
Serial.print(" to: ");
Serial.print(to);
Expand Down
2 changes: 1 addition & 1 deletion src/Meshtastic.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ typedef enum {
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, uint32_t to, const char * text));
void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, const char * text));

// 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
33 changes: 30 additions & 3 deletions src/mt_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ size_t pb_size = 0; // Number of bytes currently in the buffer
// Wait this many msec if there's nothing new on the channel
#define NO_NEWS_PAUSE 25

// Serial connections require at least one ping every 15 minutes
// Otherwise the connection is closed, and packets will no longer be received
// We will send a ping every 60 seconds, which is what the web client does
// https://github.com/meshtastic/js/blob/715e35d2374276a43ffa93c628e3710875d43907/src/adapters/serialConnection.ts#L160
#define HEARTBEAT_INTERVAL_MS 60000
uint32_t last_heartbeat_at = 0;

// The ID of the current WANT_CONFIG request
uint32_t want_config_id = 0;

// Node number of the MT node hosting our WiFi
uint32_t my_node_num = 0;

bool mt_debugging = false;
void (*text_message_callback)(uint32_t from, uint32_t to, const char* text) = NULL;
void (*text_message_callback)(uint32_t from, uint32_t to, uint8_t channel, const char* text) = NULL;
void (*node_report_callback)(mt_node_t *, mt_nr_progress_t) = NULL;
mt_node_t node;

Expand Down Expand Up @@ -116,7 +123,19 @@ bool mt_send_text(const char * text, uint32_t dest, uint8_t channel_index) {
return _mt_send_toRadio(toRadio);
}

void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, const char* text)) {
bool mt_send_heartbeat() {

d("Sending heartbeat");

meshtastic_ToRadio toRadio = meshtastic_ToRadio_init_default;
toRadio.which_payload_variant = meshtastic_ToRadio_heartbeat_tag;
toRadio.heartbeat = meshtastic_Heartbeat_init_default;

return _mt_send_toRadio(toRadio);

}

void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, const char* text)) {
text_message_callback = callback;
}

Expand Down Expand Up @@ -190,7 +209,7 @@ bool handle_mesh_packet(meshtastic_MeshPacket *meshPacket) {
if (meshPacket->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (meshPacket->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) {
if (text_message_callback != NULL)
text_message_callback(meshPacket->from, meshPacket->to, (const char*)meshPacket->decoded.payload.bytes);
text_message_callback(meshPacket->from, meshPacket->to, meshPacket->channel, (const char*)meshPacket->decoded.payload.bytes);
} else {
// TODO handle other portnums
return false;
Expand Down Expand Up @@ -308,8 +327,16 @@ bool mt_loop(uint32_t now) {
return false;
#endif
} else if (mt_serial_mode) {

rv = mt_serial_loop();
if (rv) bytes_read = mt_serial_check_radio((char *)pb_buf + pb_size, space_left);

// if heartbeat interval has passed, send a heartbeat to keep serial connection alive
if(now >= (last_heartbeat_at + HEARTBEAT_INTERVAL_MS)){
mt_send_heartbeat();
last_heartbeat_at = now;
}

} else {
Serial.println("mt_loop() called but it was never initialized");
while(1);
Expand Down

0 comments on commit 694e027

Please sign in to comment.