diff --git a/reciver/lora_reciver/src/display.cpp b/reciver/lora_reciver/src/display.cpp index 3d397d0..f77112e 100644 --- a/reciver/lora_reciver/src/display.cpp +++ b/reciver/lora_reciver/src/display.cpp @@ -5,57 +5,72 @@ extern logging::Logger logger; // Constructor to initialize the display with I2C parameters -Display::Display() - : display(DISPLAY_ADDRESS, DISPLAY_SDA, DISPLAY_SCL) { +Display::Display() + : display(DISPLAY_ADDRESS, DISPLAY_SDA, DISPLAY_SCL) +{ } -void Display::setup_display() { - if (!display.init()) { - +void Display::setup_display() +{ + pinMode(16, OUTPUT); + digitalWrite(16, LOW); // set GPIO16 low to reset OLED + delay(50); + digitalWrite(16, HIGH); + if (!display.init()) + { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "DISPLAY", "Display initialization failed!"); return; } display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_LEFT); - display.drawString(5,5,"LoRa Receiver"); - display.display(); - + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Display init done!"); } -void Display::display_toggle(bool toggle) { +void Display::display_toggle(bool toggle) +{ displayOn = toggle; - if (displayOn) { + if (displayOn) + { display.displayOn(); - } else { + } + else + { display.displayOff(); } } -void Display::cleanTFT() { +void Display::cleanTFT() +{ display.clear(); display.display(); + display.setFont(ArialMT_Plain_10); } -void Display::show_display(String header, int wait) { +void Display::show_display(String header, int wait) +{ logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "One line: %s", header.c_str()); - //cleanTFT(); - //display.drawString(0, 0, header); - display.drawString(5,5,"LoRa Receiver"); - display.display(); - delay(wait); -} - -void Display::show_display(String header, String line1, int wait) { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Two lines: %s, %s", header.c_str(), line1.c_str()); cleanTFT(); - display.drawString(0, 0, header); - display.drawString(0, 12, line1); + display.setFont(ArialMT_Plain_24); + display.drawString(0, 20, header); display.display(); delay(wait); } -void Display::show_display(String header, String line1, String line2, int wait) { +void Display::show_display(String header, String line1, int wait) +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Two lines: \n %s, \n %s", header.c_str(), line1.c_str()); + cleanTFT(); + display.setFont(ArialMT_Plain_16); + display.drawString(0, 0, header); + display.drawString(0, 18, line1); + display.display(); + delay(wait); +} + +void Display::show_display(String header, String line1, String line2, int wait) +{ logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Three lines: %s, %s, %s", header.c_str(), line1.c_str(), line2.c_str()); cleanTFT(); display.drawString(0, 0, header); @@ -65,7 +80,8 @@ void Display::show_display(String header, String line1, String line2, int wait) delay(wait); } -void Display::show_display(String header, String line1, String line2, String line3, int wait) { +void Display::show_display(String header, String line1, String line2, String line3, int wait) +{ logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Four lines: %s, %s, %s, %s", header.c_str(), line1.c_str(), line2.c_str(), line3.c_str()); cleanTFT(); display.drawString(0, 0, header); @@ -74,4 +90,36 @@ void Display::show_display(String header, String line1, String line2, String lin display.drawString(0, 36, line3); display.display(); delay(wait); -} \ No newline at end of file +} + +void Display::show_display(String header, String line1, String line2, String line3, String line4, int wait) +{ + + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Five lines: %s, %s, %s, %s, %s", header.c_str(), line1.c_str(), line2.c_str(), line3.c_str(), line4.c_str()); + cleanTFT(); + display.drawString(0, 0, header); + display.drawString(0, 12, line1); + display.drawString(0, 24, line2); + display.drawString(0, 36, line3); + display.drawString(0, 48, line4); + display.display(); + delay(wait); +} + +void Display::show_message(const char *message, int line, int wait) +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "DISPLAY", "Line %d: %s", line, message); + + display.setFont(ArialMT_Plain_10); + + // Set cursor position based on line number (optional) + if (line >= 0 || line <= 5) + { + display.drawString(0, line * 13, message); + } + + // Display the updated content + display.display(); + + delay(wait); // Convert wait time to milliseconds +} diff --git a/reciver/lora_reciver/src/display.hpp b/reciver/lora_reciver/src/display.hpp index 11b8c0b..e109de9 100644 --- a/reciver/lora_reciver/src/display.hpp +++ b/reciver/lora_reciver/src/display.hpp @@ -20,7 +20,9 @@ public: void show_display(String header, int wait = 0); void show_display(String header, String line1, int wait = 0); void show_display(String header, String line1, String line2, int wait = 0); - void show_display(String header, String line1, String line2, String line3, int wait = 0); + void show_display(String header, String line1, String line2, String line3, int wait = 0); + void show_display(String header, String line1, String line2, String line3, String line4, int wait = 0); + void show_message(const char* message, int line = 0, int wait = 0); private: SSD1306Wire display; diff --git a/reciver/lora_reciver/src/lorahandler.cpp b/reciver/lora_reciver/src/lorahandler.cpp index 4481f8f..476b5b9 100644 --- a/reciver/lora_reciver/src/lorahandler.cpp +++ b/reciver/lora_reciver/src/lorahandler.cpp @@ -5,19 +5,20 @@ extern logging::Logger logger; extern Config config; - void LoraHandler::setup() { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "Set SPI pins!"); SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS); LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ); - + long freq = config.loraConfig.frequency; - if (!LoRa.begin(freq)) { + if (!LoRa.begin(freq)) + { logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "LoRa", "Starting LoRa failed!"); - //show_display("ERROR", "Starting LoRa failed!"); - while (true) { + // show_display("ERROR", "Starting LoRa failed!"); + while (true) + { delay(1000); } } @@ -27,7 +28,27 @@ void LoraHandler::setup() LoRa.enableCrc(); LoRa.setTxPower(config.loraConfig.power); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "LoRa init done!"); - String currentLoRainfo = "LoRa Freq: " + String(config.loraConfig.frequency) + " / SF:" + String(config.loraConfig.spreadingFactor) + " / CR: " + String(config.loraConfig.codingRate4); + String currentLoRainfo = "LoRa Freq: " + String(config.loraConfig.frequency) + " / SF:" + String(config.loraConfig.spreadingFactor) + " / CR: " + String(config.loraConfig.codingRate4); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", currentLoRainfo.c_str()); +} +ReceivedLoRaPacket LoraHandler::receivePacket() +{ + ReceivedLoRaPacket receivedLoraPacket; + String packet = ""; + int packetSize = LoRa.parsePacket(); + if (packetSize) + { + while (LoRa.available()) + { + int inChar = LoRa.read(); + packet += (char)inChar; + } + receivedLoraPacket.text = packet; + receivedLoraPacket.rssi = LoRa.packetRssi(); + receivedLoraPacket.snr = LoRa.packetSnr(); + receivedLoraPacket.freqError = LoRa.packetFrequencyError(); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa Rx", "---> %s", packet.c_str()); + } + return receivedLoraPacket; } \ No newline at end of file diff --git a/reciver/lora_reciver/src/lorahandler.hpp b/reciver/lora_reciver/src/lorahandler.hpp index 3052ff3..8b5a1a3 100644 --- a/reciver/lora_reciver/src/lorahandler.hpp +++ b/reciver/lora_reciver/src/lorahandler.hpp @@ -12,10 +12,17 @@ #define LORA_RST 14 #define LORA_IRQ 26 // IRQ --> DIO0 +struct ReceivedLoRaPacket { + String text; + int rssi; + float snr; + int freqError; +}; class LoraHandler { public: void setup(); + ReceivedLoRaPacket receivePacket(); private: }; diff --git a/reciver/lora_reciver/src/main.cpp b/reciver/lora_reciver/src/main.cpp index 233e6a1..60ec006 100644 --- a/reciver/lora_reciver/src/main.cpp +++ b/reciver/lora_reciver/src/main.cpp @@ -13,13 +13,12 @@ WiFiClient wclient; Mqtt mqtt(wclient); LoraHandler lora; Display display; - +logging::Logger logger; #define TRIGGER_PIN 0 void checkButton(); bool portalRunning = false; -logging::Logger logger; void setup() { @@ -40,23 +39,42 @@ void setup() } delay(100); display.setup_display(); - // display.show_display("hej"); + display.show_display("STARTUP"); setupWifi("island_temp", false); - //mqtt.mqttSetup(); - //lora.setup(); + mqtt.mqttSetup(); + lora.setup(); + delay(5*1000); // Show text in 5s + display.cleanTFT(); } void loop() { - /* - display.show_display("First", 10); - sleep(10); - display.show_display("First line", "second line", 10); - sleep(10); - display.show_display("First line", "second line", "Third line ", 10); - sleep(10); + ReceivedLoRaPacket packet = lora.receivePacket(); + if (packet.text.isEmpty()) + { + packet.rssi = 69; + packet.snr = 3.3; + } + + if (WiFi.status() == WL_CONNECTED) { // Check WiFi connection + IPAddress ip = WiFi.localIP(); + String line0 = "IP: " + ip.toString(); + display.show_message(line0.c_str(), 0); + } else { + // Optional: Display message indicating WiFi not connected + display.show_message("WiFi not connected", 0); + } + int mqtt_pkt = 123455; + String line1 = "Mqtt pkt: " + String(mqtt_pkt); + display.show_message(line1.c_str(), 1); + String line2 = "Last lora, rssi: " + String(packet.rssi ) ; + display.show_message(line2.c_str(), 2); + display.show_message("Fourth", 3); + display.show_message("Fifth", 4); + sleep(5); + /* mqtt.mqttRun(); if (portalRunning) { diff --git a/reciver/lora_reciver/src/mqtt.cpp b/reciver/lora_reciver/src/mqtt.cpp index 7f74ddc..4842ff9 100644 --- a/reciver/lora_reciver/src/mqtt.cpp +++ b/reciver/lora_reciver/src/mqtt.cpp @@ -5,6 +5,7 @@ extern logging::Logger logger; +extern Config config; extern Config config; Mqtt::Mqtt(WiFiClient& wifiClient) : mqttClient(wifiClient) {} @@ -63,10 +64,6 @@ void Mqtt::mqttReconnect() config.setMqttId(mqttId); config.writeData(); } - if (ESP.getFreeHeap() < 5000) { // Check for minimum heap space - logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "MQTT", "Not enough heap space for MQTT connection"); - return; - } logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT CONFIG", "Server: %s", config.mqttConfig.server.c_str()); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT CONFIG", "Port: %d", config.mqttConfig.port); @@ -74,7 +71,6 @@ void Mqtt::mqttReconnect() logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT CONFIG", "Password: %s", config.mqttConfig.password.c_str()); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT CONFIG", "ID: %s", config.mqttConfig.id.c_str()); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT", "HEAP: %d", ESP.getFreeHeap()); if (mqttClient.connect(mqttId.c_str(), "simon", "bajsa123")) { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MQTT", "connected");