Compare commits
2 Commits
9ec763dc07
...
d025db2da9
| Author | SHA1 | Date | |
|---|---|---|---|
| d025db2da9 | |||
| b0070ed1b9 |
+203
-179
@@ -5,239 +5,263 @@
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
|
||||
// LoRa pins - LilyGO T3S3 SX1262
|
||||
static const int PIN_LORA_SCK = 5;
|
||||
static const int PIN_LORA_MISO = 3;
|
||||
static const int PIN_LORA_MOSI = 6;
|
||||
static const int PIN_LORA_CS = 7;
|
||||
static const int PIN_LORA_RST = 8;
|
||||
static const int PIN_LORA_DIO1 = 33;
|
||||
static const int PIN_LORA_BUSY = 34;
|
||||
|
||||
static const int mgLoraSck = 5;
|
||||
static const int mgLoraMiso = 3;
|
||||
static const int mgLoraMosi = 6;
|
||||
static const int mgLoraCs = 7;
|
||||
static const int mgLoraRst = 8;
|
||||
static const int mgLoraDio1 = 33;
|
||||
static const int mgLoraBusy = 34;
|
||||
|
||||
// OLED pins - LilyGO T3S3 OLED
|
||||
static const int OLED_SDA = 18;
|
||||
static const int OLED_SCL = 17;
|
||||
static const int OLED_ADDR = 0x3C;
|
||||
static const int SCREEN_WIDTH = 128;
|
||||
static const int SCREEN_HEIGHT = 64;
|
||||
static const int mgOledSda = 18;
|
||||
static const int mgOledScl = 17;
|
||||
static const int mgOledAddr = 0x3C;
|
||||
static const int mgScreenWidth = 128;
|
||||
static const int mgScreenHeight = 64;
|
||||
|
||||
// ACTIVE PROFILE (Shelly / custom LoRa)
|
||||
static const float LORA_FREQ_MHZ = 865.000;
|
||||
static const float LORA_BW_KHZ = 125.0;
|
||||
static const uint8_t LORA_SF = 12;
|
||||
static const uint8_t LORA_CR = 5;
|
||||
static const uint8_t LORA_SYNC_WORD = 0x12;
|
||||
static const int8_t LORA_TX_POWER = 10;
|
||||
static const uint16_t LORA_PREAMBLE = 10;
|
||||
static const float LORA_TCXO_VOLTAGE = 1.6;
|
||||
static const bool LORA_USE_LDO = false;
|
||||
static const bool LORA_USE_CRC = false;
|
||||
static const bool LORA_EXPLICIT_HDR = true;
|
||||
// Shelly HB9GPU RF Setting
|
||||
static const float mgLoraFreqMHz = 865.000;
|
||||
static const float mgLoraBwKHz = 125.0;
|
||||
static const uint8_t mgLoraSf = 12;
|
||||
static const uint8_t mgLoraCr = 5;
|
||||
static const uint8_t mgLoraSyncWord = 0x12;
|
||||
static const int8_t mgLoraTxPower = 10;
|
||||
static const uint16_t mgLoraPreamble = 10;
|
||||
static const float mgLoraTcxoVoltage = 1.6;
|
||||
static const bool mgLoraUseLdo = false;
|
||||
static const bool mgLoraUseCrc = false;
|
||||
static const bool mgLoraExplicitHeader = true;
|
||||
|
||||
|
||||
// MESHCORE SWITZERLAND PROFILE (COMMENTED)
|
||||
// MeshCore Switzerland RF Settings
|
||||
/*
|
||||
static const float LORA_FREQ_MHZ = 869.618;
|
||||
static const float LORA_BW_KHZ = 62.5;
|
||||
static const uint8_t LORA_SF = 8;
|
||||
static const uint8_t LORA_CR = 8;
|
||||
static const uint8_t LORA_SYNC_WORD = 0x34;
|
||||
static const int8_t LORA_TX_POWER = 10;
|
||||
static const uint16_t LORA_PREAMBLE = 10;
|
||||
static const float LORA_TCXO_VOLTAGE = 1.6;
|
||||
static const bool LORA_USE_LDO = false;
|
||||
static const bool LORA_USE_CRC = true;
|
||||
static const bool LORA_EXPLICIT_HDR = true;
|
||||
static const float mgLoraFreqMHz = 869.618;
|
||||
static const float mgLoraBwKHz = 62.5;
|
||||
static const uint8_t mgLoraSf = 8;
|
||||
static const uint8_t mgLoraCr = 8;
|
||||
static const uint8_t mgLoraSyncWord = 0x34;
|
||||
static const int8_t mgLoraTxPower = 10;
|
||||
static const uint16_t mgLoraPreamble = 10;
|
||||
static const float mgLoraTcxoVoltage = 1.6;
|
||||
static const bool mgLoraUseLdo = false;
|
||||
static const bool mgLoraUseCrc = true;
|
||||
static const bool mgLoraExplicitHeader = true;
|
||||
*/
|
||||
|
||||
Adafruit_SSD1306 mgDisplay(mgScreenWidth, mgScreenHeight, &Wire, -1);
|
||||
SX1262 mgRadio = new Module(mgLoraCs, mgLoraDio1, mgLoraRst, mgLoraBusy);
|
||||
|
||||
// Objects
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
||||
SX1262 radio = new Module(PIN_LORA_CS, PIN_LORA_DIO1, PIN_LORA_RST, PIN_LORA_BUSY);
|
||||
|
||||
// Runtime state
|
||||
String lastMsg = "none";
|
||||
float lastRssi = 0.0;
|
||||
float lastSnr = 0.0;
|
||||
unsigned long rxCount = 0;
|
||||
unsigned long lastIdleRefresh = 0;
|
||||
unsigned long showPacketUntil = 0;
|
||||
|
||||
// Helpers
|
||||
String formatCR(uint8_t cr) {
|
||||
return "4/" + String(cr);
|
||||
}
|
||||
|
||||
String formatHexByte(uint8_t value) {
|
||||
char buf[5];
|
||||
snprintf(buf, sizeof(buf), "%02X", value);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
// Draw one line safely
|
||||
void drawLine(int x, int y, const String& text) {
|
||||
display.setCursor(x, y);
|
||||
display.print(text);
|
||||
}
|
||||
|
||||
// Simple text wrapper for packet display
|
||||
void drawWrappedText(const String& text, int startY, int maxLines) {
|
||||
const int charsPerLine = 21; // approx for 128 px at text size 1
|
||||
int len = text.length();
|
||||
int index = 0;
|
||||
int line = 0;
|
||||
|
||||
while (index < len && line < maxLines) {
|
||||
String chunk = text.substring(index, min(index + charsPerLine, len));
|
||||
drawLine(0, startY + line * 8, chunk);
|
||||
index += charsPerLine;
|
||||
line++;
|
||||
}
|
||||
}
|
||||
|
||||
void drawListeningScreen() {
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
|
||||
drawLine(0, 0, "Listening");
|
||||
drawLine(70, 0, String(rxCount) + " pk");
|
||||
|
||||
drawLine(0, 10, "F:" + String(LORA_FREQ_MHZ, 3));
|
||||
drawLine(0, 20, "BW:" + String(LORA_BW_KHZ, 1) + " SF:" + String(LORA_SF));
|
||||
drawLine(0, 30, "CR:" + formatCR(LORA_CR) + " SW:0x" + formatHexByte(LORA_SYNC_WORD));
|
||||
|
||||
drawLine(0, 40, "Last:");
|
||||
drawWrappedText(lastMsg, 48, 2);
|
||||
|
||||
display.display();
|
||||
}
|
||||
|
||||
void drawPacketScreen() {
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
|
||||
drawLine(0, 0, "PACKET RECEIVED");
|
||||
drawLine(0, 10, "RSSI:" + String(lastRssi, 1) + " SNR:" + String(lastSnr, 1));
|
||||
drawLine(0, 20, "Len:" + String(lastMsg.length()));
|
||||
|
||||
drawLine(0, 32, "Text:");
|
||||
drawWrappedText(lastMsg, 40, 3);
|
||||
|
||||
display.display();
|
||||
}
|
||||
|
||||
void drawErrorScreen(const String& title, int code) {
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
|
||||
drawLine(0, 0, title);
|
||||
drawLine(0, 12, "code: " + String(code));
|
||||
|
||||
display.display();
|
||||
}
|
||||
String mgLastMsg = "none";
|
||||
float mgLastRssi = 0.0;
|
||||
float mgLastSnr = 0.0;
|
||||
unsigned long mgRxCount = 0;
|
||||
unsigned long mgLastIdleRefresh = 0;
|
||||
unsigned long mgShowPacketUntil = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1500);
|
||||
|
||||
Wire.begin(OLED_SDA, OLED_SCL);
|
||||
Wire.begin(mgOledSda, mgOledScl);
|
||||
|
||||
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
|
||||
if (!mgDisplay.begin(SSD1306_SWITCHCAPVCC, mgOledAddr)) {
|
||||
Serial.println("OLED init failed");
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
drawLine(0, 0, "Booting...");
|
||||
display.display();
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setTextSize(1);
|
||||
mgDisplay.setTextColor(SSD1306_WHITE);
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("Booting...");
|
||||
mgDisplay.display();
|
||||
|
||||
SPI.begin(PIN_LORA_SCK, PIN_LORA_MISO, PIN_LORA_MOSI, PIN_LORA_CS);
|
||||
SPI.begin(mgLoraSck, mgLoraMiso, mgLoraMosi, mgLoraCs);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Starting LoRa receiver...");
|
||||
|
||||
int state = radio.begin(
|
||||
LORA_FREQ_MHZ,
|
||||
LORA_BW_KHZ,
|
||||
LORA_SF,
|
||||
LORA_CR,
|
||||
LORA_SYNC_WORD,
|
||||
LORA_TX_POWER,
|
||||
LORA_PREAMBLE,
|
||||
LORA_TCXO_VOLTAGE,
|
||||
LORA_USE_LDO
|
||||
int mgState = mgRadio.begin(
|
||||
mgLoraFreqMHz,
|
||||
mgLoraBwKHz,
|
||||
mgLoraSf,
|
||||
mgLoraCr,
|
||||
mgLoraSyncWord,
|
||||
mgLoraTxPower,
|
||||
mgLoraPreamble,
|
||||
mgLoraTcxoVoltage,
|
||||
mgLoraUseLdo
|
||||
);
|
||||
|
||||
if (state != RADIOLIB_ERR_NONE) {
|
||||
if (mgState != RADIOLIB_ERR_NONE) {
|
||||
Serial.print("radio.begin failed, code ");
|
||||
Serial.println(state);
|
||||
drawErrorScreen("LoRa init failed", state);
|
||||
Serial.println(mgState);
|
||||
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("LoRa init failed");
|
||||
mgDisplay.setCursor(0, 12);
|
||||
mgDisplay.print("code: ");
|
||||
mgDisplay.print(mgState);
|
||||
mgDisplay.display();
|
||||
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
if (LORA_EXPLICIT_HDR) {
|
||||
state = radio.explicitHeader();
|
||||
if (mgLoraExplicitHeader) {
|
||||
mgState = mgRadio.explicitHeader();
|
||||
} else {
|
||||
state = radio.implicitHeader(LORA_PREAMBLE);
|
||||
mgState = mgRadio.implicitHeader(mgLoraPreamble);
|
||||
}
|
||||
|
||||
if (state != RADIOLIB_ERR_NONE) {
|
||||
if (mgState != RADIOLIB_ERR_NONE) {
|
||||
Serial.print("header mode failed, code ");
|
||||
Serial.println(state);
|
||||
drawErrorScreen("Header config failed", state);
|
||||
Serial.println(mgState);
|
||||
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("Header failed");
|
||||
mgDisplay.setCursor(0, 12);
|
||||
mgDisplay.print("code: ");
|
||||
mgDisplay.print(mgState);
|
||||
mgDisplay.display();
|
||||
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
state = radio.setCRC(LORA_USE_CRC);
|
||||
if (state != RADIOLIB_ERR_NONE) {
|
||||
mgState = mgRadio.setCRC(mgLoraUseCrc);
|
||||
|
||||
if (mgState != RADIOLIB_ERR_NONE) {
|
||||
Serial.print("CRC config failed, code ");
|
||||
Serial.println(state);
|
||||
drawErrorScreen("CRC config failed", state);
|
||||
Serial.println(mgState);
|
||||
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("CRC failed");
|
||||
mgDisplay.setCursor(0, 12);
|
||||
mgDisplay.print("code: ");
|
||||
mgDisplay.print(mgState);
|
||||
mgDisplay.display();
|
||||
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
Serial.println("Listening...");
|
||||
drawListeningScreen();
|
||||
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("Listening");
|
||||
mgDisplay.setCursor(70, 0);
|
||||
mgDisplay.print(mgRxCount);
|
||||
mgDisplay.print(" pk");
|
||||
|
||||
mgDisplay.setCursor(0, 10);
|
||||
mgDisplay.print("F:");
|
||||
mgDisplay.print(mgLoraFreqMHz, 3);
|
||||
|
||||
mgDisplay.setCursor(0, 20);
|
||||
mgDisplay.print("BW:");
|
||||
mgDisplay.print(mgLoraBwKHz, 1);
|
||||
mgDisplay.print(" SF:");
|
||||
mgDisplay.print(mgLoraSf);
|
||||
|
||||
mgDisplay.setCursor(0, 30);
|
||||
mgDisplay.print("CR:4/");
|
||||
mgDisplay.print(mgLoraCr);
|
||||
mgDisplay.print(" SW:0x");
|
||||
if (mgLoraSyncWord < 16) mgDisplay.print("0");
|
||||
mgDisplay.print(mgLoraSyncWord, HEX);
|
||||
|
||||
mgDisplay.setCursor(0, 40);
|
||||
mgDisplay.print("Last:");
|
||||
mgDisplay.setCursor(0, 50);
|
||||
mgDisplay.print(mgLastMsg.substring(0, 21));
|
||||
|
||||
mgDisplay.display();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
String str;
|
||||
int state = radio.receive(str);
|
||||
String mgStr;
|
||||
int mgState = mgRadio.receive(mgStr);
|
||||
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
lastMsg = str;
|
||||
lastRssi = radio.getRSSI();
|
||||
lastSnr = radio.getSNR();
|
||||
rxCount++;
|
||||
if (mgState == RADIOLIB_ERR_NONE) {
|
||||
mgLastMsg = mgStr;
|
||||
mgLastRssi = mgRadio.getRSSI();
|
||||
mgLastSnr = mgRadio.getSNR();
|
||||
mgRxCount++;
|
||||
|
||||
Serial.print("RX: ");
|
||||
Serial.println(lastMsg);
|
||||
Serial.println(mgLastMsg);
|
||||
Serial.print("RSSI: ");
|
||||
Serial.println(lastRssi);
|
||||
Serial.println(mgLastRssi);
|
||||
Serial.print("SNR: ");
|
||||
Serial.println(lastSnr);
|
||||
Serial.println(mgLastSnr);
|
||||
Serial.println();
|
||||
|
||||
drawPacketScreen();
|
||||
showPacketUntil = millis() + 2500; // show packet screen for 2.5 s
|
||||
}
|
||||
else if (state != RADIOLIB_ERR_RX_TIMEOUT) {
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("PACKET RECEIVED");
|
||||
|
||||
mgDisplay.setCursor(0, 10);
|
||||
mgDisplay.print("RSSI:");
|
||||
mgDisplay.print(mgLastRssi, 1);
|
||||
mgDisplay.print(" SNR:");
|
||||
mgDisplay.print(mgLastSnr, 1);
|
||||
|
||||
mgDisplay.setCursor(0, 20);
|
||||
mgDisplay.print("Len:");
|
||||
mgDisplay.print(mgLastMsg.length());
|
||||
|
||||
mgDisplay.setCursor(0, 32);
|
||||
mgDisplay.print("Text:");
|
||||
|
||||
mgDisplay.setCursor(0, 40);
|
||||
mgDisplay.print(mgLastMsg.substring(0, 21));
|
||||
|
||||
mgDisplay.setCursor(0, 50);
|
||||
mgDisplay.print(mgLastMsg.substring(21, 42));
|
||||
|
||||
mgDisplay.display();
|
||||
|
||||
mgShowPacketUntil = millis() + 2500;
|
||||
} else if (mgState != RADIOLIB_ERR_RX_TIMEOUT) {
|
||||
Serial.print("Receive failed, code ");
|
||||
Serial.println(state);
|
||||
Serial.println(mgState);
|
||||
}
|
||||
|
||||
// after packet screen timeout, return to listening screen
|
||||
if (millis() > showPacketUntil) {
|
||||
if (millis() - lastIdleRefresh > 500) {
|
||||
drawListeningScreen();
|
||||
lastIdleRefresh = millis();
|
||||
}
|
||||
if (millis() > mgShowPacketUntil && millis() - mgLastIdleRefresh > 500) {
|
||||
mgDisplay.clearDisplay();
|
||||
mgDisplay.setCursor(0, 0);
|
||||
mgDisplay.print("Listening");
|
||||
mgDisplay.setCursor(70, 0);
|
||||
mgDisplay.print(mgRxCount);
|
||||
mgDisplay.print(" pk");
|
||||
|
||||
mgDisplay.setCursor(0, 10);
|
||||
mgDisplay.print("F:");
|
||||
mgDisplay.print(mgLoraFreqMHz, 3);
|
||||
|
||||
mgDisplay.setCursor(0, 20);
|
||||
mgDisplay.print("BW:");
|
||||
mgDisplay.print(mgLoraBwKHz, 1);
|
||||
mgDisplay.print(" SF:");
|
||||
mgDisplay.print(mgLoraSf);
|
||||
|
||||
mgDisplay.setCursor(0, 30);
|
||||
mgDisplay.print("CR:4/");
|
||||
mgDisplay.print(mgLoraCr);
|
||||
mgDisplay.print(" SW:0x");
|
||||
if (mgLoraSyncWord < 16) mgDisplay.print("0");
|
||||
mgDisplay.print(mgLoraSyncWord, HEX);
|
||||
|
||||
mgDisplay.setCursor(0, 40);
|
||||
mgDisplay.print("Last:");
|
||||
mgDisplay.setCursor(0, 50);
|
||||
mgDisplay.print(mgLastMsg.substring(0, 21));
|
||||
|
||||
mgDisplay.display();
|
||||
|
||||
mgLastIdleRefresh = millis();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user