From 68b86a1d6d22381da11849a81cd14acb9322d227 Mon Sep 17 00:00:00 2001 From: acidvegas Date: Wed, 5 Jun 2024 20:04:40 -0400 Subject: [PATCH] Added SD card support and started Lora support --- src/apps/LoRa.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++ src/apps/gotify.cpp | 42 +++++++++++++++++++++++++ src/main.ino | 10 ++++++ src/pins.h | 4 ++- 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/apps/LoRa.cpp create mode 100644 src/apps/gotify.cpp diff --git a/src/apps/LoRa.cpp b/src/apps/LoRa.cpp new file mode 100644 index 0000000..0349a34 --- /dev/null +++ b/src/apps/LoRa.cpp @@ -0,0 +1,76 @@ +#include +#include "pins.h" + + +SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); + + +bool setupRadio() { + digitalWrite(BOARD_SDCARD_CS, HIGH); + digitalWrite(RADIO_CS_PIN, HIGH); + digitalWrite(BOARD_TFT_CS, HIGH); + SPI.end(); + SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); //SD + + int state = radio.begin(RADIO_FREQ); + if (state == RADIOLIB_ERR_NONE) { + Serial.println("Start Radio success!"); + } else { + Serial.print("Start Radio failed,code:"); + Serial.println(state); + return false; + } + + if (radio.setFrequency(RADIO_FREQ) == RADIOLIB_ERR_INVALID_FREQUENCY) { + Serial.println(F("Selected frequency is invalid for this module!")); + return false; + } + + if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) { + Serial.println(F("Selected bandwidth is invalid for this module!")); + return false; + } + + if (radio.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) { + Serial.println(F("Selected spreading factor is invalid for this module!")); + return false; + } + + if (radio.setCodingRate(6) == RADIOLIB_ERR_INVALID_CODING_RATE) { + Serial.println(F("Selected coding rate is invalid for this module!")); + return false; + } + + if (radio.setSyncWord(0xAB) != RADIOLIB_ERR_NONE) { + Serial.println(F("Unable to set sync word!")); + return false; + } + + // set output power to 10 dBm (accepted range is -17 - 22 dBm) + if (radio.setOutputPower(17) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { + Serial.println(F("Selected output power is invalid for this module!")); + return false; + } + + // set over current protection limit to 140 mA (accepted range is 45 - 140 mA) (set value to 0 to disable overcurrent protection) + if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) { + Serial.println(F("Selected current limit is invalid for this module!")); + return false; + } + + // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535) + if (radio.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) { + Serial.println(F("Selected preamble length is invalid for this module!")); + return false; + } + + if (radio.setCRC(false) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) { + Serial.println(F("Selected CRC is invalid for this module!")); + return false; + } + + // set the function that will be called when new packet is received + //radio.setDio1Action(setFlag); + + return true; +} \ No newline at end of file diff --git a/src/apps/gotify.cpp b/src/apps/gotify.cpp new file mode 100644 index 0000000..6be3cf0 --- /dev/null +++ b/src/apps/gotify.cpp @@ -0,0 +1,42 @@ +#include + +using namespace websockets; + +WebsocketsClient gotifyClient; + +const char* gotify_server = "ws://your.gotify.server.com/stream"; // Use ws:// or wss:// based on your server configuration +const char* gotify_token = "your_gotify_app_token"; + +// Reconnection parameters +unsigned long lastAttemptTime = 0; +const unsigned long reconnectInterval = 60000; // 1 minute + +void onMessageCallback(WebsocketsMessage message) { + Serial.println("Gotify Notification:"); + Serial.println(message.data()); +} + +void connectToGotify() { + String url = String(gotify_server) + "?token=" + gotify_token; + gotifyClient.onMessage(onMessageCallback); + gotifyClient.connect(url); + + if (gotifyClient.available()) { + Serial.println("Connected to Gotify WebSocket"); + } else { + Serial.println("Failed to connect to Gotify WebSocket"); + } +} + +void checkGotifyWebSocket() { + if (!gotifyClient.available()) { + unsigned long currentTime = millis(); + if (currentTime - lastAttemptTime > reconnectInterval) { + Serial.println("Attempting to reconnect to Gotify WebSocket..."); + lastAttemptTime = currentTime; + connectToGotify(); + } + } else { + gotifyClient.poll(); + } +} \ No newline at end of file diff --git a/src/main.ino b/src/main.ino index 8d405e5..cc7e097 100644 --- a/src/main.ino +++ b/src/main.ino @@ -136,6 +136,16 @@ void setup() { digitalWrite(TFT_BL, HIGH); setBrightness(8); // Set the screen brightness to 50% + // Give power to the SD card + pinMode(BOARD_SDCARD_CS, OUTPUT); + digitalWrite(BOARD_SDCARD_CS, HIGH); + pinMode(BOARD_SPI_MISO, INPUT_PULLUP); + SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); //SD + + // Turn on power to the radio + pinMode(RADIO_CS_PIN, OUTPUT); + digitalWrite(RADIO_CS_PIN, HIGH); + // Start the I2C bus for the keyboard Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL); diff --git a/src/pins.h b/src/pins.h index da00365..8ab652b 100644 --- a/src/pins.h +++ b/src/pins.h @@ -58,4 +58,6 @@ #define CONV_FACTOR 1.8 // Conversion factor for the ADC to voltage conversion #define READS 20 // Number of readings for averaging -#define LILYGO_KB_SLAVE_ADDRESS 0x55 \ No newline at end of file +#define LILYGO_KB_SLAVE_ADDRESS 0x55 + +#define RADIO_FREQ 903.0 \ No newline at end of file