99 which prints the message to the serial console.
1010*/
1111
12+ // Uncomment the line below to enable debugging
13+ // #define MT_DEBUGGING
14+
1215#include < Meshtastic.h>
1316
1417// Pins to use for WiFi; these defaults are for an Adafruit Feather M0 WiFi.
2326#define SERIAL_RX_PIN 13
2427#define SERIAL_TX_PIN 15
2528// A different baud rate to communicate with the Meshtastic device can be specified here
26- #define BAUD_RATE 9600
29+ #define BAUD_RATE 38400
2730
2831// Send a text message every this many seconds
2932#define SEND_PERIOD 300
@@ -38,6 +41,58 @@ void connected_callback(mt_node_t *node, mt_nr_progress_t progress) {
3841 not_yet_connected = false ;
3942}
4043
44+
45+ const char * meshtastic_portnum_to_string (meshtastic_PortNum port) {
46+ switch (port) {
47+ case meshtastic_PortNum_UNKNOWN_APP: return " UNKNOWN_APP" ;
48+ case meshtastic_PortNum_TEXT_MESSAGE_APP: return " TEXT_MESSAGE_APP" ;
49+ case meshtastic_PortNum_REMOTE_HARDWARE_APP: return " REMOTE_HARDWARE_APP" ;
50+ case meshtastic_PortNum_POSITION_APP: return " POSITION_APP" ;
51+ case meshtastic_PortNum_NODEINFO_APP: return " NODEINFO_APP" ;
52+ case meshtastic_PortNum_ROUTING_APP: return " ROUTING_APP" ;
53+ case meshtastic_PortNum_ADMIN_APP: return " ADMIN_APP" ;
54+ case meshtastic_PortNum_TEXT_MESSAGE_COMPRESSED_APP: return " TEXT_MESSAGE_COMPRESSED_APP" ;
55+ case meshtastic_PortNum_WAYPOINT_APP: return " WAYPOINT_APP" ;
56+ case meshtastic_PortNum_AUDIO_APP: return " AUDIO_APP" ;
57+ case meshtastic_PortNum_DETECTION_SENSOR_APP: return " DETECTION_SENSOR_APP" ;
58+ case meshtastic_PortNum_REPLY_APP: return " REPLY_APP" ;
59+ case meshtastic_PortNum_IP_TUNNEL_APP: return " IP_TUNNEL_APP" ;
60+ case meshtastic_PortNum_PAXCOUNTER_APP: return " PAXCOUNTER_APP" ;
61+ case meshtastic_PortNum_SERIAL_APP: return " SERIAL_APP" ;
62+ case meshtastic_PortNum_STORE_FORWARD_APP: return " STORE_FORWARD_APP" ;
63+ case meshtastic_PortNum_RANGE_TEST_APP: return " RANGE_TEST_APP" ;
64+ case meshtastic_PortNum_TELEMETRY_APP: return " TELEMETRY_APP" ;
65+ case meshtastic_PortNum_ZPS_APP: return " ZPS_APP" ;
66+ case meshtastic_PortNum_SIMULATOR_APP: return " SIMULATOR_APP" ;
67+ case meshtastic_PortNum_TRACEROUTE_APP: return " TRACEROUTE_APP" ;
68+ case meshtastic_PortNum_NEIGHBORINFO_APP: return " NEIGHBORINFO_APP" ;
69+ case meshtastic_PortNum_ATAK_PLUGIN: return " ATAK_PLUGIN" ;
70+ case meshtastic_PortNum_MAP_REPORT_APP: return " MAP_REPORT_APP" ;
71+ case meshtastic_PortNum_POWERSTRESS_APP: return " POWERSTRESS_APP" ;
72+ case meshtastic_PortNum_PRIVATE_APP: return " PRIVATE_APP" ;
73+ case meshtastic_PortNum_ATAK_FORWARDER: return " ATAK_FORWARDER" ;
74+ case meshtastic_PortNum_MAX: return " MAX" ;
75+ default : return " UNKNOWN_PORTNUM" ;
76+ }
77+ }
78+
79+ void displayPubKey (meshtastic_MeshPacket_public_key_t pubKey, char *hex_str) {
80+ for (int i = 0 ; i < 32 ; i++) {
81+ sprintf (&hex_str[i * 2 ], " %02x" , (unsigned char )pubKey.bytes [i]);
82+ }
83+
84+ hex_str[64 ] = ' \0 ' ; // Null terminator
85+ }
86+
87+
88+ void encrypted_callback (uint32_t from, uint32_t to, uint8_t channel, meshtastic_MeshPacket_public_key_t pubKey, meshtastic_MeshPacket_encrypted_t *enc_payload) {
89+ Serial.printf (" Received an ENCRYPTED callback from: %x to: %x\r\n " , from, to);
90+ }
91+
92+ void portnum_callback (uint32_t from, uint32_t to, uint8_t channel, meshtastic_PortNum portNum, meshtastic_Data_payload_t *payload) {
93+ Serial.printf (" Received a callback for PortNum %s\r\n " , meshtastic_portnum_to_string (portNum));
94+ }
95+
4196// This callback function will be called whenever the radio receives a text message
4297void text_message_callback (uint32_t from, uint32_t to, uint8_t channel, const char * text) {
4398 // Do your own thing here. This example just prints the message to the serial console.
@@ -59,8 +114,8 @@ void text_message_callback(uint32_t from, uint32_t to, uint8_t channel, const c
59114}
60115
61116void setup () {
62- // Try for up to five seconds to find a serial port; if not, the show must go on
63- Serial.begin (9600 );
117+ // Try for up to five seconds to find a serial port; if not, the show must gox on
118+ Serial.begin (115200 );
64119 while (true ) {
65120 if (Serial) break ;
66121 if (millis () > 5000 ) {
@@ -82,16 +137,15 @@ void setup() {
82137#endif
83138 Serial.println (" mode" );
84139
85- // Set to true if you want debug messages
86- mt_set_debug (false );
87-
88140 randomSeed (micros ());
89141
90142 // Initial connection to the Meshtastic device
91143 mt_request_node_report (connected_callback);
92144
93145 // Register a callback function to be called whenever a text message is received
94146 set_text_message_callback (text_message_callback);
147+ set_portnum_callback (portnum_callback);
148+ set_encrypted_callback (encrypted_callback);
95149}
96150
97151void loop () {
0 commit comments