From fb957ff05b597509f209ff27a5a88d0a6ebfa93b Mon Sep 17 00:00:00 2001 From: Malko Gindrat Date: Wed, 20 May 2026 00:08:18 +0200 Subject: [PATCH] Initial Code --- .gitignore | 5 + .vscode/extensions.json | 10 ++ include/README | 37 +++++++ lib/README | 46 ++++++++ platformio.ini | 21 ++++ src/main.cpp | 235 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++ 7 files changed, 365 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..85b4801 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:lilygo-t3-s3] +platform = espressif32 +board = lilygo-t3-s3 +framework = arduino +monitor_speed = 115200 +build_flags = + -D ARDUINO_USB_CDC_ON_BOOT=1 +lib_deps = + jgromes/RadioLib + adafruit/Adafruit SSD1306 + adafruit/Adafruit GFX Library diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f45e536 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include + +// ========================= +// 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; + +// ========================= +// Button - LilyGO T3S3 +// BOOT button usually GPIO0, active low +// ========================= +static const int PIN_BUTTON = 0; + +// ========================= +// 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; + +// ========================= +// 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; + +// ========================= +// Protocol +// ========================= +static const String DEVICE_ID = "MG64738"; +static const String CMD_ON = "ON"; +static const String CMD_OFF = "OF"; + +// ========================= +// 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 +// ========================= +bool outputState = true; // start with ON +bool lastButtonState = HIGH; +unsigned long lastDebounceMs = 0; +unsigned long txCount = 0; +int lastTxState = 0; + +static const unsigned long DEBOUNCE_MS = 60; + +// ========================= +// 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); +} + +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() { + Serial.begin(115200); + delay(1500); + + pinMode(PIN_BUTTON, INPUT_PULLUP); + + Wire.begin(OLED_SDA, OLED_SCL); + + if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("OLED init failed"); + while (true) delay(1000); + } + + drawStatusScreen("Booting..."); + + SPI.begin(PIN_LORA_SCK, PIN_LORA_MISO, PIN_LORA_MOSI, PIN_LORA_CS); + + Serial.println(); + Serial.println("Starting LoRa button sender..."); + + 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 + ); + + if (state != RADIOLIB_ERR_NONE) { + Serial.print("radio.begin failed, code "); + Serial.println(state); + drawErrorScreen("LoRa init failed", state); + while (true) delay(1000); + } + + if (LORA_EXPLICIT_HDR) { + state = radio.explicitHeader(); + } else { + state = radio.implicitHeader(LORA_PREAMBLE); + } + + if (state != RADIOLIB_ERR_NONE) { + Serial.print("header mode failed, code "); + Serial.println(state); + drawErrorScreen("Header config failed", state); + while (true) delay(1000); + } + + state = radio.setCRC(LORA_USE_CRC); + + if (state != RADIOLIB_ERR_NONE) { + Serial.print("CRC config failed, code "); + Serial.println(state); + drawErrorScreen("CRC config failed", state); + while (true) delay(1000); + } + + drawStatusScreen("Ready"); + + sendCurrentState(); // first send = MG64738|ON +} + +void loop() { + bool buttonState = digitalRead(PIN_BUTTON); + + if (buttonState != lastButtonState) { + lastDebounceMs = millis(); + lastButtonState = buttonState; + } + + if ((millis() - lastDebounceMs) > DEBOUNCE_MS) { + static bool pressHandled = false; + + if (buttonState == LOW && !pressHandled) { + pressHandled = true; + + outputState = !outputState; + sendCurrentState(); + } + + if (buttonState == HIGH) { + pressHandled = false; + } + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html