Adapt to Listener Structure
This commit is contained in:
+251
-154
@@ -6,213 +6,310 @@
|
|||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
|
|
||||||
// LoRa pins - LilyGO T3S3 SX1262
|
// LoRa pins - LilyGO T3S3 SX1262
|
||||||
static const int PIN_LORA_SCK = 5;
|
static const int mgLoraSck = 5;
|
||||||
static const int PIN_LORA_MISO = 3;
|
static const int mgLoraMiso = 3;
|
||||||
static const int PIN_LORA_MOSI = 6;
|
static const int mgLoraMosi = 6;
|
||||||
static const int PIN_LORA_CS = 7;
|
static const int mgLoraCs = 7;
|
||||||
static const int PIN_LORA_RST = 8;
|
static const int mgLoraRst = 8;
|
||||||
static const int PIN_LORA_DIO1 = 33;
|
static const int mgLoraDio1 = 33;
|
||||||
static const int PIN_LORA_BUSY = 34;
|
static const int mgLoraBusy = 34;
|
||||||
|
|
||||||
// Button - LilyGO T3S3
|
// Button - LilyGO T3S3
|
||||||
static const int PIN_BUTTON = 0;
|
static const int mgButton = 0;
|
||||||
|
|
||||||
// OLED pins - LilyGO T3S3 OLED
|
// OLED pins - LilyGO T3S3 OLED
|
||||||
static const int OLED_SDA = 18;
|
static const int mgOledSda = 18;
|
||||||
static const int OLED_SCL = 17;
|
static const int mgOledScl = 17;
|
||||||
static const int OLED_ADDR = 0x3C;
|
static const int mgOledAddr = 0x3C;
|
||||||
static const int SCREEN_WIDTH = 128;
|
static const int mgScreenWidth = 128;
|
||||||
static const int SCREEN_HEIGHT = 64;
|
static const int mgScreenHeight = 64;
|
||||||
|
|
||||||
// ACTIVE PROFILE - Shelly / custom LoRa
|
// Shelly HB9GPU RF Setting
|
||||||
static const float LORA_FREQ_MHZ = 865.000;
|
static const float mgLoraFreqMHz = 865.000;
|
||||||
static const float LORA_BW_KHZ = 125.0;
|
static const float mgLoraBwKHz = 125.0;
|
||||||
static const uint8_t LORA_SF = 12;
|
static const uint8_t mgLoraSf = 12;
|
||||||
static const uint8_t LORA_CR = 5;
|
static const uint8_t mgLoraCr = 5;
|
||||||
static const uint8_t LORA_SYNC_WORD = 0x12;
|
static const uint8_t mgLoraSyncWord = 0x12;
|
||||||
static const int8_t LORA_TX_POWER = 10;
|
static const int8_t mgLoraTxPower = 10;
|
||||||
static const uint16_t LORA_PREAMBLE = 10;
|
static const uint16_t mgLoraPreamble = 10;
|
||||||
static const float LORA_TCXO_VOLTAGE = 1.6;
|
static const float mgLoraTcxoVoltage = 1.6;
|
||||||
static const bool LORA_USE_LDO = false;
|
static const bool mgLoraUseLdo = false;
|
||||||
static const bool LORA_USE_CRC = false;
|
static const bool mgLoraUseCrc = false;
|
||||||
static const bool LORA_EXPLICIT_HDR = true;
|
static const bool mgLoraExplicitHeader = true;
|
||||||
|
|
||||||
// Protocol
|
// MeshCore Switzerland RF Settings
|
||||||
static const String DEVICE_ID = "MG64738";
|
/*
|
||||||
static const String CMD_ON = "ON";
|
static const float mgLoraFreqMHz = 869.618;
|
||||||
static const String CMD_OFF = "OF";
|
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;
|
||||||
|
*/
|
||||||
|
|
||||||
// Objects
|
Adafruit_SSD1306 mgDisplay(mgScreenWidth, mgScreenHeight, &Wire, -1);
|
||||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
SX1262 mgRadio = new Module(mgLoraCs, mgLoraDio1, mgLoraRst, mgLoraBusy);
|
||||||
SX1262 radio = new Module(PIN_LORA_CS, PIN_LORA_DIO1, PIN_LORA_RST, PIN_LORA_BUSY);
|
|
||||||
|
|
||||||
// Runtime state
|
static const String mgDeviceId = "MG64738";
|
||||||
bool outputState = true; // start with ON
|
static const String mgCmdOn = "ON";
|
||||||
bool lastButtonState = HIGH;
|
static const String mgCmdOff = "OF";
|
||||||
unsigned long lastDebounceMs = 0;
|
static const unsigned long mgDebounceMs = 60;
|
||||||
unsigned long txCount = 0;
|
|
||||||
int lastTxState = 0;
|
|
||||||
|
|
||||||
static const unsigned long DEBOUNCE_MS = 60;
|
bool mgOutputState = true;
|
||||||
|
bool mgLastButtonState = HIGH;
|
||||||
// Helpers
|
bool mgPressHandled = false;
|
||||||
String formatCR(uint8_t cr) {
|
unsigned long mgLastDebounceMs = 0;
|
||||||
return "4/" + String(cr);
|
unsigned long mgTxCount = 0;
|
||||||
}
|
int mgLastTxState = 0;
|
||||||
|
|
||||||
String formatHexByte(uint8_t value) {
|
|
||||||
char buf[5];
|
|
||||||
snprintf(buf, sizeof(buf), "%02X", value);
|
|
||||||
return String(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
String currentPayload() {
|
|
||||||
return DEVICE_ID + "|" + (outputState ? CMD_ON : CMD_OFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawLine(int x, int y, const String& text) {
|
|
||||||
display.setCursor(x, y);
|
|
||||||
display.print(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawStatusScreen(const String& status) {
|
|
||||||
display.clearDisplay();
|
|
||||||
display.setTextSize(1);
|
|
||||||
display.setTextColor(SSD1306_WHITE);
|
|
||||||
|
|
||||||
drawLine(0, 0, "LoRa Button TX");
|
|
||||||
drawLine(88, 0, String(txCount) + " tx");
|
|
||||||
|
|
||||||
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, 42, status);
|
|
||||||
drawLine(0, 54, currentPayload());
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendCurrentState() {
|
|
||||||
String payload = currentPayload();
|
|
||||||
|
|
||||||
drawStatusScreen("Sending...");
|
|
||||||
|
|
||||||
Serial.print("TX: ");
|
|
||||||
Serial.println(payload);
|
|
||||||
|
|
||||||
int state = radio.transmit(payload);
|
|
||||||
lastTxState = state;
|
|
||||||
|
|
||||||
if (state == RADIOLIB_ERR_NONE) {
|
|
||||||
txCount++;
|
|
||||||
|
|
||||||
Serial.println("TX OK");
|
|
||||||
drawStatusScreen(outputState ? "Sent ON" : "Sent OF");
|
|
||||||
} else {
|
|
||||||
Serial.print("TX failed, code ");
|
|
||||||
Serial.println(state);
|
|
||||||
drawStatusScreen("TX failed: " + String(state));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1500);
|
delay(1500);
|
||||||
|
|
||||||
pinMode(PIN_BUTTON, INPUT_PULLUP);
|
pinMode(mgButton, INPUT_PULLUP);
|
||||||
|
|
||||||
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");
|
Serial.println("OLED init failed");
|
||||||
while (true) delay(1000);
|
while (true) delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawStatusScreen("Booting...");
|
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();
|
||||||
Serial.println("Starting LoRa button sender...");
|
Serial.println("Starting LoRa sender...");
|
||||||
|
|
||||||
int state = radio.begin(
|
int mgState = mgRadio.begin(
|
||||||
LORA_FREQ_MHZ,
|
mgLoraFreqMHz,
|
||||||
LORA_BW_KHZ,
|
mgLoraBwKHz,
|
||||||
LORA_SF,
|
mgLoraSf,
|
||||||
LORA_CR,
|
mgLoraCr,
|
||||||
LORA_SYNC_WORD,
|
mgLoraSyncWord,
|
||||||
LORA_TX_POWER,
|
mgLoraTxPower,
|
||||||
LORA_PREAMBLE,
|
mgLoraPreamble,
|
||||||
LORA_TCXO_VOLTAGE,
|
mgLoraTcxoVoltage,
|
||||||
LORA_USE_LDO
|
mgLoraUseLdo
|
||||||
);
|
);
|
||||||
|
|
||||||
if (state != RADIOLIB_ERR_NONE) {
|
if (mgState != RADIOLIB_ERR_NONE) {
|
||||||
Serial.print("radio.begin failed, code ");
|
Serial.print("radio.begin failed, code ");
|
||||||
Serial.println(state);
|
Serial.println(mgState);
|
||||||
drawErrorScreen("LoRa init failed", state);
|
|
||||||
|
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);
|
while (true) delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LORA_EXPLICIT_HDR) {
|
if (mgLoraExplicitHeader) {
|
||||||
state = radio.explicitHeader();
|
mgState = mgRadio.explicitHeader();
|
||||||
} else {
|
} 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.print("header mode failed, code ");
|
||||||
Serial.println(state);
|
Serial.println(mgState);
|
||||||
drawErrorScreen("Header config failed", state);
|
|
||||||
|
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);
|
while (true) delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
state = radio.setCRC(LORA_USE_CRC);
|
mgState = mgRadio.setCRC(mgLoraUseCrc);
|
||||||
|
|
||||||
if (state != RADIOLIB_ERR_NONE) {
|
if (mgState != RADIOLIB_ERR_NONE) {
|
||||||
Serial.print("CRC config failed, code ");
|
Serial.print("CRC config failed, code ");
|
||||||
Serial.println(state);
|
Serial.println(mgState);
|
||||||
drawErrorScreen("CRC config failed", state);
|
|
||||||
|
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);
|
while (true) delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawStatusScreen("Ready");
|
String mgPayload = mgDeviceId + "|" + (mgOutputState ? mgCmdOn : mgCmdOff);
|
||||||
|
|
||||||
sendCurrentState(); // first send = MG64738|ON
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("Ready");
|
||||||
|
mgDisplay.setCursor(70, 0);
|
||||||
|
mgDisplay.print(mgTxCount);
|
||||||
|
mgDisplay.print(" tx");
|
||||||
|
|
||||||
|
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, 42);
|
||||||
|
mgDisplay.print("Payload:");
|
||||||
|
mgDisplay.setCursor(0, 54);
|
||||||
|
mgDisplay.print(mgPayload.substring(0, 21));
|
||||||
|
|
||||||
|
mgDisplay.display();
|
||||||
|
|
||||||
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("Sending...");
|
||||||
|
mgDisplay.setCursor(0, 12);
|
||||||
|
mgDisplay.print(mgPayload.substring(0, 21));
|
||||||
|
mgDisplay.display();
|
||||||
|
|
||||||
|
Serial.print("TX: ");
|
||||||
|
Serial.println(mgPayload);
|
||||||
|
|
||||||
|
mgState = mgRadio.transmit(mgPayload);
|
||||||
|
mgLastTxState = mgState;
|
||||||
|
|
||||||
|
if (mgState == RADIOLIB_ERR_NONE) {
|
||||||
|
mgTxCount++;
|
||||||
|
|
||||||
|
Serial.println("TX OK");
|
||||||
|
|
||||||
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("Sent ON");
|
||||||
|
mgDisplay.setCursor(70, 0);
|
||||||
|
mgDisplay.print(mgTxCount);
|
||||||
|
mgDisplay.print(" tx");
|
||||||
|
mgDisplay.setCursor(0, 12);
|
||||||
|
mgDisplay.print(mgPayload.substring(0, 21));
|
||||||
|
mgDisplay.display();
|
||||||
|
} else {
|
||||||
|
Serial.print("TX failed, code ");
|
||||||
|
Serial.println(mgState);
|
||||||
|
|
||||||
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("TX failed");
|
||||||
|
mgDisplay.setCursor(0, 12);
|
||||||
|
mgDisplay.print("code: ");
|
||||||
|
mgDisplay.print(mgState);
|
||||||
|
mgDisplay.display();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
bool buttonState = digitalRead(PIN_BUTTON);
|
bool mgButtonState = digitalRead(mgButton);
|
||||||
|
|
||||||
if (buttonState != lastButtonState) {
|
if (mgButtonState != mgLastButtonState) {
|
||||||
lastDebounceMs = millis();
|
mgLastDebounceMs = millis();
|
||||||
lastButtonState = buttonState;
|
mgLastButtonState = mgButtonState;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((millis() - lastDebounceMs) > DEBOUNCE_MS) {
|
if ((millis() - mgLastDebounceMs) > mgDebounceMs) {
|
||||||
static bool pressHandled = false;
|
if (mgButtonState == LOW && !mgPressHandled) {
|
||||||
|
mgPressHandled = true;
|
||||||
|
mgOutputState = !mgOutputState;
|
||||||
|
|
||||||
if (buttonState == LOW && !pressHandled) {
|
String mgPayload = mgDeviceId + "|" + (mgOutputState ? mgCmdOn : mgCmdOff);
|
||||||
pressHandled = true;
|
|
||||||
|
|
||||||
outputState = !outputState;
|
mgDisplay.clearDisplay();
|
||||||
sendCurrentState();
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("Sending...");
|
||||||
|
mgDisplay.setCursor(0, 12);
|
||||||
|
mgDisplay.print(mgPayload.substring(0, 21));
|
||||||
|
mgDisplay.display();
|
||||||
|
|
||||||
|
Serial.print("TX: ");
|
||||||
|
Serial.println(mgPayload);
|
||||||
|
|
||||||
|
int mgState = mgRadio.transmit(mgPayload);
|
||||||
|
mgLastTxState = mgState;
|
||||||
|
|
||||||
|
if (mgState == RADIOLIB_ERR_NONE) {
|
||||||
|
mgTxCount++;
|
||||||
|
|
||||||
|
Serial.println("TX OK");
|
||||||
|
|
||||||
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print(mgOutputState ? "Sent ON" : "Sent OF");
|
||||||
|
mgDisplay.setCursor(70, 0);
|
||||||
|
mgDisplay.print(mgTxCount);
|
||||||
|
mgDisplay.print(" tx");
|
||||||
|
|
||||||
|
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, 42);
|
||||||
|
mgDisplay.print("Payload:");
|
||||||
|
mgDisplay.setCursor(0, 54);
|
||||||
|
mgDisplay.print(mgPayload.substring(0, 21));
|
||||||
|
|
||||||
|
mgDisplay.display();
|
||||||
|
} else {
|
||||||
|
Serial.print("TX failed, code ");
|
||||||
|
Serial.println(mgState);
|
||||||
|
|
||||||
|
mgDisplay.clearDisplay();
|
||||||
|
mgDisplay.setCursor(0, 0);
|
||||||
|
mgDisplay.print("TX failed");
|
||||||
|
mgDisplay.setCursor(0, 12);
|
||||||
|
mgDisplay.print("code: ");
|
||||||
|
mgDisplay.print(mgState);
|
||||||
|
mgDisplay.display();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buttonState == HIGH) {
|
if (mgButtonState == HIGH) {
|
||||||
pressHandled = false;
|
mgPressHandled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user