Stay up-to-date wrt master (cc @mc-hamster)

# Conflicts:
#	src/plugins/Plugins.cpp
#	src/plugins/RangeTestPlugin.cpp
#	src/plugins/StoreForwardPlugin.cpp
1.2-legacy
Kevin Hester 2021-02-22 10:39:46 +08:00
commit 3088fe3f91
19 zmienionych plików z 845 dodań i 278 usunięć

Wyświetl plik

@ -241,7 +241,7 @@ def print_addr(name, value, resolver):
def print_stack_full(lines, resolver): def print_stack_full(lines, resolver):
print("stack:") print("stack:")
for line in lines: for line in lines:
print(line.offset + ":") print(str(line.offset) + ":")
for content in line.content: for content in line.content:
print(" " + resolver.resolve_stack_addr(content)) print(" " + resolver.resolve_stack_addr(content))

Wyświetl plik

@ -0,0 +1,93 @@
# About
The RangeTest Plugin will help you perform range and coverage tests.
# Configuration
These are the settings that can be configured.
range_test_plugin_enabled
Is the plugin enabled?
0 = Disabled (Default)
1 = Enabled
range_test_plugin_save
If enabled, we will save a log of all received messages to /static/rangetest.csv which you can access from the webserver. We will abort
writing if there is less than 50k of space on the filesystem to prevent filling up the storage.
0 = Disabled (Default)
1 = Enabled
range_test_plugin_sender
Number of seconds to wait between sending packets. Using the long_slow channel configuration, it's best not to go more frequent than once every 60 seconds. You can be more agressive with faster settings. 0 is default which disables sending messages.
# Usage Notes
For basic usage, you will need two devices both with a GPS. A device with a paired phone with GPS may work, I have not tried it.
The first thing to do is to turn on the plugin. With the plugin turned on, the other settings will be available:
range_test_plugin_enabled = 1
If you want to send a message every 60 seconds:
range_test_plugin_sender = 60
To save a log of the messages:
range_test_plugin_save = 1
Recommended settings for a sender at different radio settings:
Long Slow ... range_test_plugin_sender = 60
Long Alt ... range_test_plugin_sender = 30
Medium ... range_test_plugin_sender = 15
Short Fast ... range_test_plugin_sender = 15
## Other things to keep in mind
Be sure to turn off either the plugin configured as a sender or the device where the plugin setup as sender when not in use. This will use a lot of time on air and will spam your channel.
Also be mindful of your space usage on the file system. It has protections from filling up the space but it's best to delete old range test results.
# Known Problems
* If turned on, using mesh network will become unwieldly because messages are sent over the same channel as the other messages. See TODO below.
# TODO
* Right now range test messages go over the TEXT_MESSAGE_APP port. We need a toggle to switch to optionally send over RANGE_TEST_APP.
# FAQ
Q: Where is rangetest.csv saved?
A: Turn on the WiFi on your device as either a WiFi client or a WiFi AP. Once you can connect to your device, go to /static and you will see rangetest.csv.
Q: Do I need to have WiFi turned on for the file to be saved?
A: Nope, it'll just work.
Q: Do I need a phone for this plugin?
A: There's no need for a phone.
Q: Can I use this as a message logger?
A: While it's not the intended purpose, sure, why not. Do it!
Q: What will happen if I run out of space on my device?
A: We have a protection in place to keep you from completly filling up your device. This will make sure that other device critical functions will continue to work. We will reserve at least 50k of free space.
Q: What do I do with the rangetest.csv file when I'm done?
A: Go to /static and delete the file.
Q: Can I use this as a sender while on battery power?
A: Yes, but your battery will run down quicker than normal. While sending, we tell the device not to go into low-power mode since it needs to keep to a fairly strict timer.
Q: Why is this operating on incoming messages instead of the existing location discovery protocol?
A: This plugin is still young and currently supports monitoring just one port at a time. I decided to use the existing message port because that is easy to test with. A future version will listen to multiple ports to be more promiscuous.
# Need more help?
Go to the Meshtastic Discourse Group if you have any questions or to share how you have used this.
https://meshtastic.discourse.group

Wyświetl plik

@ -1,14 +1,12 @@
# About # About
This is a work in progress and is not yet available. This is a work in progress and is not yet available.
The Store Request Plugin is an implementation of a Store and Forward system to enable resilient messaging in the event that a client device is disconnected from the main network. The Store Forward Plugin is an implementation of a Store and Forward system to enable resilient messaging in the event that a client device is disconnected from the main network.
Because of the increased network traffic for this overhead, it's not adviced to use this if you are duty cycle limited for your airtime usage nor is it adviced to use this for SF12. Because of the increased network traffic for this overhead, it's not adviced to use this if you are duty cycle limited for your airtime usage nor is it adviced to use this for SF12 (Long range but Slow).
# Running notes # Requirements
This will only work on nodes that are designated as a Router.
Initial Requirements: Initial Requirements:
@ -17,5 +15,91 @@ Initial Requirements:
* * Router nodes are intended to be always online. If this plugin misses any messages, the reliability of the stored messages will be reduced * * Router nodes are intended to be always online. If this plugin misses any messages, the reliability of the stored messages will be reduced
* Esp32 Processor based device with external PSRAM. (tbeam v1.0 and tbeamv1.1, maybe others) * Esp32 Processor based device with external PSRAM. (tbeam v1.0 and tbeamv1.1, maybe others)
Initial Features # Implementation timeline
*
Not necessarily in this order:
UC 1) MVP - automagically forward packets to a client that may have missed packets.
UC 2) Client Interface (Web, Android, Python or iOS when that happens) to optionally request packets be resent. This is to support the case where Router has not detected that the client was away. This is because the router will only know you're away if you've been gone for a period of time but will have no way of knowing if you were offline for a short number of minutes. This will cover the case where you have ducked into a cave or you're swapping out your battery.
UC 3) router sends a periodic “heartbeat” to let the clients know theyre part of the main mesh
UC 4) support for a mesh to have multiple routers that have the store & forward functionality (for redundancy)
UC 5) Support for "long term" delayed messages and "short term" delayed messages. Handle the cases slightly different to improve user expierence. A short term delayed message would be a message that was resent becaue a node was not heard from for <5 minutes. A long term delayed message is a message that has not been delivered in >5 minutes.
UC 6) Eventually we could add a "want_store_and_forward" bit to MeshPacket and that could be nicer than whitelists in this plugin. Initially we'd only set that bit in text messages (and any other plugin messages that can cope with this). This change would be backward wire compatible so can add easily later.
UC 7) Currently the way we allocate messages in the device code is super inefficient. It always allocates the worst case message size. Really we should dynamically allocate just the # of bytes we need. This would allow many more MeshPackets to be kept in RAM.
UC 8) We'll want a "delayed" bit in MeshPacket. This will indicate that the message was not received in real time.
# Things to consider
Not all these cases will be initially implemented. It's just a running stream of thoughts to be considered.
## Main Mesh Network with Router
The store and forward plugin is intended to be enabled on a router that designates your "main" mesh network.
## Store and Forward on Multiple Routers
If multiple routers with the plugin are enabled, they should be able to share their stored database amongst each other. This enable resilliancy from one router going offline.
## Fragmented networks - No router
In this case, the mesh network has been fragmented by two client devices leaving the main network.
If two Meshtastic devices walk away from the main mesh, they will be able to message each other but not message the main network. When they return to the main network, they will receive the messages they have missed from the main mesh network.
## Fragmented network - With routers
In this case, we have two routers separate by a great distance, each serving multiple devices. One of the routers have gone offline. This has now created two physically seaprated mesh networks using the same channel configuration.
Q: How do we rejoin both fragmented networks? Do we care about messages that were unrouted between fagments?
## Identifing Delayed Messages
When a message is replayed for a node, identify the packet as "Delayed". This will indicate that the message was not received in real time.
# Router Data Structures
Structure of received messages:
receivedMessages
Port_No
packetID
to
from
rxTimeMsec
data
Structure of nodes and last time we heard from them. This is a record of any packet type.
receivedRecord
From
rxTimeMillis
# General Operation for UC1 - automagically forward packets to a client that may have missed packets
On every handled packet
* Record the sender from and the time we heard from that sender into senderRecord.
On every handled packet
* If the packet is a message, save the messsage into receivedMessages
On every handled packet, if we have not heard from that sender in a period of time greater than timeAway, let's assume that they have been away from the network.
* In this case, we will resend them all the messages they have missed since they were gone
## Expected problems this implementation
* If the client has been away for less than 5 minutes and has received the previously sent message, the client will gracefully ignore it. This is thanks to PacketHistory::wasSeenRecently in PacketHistory.cpp.
* * If the client has been away for more than 5 minutes and we resend packets that they have already received, it's possible they will see duplicate messages. This should be unlikely but is still possible.
# Designed limitations
The Store and Forward plugin will subscribe to specific packet types and channels and only save those. This will both reduce the amount of data we will need to store and reduce the overhead on the network. Eg: There's no need to replay ACK packets nor is there's no need to replay old location packets.

Wyświetl plik

@ -105,6 +105,8 @@ build_flags =
lib_deps = lib_deps =
${arduino_base.lib_deps} ${arduino_base.lib_deps}
https://github.com/meshtastic/esp32_https_server.git https://github.com/meshtastic/esp32_https_server.git
adafruit/DHT sensor library@^1.4.1
adafruit/Adafruit Unified Sensor@^1.1.4
# Hmm - this doesn't work yet # Hmm - this doesn't work yet
# board_build.ldscript = linker/esp32.extram.bss.ld # board_build.ldscript = linker/esp32.extram.bss.ld
lib_ignore = segger_rtt lib_ignore = segger_rtt
@ -189,7 +191,7 @@ build_flags =
-Isdk-nrfxlib/crypto/nrf_oberon/include -Lsdk-nrfxlib/crypto/nrf_oberon/lib/cortex-m4/hard-float/ -lliboberon_3.0.7 -Isdk-nrfxlib/crypto/nrf_oberon/include -Lsdk-nrfxlib/crypto/nrf_oberon/lib/cortex-m4/hard-float/ -lliboberon_3.0.7
;-DCFG_DEBUG=3 ;-DCFG_DEBUG=3
src_filter = src_filter =
${arduino_base.src_filter} -<esp32/> -<nimble/> -<mesh/wifi/> -<mesh/http/> ${arduino_base.src_filter} -<esp32/> -<nimble/> -<mesh/wifi/> -<mesh/http/> -<plugins/esp32>
lib_ignore = lib_ignore =
BluetoothOTA BluetoothOTA
monitor_port = /dev/ttyACM1 monitor_port = /dev/ttyACM1
@ -330,7 +332,7 @@ lib_deps =
; The Portduino based sim environment on top of linux ; The Portduino based sim environment on top of linux
[env:linux] [env:linux]
platform = https://github.com/geeksville/platform-portduino.git platform = https://github.com/geeksville/platform-portduino.git
src_filter = ${env.src_filter} -<esp32/> -<nimble/> -<nrf52/> -<mesh/http/> src_filter = ${env.src_filter} -<esp32/> -<nimble/> -<nrf52/> -<mesh/http/> -<plugins/esp32>
build_flags = ${arduino_base.build_flags} -O0 build_flags = ${arduino_base.build_flags} -O0
framework = arduino framework = arduino
board = linux_x86_64 board = linux_x86_64

Wyświetl plik

@ -78,9 +78,9 @@ size_t RedirectablePrint::logDebug(const char *format, ...)
int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN;
int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN
r += printf("%02d:%02d:%02d ", hour, min, sec); r += printf("%02d:%02d:%02d %u ", hour, min, sec, millis() / 1000);
} else } else
r += printf("??:??:?? "); r += printf("??:??:?? %u ", millis() / 1000);
auto thread = concurrency::OSThread::currentThread; auto thread = concurrency::OSThread::currentThread;
if(thread) { if(thread) {

Wyświetl plik

@ -343,7 +343,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#undef GPS_RX_PIN #undef GPS_RX_PIN
#undef GPS_TX_PIN #undef GPS_TX_PIN
#define GPS_RX_PIN 36 #define GPS_RX_PIN 36
#define GPS_TX_PIN 39 #define GPS_TX_PIN 13
#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
@ -354,9 +354,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define VEXT_ENABLE 21 // active low, powers the oled display and the lora antenna boost #define VEXT_ENABLE 21 // active low, powers the oled display and the lora antenna boost
#define LED_PIN 25 // If defined we will blink this LED #define LED_PIN 25 // If defined we will blink this LED
#define BUTTON_PIN \ #define BUTTON_PIN 12 // If defined, this will be used for user button presses,
12 // If defined, this will be used for user button presses, if your board doesn't have a physical switch, you can wire one
// between this pin and ground
#define BUTTON_NEED_PULLUP #define BUTTON_NEED_PULLUP
#define USE_RF95 #define USE_RF95
@ -375,15 +374,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define GPS_RESET_N 10 #define GPS_RESET_N 10
#define GPS_EXTINT 23 // On MAX-M8 module pin name is EXTINT. On L70 module pin name is STANDBY. #define GPS_EXTINT 23 // On MAX-M8 module pin name is EXTINT. On L70 module pin name is STANDBY.
#define BATTERY_PIN 39 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage #define BATTERY_PIN 39 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define BATTERY_EN_PIN 14 // Voltage voltage divider enable pin connected to mosfet #define BATTERY_EN_PIN 14 // Voltage voltage divider enable pin connected to mosfet
#define I2C_SDA 4 // I2C pins for this board #define I2C_SDA 4 // I2C pins for this board
#define I2C_SCL 2 #define I2C_SCL 2
#define LED_PIN 12 // If defined we will blink this LED #define LED_PIN 12 // If defined we will blink this LED
//#define BUTTON_PIN 36 // If defined, this will be used for user button presses (ToDo problem on that line on debug screen --> Long press start!) //#define BUTTON_PIN 36 // If defined, this will be used for user button presses (ToDo problem on that line on debug screen -->
//#define BUTTON_NEED_PULLUP //GPIOs 34 to 39 are GPIs – input only pins. These pins dont have internal pull-ups or pull-down resistors. //Long press start!) #define BUTTON_NEED_PULLUP //GPIOs 34 to 39 are GPIs – input only pins. These pins dont have internal
//pull-ups or pull-down resistors.
#define USE_RF95 #define USE_RF95
#define LORA_DIO0 38 // a No connect on the SX1262 module #define LORA_DIO0 38 // a No connect on the SX1262 module

Wyświetl plik

@ -305,12 +305,15 @@ void setup()
#ifdef BUTTON_PIN #ifdef BUTTON_PIN
#ifndef NO_ESP32 #ifndef NO_ESP32
// If BUTTON_PIN is held down during the startup process,
// force the device to go into a SoftAP mode.
bool forceSoftAP = 0; bool forceSoftAP = 0;
// If the button is connected to GPIO 12, don't enable the ability to use
// meshtasticAdmin on the device.
pinMode(BUTTON_PIN, INPUT); pinMode(BUTTON_PIN, INPUT);
#ifdef BUTTON_NEED_PULLUP #ifdef BUTTON_NEED_PULLUP
gpio_pullup_en((gpio_num_t)BUTTON_PIN); gpio_pullup_en((gpio_num_t)BUTTON_PIN);
delay(10);
#endif #endif
// BUTTON_PIN is pulled high by a 12k resistor. // BUTTON_PIN is pulled high by a 12k resistor.

Wyświetl plik

@ -0,0 +1,12 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.4 */
#include "environmental_measurement.pb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
PB_BIND(EnvironmentalMeasurement, EnvironmentalMeasurement, AUTO)

Wyświetl plik

@ -0,0 +1,53 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.4 */
#ifndef PB_ENVIRONMENTAL_MEASUREMENT_PB_H_INCLUDED
#define PB_ENVIRONMENTAL_MEASUREMENT_PB_H_INCLUDED
#include <pb.h>
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
/* Struct definitions */
typedef struct _EnvironmentalMeasurement {
float temperature;
float relative_humidity;
float barometric_pressure;
} EnvironmentalMeasurement;
#ifdef __cplusplus
extern "C" {
#endif
/* Initializer values for message structs */
#define EnvironmentalMeasurement_init_default {0, 0, 0}
#define EnvironmentalMeasurement_init_zero {0, 0, 0}
/* Field tags (for use in manual encoding/decoding) */
#define EnvironmentalMeasurement_temperature_tag 1
#define EnvironmentalMeasurement_relative_humidity_tag 2
#define EnvironmentalMeasurement_barometric_pressure_tag 3
/* Struct field encoding specification for nanopb */
#define EnvironmentalMeasurement_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, FLOAT, temperature, 1) \
X(a, STATIC, SINGULAR, FLOAT, relative_humidity, 2) \
X(a, STATIC, SINGULAR, FLOAT, barometric_pressure, 3)
#define EnvironmentalMeasurement_CALLBACK NULL
#define EnvironmentalMeasurement_DEFAULT NULL
extern const pb_msgdesc_t EnvironmentalMeasurement_msg;
/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
#define EnvironmentalMeasurement_fields &EnvironmentalMeasurement_msg
/* Maximum encoded size of messages (where known) */
#define EnvironmentalMeasurement_size 15
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif

Wyświetl plik

@ -20,6 +20,7 @@ typedef enum _PortNum {
PortNum_ADMIN_APP = 6, PortNum_ADMIN_APP = 6,
PortNum_REPLY_APP = 32, PortNum_REPLY_APP = 32,
PortNum_IP_TUNNEL_APP = 33, PortNum_IP_TUNNEL_APP = 33,
PortNum_ENVIRONMENTAL_MEASUREMENT_APP = 34,
PortNum_SERIAL_APP = 64, PortNum_SERIAL_APP = 64,
PortNum_STORE_FORWARD_APP = 65, PortNum_STORE_FORWARD_APP = 65,
PortNum_RANGE_TEST_APP = 66, PortNum_RANGE_TEST_APP = 66,

Wyświetl plik

@ -6,7 +6,7 @@
#include "configuration.h" #include "configuration.h"
#include <Arduino.h> #include <Arduino.h>
#include <assert.h> //#include <assert.h>
/* /*

Wyświetl plik

@ -1,9 +1,13 @@
#include "plugins/ExternalNotificationPlugin.h" #include "plugins/ExternalNotificationPlugin.h"
#include "plugins/NodeInfoPlugin.h" #include "plugins/NodeInfoPlugin.h"
#include "plugins/PositionPlugin.h" #include "plugins/PositionPlugin.h"
#include "plugins/RangeTestPlugin.h"
#include "plugins/RemoteHardwarePlugin.h" #include "plugins/RemoteHardwarePlugin.h"
#include "plugins/ReplyPlugin.h" #include "plugins/ReplyPlugin.h"
#include "plugins/TextMessagePlugin.h"
#ifndef NO_ESP32
#include "plugins/esp32/EnvironmentalMeasurementPlugin.h"
#include "plugins/esp32/RangeTestPlugin.h"
#endif
#include "plugins/SerialPlugin.h" #include "plugins/SerialPlugin.h"
#include "plugins/StoreForwardPlugin.h" #include "plugins/StoreForwardPlugin.h"
#include "plugins/TextMessagePlugin.h" #include "plugins/TextMessagePlugin.h"
@ -35,7 +39,12 @@ void setupPlugins()
*/ */
new SerialPlugin(); new SerialPlugin();
new ExternalNotificationPlugin(); new ExternalNotificationPlugin();
//storeForwardPlugin = new StoreForwardPlugin();
rangeTestPlugin = new RangeTestPlugin(); // rangeTestPlugin = new RangeTestPlugin();
storeForwardPlugin = new StoreForwardPlugin();
new RangeTestPlugin();
// new StoreForwardPlugin();
new EnvironmentalMeasurementPlugin();
#endif #endif
} }

Wyświetl plik

@ -1,174 +0,0 @@
#include "RangeTestPlugin.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "RTC.h"
#include "Router.h"
#include "configuration.h"
#include <Arduino.h>
#include <assert.h>
/*
As a sender, I can send packets every n-seonds. These packets include an incramented PacketID.
As a receiver, I can receive packets from multiple senders. These packets can be saved to the spiffs.
*/
RangeTestPlugin *rangeTestPlugin;
RangeTestPluginRadio *rangeTestPluginRadio;
RangeTestPlugin::RangeTestPlugin() : concurrency::OSThread("RangeTestPlugin") {}
uint16_t packetSequence = 0;
// char serialStringChar[Constants_DATA_PAYLOAD_LEN];
int32_t RangeTestPlugin::runOnce()
{
#ifndef NO_ESP32
/*
Uncomment the preferences below if you want to use the plugin
without having to configure it from the PythonAPI or WebUI.
*/
// radioConfig.preferences.range_test_plugin_enabled = 1;
// radioConfig.preferences.range_test_plugin_sender = 0;
// radioConfig.preferences.fixed_position = 1;
uint32_t senderHeartbeat = radioConfig.preferences.range_test_plugin_sender * 1000;
if (radioConfig.preferences.range_test_plugin_enabled) {
if (firstTime) {
// Interface with the serial peripheral from in here.
rangeTestPluginRadio = new RangeTestPluginRadio();
firstTime = 0;
if (radioConfig.preferences.range_test_plugin_sender) {
DEBUG_MSG("Initializing Range Test Plugin -- Sender\n");
return (senderHeartbeat);
} else {
DEBUG_MSG("Initializing Range Test Plugin -- Receiver\n");
return (500);
}
} else {
if (radioConfig.preferences.range_test_plugin_sender) {
// If sender
DEBUG_MSG("Range Test Plugin - Sending heartbeat every %d ms\n", (senderHeartbeat));
DEBUG_MSG("gpsStatus->getLatitude() %d\n", gpsStatus->getLatitude());
DEBUG_MSG("gpsStatus->getLongitude() %d\n", gpsStatus->getLongitude());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("gpsStatus->getDOP() %d\n", gpsStatus->getDOP());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("pref.fixed_position() %d\n", radioConfig.preferences.fixed_position);
rangeTestPluginRadio->sendPayload();
return ((senderHeartbeat));
} else {
// Otherwise, we're a receiver.
return (500);
}
// TBD
}
} else {
DEBUG_MSG("Range Test Plugin - Disabled\n");
}
return (INT32_MAX);
#endif
}
MeshPacket *RangeTestPluginRadio::allocReply()
{
auto reply = allocDataPacket(); // Allocate a packet for sending
return reply;
}
void RangeTestPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
{
MeshPacket *p = allocReply();
p->to = dest;
p->decoded.want_response = wantReplies;
p->want_ack = true;
packetSequence++;
static char heartbeatString[20];
snprintf(heartbeatString, sizeof(heartbeatString), "seq %d", packetSequence);
p->decoded.payload.size = strlen(heartbeatString); // You must specify how many bytes are in the reply
memcpy(p->decoded.payload.bytes, heartbeatString, p->decoded.payload.size);
service.sendToMesh(p);
}
bool RangeTestPluginRadio::handleReceived(const MeshPacket &mp)
{
#ifndef NO_ESP32
if (radioConfig.preferences.range_test_plugin_enabled) {
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);
if (mp.from != nodeDB.getNodeNum()) {
// DEBUG_MSG("* * Message came from the mesh\n");
// Serial2.println("* * Message came from the mesh");
// Serial2.printf("%s", p.payload.bytes);
/*
*/
NodeInfo *n = nodeDB.getNode(mp.from);
DEBUG_MSG("-----------------------------------------\n");
DEBUG_MSG("p.payload.bytes \"%s\"\n", p.payload.bytes);
DEBUG_MSG("p.payload.size %d\n", p.payload.size);
DEBUG_MSG("---- Received Packet:\n");
DEBUG_MSG("mp.from %d\n", mp.from);
DEBUG_MSG("mp.rx_snr %f\n", mp.rx_snr);
DEBUG_MSG("mp.hop_limit %d\n", mp.hop_limit);
//deprecated and unpopulated for sometime
//DEBUG_MSG("mp.decoded.position.latitude_i %d\n", mp.decoded.position.latitude_i);
//DEBUG_MSG("mp.decoded.position.longitude_i %d\n", mp.decoded.position.longitude_i);
DEBUG_MSG("---- Node Information of Received Packet (mp.from):\n");
DEBUG_MSG("n->user.long_name %s\n", n->user.long_name);
DEBUG_MSG("n->user.short_name %s\n", n->user.short_name);
DEBUG_MSG("n->user.macaddr %X\n", n->user.macaddr);
DEBUG_MSG("n->has_position %d\n", n->has_position);
DEBUG_MSG("n->position.latitude_i %d\n", n->position.latitude_i);
DEBUG_MSG("n->position.longitude_i %d\n", n->position.longitude_i);
DEBUG_MSG("n->position.battery_level %d\n", n->position.battery_level);
DEBUG_MSG("---- Current device location information:\n");
DEBUG_MSG("gpsStatus->getLatitude() %d\n", gpsStatus->getLatitude());
DEBUG_MSG("gpsStatus->getLongitude() %d\n", gpsStatus->getLongitude());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("gpsStatus->getDOP() %d\n", gpsStatus->getDOP());
DEBUG_MSG("-----------------------------------------\n");
}
} else {
DEBUG_MSG("Range Test Plugin Disabled\n");
}
#endif
return true; // Let others look at this message also if they want
}

Wyświetl plik

@ -5,31 +5,16 @@
#include "Router.h" #include "Router.h"
#include "configuration.h" #include "configuration.h"
#include <Arduino.h> #include <Arduino.h>
#include <map>
#include <assert.h>
#define STORE_RECORDS 5000
#define BYTES_PER_RECORDS 512
struct sfRecord
{
uint8_t bytes[BYTES_PER_RECORDS];
uint32_t timestamp; // Time the packet was received
};
struct sfRecord records[STORE_RECORDS];
#define STOREFORWARDPLUGIN_ENABLED 0
StoreForwardPlugin *storeForwardPlugin; StoreForwardPlugin *storeForwardPlugin;
StoreForwardPluginRadio *storeForwardPluginRadio; StoreForwardPluginRadio *storeForwardPluginRadio;
StoreForwardPlugin::StoreForwardPlugin() : concurrency::OSThread("SerialPlugin") {}
// char serialStringChar[Constants_DATA_PAYLOAD_LEN]; StoreForwardPlugin::StoreForwardPlugin() : concurrency::OSThread("StoreForwardPlugin") {}
int32_t StoreForwardPlugin::runOnce() int32_t StoreForwardPlugin::runOnce()
{ {
#ifndef NO_ESP32 #ifndef NO_ESP32
/* /*
@ -37,21 +22,25 @@ int32_t StoreForwardPlugin::runOnce()
without having to configure it from the PythonAPI or WebUI. without having to configure it from the PythonAPI or WebUI.
*/ */
//radioConfig.preferences.store_forward_plugin_enabled = 1; // radioConfig.preferences.store_forward_plugin_enabled = 1;
//radioConfig.preferences.store_forward_plugin_records = 80; // radioConfig.preferences.is_router = 1;
if (radioConfig.preferences.store_forward_plugin_enabled) { if (radioConfig.preferences.store_forward_plugin_enabled) {
if (firstTime) { if (firstTime) {
// Interface with the serial peripheral from in here. /*
DEBUG_MSG("Initializing Store & Forward Plugin\n"); */
// Router
if (radioConfig.preferences.is_router) { if (radioConfig.preferences.is_router) {
DEBUG_MSG("Initializing Store & Forward Plugin - Enabled\n");
// Router
if (ESP.getPsramSize()) { if (ESP.getPsramSize()) {
if (ESP.getFreePsram() <= 1024 * 1024) { if (ESP.getFreePsram() >= 1024 * 1024) {
// Do the startup here // Do the startup here
storeForwardPluginRadio = new StoreForwardPluginRadio();
firstTime = 0;
} else { } else {
DEBUG_MSG("Device has less than 1M of PSRAM free. Aborting startup.\n"); DEBUG_MSG("Device has less than 1M of PSRAM free. Aborting startup.\n");
@ -67,20 +56,21 @@ int32_t StoreForwardPlugin::runOnce()
return (INT32_MAX); return (INT32_MAX);
} }
// Non-Router
} else { } else {
DEBUG_MSG("Initializing Store & Forward Plugin - Enabled but is_router is not turned on.\n");
DEBUG_MSG(
"Initializing Store & Forward Plugin - If you want to use this plugin, you must also turn on is_router.\n");
// Non-Router
return (30 * 1000);
} }
storeForwardPluginRadio = new StoreForwardPluginRadio();
firstTime = 0;
} else { } else {
// TBD // What do we do if it's not our first time?
// Maybe some cleanup functions?
} }
return (10);
} else { } else {
DEBUG_MSG("Store & Forward Plugin - Disabled\n"); DEBUG_MSG("Store & Forward Plugin - Disabled\n");
@ -88,6 +78,44 @@ int32_t StoreForwardPlugin::runOnce()
} }
#endif #endif
return (INT32_MAX);
}
// We saw a node.
uint32_t StoreForwardPlugin::sawNode(uint32_t node)
{
/*
TODO: Move receivedRecord into the PSRAM
TODO: Gracefully handle the case where we run out of records.
Maybe replace the oldest record that hasn't been seen in a while and assume they won't be back.
TODO: Implment this as a std::map for quicker lookups (maybe it doesn't matter?).
*/
DEBUG_MSG("looking for node - %i\n", node);
for (int i = 0; i < 50; i++) {
DEBUG_MSG("Iterating through the seen nodes - %d %d %d\n", i, receivedRecord[i][0], receivedRecord[i][1]);
// First time seeing that node.
if (receivedRecord[i][0] == 0) {
DEBUG_MSG("New node! Woohoo! Win!\n");
receivedRecord[i][0] = node;
receivedRecord[i][1] = millis();
return receivedRecord[i][1];
}
// We've seen this node before.
if (receivedRecord[i][0] == node) {
DEBUG_MSG("We've seen this node before\n");
uint32_t lastSaw = receivedRecord[i][1];
receivedRecord[i][1] = millis();
return lastSaw;
}
}
return 0;
} }
MeshPacket *StoreForwardPluginRadio::allocReply() MeshPacket *StoreForwardPluginRadio::allocReply()
@ -104,50 +132,25 @@ void StoreForwardPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
p->to = dest; p->to = dest;
p->decoded.want_response = wantReplies; p->decoded.want_response = wantReplies;
//p->want_ack = SERIALPLUGIN_ACK;
// 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); service.sendToMesh(p);
} }
bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp) bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp)
{ {
#ifndef NO_ESP32 #ifndef NO_ESP32
if (radioConfig.preferences.store_forward_plugin_enabled) {
// auto &p = mp.decoded.data;
if (STOREFORWARDPLUGIN_ENABLED) { if (mp.from != nodeDB.getNodeNum()) {
DEBUG_MSG("Store & Forward Plugin -- Print Start ---------- ---------- ---------- ---------- ----------\n\n\n");
auto &p = mp.decoded; printPacket("----- PACKET FROM RADIO", &mp);
// DEBUG_MSG("Received text msg self=0x%0x, from=0x%0x, to=0x%0x, id=%d, msg=%.*s\n", // DEBUG_MSG("\n\nStore & Forward Plugin -- Print End ---------- ---------- ---------- ---------- ----------\n");
// nodeDB.getNodeNum(), mp.from, mp.to, mp.id, p.payload.size, p.payload.bytes); uint32_t sawTime = storeForwardPlugin->sawNode(mp.from);
DEBUG_MSG("Last Saw this node %d, %d millis ago\n", mp.from, (millis() - sawTime));
if (mp.from == nodeDB.getNodeNum()) {
/*
* 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) {
// 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);
}
}
} else {
// DEBUG_MSG("* * Message came from the mesh\n");
// Serial2.println("* * Message came from the mesh");
Serial2.printf("%s", p.payload.bytes);
} }
} else { } else {
DEBUG_MSG("Serial Plugin Disabled\n"); DEBUG_MSG("Store & Forward Plugin - Disabled\n");
} }
#endif #endif

Wyświetl plik

@ -10,9 +10,22 @@ class StoreForwardPlugin : private concurrency::OSThread
{ {
bool firstTime = 1; bool firstTime = 1;
// TODO: Move this into the PSRAM
// TODO: Allow configuration of the maximum number of records.
uint32_t receivedRecord[50][2] = {{0}};
public: public:
StoreForwardPlugin(); StoreForwardPlugin();
/**
Update our local reference of when we last saw that node.
@return 0 if we have never seen that node before otherwise return the last time we saw the node.
*/
uint32_t sawNode(uint32_t);
private:
// Nothing here
protected: protected:
virtual int32_t runOnce(); virtual int32_t runOnce();
}; };
@ -20,21 +33,16 @@ class StoreForwardPlugin : private concurrency::OSThread
extern StoreForwardPlugin *storeForwardPlugin; extern StoreForwardPlugin *storeForwardPlugin;
/* /*
* Radio interface for SerialPlugin * Radio interface for StoreForwardPlugin
* *
*/ */
class StoreForwardPluginRadio : public SinglePortPlugin class StoreForwardPluginRadio : public SinglePortPlugin
{ {
uint32_t lastRxID; // uint32_t lastRxID;
public: public:
/* StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_STORE_FORWARD_APP) {}
TODO: Switch this to PortNum_SERIAL_APP once the change is able to be merged back here // StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_TEXT_MESSAGE_APP) {}
from the main code.
*/
// SerialPluginRadio() : SinglePortPlugin("SerialPluginRadio", PortNum_TEXT_MESSAGE_APP) {}
StoreForwardPluginRadio() : SinglePortPlugin("SerialPluginRadio", PortNum_SERIAL_APP) {}
/** /**
* Send our payload into the mesh * Send our payload into the mesh
@ -44,6 +52,8 @@ class StoreForwardPluginRadio : public SinglePortPlugin
protected: protected:
virtual MeshPacket *allocReply(); virtual MeshPacket *allocReply();
virtual bool wantPortnum(PortNum p) { return true; };
/** Called to handle a particular incoming message /** Called to handle a particular incoming message
@return true if you've guaranteed you've handled this message and no other handlers should be considered for it @return true if you've guaranteed you've handled this message and no other handlers should be considered for it
@ -51,4 +61,4 @@ class StoreForwardPluginRadio : public SinglePortPlugin
virtual bool handleReceived(const MeshPacket &mp); virtual bool handleReceived(const MeshPacket &mp);
}; };
extern StoreForwardPluginRadio *storeForwardPluginRadio; extern StoreForwardPluginRadio *storeForwardPluginRadio;

Wyświetl plik

@ -0,0 +1,102 @@
#include "EnvironmentalMeasurementPlugin.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "RTC.h"
#include "Router.h"
#include "configuration.h"
#include "main.h"
#include "../mesh/generated/environmental_measurement.pb.h"
#include <DHT.h>
EnvironmentalMeasurementPlugin *environmentalMeasurementPlugin;
EnvironmentalMeasurementPluginRadio *environmentalMeasurementPluginRadio;
EnvironmentalMeasurementPlugin::EnvironmentalMeasurementPlugin() : concurrency::OSThread("EnvironmentalMeasurementPlugin") {}
uint32_t sensor_read_error_count = 0;
#define DHT_11_GPIO_PIN 13
//TODO: Make a related radioconfig preference to allow less-frequent reads
#define DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS 1000
#define SENSOR_READ_ERROR_COUNT_THRESHOLD 5
#define SENSOR_READ_MULTIPLIER 3
DHT dht(DHT_11_GPIO_PIN,DHT11);
int32_t EnvironmentalMeasurementPlugin::runOnce() {
#ifndef NO_ESP32
if (firstTime) {
// This is the first time the OSThread library has called this function, so do some setup
DEBUG_MSG("Initializing Environmental Measurement Plugin -- Sender\n");
environmentalMeasurementPluginRadio = new EnvironmentalMeasurementPluginRadio();
firstTime = 0;
// begin reading measurements from the sensor
// DHT have a max read-rate of 1HZ, so we should wait at least 1 second
// after initializing the sensor before we try to read from it.
// returning the interval here means that the next time OSThread
// calls our plugin, we'll run the other branch of this if statement
// and actually do a "sendOurEnvironmentalMeasurement()"
dht.begin();
return(DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
}
else {
// this is not the first time OSThread library has called this function
// so just do what we intend to do on the interval
if(sensor_read_error_count > SENSOR_READ_ERROR_COUNT_THRESHOLD)
{
DEBUG_MSG("Environmental Measurement Plugin: DISABLED; The SENSOR_READ_ERROR_COUNT_THRESHOLD has been exceed: %d\n",SENSOR_READ_ERROR_COUNT_THRESHOLD);
return(DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
}
else if (sensor_read_error_count > 0){
DEBUG_MSG("Environmental Measurement Plugin: There have been %d sensor read failures.\n",sensor_read_error_count);
}
if (! environmentalMeasurementPluginRadio->sendOurEnvironmentalMeasurement() ){
// if we failed to read the sensor, then try again
// as soon as we can according to the maximum polling frequency
return(DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
}
}
// The return of runOnce is an int32 representing the desired number of
// miliseconds until the function should be called again by the
// OSThread library.
return(SENSOR_READ_MULTIPLIER * DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
#endif
}
bool EnvironmentalMeasurementPluginRadio::handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement *p)
{
// This plugin doesn't really do anything with the messages it receives.
return false; // Let others look at this message also if they want
}
bool EnvironmentalMeasurementPluginRadio::sendOurEnvironmentalMeasurement(NodeNum dest, bool wantReplies)
{
EnvironmentalMeasurement m;
m.barometric_pressure = 0; // TODO: Add support for barometric sensors
m.relative_humidity = dht.readHumidity();
m.temperature = dht.readTemperature();;
DEBUG_MSG("-----------------------------------------\n");
DEBUG_MSG("Environmental Measurement Plugin: Read data\n");
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", m.relative_humidity);
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", m.temperature);
if (isnan(m.relative_humidity) || isnan(m.temperature) ){
sensor_read_error_count++;
DEBUG_MSG("Environmental Measurement Plugin: FAILED TO READ DATA\n");
return false;
}
sensor_read_error_count = 0;
MeshPacket *p = allocDataProtobuf(m);
p->to = dest;
p->decoded.want_response = wantReplies;
service.sendToMesh(p);
return true;
}

Wyświetl plik

@ -0,0 +1,45 @@
#pragma once
#include "ProtobufPlugin.h"
#include "../mesh/generated/environmental_measurement.pb.h"
class EnvironmentalMeasurementPlugin : private concurrency::OSThread
{
bool firstTime = 1;
public:
EnvironmentalMeasurementPlugin();
protected:
virtual int32_t runOnce();
};
extern EnvironmentalMeasurementPlugin *environmentalMeasurementPlugin;
/**
* EnvironmentalMeasurementPluginRadio plugin for sending/receiving environmental measurements to/from the mesh
*/
class EnvironmentalMeasurementPluginRadio : public ProtobufPlugin<EnvironmentalMeasurement>
{
public:
/** Constructor
* name is for debugging output
*/
EnvironmentalMeasurementPluginRadio() : ProtobufPlugin("EnvironmentalMeasurement", PortNum_ENVIRONMENTAL_MEASUREMENT_APP, &EnvironmentalMeasurement_msg) {}
/**
* Send our EnvironmentalMeasurement into the mesh
*/
bool sendOurEnvironmentalMeasurement(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
protected:
/** Called to handle a particular incoming message
@return true if you've guaranteed you've handled this message and no other handlers should be considered for it
*/
virtual bool handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement *p);
};
extern EnvironmentalMeasurementPluginRadio *environmentalMeasurementPluginRadio;

Wyświetl plik

@ -0,0 +1,315 @@
#include "RangeTestPlugin.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "PowerFSM.h"
#include "RTC.h"
#include "Router.h"
#include "configuration.h"
#include <Arduino.h>
#include <SPIFFS.h>
//#include <assert.h>
/*
As a sender, I can send packets every n-seonds. These packets include an incramented PacketID.
As a receiver, I can receive packets from multiple senders. These packets can be saved to the spiffs.
*/
RangeTestPlugin *rangeTestPlugin;
RangeTestPluginRadio *rangeTestPluginRadio;
RangeTestPlugin::RangeTestPlugin() : concurrency::OSThread("RangeTestPlugin") {}
uint32_t packetSequence = 0;
#define SEC_PER_DAY 86400
#define SEC_PER_HOUR 3600
#define SEC_PER_MIN 60
int32_t RangeTestPlugin::runOnce()
{
#ifndef NO_ESP32
/*
Uncomment the preferences below if you want to use the plugin
without having to configure it from the PythonAPI or WebUI.
*/
//radioConfig.preferences.range_test_plugin_enabled = 1;
//radioConfig.preferences.range_test_plugin_sender = 45;
//radioConfig.preferences.range_test_plugin_save = 1;
// Fixed position is useful when testing indoors.
// radioConfig.preferences.fixed_position = 1;
uint32_t senderHeartbeat = radioConfig.preferences.range_test_plugin_sender * 1000;
if (radioConfig.preferences.range_test_plugin_enabled) {
if (firstTime) {
// Interface with the serial peripheral from in here.
rangeTestPluginRadio = new RangeTestPluginRadio();
firstTime = 0;
if (radioConfig.preferences.range_test_plugin_sender) {
DEBUG_MSG("Initializing Range Test Plugin -- Sender\n");
return (5000); // Sending first message 5 seconds after initilization.
} else {
DEBUG_MSG("Initializing Range Test Plugin -- Receiver\n");
return (500);
}
} else {
if (radioConfig.preferences.range_test_plugin_sender) {
// If sender
DEBUG_MSG("Range Test Plugin - Sending heartbeat every %d ms\n", (senderHeartbeat));
DEBUG_MSG("gpsStatus->getLatitude() %d\n", gpsStatus->getLatitude());
DEBUG_MSG("gpsStatus->getLongitude() %d\n", gpsStatus->getLongitude());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("gpsStatus->getDOP() %d\n", gpsStatus->getDOP());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("pref.fixed_position() %d\n", radioConfig.preferences.fixed_position);
rangeTestPluginRadio->sendPayload();
return (senderHeartbeat);
} else {
// Otherwise, we're a receiver.
return (500);
}
// TBD
}
} else {
DEBUG_MSG("Range Test Plugin - Disabled\n");
}
return (INT32_MAX);
#endif
}
MeshPacket *RangeTestPluginRadio::allocReply()
{
auto reply = allocDataPacket(); // Allocate a packet for sending
return reply;
}
void RangeTestPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
{
MeshPacket *p = allocReply();
p->to = dest;
p->decoded.want_response = wantReplies;
p->want_ack = true;
packetSequence++;
static char heartbeatString[20];
snprintf(heartbeatString, sizeof(heartbeatString), "seq %d", packetSequence);
p->decoded.payload.size = strlen(heartbeatString); // You must specify how many bytes are in the reply
memcpy(p->decoded.payload.bytes, heartbeatString, p->decoded.payload.size);
service.sendToMesh(p);
// TODO: Handle this better. We want to keep the phone awake otherwise it stops sending.
powerFSM.trigger(EVENT_CONTACT_FROM_PHONE);
}
bool RangeTestPluginRadio::handleReceived(const MeshPacket &mp)
{
#ifndef NO_ESP32
if (radioConfig.preferences.range_test_plugin_enabled) {
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);
if (mp.from != nodeDB.getNodeNum()) {
// DEBUG_MSG("* * Message came from the mesh\n");
// Serial2.println("* * Message came from the mesh");
// Serial2.printf("%s", p.payload.bytes);
/*
*/
NodeInfo *n = nodeDB.getNode(mp.from);
if (radioConfig.preferences.range_test_plugin_save) {
appendFile(mp);
}
DEBUG_MSG("-----------------------------------------\n");
DEBUG_MSG("p.payload.bytes \"%s\"\n", p.payload.bytes);
DEBUG_MSG("p.payload.size %d\n", p.payload.size);
DEBUG_MSG("---- Received Packet:\n");
DEBUG_MSG("mp.from %d\n", mp.from);
DEBUG_MSG("mp.rx_snr %f\n", mp.rx_snr);
DEBUG_MSG("mp.hop_limit %d\n", mp.hop_limit);
// DEBUG_MSG("mp.decoded.position.latitude_i %d\n", mp.decoded.position.latitude_i); // Depricated
// DEBUG_MSG("mp.decoded.position.longitude_i %d\n", mp.decoded.position.longitude_i); // Depricated
DEBUG_MSG("---- Node Information of Received Packet (mp.from):\n");
DEBUG_MSG("n->user.long_name %s\n", n->user.long_name);
DEBUG_MSG("n->user.short_name %s\n", n->user.short_name);
// DEBUG_MSG("n->user.macaddr %X\n", n->user.macaddr);
DEBUG_MSG("n->has_position %d\n", n->has_position);
DEBUG_MSG("n->position.latitude_i %d\n", n->position.latitude_i);
DEBUG_MSG("n->position.longitude_i %d\n", n->position.longitude_i);
DEBUG_MSG("n->position.battery_level %d\n", n->position.battery_level);
DEBUG_MSG("---- Current device location information:\n");
DEBUG_MSG("gpsStatus->getLatitude() %d\n", gpsStatus->getLatitude());
DEBUG_MSG("gpsStatus->getLongitude() %d\n", gpsStatus->getLongitude());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("gpsStatus->getDOP() %d\n", gpsStatus->getDOP());
DEBUG_MSG("-----------------------------------------\n");
}
} else {
DEBUG_MSG("Range Test Plugin Disabled\n");
}
#endif
return true; // Let others look at this message also if they want
}
/// Ported from my old java code, returns distance in meters along the globe
/// surface (by magic?)
float RangeTestPluginRadio::latLongToMeter(double lat_a, double lng_a, double lat_b, double lng_b)
{
double pk = (180 / 3.14169);
double a1 = lat_a / pk;
double a2 = lng_a / pk;
double b1 = lat_b / pk;
double b2 = lng_b / pk;
double cos_b1 = cos(b1);
double cos_a1 = cos(a1);
double t1 = cos_a1 * cos(a2) * cos_b1 * cos(b2);
double t2 = cos_a1 * sin(a2) * cos_b1 * sin(b2);
double t3 = sin(a1) * sin(b1);
double tt = acos(t1 + t2 + t3);
if (isnan(tt))
tt = 0.0; // Must have been the same point?
return (float)(6366000 * tt);
}
bool RangeTestPluginRadio::appendFile(const MeshPacket &mp)
{
auto &p = mp.decoded;
NodeInfo *n = nodeDB.getNode(mp.from);
/*
DEBUG_MSG("-----------------------------------------\n");
DEBUG_MSG("p.payload.bytes \"%s\"\n", p.payload.bytes);
DEBUG_MSG("p.payload.size %d\n", p.payload.size);
DEBUG_MSG("---- Received Packet:\n");
DEBUG_MSG("mp.from %d\n", mp.from);
DEBUG_MSG("mp.rx_snr %f\n", mp.rx_snr);
DEBUG_MSG("mp.hop_limit %d\n", mp.hop_limit);
// DEBUG_MSG("mp.decoded.position.latitude_i %d\n", mp.decoded.position.latitude_i); // Depricated
// DEBUG_MSG("mp.decoded.position.longitude_i %d\n", mp.decoded.position.longitude_i); // Depricated
DEBUG_MSG("---- Node Information of Received Packet (mp.from):\n");
DEBUG_MSG("n->user.long_name %s\n", n->user.long_name);
DEBUG_MSG("n->user.short_name %s\n", n->user.short_name);
DEBUG_MSG("n->user.macaddr %X\n", n->user.macaddr);
DEBUG_MSG("n->has_position %d\n", n->has_position);
DEBUG_MSG("n->position.latitude_i %d\n", n->position.latitude_i);
DEBUG_MSG("n->position.longitude_i %d\n", n->position.longitude_i);
DEBUG_MSG("n->position.battery_level %d\n", n->position.battery_level);
DEBUG_MSG("---- Current device location information:\n");
DEBUG_MSG("gpsStatus->getLatitude() %d\n", gpsStatus->getLatitude());
DEBUG_MSG("gpsStatus->getLongitude() %d\n", gpsStatus->getLongitude());
DEBUG_MSG("gpsStatus->getHasLock() %d\n", gpsStatus->getHasLock());
DEBUG_MSG("gpsStatus->getDOP() %d\n", gpsStatus->getDOP());
DEBUG_MSG("-----------------------------------------\n");
*/
if (!SPIFFS.begin(true)) {
DEBUG_MSG("An Error has occurred while mounting SPIFFS\n");
return 0;
}
if (SPIFFS.totalBytes() - SPIFFS.usedBytes() < 51200) {
DEBUG_MSG("SPIFFS doesn't have enough free space. Abourting write.\n");
return 0;
}
// If the file doesn't exist, write the header.
if (!SPIFFS.exists("/static/rangetest.csv")) {
//--------- Write to file
File fileToWrite = SPIFFS.open("/static/rangetest.csv", FILE_WRITE);
if (!fileToWrite) {
DEBUG_MSG("There was an error opening the file for writing\n");
return 0;
}
if (fileToWrite.println("time,from,sender name,sender lat,sender long,rx lat,rx long,rx snr,distance,payload")) {
DEBUG_MSG("File was written\n");
} else {
DEBUG_MSG("File write failed\n");
}
fileToWrite.close();
}
//--------- Apend content to file
File fileToAppend = SPIFFS.open("/static/rangetest.csv", FILE_APPEND);
if (!fileToAppend) {
DEBUG_MSG("There was an error opening the file for appending\n");
return 0;
}
struct timeval tv;
if (!gettimeofday(&tv, NULL)) {
long hms = tv.tv_sec % SEC_PER_DAY;
// hms += tz.tz_dsttime * SEC_PER_HOUR;
// hms -= tz.tz_minuteswest * SEC_PER_MIN;
// mod `hms` to ensure in positive range of [0...SEC_PER_DAY)
hms = (hms + SEC_PER_DAY) % SEC_PER_DAY;
// Tear apart hms into h:m:s
int hour = hms / SEC_PER_HOUR;
int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN;
int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN
fileToAppend.printf("%02d:%02d:%02d,", hour, min, sec); // Time
} else {
fileToAppend.printf("??:??:??,"); // Time
}
fileToAppend.printf("%d,", mp.from); // From
fileToAppend.printf("%s,", n->user.long_name); // Long Name
fileToAppend.printf("%f,", n->position.latitude_i * 1e-7); // Sender Lat
fileToAppend.printf("%f,", n->position.longitude_i * 1e-7); // Sender Long
fileToAppend.printf("%f,", gpsStatus->getLatitude() * 1e-7); // RX Lat
fileToAppend.printf("%f,", gpsStatus->getLongitude() * 1e-7); // RX Long
fileToAppend.printf("%f,", mp.rx_snr); // RX SNR
if (n->position.latitude_i && n->position.longitude_i && gpsStatus->getLatitude() && gpsStatus->getLongitude()) {
float distance = latLongToMeter(n->position.latitude_i * 1e-7, n->position.longitude_i * 1e-7,
gpsStatus->getLatitude() * 1e-7, gpsStatus->getLongitude() * 1e-7);
fileToAppend.printf("%f,", distance); // Distance in meters
} else {
fileToAppend.printf("0,");
}
// TODO: If quotes are found in the payload, it has to be escaped.
fileToAppend.printf("\"%s\"\n", p.payload.bytes);
fileToAppend.close();
return 1;
}

Wyświetl plik

@ -28,7 +28,6 @@ class RangeTestPluginRadio : public SinglePortPlugin
uint32_t lastRxID; uint32_t lastRxID;
public: public:
RangeTestPluginRadio() : SinglePortPlugin("RangeTestPluginRadio", PortNum_TEXT_MESSAGE_APP) {} RangeTestPluginRadio() : SinglePortPlugin("RangeTestPluginRadio", PortNum_TEXT_MESSAGE_APP) {}
/** /**
@ -36,6 +35,16 @@ class RangeTestPluginRadio : public SinglePortPlugin
*/ */
void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
/**
* Append range test data to the file on the spiffs
*/
bool appendFile(const MeshPacket &mp);
/**
* Kevin's magical calculation of two points to meters.
*/
float latLongToMeter(double lat_a, double lng_a, double lat_b, double lng_b);
protected: protected:
virtual MeshPacket *allocReply(); virtual MeshPacket *allocReply();
@ -46,4 +55,4 @@ class RangeTestPluginRadio : public SinglePortPlugin
virtual bool handleReceived(const MeshPacket &mp); virtual bool handleReceived(const MeshPacket &mp);
}; };
extern RangeTestPluginRadio *rangeTestPluginRadio; extern RangeTestPluginRadio *rangeTestPluginRadio;