firmware/src/modules/SerialModule.cpp

264 lines
8.7 KiB
C++
Raw Normal View History

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"
2022-05-07 10:31:21 +00:00
#include "configuration.h"
2021-01-14 04:22:59 +00:00
#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.
Default is to use RX GPIO 16 and TX GPIO 17.
Need help with this module? Post your question on the Meshtastic Discourse:
https://meshtastic.discourse.group
Basic Usage:
2022-05-07 10:31:21 +00:00
1) Enable the module by setting enabled to 1.
2) Set the pins (rxd / rxd) for your preferred RX and TX GPIO pins.
On tbeam, recommend to use:
2021-01-21 03:02:08 +00:00
RXD 35
TXD 15
2022-05-07 10:31:21 +00:00
3) Set timeout to the amount of time to wait before we consider
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
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-05-07 10:31:21 +00:00
7) (Optional) Set echo to 1 and any message you send out will be echoed back
to your device.
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
* Until the module is initilized by the startup sequence, the TX pin is in a floating
state. Device connected to that pin may see this as "noise".
* Will not work on NRF and the Linux device targets.
2021-01-14 07:02:13 +00:00
2021-01-14 04:22:59 +00:00
*/
#define RXD2 16
#define TXD2 17
2022-05-07 10:31:21 +00:00
#define RX_BUFFER 128
#define STRING_MAX Constants_DATA_PAYLOAD_LEN
#define TIMEOUT 250
#define BAUD 38400
#define 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];
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
{
2022-09-12 08:08:32 +00:00
#if defined(ARCH_ESP32) || defined(ARCH_NRF52)
/*
Uncomment the preferences below if you want to use the module
without having to configure it from the PythonAPI or WebUI.
*/
// moduleConfig.serial.enabled = 1;
// moduleConfig.serial.rxd = 35;
// moduleConfig.serial.txd = 15;
// moduleConfig.serial.timeout = 1000;
// moduleConfig.serial.echo = 1;
if (moduleConfig.serial.enabled) {
2021-01-14 04:22:59 +00:00
if (firstTime) {
2021-01-14 04:22:59 +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-05-07 10:31:21 +00:00
uint32_t baud = 0;
if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_DEFAULT) {
baud = 38400;
2022-05-07 10:31:21 +00:00
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_110) {
2022-04-19 20:37:04 +00:00
baud = 110;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_300) {
2022-04-19 20:37:04 +00:00
baud = 300;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_600) {
2022-04-19 20:37:04 +00:00
baud = 600;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_1200) {
2022-04-19 20:37:04 +00:00
baud = 1200;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_2400) {
baud = 2400;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_4800) {
baud = 4800;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_9600) {
baud = 9600;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_19200) {
baud = 19200;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_38400) {
baud = 38400;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_57600) {
baud = 57600;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_115200) {
baud = 115200;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_230400) {
baud = 230400;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_460800) {
baud = 460800;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_576000) {
baud = 576000;
} else if (moduleConfig.serial.baud == ModuleConfig_SerialConfig_Serial_Baud_BAUD_921600) {
baud = 921600;
}
2021-01-14 04:22:59 +00:00
2022-09-12 08:08:32 +00:00
#ifdef ARCH_ESP32
if (moduleConfig.serial.rxd && moduleConfig.serial.txd) {
Serial2.begin(baud, SERIAL_8N1, moduleConfig.serial.rxd, moduleConfig.serial.txd);
2021-01-14 04:22:59 +00:00
} else {
Serial2.begin(baud, SERIAL_8N1, RXD2, TXD2);
}
2022-09-12 08:08:32 +00:00
#else
if (moduleConfig.serial.rxd && moduleConfig.serial.txd)
Serial2.setPins(moduleConfig.serial.rxd, moduleConfig.serial.txd);
2022-09-12 08:08:32 +00:00
Serial2.begin(baud, SERIAL_8N1);
2022-09-12 08:08:32 +00:00
#endif
if (moduleConfig.serial.timeout) {
Serial2.setTimeout(moduleConfig.serial.timeout); // Number of MS to wait to set the timeout for the string.
} else {
2022-05-07 10:31:21 +00:00
Serial2.setTimeout(TIMEOUT); // Number of MS to wait to set the timeout for the string.
}
2022-09-12 08:08:32 +00:00
#ifdef ARCH_ESP32
2022-05-07 10:31:21 +00:00
Serial2.setRxBufferSize(RX_BUFFER);
2022-09-12 08:08:32 +00:00
#endif
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
firstTime = 0;
2021-01-14 04:22:59 +00:00
} else {
String serialString;
2021-01-14 04:22:59 +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
DEBUG_MSG("Received: %s\n", serialStringChar);
}
2021-01-14 04:22:59 +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
return (INT32_MAX);
}
2021-03-08 07:20:43 +00:00
#else
return INT32_MAX;
#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-05-07 10:31:21 +00:00
p->want_ack = ACK;
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
{
2022-09-12 08:08:32 +00:00
#if defined(ARCH_ESP32) || defined(ARCH_NRF52)
if (moduleConfig.serial.enabled) {
2021-02-17 08:17:46 +00:00
auto &p = mp.decoded;
// 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
if (getFrom(&mp) == nodeDB.getNodeNum()) {
2021-01-14 04:22:59 +00:00
/*
* If moduleConfig.serial.echo is true, then echo the packets that are sent out
2022-05-07 10:31:21 +00:00
* back to the TX of the serial interface.
*/
if (moduleConfig.serial.echo) {
2021-01-14 04:22:59 +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
}
} else {
if (moduleConfig.serial.mode == ModuleConfig_SerialConfig_Serial_Mode_DEFAULT ||
moduleConfig.serial.mode == ModuleConfig_SerialConfig_Serial_Mode_SIMPLE) {
// DEBUG_MSG("* * Message came from the mesh\n");
// Serial2.println("* * Message came from the mesh");
Serial2.printf("%s", p.payload.bytes);
} else if (moduleConfig.serial.mode == ModuleConfig_SerialConfig_Serial_Mode_PROTO) {
2022-09-12 08:08:32 +00:00
// TODO this needs to be implemented
}
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
}
#endif
return ProcessMessage::CONTINUE; // Let others look at this message also if they want
2021-01-14 04:22:59 +00:00
}