2021-06-27 17:56:28 +00:00
|
|
|
#include "configuration.h"
|
2022-02-27 09:27:17 +00:00
|
|
|
#include "SerialModule.h"
|
2021-01-14 04:22:59 +00:00
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/*
|
2022-02-27 10:21:02 +00:00
|
|
|
SerialModule
|
2021-01-21 03:02:08 +00:00
|
|
|
A simple interface to send messages over the mesh network by sending strings
|
2021-01-15 02:08:23 +00:00
|
|
|
over a serial port.
|
|
|
|
|
2021-01-17 06:31:56 +00:00
|
|
|
Default is to use RX GPIO 16 and TX GPIO 17.
|
2021-01-14 06:50:02 +00:00
|
|
|
|
2022-02-27 08:29:05 +00:00
|
|
|
Need help with this module? Post your question on the Meshtastic Discourse:
|
2021-01-14 06:50:02 +00:00
|
|
|
https://meshtastic.discourse.group
|
|
|
|
|
|
|
|
Basic Usage:
|
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
1) Enable the module by setting serial_module_enabled to 1.
|
|
|
|
2) Set the pins (serial_module_rxd / serial_module_rxd) for your preferred RX and TX GPIO pins.
|
2021-01-15 06:36:51 +00:00
|
|
|
On tbeam, recommend to use:
|
2021-01-21 03:02:08 +00:00
|
|
|
RXD 35
|
|
|
|
TXD 15
|
2022-03-09 07:37:41 +00:00
|
|
|
3) Set serial_module_timeout to the amount of time to wait before we consider
|
2021-01-14 06:50:02 +00:00
|
|
|
your packet as "done".
|
2022-02-27 09:27:17 +00:00
|
|
|
4) (Optional) In SerialModule.h set the port to PortNum_TEXT_MESSAGE_APP if you want to
|
2021-01-14 06:50:02 +00:00
|
|
|
send messages to/from the general text message channel.
|
|
|
|
5) Connect to your device over the serial interface at 38400 8N1.
|
|
|
|
6) Send a packet up to 240 bytes in length. This will get relayed over the mesh network.
|
2022-03-09 07:37:41 +00:00
|
|
|
7) (Optional) Set serial_module_echo to 1 and any message you send out will be echoed back
|
2021-01-14 06:50:02 +00:00
|
|
|
to your device.
|
|
|
|
|
2021-01-14 07:21:55 +00:00
|
|
|
TODO (in this order):
|
|
|
|
* Define a verbose RX mode to report on mesh and packet infomration.
|
|
|
|
- This won't happen any time soon.
|
|
|
|
|
|
|
|
KNOWN PROBLEMS
|
2022-02-27 08:29:05 +00:00
|
|
|
* Until the module is initilized by the startup sequence, the TX pin is in a floating
|
2021-01-14 07:21:55 +00:00
|
|
|
state. Device connected to that pin may see this as "noise".
|
2021-01-15 06:36:51 +00:00
|
|
|
* Will not work on NRF and the Linux device targets.
|
2021-01-14 07:21:55 +00:00
|
|
|
|
2021-01-14 07:02:13 +00:00
|
|
|
|
2021-01-14 04:22:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define RXD2 16
|
|
|
|
#define TXD2 17
|
2022-03-09 07:37:41 +00:00
|
|
|
#define SERIAL_MODULE_RX_BUFFER 128
|
|
|
|
#define SERIAL_MODULE_STRING_MAX Constants_DATA_PAYLOAD_LEN
|
|
|
|
#define SERIAL_MODULE_TIMEOUT 250
|
|
|
|
#define SERIAL_MODULE_BAUD 38400
|
|
|
|
#define SERIAL_MODULE_ACK 1
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
SerialModule *serialModule;
|
|
|
|
SerialModuleRadio *serialModuleRadio;
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
SerialModule::SerialModule() : concurrency::OSThread("SerialModule") {}
|
2021-01-14 04:22:59 +00:00
|
|
|
|
|
|
|
char serialStringChar[Constants_DATA_PAYLOAD_LEN];
|
|
|
|
|
2022-03-09 08:01:43 +00:00
|
|
|
SerialModuleRadio::SerialModuleRadio() : SinglePortModule("SerialModuleRadio", PortNum_SERIAL_APP)
|
2021-03-13 05:14:27 +00:00
|
|
|
{
|
|
|
|
// restrict to the admin channel for rx
|
|
|
|
boundChannel = Channels::serialChannel;
|
|
|
|
}
|
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
int32_t SerialModule::runOnce()
|
2021-01-14 04:22:59 +00:00
|
|
|
{
|
2021-01-17 06:27:33 +00:00
|
|
|
#ifndef NO_ESP32
|
2021-01-17 23:40:25 +00:00
|
|
|
|
|
|
|
/*
|
2022-02-27 08:52:25 +00:00
|
|
|
Uncomment the preferences below if you want to use the module
|
2021-01-17 23:40:25 +00:00
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
*/
|
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
// radioConfig.preferences.serial_module_enabled = 1;
|
|
|
|
// radioConfig.preferences.serial_module_rxd = 35;
|
|
|
|
// radioConfig.preferences.serial_module_txd = 15;
|
|
|
|
// radioConfig.preferences.serial_module_timeout = 1000;
|
|
|
|
// radioConfig.preferences.serial_module_echo = 1;
|
2021-01-17 23:40:25 +00:00
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
if (radioConfig.preferences.serial_module_enabled) {
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
if (firstTime) {
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
// Interface with the serial peripheral from in here.
|
2021-01-21 03:02:08 +00:00
|
|
|
DEBUG_MSG("Initializing serial peripheral interface\n");
|
2022-03-26 16:35:06 +00:00
|
|
|
|
|
|
|
uint32_t baud = 0;
|
|
|
|
|
|
|
|
if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_Default) {
|
|
|
|
baud = 38400;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_2400) {
|
|
|
|
baud = 2400;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_4800) {
|
|
|
|
baud = 4800;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_9600) {
|
|
|
|
baud = 9600;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_19200) {
|
|
|
|
baud = 19200;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_38400) {
|
|
|
|
baud = 38400;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_57600) {
|
|
|
|
baud = 57600;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_115200) {
|
|
|
|
baud = 115200;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_230400) {
|
|
|
|
baud = 230400;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_460800) {
|
|
|
|
baud = 460800;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_576000) {
|
|
|
|
baud = 576000;
|
|
|
|
|
|
|
|
} else if (radioConfig.preferences.serial_module_baud == RadioConfig_UserPreferences_Serial_Baud_BAUD_921600) {
|
|
|
|
baud = 921600;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
if (radioConfig.preferences.serial_module_rxd && radioConfig.preferences.serial_module_txd) {
|
2022-03-26 16:35:06 +00:00
|
|
|
Serial2.begin(baud, SERIAL_8N1, radioConfig.preferences.serial_module_rxd,
|
2022-03-09 07:37:41 +00:00
|
|
|
radioConfig.preferences.serial_module_txd);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
} else {
|
2022-03-26 16:35:06 +00:00
|
|
|
Serial2.begin(baud, SERIAL_8N1, RXD2, TXD2);
|
2021-01-17 06:27:33 +00:00
|
|
|
}
|
2021-01-17 06:39:28 +00:00
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
if (radioConfig.preferences.serial_module_timeout) {
|
2021-01-17 06:39:28 +00:00
|
|
|
Serial2.setTimeout(
|
2022-03-09 07:37:41 +00:00
|
|
|
radioConfig.preferences.serial_module_timeout); // Number of MS to wait to set the timeout for the string.
|
2021-01-17 06:39:28 +00:00
|
|
|
|
|
|
|
} else {
|
2022-03-09 07:37:41 +00:00
|
|
|
Serial2.setTimeout(SERIAL_MODULE_TIMEOUT); // Number of MS to wait to set the timeout for the string.
|
2021-01-17 06:39:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
Serial2.setRxBufferSize(SERIAL_MODULE_RX_BUFFER);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
serialModuleRadio = new SerialModuleRadio();
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
firstTime = 0;
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
} else {
|
|
|
|
String serialString;
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
while (Serial2.available()) {
|
|
|
|
serialString = Serial2.readString();
|
|
|
|
serialString.toCharArray(serialStringChar, Constants_DATA_PAYLOAD_LEN);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
serialModuleRadio->sendPayload();
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
DEBUG_MSG("Received: %s\n", serialStringChar);
|
|
|
|
}
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
return (10);
|
|
|
|
} else {
|
2022-02-27 10:21:02 +00:00
|
|
|
DEBUG_MSG("Serial Module Disabled\n");
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
2021-03-08 07:20:43 +00:00
|
|
|
#else
|
|
|
|
return INT32_MAX;
|
2021-01-15 05:59:26 +00:00
|
|
|
#endif
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
MeshPacket *SerialModuleRadio::allocReply()
|
2021-01-14 04:22:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
auto reply = allocDataPacket(); // Allocate a packet for sending
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
void SerialModuleRadio::sendPayload(NodeNum dest, bool wantReplies)
|
2021-01-14 04:22:59 +00:00
|
|
|
{
|
|
|
|
MeshPacket *p = allocReply();
|
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
p->want_ack = SERIAL_MODULE_ACK;
|
2021-01-14 07:21:55 +00:00
|
|
|
|
2021-02-17 05:06:23 +00:00
|
|
|
p->decoded.payload.size = strlen(serialStringChar); // You must specify how many bytes are in the reply
|
|
|
|
memcpy(p->decoded.payload.bytes, serialStringChar, p->decoded.payload.size);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
ProcessMessage SerialModuleRadio::handleReceived(const MeshPacket &mp)
|
2021-01-14 04:22:59 +00:00
|
|
|
{
|
2021-01-17 06:27:33 +00:00
|
|
|
#ifndef NO_ESP32
|
2021-01-15 05:59:26 +00:00
|
|
|
|
2022-03-09 07:37:41 +00:00
|
|
|
if (radioConfig.preferences.serial_module_enabled) {
|
2021-01-15 05:59:26 +00:00
|
|
|
|
2021-02-17 08:17:46 +00:00
|
|
|
auto &p = mp.decoded;
|
2021-01-17 23:40:25 +00:00
|
|
|
// DEBUG_MSG("Received text msg self=0x%0x, from=0x%0x, to=0x%0x, id=%d, msg=%.*s\n",
|
|
|
|
// nodeDB.getNodeNum(), mp.from, mp.to, mp.id, p.payload.size, p.payload.bytes);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-03-05 02:19:27 +00:00
|
|
|
if (getFrom(&mp) == nodeDB.getNodeNum()) {
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
/*
|
2022-03-09 07:37:41 +00:00
|
|
|
* If radioConfig.preferences.serial_module_echo is true, then echo the packets that are sent out back to the TX
|
2021-01-17 06:27:33 +00:00
|
|
|
* of the serial interface.
|
|
|
|
*/
|
2022-03-09 07:37:41 +00:00
|
|
|
if (radioConfig.preferences.serial_module_echo) {
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
// For some reason, we get the packet back twice when we send out of the radio.
|
|
|
|
// TODO: need to find out why.
|
|
|
|
if (lastRxID != mp.id) {
|
|
|
|
lastRxID = mp.id;
|
|
|
|
// DEBUG_MSG("* * Message came this device\n");
|
|
|
|
// Serial2.println("* * Message came this device");
|
|
|
|
Serial2.printf("%s", p.payload.bytes);
|
|
|
|
}
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
2021-01-17 06:27:33 +00:00
|
|
|
|
|
|
|
} else {
|
2021-03-18 04:52:30 +00:00
|
|
|
|
2022-03-26 16:35:06 +00:00
|
|
|
if (radioConfig.preferences.serial_module_mode == RadioConfig_UserPreferences_Serial_Mode_MODE_Default || radioConfig.preferences.serial_module_mode == RadioConfig_UserPreferences_Serial_Mode_MODE_SIMPLE) {
|
2021-03-18 04:52:30 +00:00
|
|
|
// DEBUG_MSG("* * Message came from the mesh\n");
|
|
|
|
// Serial2.println("* * Message came from the mesh");
|
|
|
|
Serial2.printf("%s", p.payload.bytes);
|
|
|
|
|
2022-03-26 16:35:06 +00:00
|
|
|
} else if (radioConfig.preferences.serial_module_mode == RadioConfig_UserPreferences_Serial_Mode_MODE_PROTO) {
|
|
|
|
|
2021-03-18 04:52:30 +00:00
|
|
|
}
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-02-27 10:21:02 +00:00
|
|
|
DEBUG_MSG("Serial Module Disabled\n");
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 05:59:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-23 01:42:09 +00:00
|
|
|
return ProcessMessage::CONTINUE; // Let others look at this message also if they want
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|