2021-01-14 04:22:59 +00:00
|
|
|
#include "SerialPlugin.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/*
|
2021-01-15 02:08:23 +00:00
|
|
|
SerialPlugin
|
|
|
|
An overly simplistic interface to send messages over the mesh network by sending strings
|
|
|
|
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
|
|
|
|
|
|
|
Need help with this plugin? Post your question on the Meshtastic Discourse:
|
|
|
|
https://meshtastic.discourse.group
|
|
|
|
|
|
|
|
Basic Usage:
|
|
|
|
|
|
|
|
1) Enable the plugin by setting SERIALPLUGIN_ENABLED to 1.
|
2021-01-14 06:51:36 +00:00
|
|
|
2) Set the pins (RXD2 / TXD2) for your preferred RX and TX GPIO pins.
|
2021-01-15 06:36:51 +00:00
|
|
|
On tbeam, recommend to use:
|
|
|
|
#define RXD2 35
|
|
|
|
#define TXD2 15
|
2021-01-14 06:50:02 +00:00
|
|
|
3) Set SERIALPLUGIN_TIMEOUT to the amount of time to wait before we consider
|
|
|
|
your packet as "done".
|
|
|
|
4) (Optional) In SerialPlugin.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.
|
|
|
|
7) (Optional) Set SERIALPLUGIN_ECHO to 1 and any message you send out will be echoed back
|
|
|
|
to your device.
|
|
|
|
|
2021-01-14 07:21:55 +00:00
|
|
|
TODO (in this order):
|
2021-01-14 07:02:13 +00:00
|
|
|
* Once protobufs regenerated with the new port, update SerialPlugin.h
|
|
|
|
* Ensure this works on a tbeam
|
2021-01-14 07:21:55 +00:00
|
|
|
* Define a verbose RX mode to report on mesh and packet infomration.
|
|
|
|
- This won't happen any time soon.
|
|
|
|
|
|
|
|
KNOWN PROBLEMS
|
|
|
|
* Until the plugin is initilized by the startup sequence, the TX pin is in a floating
|
|
|
|
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
|
|
|
|
#define SERIALPLUGIN_RX_BUFFER 128
|
|
|
|
#define SERIALPLUGIN_STRING_MAX Constants_DATA_PAYLOAD_LEN
|
2021-01-14 07:21:55 +00:00
|
|
|
#define SERIALPLUGIN_TIMEOUT 250
|
2021-01-14 04:22:59 +00:00
|
|
|
#define SERIALPLUGIN_BAUD 38400
|
2021-01-17 03:40:47 +00:00
|
|
|
#define SERIALPLUGIN_ACK 1
|
2021-01-14 04:22:59 +00:00
|
|
|
|
|
|
|
SerialPlugin *serialPlugin;
|
|
|
|
SerialPluginRadio *serialPluginRadio;
|
|
|
|
|
|
|
|
SerialPlugin::SerialPlugin() : concurrency::OSThread("SerialPlugin") {}
|
|
|
|
|
|
|
|
char serialStringChar[Constants_DATA_PAYLOAD_LEN];
|
|
|
|
|
|
|
|
int32_t SerialPlugin::runOnce()
|
|
|
|
{
|
2021-01-17 06:27:33 +00:00
|
|
|
#ifndef NO_ESP32
|
|
|
|
if (radioConfig.preferences.serialplugin_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.
|
|
|
|
DEBUG_MSG("Initilizing serial peripheral interface\n");
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
if (radioConfig.preferences.serialplugin_rxd && radioConfig.preferences.serialplugin_txd) {
|
|
|
|
Serial2.begin(SERIALPLUGIN_BAUD, SERIAL_8N1, radioConfig.preferences.serialplugin_rxd,
|
|
|
|
radioConfig.preferences.serialplugin_txd);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
} else {
|
|
|
|
Serial2.begin(SERIALPLUGIN_BAUD, SERIAL_8N1, RXD2, TXD2);
|
|
|
|
}
|
2021-01-17 06:39:28 +00:00
|
|
|
|
|
|
|
if (radioConfig.preferences.serialplugin_timeout) {
|
|
|
|
Serial2.setTimeout(
|
|
|
|
radioConfig.preferences.serialplugin_timeout); // Number of MS to wait to set the timeout for the string.
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Serial2.setTimeout(SERIALPLUGIN_TIMEOUT); // Number of MS to wait to set the timeout for the string.
|
|
|
|
}
|
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
Serial2.setRxBufferSize(SERIALPLUGIN_RX_BUFFER);
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
serialPluginRadio = new SerialPluginRadio();
|
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
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
serialPluginRadio->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 {
|
|
|
|
DEBUG_MSG("Serial Plugin Disabled\n");
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
2021-01-15 05:59:26 +00:00
|
|
|
|
|
|
|
#endif
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshPacket *SerialPluginRadio::allocReply()
|
|
|
|
{
|
|
|
|
|
|
|
|
auto reply = allocDataPacket(); // Allocate a packet for sending
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
|
|
|
|
{
|
|
|
|
MeshPacket *p = allocReply();
|
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
2021-01-14 07:21:55 +00:00
|
|
|
p->want_ack = SERIALPLUGIN_ACK;
|
|
|
|
|
2021-01-14 04:22:59 +00:00
|
|
|
p->decoded.data.payload.size = strlen(serialStringChar); // You must specify how many bytes are in the reply
|
|
|
|
memcpy(p->decoded.data.payload.bytes, serialStringChar, p->decoded.data.payload.size);
|
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SerialPluginRadio::handleReceived(const MeshPacket &mp)
|
|
|
|
{
|
2021-01-17 06:27:33 +00:00
|
|
|
#ifndef NO_ESP32
|
2021-01-15 05:59:26 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
if (radioConfig.preferences.serialplugin_enabled) {
|
2021-01-15 05:59:26 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
auto &p = mp.decoded.data;
|
|
|
|
// 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-01-17 06:27:33 +00:00
|
|
|
if (mp.from == nodeDB.getNodeNum()) {
|
2021-01-14 04:22:59 +00:00
|
|
|
|
2021-01-17 06:27:33 +00:00
|
|
|
/*
|
|
|
|
* If radioConfig.preferences.serialplugin_echo is true, then echo the packets that are sent out back to the TX
|
|
|
|
* of the serial interface.
|
|
|
|
*/
|
|
|
|
if (radioConfig.preferences.serialplugin_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 {
|
|
|
|
// DEBUG_MSG("* * Message came from the mesh\n");
|
|
|
|
// Serial2.println("* * Message came from the mesh");
|
|
|
|
Serial2.printf("%s", p.payload.bytes);
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-01-17 06:27:33 +00:00
|
|
|
DEBUG_MSG("Serial Plugin Disabled\n");
|
2021-01-14 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 05:59:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 04:22:59 +00:00
|
|
|
return true; // Let others look at this message also if they want
|
|
|
|
}
|