2026-07-01 16:02:33 +01:00
|
|
|
// archy-messh Phase 1 — tri-protocol RX scanner + trippy acid display
|
2026-07-01 15:45:36 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2026-07-01 16:02:33 +01:00
|
|
|
// Target: Heltec WiFi LoRa 32 V3 (ESP32-S3 + single SX1262 + 128x64 SSD1306).
|
2026-07-01 15:45:36 +01:00
|
|
|
//
|
2026-07-01 16:23:45 +01:00
|
|
|
// The radio rotates through the Reticulum / meshTastic / meshCore PHY presets
|
|
|
|
|
// (RTC), listening in each dwell window and logging every raw packet it
|
|
|
|
|
// demodulates. The OLED shows all three networks live — R T C — behind an
|
|
|
|
|
// animated acid-house smiley, with per-network activity blips. Received
|
|
|
|
|
// Meshtastic packets are decoded on-chip (AES-128-CTR + protobuf).
|
2026-07-01 15:45:36 +01:00
|
|
|
//
|
2026-07-01 16:02:33 +01:00
|
|
|
// HONEST HARDWARE NOTE: the single onboard SX1262 can only be tuned to ONE
|
|
|
|
|
// PHY at a time (ARCHITECTURE.md §2), so "all three active" is fast time-
|
|
|
|
|
// slicing, not literal simultaneity. True concurrent reception needs added
|
|
|
|
|
// radios (Phase 2 / Option B). The UI presents all three as armed; the scan
|
|
|
|
|
// cycles between them faster than a human notices.
|
2026-07-01 15:45:36 +01:00
|
|
|
//
|
2026-07-01 16:02:33 +01:00
|
|
|
// This increment still does NOT decode/bridge/transmit — that is the next
|
|
|
|
|
// roadmap step (firmware/README.md). It proves RX + the device UX.
|
2026-07-01 15:45:36 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <RadioLib.h>
|
2026-07-01 16:02:33 +01:00
|
|
|
#include <U8g2lib.h>
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
#include <math.h>
|
2026-07-01 16:23:45 +01:00
|
|
|
#include "mbedtls/aes.h"
|
2026-07-01 16:35:18 +01:00
|
|
|
#include "esp_mac.h"
|
|
|
|
|
#include "esp_random.h"
|
2026-07-01 18:07:05 +01:00
|
|
|
#include <Ed25519.h> // rweather/Crypto — MeshCore identity + advert signing
|
|
|
|
|
#include <Preferences.h> // NVS-backed persistent Ed25519 key
|
2026-07-01 16:02:33 +01:00
|
|
|
|
|
|
|
|
// --- Heltec V3 SX1262 pin map (from the Heltec V3 schematic) ----------------
|
|
|
|
|
static const int PIN_LORA_NSS = 8;
|
|
|
|
|
static const int PIN_LORA_DIO1 = 14;
|
|
|
|
|
static const int PIN_LORA_NRST = 12;
|
|
|
|
|
static const int PIN_LORA_BUSY = 13;
|
2026-07-01 15:45:36 +01:00
|
|
|
static const int PIN_LORA_SCK = 9;
|
|
|
|
|
static const int PIN_LORA_MISO = 11;
|
|
|
|
|
static const int PIN_LORA_MOSI = 10;
|
|
|
|
|
static const float TCXO_VOLTAGE = 1.8f;
|
2026-07-01 16:02:33 +01:00
|
|
|
static const int8_t RX_TX_POWER = 10;
|
|
|
|
|
|
|
|
|
|
// --- Heltec V3 OLED + power ------------------------------------------------
|
|
|
|
|
static const int PIN_OLED_SDA = 17;
|
|
|
|
|
static const int PIN_OLED_SCL = 18;
|
|
|
|
|
static const int PIN_OLED_RST = 21;
|
|
|
|
|
static const int PIN_VEXT = 36; // drives Vext rail; LOW = ON (powers OLED)
|
2026-07-01 15:45:36 +01:00
|
|
|
|
|
|
|
|
SX1262 radio = new Module(PIN_LORA_NSS, PIN_LORA_DIO1, PIN_LORA_NRST, PIN_LORA_BUSY);
|
2026-07-01 16:02:33 +01:00
|
|
|
U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled(U8G2_R0, PIN_OLED_RST, PIN_OLED_SCL, PIN_OLED_SDA);
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// --- The three PHY presets (RTC) -------------------------------------------
|
2026-07-01 16:02:33 +01:00
|
|
|
// Values from docs/ARCHITECTURE.md §1. Items flagged are open questions the
|
|
|
|
|
// scan is meant to test against reality.
|
2026-07-01 15:45:36 +01:00
|
|
|
struct PhyConfig {
|
|
|
|
|
const char* name;
|
2026-07-01 16:23:45 +01:00
|
|
|
char letter; // R / T / C for the display
|
2026-07-01 15:45:36 +01:00
|
|
|
float freqMHz;
|
|
|
|
|
float bwKHz;
|
|
|
|
|
uint8_t sf;
|
2026-07-01 16:02:33 +01:00
|
|
|
uint8_t cr; // 4/cr
|
2026-07-01 15:45:36 +01:00
|
|
|
uint8_t syncWord;
|
|
|
|
|
uint16_t preamble;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const PhyConfig CONFIGS[] = {
|
2026-07-01 16:23:45 +01:00
|
|
|
// meshTastic EU868 "LongFast" — confirmed correct (received live packets).
|
|
|
|
|
{ "meshtastic", 'T', 869.525f, 250.0f, 11, 5, 0x2b, 16 },
|
2026-07-01 18:07:05 +01:00
|
|
|
// MeshCore narrowband — read live from the "HP Pro Desk" node's SELF_INFO
|
|
|
|
|
// (companion serial protocol, 2026-07-01): 869.618 MHz / 62.5 kHz / SF8 / CR5.
|
|
|
|
|
{ "meshcore", 'C', 869.618f, 62.5f, 8, 5, 0x12, 16 },
|
2026-07-01 16:02:33 +01:00
|
|
|
// Reticulum has no fixed preset; PLACEHOLDER — set to your RNS RNode config.
|
|
|
|
|
{ "reticulum", 'R', 867.200f, 125.0f, 8, 5, 0x12, 16 },
|
2026-07-01 15:45:36 +01:00
|
|
|
};
|
|
|
|
|
static const size_t NUM_CONFIGS = sizeof(CONFIGS) / sizeof(CONFIGS[0]);
|
2026-07-01 18:07:05 +01:00
|
|
|
static const uint32_t DWELL_MS = 3000; // camp long enough to sit through a
|
|
|
|
|
// flood-rebroadcast burst on each PHY;
|
|
|
|
|
// tune vs. how "live" C/R need to feel
|
2026-07-01 16:02:33 +01:00
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// Device identity — this single name is what the bridge will present on ALL
|
|
|
|
|
// three networks once the participation/TX layer lands, so a message addressed
|
|
|
|
|
// to "Reticutasticore" on Meshtastic, MeshCore or Reticulum reaches this device:
|
|
|
|
|
// - Meshtastic: User.long_name = DEVICE_NAME, short_name = DEVICE_SHORT (<=4)
|
|
|
|
|
// - MeshCore: advertised contact name = DEVICE_NAME
|
|
|
|
|
// - Reticulum: LXMF delivery display_name = DEVICE_NAME
|
|
|
|
|
// (RX-only today; the constants exist now so TX inherits them with no churn.)
|
|
|
|
|
static const char* DEVICE_NAME = "Reticutasticore";
|
|
|
|
|
static const char* DEVICE_SHORT = "RTC";
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
// --- Runtime stats ----------------------------------------------------------
|
|
|
|
|
struct NetStat {
|
|
|
|
|
uint32_t pkts = 0;
|
|
|
|
|
int16_t lastRssi = 0;
|
|
|
|
|
bool seen = false;
|
|
|
|
|
uint32_t lastRxMs = 0; // for the RX activity blip
|
|
|
|
|
};
|
|
|
|
|
static NetStat stats[NUM_CONFIGS];
|
|
|
|
|
static int activeIdx = 0;
|
|
|
|
|
static uint32_t totalPkts = 0;
|
|
|
|
|
static uint32_t dwellStart = 0;
|
|
|
|
|
static uint32_t frame = 0;
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 18:07:05 +01:00
|
|
|
// Outbound message queued from the serial console ("T:hello", or bare "hello"
|
|
|
|
|
// == all nets). Flushed on the matching PHY window so the radio is tuned right.
|
|
|
|
|
static char txText[200] = {0};
|
|
|
|
|
static bool txPending = false;
|
|
|
|
|
static char txNets[4] = {0}; // subset of "TCR"; empty == all
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// Last decoded human-readable event, shown along the bottom of the display.
|
|
|
|
|
static char lastMsg[40] = {0};
|
|
|
|
|
static char lastMsgNet = ' ';
|
|
|
|
|
static uint32_t lastMsgMs = 0;
|
|
|
|
|
|
2026-07-01 16:35:18 +01:00
|
|
|
// Meshtastic node identity (this device's presence on the mesh).
|
|
|
|
|
static uint32_t nodeNum = 0;
|
|
|
|
|
static char nodeId[12] = {0}; // "!xxxxxxxx"
|
|
|
|
|
static uint32_t lastAnnounceMs = 0;
|
|
|
|
|
static const uint32_t ANNOUNCE_INTERVAL_MS = 60000; // re-announce every 60s
|
|
|
|
|
|
2026-07-01 18:07:05 +01:00
|
|
|
// Message modal: on an inbound text we play an intro animation (upside-down
|
|
|
|
|
// spinning striped smiley + lightning bolts) then a Meshtastic-style modal
|
|
|
|
|
// stating the network, sender and text. Network-agnostic — any protocol that
|
|
|
|
|
// decodes a message drives it.
|
|
|
|
|
static bool msgModalActive = false;
|
|
|
|
|
static uint32_t msgModalStart = 0;
|
|
|
|
|
static char modalNet[14] = {0};
|
|
|
|
|
static char modalSender[20] = {0};
|
|
|
|
|
static char modalText[80] = {0};
|
|
|
|
|
static const uint32_t MODAL_INTRO_MS = 1400;
|
|
|
|
|
static const uint32_t MODAL_TOTAL_MS = 6500;
|
|
|
|
|
|
|
|
|
|
static void triggerMessageModal(const char* net, const char* sender, const char* text) {
|
|
|
|
|
strncpy(modalNet, net, sizeof(modalNet) - 1); modalNet[sizeof(modalNet) - 1] = 0;
|
|
|
|
|
strncpy(modalSender, sender, sizeof(modalSender) - 1); modalSender[sizeof(modalSender) - 1] = 0;
|
|
|
|
|
strncpy(modalText, text, sizeof(modalText) - 1); modalText[sizeof(modalText) - 1] = 0;
|
|
|
|
|
msgModalActive = true; msgModalStart = millis();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
// --- RX interrupt -----------------------------------------------------------
|
2026-07-01 15:45:36 +01:00
|
|
|
static volatile bool rxFlag = false;
|
|
|
|
|
ICACHE_RAM_ATTR static void onDio1() { rxFlag = true; }
|
|
|
|
|
|
|
|
|
|
static bool applyConfig(const PhyConfig& c) {
|
|
|
|
|
radio.standby();
|
|
|
|
|
bool ok = true;
|
2026-07-01 16:02:33 +01:00
|
|
|
ok &= radio.setFrequency(c.freqMHz) == RADIOLIB_ERR_NONE;
|
|
|
|
|
ok &= radio.setBandwidth(c.bwKHz) == RADIOLIB_ERR_NONE;
|
|
|
|
|
ok &= radio.setSpreadingFactor(c.sf) == RADIOLIB_ERR_NONE;
|
|
|
|
|
ok &= radio.setCodingRate(c.cr) == RADIOLIB_ERR_NONE;
|
|
|
|
|
ok &= radio.setSyncWord(c.syncWord) == RADIOLIB_ERR_NONE;
|
|
|
|
|
ok &= radio.setPreambleLength(c.preamble) == RADIOLIB_ERR_NONE;
|
2026-07-01 15:45:36 +01:00
|
|
|
if (!ok) return false;
|
|
|
|
|
return radio.startReceive() == RADIOLIB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// --- On-chip Meshtastic decode ---------------------------------------------
|
|
|
|
|
// Default-channel ("LongFast") packets use AES-128-CTR with a PUBLIC key.
|
|
|
|
|
// Validated offline against real captured packets (firmware/tools/
|
|
|
|
|
// meshtastic_decode.py). Wire format: 16-byte header + encrypted Data protobuf.
|
|
|
|
|
static const uint8_t MT_KEY[16] = {
|
|
|
|
|
0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59,
|
|
|
|
|
0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0x01};
|
|
|
|
|
|
|
|
|
|
static uint32_t readVarint(const uint8_t* b, size_t len, size_t& i) {
|
|
|
|
|
uint32_t v = 0; int shift = 0;
|
|
|
|
|
while (i < len) {
|
|
|
|
|
uint8_t c = b[i++];
|
|
|
|
|
v |= (uint32_t)(c & 0x7f) << shift; shift += 7;
|
|
|
|
|
if (!(c & 0x80)) break;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode a Meshtastic packet into a short human string (text, or node name for
|
|
|
|
|
// NodeInfo). Returns false if not decodable / not an interesting type.
|
2026-07-01 18:07:05 +01:00
|
|
|
static bool decodeMeshtastic(const uint8_t* pkt, size_t len, char* out, size_t outsz,
|
|
|
|
|
bool* isText, uint32_t* senderOut) {
|
2026-07-01 16:23:45 +01:00
|
|
|
if (len <= 16) return false;
|
|
|
|
|
uint32_t sender, pid;
|
|
|
|
|
memcpy(&sender, pkt + 4, 4);
|
|
|
|
|
memcpy(&pid, pkt + 8, 4);
|
2026-07-01 18:07:05 +01:00
|
|
|
if (senderOut) *senderOut = sender;
|
|
|
|
|
if (isText) *isText = false;
|
2026-07-01 16:23:45 +01:00
|
|
|
|
|
|
|
|
// CTR nonce = packetId (8 LE, high 4 zero) + fromNode (4 LE) + extraNonce (4 = 0)
|
|
|
|
|
uint8_t nonce[16]; memset(nonce, 0, 16);
|
|
|
|
|
memcpy(nonce + 0, &pid, 4);
|
|
|
|
|
memcpy(nonce + 8, &sender, 4);
|
|
|
|
|
|
|
|
|
|
size_t plen = len - 16;
|
|
|
|
|
if (plen > 240) return false;
|
|
|
|
|
uint8_t plain[240];
|
|
|
|
|
mbedtls_aes_context aes; mbedtls_aes_init(&aes);
|
|
|
|
|
mbedtls_aes_setkey_enc(&aes, MT_KEY, 128);
|
|
|
|
|
size_t nc_off = 0; uint8_t stream[16]; memset(stream, 0, 16);
|
|
|
|
|
mbedtls_aes_crypt_ctr(&aes, plen, &nc_off, nonce, stream, pkt + 16, plain);
|
|
|
|
|
mbedtls_aes_free(&aes);
|
|
|
|
|
|
|
|
|
|
// Data protobuf: field 1 = portnum (varint), field 2 = payload (bytes)
|
|
|
|
|
size_t i = 0; uint8_t port = 0; const uint8_t* payload = nullptr; size_t payLen = 0;
|
|
|
|
|
while (i < plen) {
|
|
|
|
|
uint8_t tag = plain[i++]; uint8_t field = tag >> 3, wt = tag & 7;
|
|
|
|
|
if (wt == 0) { uint32_t v = readVarint(plain, plen, i); if (field == 1) port = (uint8_t)v; }
|
|
|
|
|
else if (wt == 2) { uint32_t l = readVarint(plain, plen, i);
|
|
|
|
|
if (field == 2) { payload = plain + i; payLen = l; } i += l; }
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
if (!payload) return false;
|
|
|
|
|
|
|
|
|
|
if (port == 1) { // TEXT_MESSAGE_APP
|
|
|
|
|
size_t n = payLen < outsz - 1 ? payLen : outsz - 1;
|
|
|
|
|
memcpy(out, payload, n); out[n] = 0;
|
2026-07-01 18:07:05 +01:00
|
|
|
if (isText) *isText = true;
|
2026-07-01 16:23:45 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (port == 4) { // NODEINFO_APP -> User protobuf; field 3 = shortName
|
|
|
|
|
size_t j = 0; const uint8_t* sn = nullptr; size_t snl = 0;
|
|
|
|
|
while (j < payLen) {
|
|
|
|
|
uint8_t tag = payload[j++]; uint8_t f = tag >> 3, w = tag & 7;
|
|
|
|
|
if (w == 2) { uint32_t l = readVarint(payload, payLen, j);
|
|
|
|
|
if (f == 3) { sn = payload + j; snl = l; } j += l; }
|
|
|
|
|
else if (w == 0) { readVarint(payload, payLen, j); }
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
if (sn) { size_t n = snl < outsz - 1 ? snl : outsz - 1;
|
|
|
|
|
memcpy(out, sn, n); out[n] = 0; return true; }
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:35:18 +01:00
|
|
|
// AES-128-CTR (symmetric, so same call encrypts) with the Meshtastic default key.
|
|
|
|
|
static void mtCryptCtr(uint32_t sender, uint32_t pid, const uint8_t* in, uint8_t* out, size_t n) {
|
|
|
|
|
uint8_t nonce[16]; memset(nonce, 0, 16);
|
|
|
|
|
memcpy(nonce + 0, &pid, 4);
|
|
|
|
|
memcpy(nonce + 8, &sender, 4);
|
|
|
|
|
mbedtls_aes_context aes; mbedtls_aes_init(&aes);
|
|
|
|
|
mbedtls_aes_setkey_enc(&aes, MT_KEY, 128);
|
|
|
|
|
size_t nc_off = 0; uint8_t stream[16]; memset(stream, 0, 16);
|
|
|
|
|
mbedtls_aes_crypt_ctr(&aes, n, &nc_off, nonce, stream, in, out);
|
|
|
|
|
mbedtls_aes_free(&aes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Announce this device as a Meshtastic node named DEVICE_NAME. Broadcasts a
|
|
|
|
|
// NodeInfo (User protobuf) on the LongFast primary channel (hash 0x08) so it
|
|
|
|
|
// appears in other Meshtastic clients — and, because we advertise no public
|
|
|
|
|
// key, DMs to us fall back to channel encryption, which we can already decode.
|
|
|
|
|
static void announceMeshtastic() {
|
|
|
|
|
// User protobuf: field1 id, field2 long_name, field3 short_name
|
|
|
|
|
uint8_t user[64]; size_t u = 0;
|
|
|
|
|
size_t idl = strlen(nodeId), lnl = strlen(DEVICE_NAME), snl = strlen(DEVICE_SHORT);
|
|
|
|
|
user[u++] = 0x0a; user[u++] = idl; memcpy(user + u, nodeId, idl); u += idl;
|
|
|
|
|
user[u++] = 0x12; user[u++] = lnl; memcpy(user + u, DEVICE_NAME, lnl); u += lnl;
|
|
|
|
|
user[u++] = 0x1a; user[u++] = snl; memcpy(user + u, DEVICE_SHORT, snl); u += snl;
|
|
|
|
|
|
|
|
|
|
// Data protobuf: portnum=4 (NODEINFO_APP), payload=User
|
|
|
|
|
uint8_t data[96]; size_t d = 0;
|
|
|
|
|
data[d++] = 0x08; data[d++] = 0x04; // portnum = 4
|
|
|
|
|
data[d++] = 0x12; data[d++] = (uint8_t)u; memcpy(data + d, user, u); d += u;
|
|
|
|
|
|
|
|
|
|
uint32_t pid = esp_random();
|
|
|
|
|
uint8_t enc[96];
|
|
|
|
|
mtCryptCtr(nodeNum, pid, data, enc, d);
|
|
|
|
|
|
|
|
|
|
// 16-byte header + ciphertext
|
|
|
|
|
uint8_t pkt[128]; uint32_t dest = 0xffffffff;
|
|
|
|
|
memcpy(pkt + 0, &dest, 4);
|
|
|
|
|
memcpy(pkt + 4, &nodeNum, 4);
|
|
|
|
|
memcpy(pkt + 8, &pid, 4);
|
|
|
|
|
pkt[12] = 0x63; // flags: hop_limit=3, hop_start=3
|
|
|
|
|
pkt[13] = 0x08; // channel hash (LongFast primary)
|
|
|
|
|
pkt[14] = 0x00; // next_hop
|
|
|
|
|
pkt[15] = 0x00; // relay_node
|
|
|
|
|
memcpy(pkt + 16, enc, d);
|
|
|
|
|
|
|
|
|
|
radio.standby();
|
|
|
|
|
int st = radio.transmit(pkt, 16 + d);
|
2026-07-01 18:07:05 +01:00
|
|
|
Serial.printf(" [T] announce '%s' (%s) id=0x%08x -> tx=%d : ",
|
2026-07-01 16:35:18 +01:00
|
|
|
DEVICE_NAME, nodeId, pid, st);
|
2026-07-01 18:07:05 +01:00
|
|
|
for (size_t i = 0; i < 16 + d; i++) Serial.printf("%02x", pkt[i]); // TX hex for self-verify
|
|
|
|
|
Serial.println();
|
|
|
|
|
radio.startReceive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send a text message on the Meshtastic LongFast primary channel. Same wire
|
|
|
|
|
// format as announceMeshtastic() but portnum=1 (TEXT_MESSAGE_APP), payload =
|
|
|
|
|
// the raw UTF-8 text. Assumes the radio is already tuned to the 'T' PHY.
|
|
|
|
|
static void sendMeshtasticText(const char* text) {
|
|
|
|
|
size_t tl = strlen(text);
|
|
|
|
|
if (tl > 200) tl = 200;
|
|
|
|
|
|
|
|
|
|
// Data protobuf: field1 portnum=1 (varint), field2 payload=text (bytes)
|
|
|
|
|
uint8_t data[224]; size_t d = 0;
|
|
|
|
|
data[d++] = 0x08; data[d++] = 0x01; // portnum = 1
|
|
|
|
|
data[d++] = 0x12; data[d++] = (uint8_t)tl; // payload, len-delimited
|
|
|
|
|
memcpy(data + d, text, tl); d += tl;
|
|
|
|
|
|
|
|
|
|
uint32_t pid = esp_random();
|
|
|
|
|
uint8_t enc[224];
|
|
|
|
|
mtCryptCtr(nodeNum, pid, data, enc, d);
|
|
|
|
|
|
|
|
|
|
uint8_t pkt[240]; uint32_t dest = 0xffffffff;
|
|
|
|
|
memcpy(pkt + 0, &dest, 4);
|
|
|
|
|
memcpy(pkt + 4, &nodeNum, 4);
|
|
|
|
|
memcpy(pkt + 8, &pid, 4);
|
|
|
|
|
pkt[12] = 0x63; // flags: hop_limit=3, hop_start=3
|
|
|
|
|
pkt[13] = 0x08; // channel hash (LongFast primary)
|
|
|
|
|
pkt[14] = 0x00; // next_hop
|
|
|
|
|
pkt[15] = 0x00; // relay_node
|
|
|
|
|
memcpy(pkt + 16, enc, d);
|
|
|
|
|
|
|
|
|
|
radio.standby();
|
|
|
|
|
int st = radio.transmit(pkt, 16 + d);
|
|
|
|
|
Serial.printf(" [T] TX text id=0x%08x -> tx=%d : \"%s\"\n", pid, st, text);
|
|
|
|
|
radio.startReceive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- MeshCore identity + advert TX -----------------------------------------
|
|
|
|
|
// MeshCore nodes prove identity with an Ed25519 keypair. To appear as a contact
|
|
|
|
|
// in other clients we broadcast a signed ADVERT (PAYLOAD_TYPE_ADVERT = 0x04):
|
|
|
|
|
// on-air: [header 0x12][path_len 0x00][pub_key 32][timestamp 4 LE][sig 64][app_data]
|
|
|
|
|
// signed message = pub_key || timestamp || app_data (verified in MeshCore
|
|
|
|
|
// Mesh.cpp onRecvPacket PAYLOAD_TYPE_ADVERT). app_data = flags(0x81 = chat
|
|
|
|
|
// node + has-name) followed by the UTF-8 name.
|
|
|
|
|
static uint8_t mcPrv[32]; // Ed25519 private key (seed)
|
|
|
|
|
static uint8_t mcPub[32]; // Ed25519 public key = MeshCore identity
|
|
|
|
|
static uint32_t lastMcAdvertMs = 0;
|
|
|
|
|
static const uint32_t MC_ADVERT_INTERVAL_MS = 30000;
|
|
|
|
|
// Build-time epoch base (2026-07-01 UTC) + uptime — advert timestamps just need
|
|
|
|
|
// to be fresh/monotonic; the node has no RTC.
|
|
|
|
|
static const uint32_t MC_EPOCH_BASE = 1782864000UL;
|
|
|
|
|
static uint32_t mcNow() { return MC_EPOCH_BASE + millis() / 1000; }
|
|
|
|
|
|
|
|
|
|
static void initMeshCoreIdentity() {
|
|
|
|
|
Preferences prefs;
|
|
|
|
|
prefs.begin("meshcore", false);
|
|
|
|
|
if (prefs.getBytes("prv", mcPrv, 32) != 32) {
|
|
|
|
|
Ed25519::generatePrivateKey(mcPrv); // uses the Crypto lib CSPRNG
|
|
|
|
|
prefs.putBytes("prv", mcPrv, 32);
|
|
|
|
|
Serial.println("MeshCore: generated new Ed25519 identity");
|
|
|
|
|
}
|
|
|
|
|
prefs.end();
|
|
|
|
|
Ed25519::derivePublicKey(mcPub, mcPrv);
|
|
|
|
|
Serial.print("MeshCore identity pub: ");
|
|
|
|
|
for (int i = 0; i < 32; i++) Serial.printf("%02x", mcPub[i]);
|
|
|
|
|
Serial.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sendMeshCoreAdvert() {
|
|
|
|
|
uint8_t app_data[40]; size_t ad = 0;
|
|
|
|
|
app_data[ad++] = 0x81; // flags: chat node + has name
|
|
|
|
|
size_t nl = strlen(DEVICE_NAME); if (nl > 32) nl = 32;
|
|
|
|
|
memcpy(app_data + ad, DEVICE_NAME, nl); ad += nl;
|
|
|
|
|
|
|
|
|
|
uint32_t ts = mcNow();
|
|
|
|
|
uint8_t message[32 + 4 + 40]; size_t ml = 0;
|
|
|
|
|
memcpy(message + ml, mcPub, 32); ml += 32;
|
|
|
|
|
memcpy(message + ml, &ts, 4); ml += 4;
|
|
|
|
|
memcpy(message + ml, app_data, ad); ml += ad;
|
|
|
|
|
|
|
|
|
|
uint8_t sig[64];
|
|
|
|
|
Ed25519::sign(sig, mcPrv, mcPub, message, ml);
|
|
|
|
|
|
|
|
|
|
uint8_t pkt[2 + 32 + 4 + 64 + 40]; size_t p = 0;
|
|
|
|
|
pkt[p++] = 0x12; // route=flood(2) | type=advert(4)
|
|
|
|
|
pkt[p++] = 0x00; // path_len = 0 (direct/flood)
|
|
|
|
|
memcpy(pkt + p, mcPub, 32); p += 32;
|
|
|
|
|
memcpy(pkt + p, &ts, 4); p += 4;
|
|
|
|
|
memcpy(pkt + p, sig, 64); p += 64;
|
|
|
|
|
memcpy(pkt + p, app_data, ad); p += ad;
|
|
|
|
|
|
|
|
|
|
radio.standby();
|
|
|
|
|
int st = radio.transmit(pkt, p);
|
|
|
|
|
Serial.printf(" [C] advert '%s' ts=%u -> tx=%d (%u bytes) : ",
|
|
|
|
|
DEVICE_NAME, ts, st, (unsigned)p);
|
|
|
|
|
for (size_t i = 0; i < p; i++) Serial.printf("%02x", pkt[i]); // TX hex for self-verify
|
|
|
|
|
Serial.println();
|
2026-07-01 16:35:18 +01:00
|
|
|
radio.startReceive();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
static void drainPacket() {
|
2026-07-01 15:45:36 +01:00
|
|
|
size_t len = radio.getPacketLength();
|
|
|
|
|
uint8_t buf[256];
|
|
|
|
|
if (len > sizeof(buf)) len = sizeof(buf);
|
|
|
|
|
int state = radio.readData(buf, len);
|
|
|
|
|
if (state != RADIOLIB_ERR_NONE) {
|
2026-07-01 16:02:33 +01:00
|
|
|
Serial.printf(" [%s] RX error state=%d\n", CONFIGS[activeIdx].name, state);
|
2026-07-01 15:45:36 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2026-07-01 16:02:33 +01:00
|
|
|
NetStat& s = stats[activeIdx];
|
|
|
|
|
s.pkts++; s.seen = true; s.lastRssi = (int16_t)radio.getRSSI(); s.lastRxMs = millis();
|
|
|
|
|
totalPkts++;
|
|
|
|
|
Serial.printf(" [%s] PACKET len=%u rssi=%ddBm snr=%.1fdB : ",
|
|
|
|
|
CONFIGS[activeIdx].name, (unsigned)len, s.lastRssi, radio.getSNR());
|
2026-07-01 15:45:36 +01:00
|
|
|
for (size_t i = 0; i < len; i++) Serial.printf("%02x", buf[i]);
|
|
|
|
|
Serial.println();
|
2026-07-01 16:23:45 +01:00
|
|
|
|
|
|
|
|
// meshTastic packets we can decode on-chip today.
|
|
|
|
|
if (CONFIGS[activeIdx].letter == 'T') {
|
2026-07-01 18:07:05 +01:00
|
|
|
char msg[64]; bool isText = false; uint32_t snd = 0;
|
|
|
|
|
if (decodeMeshtastic(buf, len, msg, sizeof(msg), &isText, &snd)) {
|
2026-07-01 16:23:45 +01:00
|
|
|
strncpy(lastMsg, msg, sizeof(lastMsg) - 1); lastMsg[sizeof(lastMsg) - 1] = 0;
|
|
|
|
|
lastMsgNet = 'T'; lastMsgMs = millis();
|
2026-07-01 18:07:05 +01:00
|
|
|
Serial.printf(" decoded[T]%s: %s\n", isText ? "(text)" : "(info)", msg);
|
|
|
|
|
if (isText) {
|
|
|
|
|
char sid[16]; snprintf(sid, sizeof(sid), "!%08x", snd);
|
|
|
|
|
triggerMessageModal("meshTastic", sid, msg);
|
|
|
|
|
}
|
2026-07-01 16:23:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-07-01 15:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
// --- The trippy acid display ------------------------------------------------
|
|
|
|
|
// Left: an acid-house smiley wobbling inside expanding psychedelic rings.
|
|
|
|
|
// Right: "RMC" and the three network rows with live counts + RX blips.
|
|
|
|
|
static void drawAcidSmiley(int cx, int cy) {
|
|
|
|
|
// expanding ripple rings (the trip)
|
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
|
int r = (int)((frame * 2 + k * 13) % 42);
|
|
|
|
|
if (r > 3) oled.drawCircle(cx, cy, r, U8G2_DRAW_ALL);
|
|
|
|
|
}
|
|
|
|
|
// face: filled disc, eyes + smile cut back out in background colour
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
oled.drawDisc(cx, cy, 18, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(0);
|
|
|
|
|
oled.drawBox(cx - 8, cy - 8, 3, 7); // left eye
|
|
|
|
|
oled.drawBox(cx + 5, cy - 8, 3, 7); // right eye
|
|
|
|
|
for (int dx = -10; dx <= 10; dx++) { // grinning acid smile
|
|
|
|
|
int dy = 4 + (100 - dx * dx) / 22; // corners up, dips in the middle
|
|
|
|
|
oled.drawPixel(cx + dx, cy + dy);
|
|
|
|
|
oled.drawPixel(cx + dx, cy + dy + 1);
|
|
|
|
|
}
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// Periodic freak-out: the face becomes a spinning striped ball with wide-open
|
|
|
|
|
// googly eyes and a gaping mouth.
|
|
|
|
|
static void drawAcidSmileyFreaky(int cx, int cy) {
|
|
|
|
|
const int R = 18;
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
oled.drawDisc(cx, cy, R, U8G2_DRAW_ALL);
|
|
|
|
|
// rotating stripes carved into the ball (the spin)
|
|
|
|
|
float th = frame * 0.35f;
|
|
|
|
|
float c = cosf(th), s = sinf(th);
|
|
|
|
|
oled.setDrawColor(0);
|
|
|
|
|
for (int off = -R; off <= R; off += 4) {
|
|
|
|
|
float px = cx - s * off, py = cy + c * off;
|
|
|
|
|
oled.drawLine((int)(px - c * R), (int)(py - s * R),
|
|
|
|
|
(int)(px + c * R), (int)(py + s * R));
|
|
|
|
|
}
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
oled.drawCircle(cx, cy, R, U8G2_DRAW_ALL); // crisp rim
|
|
|
|
|
// wide-open googly eyes: white halo -> dark iris -> white pupil highlight,
|
|
|
|
|
// so they read against the striped ball (a plain white disc would vanish)
|
|
|
|
|
for (int sgn = -1; sgn <= 1; sgn += 2) {
|
|
|
|
|
int ex = cx + sgn * 7;
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(ex, cy - 6, 4, U8G2_DRAW_ALL); // halo
|
|
|
|
|
oled.setDrawColor(0); oled.drawDisc(ex, cy - 6, 3, U8G2_DRAW_ALL); // iris
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(ex, cy - 6, 1, U8G2_DRAW_ALL); // pupil
|
|
|
|
|
}
|
|
|
|
|
// gaping mouth: white ring around a dark O
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(cx, cy + 7, 5, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(0); oled.drawDisc(cx, cy + 7, 3, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
static void drawMarchingBorder() {
|
|
|
|
|
for (int x = 0; x < 128; x++) {
|
|
|
|
|
if (((x + frame) & 3) == 0) { oled.drawPixel(x, 0); oled.drawPixel(x, 63); }
|
|
|
|
|
}
|
|
|
|
|
for (int y = 0; y < 64; y++) {
|
|
|
|
|
if (((y + frame) & 3) == 0) { oled.drawPixel(0, y); oled.drawPixel(127, y); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const PhyConfig* configByLetter(char L) {
|
|
|
|
|
for (size_t i = 0; i < NUM_CONFIGS; i++) if (CONFIGS[i].letter == L) return &CONFIGS[i];
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
static int indexByLetter(char L) {
|
|
|
|
|
for (size_t i = 0; i < NUM_CONFIGS; i++) if (CONFIGS[i].letter == L) return (int)i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 18:07:05 +01:00
|
|
|
// --- Message-received animation + modal ------------------------------------
|
|
|
|
|
static void drawBolt(int x, int y, int h) {
|
|
|
|
|
oled.drawLine(x, y, x - 3, y + h / 2);
|
|
|
|
|
oled.drawLine(x - 3, y + h / 2, x + 3, y + h / 2);
|
|
|
|
|
oled.drawLine(x + 3, y + h / 2, x - 1, y + h);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Greedy word-wrap for a fixed-width font (6px/char here).
|
|
|
|
|
static void drawWrapped(const char* text, int x, int y0, int cpl, int lineh, int maxlines) {
|
|
|
|
|
int n = strlen(text), i = 0, line = 0;
|
|
|
|
|
while (i < n && line < maxlines) {
|
|
|
|
|
int end = i + cpl;
|
|
|
|
|
if (end < n) { int b = end; while (b > i && text[b] != ' ') b--; if (b > i) end = b; }
|
|
|
|
|
else end = n;
|
|
|
|
|
char buf[40]; int len = end - i; if (len > 39) len = 39;
|
|
|
|
|
memcpy(buf, text + i, len); buf[len] = 0;
|
|
|
|
|
oled.drawStr(x, y0 + line * lineh, buf);
|
|
|
|
|
i = end; while (i < n && text[i] == ' ') i++;
|
|
|
|
|
line++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Intro: the striped smiley spins UPSIDE DOWN (eyes below, mouth above) while
|
|
|
|
|
// lightning bolts flash — a beat before the modal, à la Meshtastic.
|
|
|
|
|
static void drawMsgIntro() {
|
|
|
|
|
oled.clearBuffer();
|
|
|
|
|
int cx = 64, cy = 30; const int R = 22;
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(cx, cy, R, U8G2_DRAW_ALL);
|
|
|
|
|
float th = -frame * 0.5f; // reversed spin
|
|
|
|
|
float c = cosf(th), s = sinf(th);
|
|
|
|
|
oled.setDrawColor(0);
|
|
|
|
|
for (int off = -R; off <= R; off += 4) {
|
|
|
|
|
float px = cx - s * off, py = cy + c * off;
|
|
|
|
|
oled.drawLine((int)(px - c * R), (int)(py - s * R), (int)(px + c * R), (int)(py + s * R));
|
|
|
|
|
}
|
|
|
|
|
oled.setDrawColor(1); oled.drawCircle(cx, cy, R, U8G2_DRAW_ALL);
|
|
|
|
|
for (int sgn = -1; sgn <= 1; sgn += 2) { // eyes BELOW centre (upside down)
|
|
|
|
|
int ex = cx + sgn * 8;
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(ex, cy + 8, 4, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(0); oled.drawDisc(ex, cy + 8, 3, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(ex, cy + 8, 1, U8G2_DRAW_ALL);
|
|
|
|
|
}
|
|
|
|
|
oled.setDrawColor(1); oled.drawDisc(cx, cy - 9, 5, U8G2_DRAW_ALL); // mouth ABOVE
|
|
|
|
|
oled.setDrawColor(0); oled.drawDisc(cx, cy - 9, 3, U8G2_DRAW_ALL);
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
if ((frame / 2) & 1) { drawBolt(22, 5, 22); drawBolt(106, 5, 22); }
|
|
|
|
|
else { drawBolt(14, 12, 20); drawBolt(114, 10, 22); }
|
|
|
|
|
oled.setFont(u8g2_font_5x8_tf);
|
|
|
|
|
const char* t = "INCOMING";
|
|
|
|
|
oled.drawStr((128 - oled.getStrWidth(t)) / 2, 62, t);
|
|
|
|
|
oled.sendBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Modal: network banner + sender + wrapped message text.
|
|
|
|
|
static void drawMsgModal() {
|
|
|
|
|
oled.clearBuffer();
|
|
|
|
|
oled.drawRFrame(1, 1, 126, 62, 3);
|
|
|
|
|
oled.drawBox(1, 1, 126, 13); // header bar
|
|
|
|
|
oled.setDrawColor(0);
|
|
|
|
|
oled.setFont(u8g2_font_6x10_tf);
|
|
|
|
|
oled.drawStr((128 - oled.getStrWidth(modalNet)) / 2, 11, modalNet);
|
|
|
|
|
oled.setDrawColor(1);
|
|
|
|
|
oled.setFont(u8g2_font_5x8_tf);
|
|
|
|
|
char sl[28]; snprintf(sl, sizeof(sl), "from %s", modalSender);
|
|
|
|
|
oled.drawStr(6, 24, sl);
|
|
|
|
|
oled.setFont(u8g2_font_6x10_tf);
|
|
|
|
|
drawWrapped(modalText, 6, 36, 19, 11, 3);
|
|
|
|
|
oled.sendBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
static void drawUI() {
|
2026-07-01 18:07:05 +01:00
|
|
|
if (msgModalActive) {
|
|
|
|
|
uint32_t e = millis() - msgModalStart;
|
|
|
|
|
if (e < MODAL_INTRO_MS) { drawMsgIntro(); return; }
|
|
|
|
|
if (e < MODAL_TOTAL_MS) { drawMsgModal(); return; }
|
|
|
|
|
msgModalActive = false;
|
|
|
|
|
}
|
2026-07-01 16:02:33 +01:00
|
|
|
oled.clearBuffer();
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// Every ~9s the smiley freaks out (spins, stripes, eyes + mouth open) for ~2.5s.
|
|
|
|
|
bool freaky = (millis() % 9000) < 2500;
|
|
|
|
|
if (freaky) drawAcidSmileyFreaky(31, 33);
|
|
|
|
|
else drawAcidSmiley(31, 33);
|
2026-07-01 16:02:33 +01:00
|
|
|
drawMarchingBorder();
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// "RTC" punk header on the right
|
|
|
|
|
oled.setFont(u8g2_font_bubble_tr);
|
|
|
|
|
const char* rtc = "RTC";
|
|
|
|
|
int rw = oled.getStrWidth(rtc);
|
2026-07-01 16:02:33 +01:00
|
|
|
int rx = 64 + (64 - rw) / 2;
|
|
|
|
|
if ((frame / 10) & 1) { // strobe invert
|
2026-07-01 16:23:45 +01:00
|
|
|
oled.drawBox(64, 0, 64, 22);
|
|
|
|
|
oled.setDrawColor(0); oled.drawStr(rx, 19, rtc); oled.setDrawColor(1);
|
2026-07-01 16:02:33 +01:00
|
|
|
} else {
|
2026-07-01 16:23:45 +01:00
|
|
|
oled.drawStr(rx, 19, rtc);
|
2026-07-01 16:02:33 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// three network rows, in R T C order
|
|
|
|
|
const char order[3] = {'R', 'T', 'C'};
|
2026-07-01 16:02:33 +01:00
|
|
|
oled.setFont(u8g2_font_6x10_tf);
|
|
|
|
|
uint32_t now = millis();
|
|
|
|
|
for (int r = 0; r < 3; r++) {
|
|
|
|
|
int idx = indexByLetter(order[r]);
|
|
|
|
|
if (idx < 0) continue;
|
|
|
|
|
const NetStat& s = stats[idx];
|
2026-07-01 16:23:45 +01:00
|
|
|
int y = 32 + r * 11; // baseline (below the taller header)
|
2026-07-01 16:02:33 +01:00
|
|
|
bool active = (idx == activeIdx);
|
2026-07-01 16:23:45 +01:00
|
|
|
if (active) oled.drawFrame(64, y - 9, 64, 11);
|
2026-07-01 16:02:33 +01:00
|
|
|
char line[24];
|
|
|
|
|
if (s.seen) snprintf(line, sizeof(line), "%c %4lu", order[r], (unsigned long)s.pkts);
|
|
|
|
|
else snprintf(line, sizeof(line), "%c --", order[r]);
|
|
|
|
|
oled.drawStr(67, y, line);
|
|
|
|
|
// RX activity blip (filled when a packet landed in the last 400ms)
|
|
|
|
|
bool blip = s.seen && (now - s.lastRxMs < 400);
|
|
|
|
|
if (blip) oled.drawDisc(121, y - 4, 3, U8G2_DRAW_ALL);
|
|
|
|
|
else oled.drawCircle(121, y - 4, 3, U8G2_DRAW_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// last decoded event along the bottom of the right column
|
|
|
|
|
if (lastMsg[0]) {
|
|
|
|
|
oled.setFont(u8g2_font_4x6_tf);
|
|
|
|
|
char line[24];
|
|
|
|
|
snprintf(line, sizeof(line), "%c:%s", lastMsgNet, lastMsg);
|
|
|
|
|
line[15] = 0; // truncate to fit the ~60px column
|
|
|
|
|
oled.drawStr(66, 62, line);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
oled.sendBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:45:36 +01:00
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
uint32_t t0 = millis();
|
2026-07-01 16:02:33 +01:00
|
|
|
while (!Serial && millis() - t0 < 2000) { }
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
Serial.println("\n=== archy-messh Phase 1 — RTC scanner + acid display ===");
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
// Power the OLED rail, then reset the panel, then bring up U8g2.
|
|
|
|
|
pinMode(PIN_VEXT, OUTPUT); digitalWrite(PIN_VEXT, LOW); delay(60);
|
|
|
|
|
pinMode(PIN_OLED_RST, OUTPUT);
|
|
|
|
|
digitalWrite(PIN_OLED_RST, LOW); delay(20);
|
|
|
|
|
digitalWrite(PIN_OLED_RST, HIGH); delay(20);
|
|
|
|
|
oled.begin();
|
|
|
|
|
oled.setBusClock(400000);
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 16:23:45 +01:00
|
|
|
// Boot splash: the device identity, shared across all three networks.
|
|
|
|
|
oled.clearBuffer();
|
|
|
|
|
drawAcidSmiley(64, 24);
|
|
|
|
|
oled.setFont(u8g2_font_6x10_tf);
|
|
|
|
|
int nw = oled.getStrWidth(DEVICE_NAME);
|
|
|
|
|
oled.drawStr((128 - nw) / 2, 52, DEVICE_NAME);
|
|
|
|
|
oled.setFont(u8g2_font_5x8_tf);
|
|
|
|
|
const char* sub = "R T C bridge";
|
|
|
|
|
int sw = oled.getStrWidth(sub);
|
|
|
|
|
oled.drawStr((128 - sw) / 2, 62, sub);
|
|
|
|
|
oled.sendBuffer();
|
|
|
|
|
Serial.printf("Device identity (all networks): %s / %s\n", DEVICE_NAME, DEVICE_SHORT);
|
|
|
|
|
delay(2200);
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
SPI.begin(PIN_LORA_SCK, PIN_LORA_MISO, PIN_LORA_MOSI, PIN_LORA_NSS);
|
2026-07-01 15:45:36 +01:00
|
|
|
const PhyConfig& first = CONFIGS[0];
|
|
|
|
|
int state = radio.begin(first.freqMHz, first.bwKHz, first.sf, first.cr,
|
2026-07-01 16:02:33 +01:00
|
|
|
first.syncWord, RX_TX_POWER, first.preamble, TCXO_VOLTAGE);
|
|
|
|
|
Serial.printf("radio.begin -> %d %s\n", state, state == RADIOLIB_ERR_NONE ? "(OK)" : "(ERR)");
|
2026-07-01 15:45:36 +01:00
|
|
|
if (state != RADIOLIB_ERR_NONE) {
|
|
|
|
|
Serial.println("!! SX1262 init failed — check wiring/TCXO. Halting.");
|
2026-07-01 16:02:33 +01:00
|
|
|
while (true) { drawUI(); frame++; delay(60); } // still show the trip
|
2026-07-01 15:45:36 +01:00
|
|
|
}
|
2026-07-01 16:02:33 +01:00
|
|
|
radio.setDio2AsRfSwitch(true);
|
2026-07-01 15:45:36 +01:00
|
|
|
radio.setDio1Action(onDio1);
|
2026-07-01 16:35:18 +01:00
|
|
|
radio.setOutputPower(14); // EU868-safe TX power for announces
|
|
|
|
|
|
|
|
|
|
// Derive this device's Meshtastic NodeNum from the last 4 bytes of the MAC.
|
|
|
|
|
uint8_t mac[6]; esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
|
|
|
|
nodeNum = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) |
|
|
|
|
|
((uint32_t)mac[4] << 8) | mac[5];
|
|
|
|
|
snprintf(nodeId, sizeof(nodeId), "!%08x", nodeNum);
|
|
|
|
|
Serial.printf("Meshtastic node: %s (%s)\n", DEVICE_NAME, nodeId);
|
2026-07-01 15:45:36 +01:00
|
|
|
|
2026-07-01 18:07:05 +01:00
|
|
|
initMeshCoreIdentity();
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
activeIdx = 0;
|
|
|
|
|
applyConfig(CONFIGS[0]);
|
|
|
|
|
dwellStart = millis();
|
2026-07-01 15:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 18:07:05 +01:00
|
|
|
// Read a line from the serial console into the TX queue. Syntax:
|
|
|
|
|
// "hello" -> send "hello" on all networks
|
|
|
|
|
// "T:hello" -> send only on Meshtastic (T / C / R prefix, case-insensitive)
|
|
|
|
|
static void pollSerialTx() {
|
|
|
|
|
static char buf[210]; static size_t n = 0;
|
|
|
|
|
while (Serial.available()) {
|
|
|
|
|
char c = Serial.read();
|
|
|
|
|
if (c == '\r') continue;
|
|
|
|
|
if (c == '\n') {
|
|
|
|
|
buf[n] = 0;
|
|
|
|
|
if (n > 0) {
|
|
|
|
|
const char* msg = buf;
|
|
|
|
|
strcpy(txNets, "TCR"); // default: all networks
|
|
|
|
|
if (n >= 2 && buf[1] == ':') {
|
|
|
|
|
char L = toupper(buf[0]);
|
|
|
|
|
if (L == 'T' || L == 'C' || L == 'R') { txNets[0] = L; txNets[1] = 0; msg = buf + 2; }
|
|
|
|
|
}
|
|
|
|
|
strncpy(txText, msg, sizeof(txText) - 1); txText[sizeof(txText) - 1] = 0;
|
|
|
|
|
txPending = true;
|
|
|
|
|
Serial.printf(" [tx] queued for %s: \"%s\"\n", txNets, txText);
|
|
|
|
|
}
|
|
|
|
|
n = 0;
|
|
|
|
|
} else if (n < sizeof(buf) - 1) {
|
|
|
|
|
buf[n++] = c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If a message is queued for the currently-tuned network, send it now.
|
|
|
|
|
static void flushTxOnWindow() {
|
|
|
|
|
if (!txPending) return;
|
|
|
|
|
char L = CONFIGS[activeIdx].letter;
|
|
|
|
|
char* p = strchr(txNets, L);
|
|
|
|
|
if (!p) return; // not queued for this network
|
|
|
|
|
|
|
|
|
|
if (L == 'T') {
|
|
|
|
|
sendMeshtasticText(txText);
|
|
|
|
|
} else {
|
|
|
|
|
// MeshCore / Reticulum TX framing not built yet — acknowledge, don't fake it.
|
|
|
|
|
Serial.printf(" [%c] TX not implemented yet (msg: \"%s\")\n", L, txText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove this network from the pending set; done when none remain.
|
|
|
|
|
memmove(p, p + 1, strlen(p)); // includes the NUL terminator
|
|
|
|
|
if (txNets[0] == 0) txPending = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:45:36 +01:00
|
|
|
void loop() {
|
2026-07-01 18:07:05 +01:00
|
|
|
pollSerialTx();
|
|
|
|
|
|
2026-07-01 16:02:33 +01:00
|
|
|
// Non-blocking scan: rotate the radio when the dwell window elapses, so the
|
|
|
|
|
// display keeps animating smoothly the whole time.
|
|
|
|
|
if (millis() - dwellStart >= DWELL_MS) {
|
|
|
|
|
activeIdx = (activeIdx + 1) % NUM_CONFIGS;
|
|
|
|
|
applyConfig(CONFIGS[activeIdx]);
|
|
|
|
|
dwellStart = millis();
|
|
|
|
|
}
|
2026-07-01 18:07:05 +01:00
|
|
|
flushTxOnWindow();
|
2026-07-01 16:02:33 +01:00
|
|
|
if (rxFlag) {
|
|
|
|
|
rxFlag = false;
|
|
|
|
|
drainPacket();
|
|
|
|
|
radio.startReceive();
|
2026-07-01 15:45:36 +01:00
|
|
|
}
|
2026-07-01 16:35:18 +01:00
|
|
|
// Announce ourselves as a Meshtastic node while tuned to that PHY.
|
|
|
|
|
if (CONFIGS[activeIdx].letter == 'T' &&
|
|
|
|
|
(lastAnnounceMs == 0 || millis() - lastAnnounceMs > ANNOUNCE_INTERVAL_MS)) {
|
|
|
|
|
announceMeshtastic();
|
|
|
|
|
lastAnnounceMs = millis();
|
|
|
|
|
}
|
2026-07-01 18:07:05 +01:00
|
|
|
// Advertise ourselves as a MeshCore chat node while tuned to that PHY, so we
|
|
|
|
|
// show up as a contact in other MeshCore clients.
|
|
|
|
|
if (CONFIGS[activeIdx].letter == 'C' &&
|
|
|
|
|
(lastMcAdvertMs == 0 || millis() - lastMcAdvertMs > MC_ADVERT_INTERVAL_MS)) {
|
|
|
|
|
sendMeshCoreAdvert();
|
|
|
|
|
lastMcAdvertMs = millis();
|
|
|
|
|
}
|
2026-07-01 16:02:33 +01:00
|
|
|
drawUI();
|
|
|
|
|
frame++;
|
|
|
|
|
delay(45); // ~20 fps
|
2026-07-01 15:45:36 +01:00
|
|
|
}
|