From 2b9036abcb81be3a9593da0175e065bcaabad6dd Mon Sep 17 00:00:00 2001 From: Simon Milvert Date: Sat, 25 May 2024 22:38:38 +0200 Subject: [PATCH] Added display and lora --- reciver/lora_reciver/data/wifimanager.html | 33 ++++++++++ reciver/lora_reciver/platformio.ini | 2 + reciver/lora_reciver/src/config.hpp | 1 + reciver/lora_reciver/src/display.cpp | 77 ++++++++++++++++++++++ reciver/lora_reciver/src/display.hpp | 30 +++++++++ reciver/lora_reciver/src/lorahandler.cpp | 33 ++++++++++ reciver/lora_reciver/src/lorahandler.hpp | 22 +++++++ reciver/lora_reciver/src/main.cpp | 21 +++++- 8 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 reciver/lora_reciver/data/wifimanager.html create mode 100644 reciver/lora_reciver/src/display.cpp create mode 100644 reciver/lora_reciver/src/display.hpp create mode 100644 reciver/lora_reciver/src/lorahandler.cpp create mode 100644 reciver/lora_reciver/src/lorahandler.hpp diff --git a/reciver/lora_reciver/data/wifimanager.html b/reciver/lora_reciver/data/wifimanager.html new file mode 100644 index 0000000..901791b --- /dev/null +++ b/reciver/lora_reciver/data/wifimanager.html @@ -0,0 +1,33 @@ + + + + ESP Wi-Fi Manager + + + + + +
+

ESP Wi-Fi Manager

+
+
+
+
+
+

+ +
+ +
+ +
+ +
+ +

+
+
+
+
+ + diff --git a/reciver/lora_reciver/platformio.ini b/reciver/lora_reciver/platformio.ini index ea87e67..388648d 100644 --- a/reciver/lora_reciver/platformio.ini +++ b/reciver/lora_reciver/platformio.ini @@ -20,3 +20,5 @@ lib_deps = bblanchon/ArduinoJson@^7.0.4 peterus/esp-logger@^1.0.0 knolleary/PubSubClient@^2.8 + sandeepmistry/LoRa@^0.8.0 + thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.5.0 diff --git a/reciver/lora_reciver/src/config.hpp b/reciver/lora_reciver/src/config.hpp index 90e6084..232223a 100644 --- a/reciver/lora_reciver/src/config.hpp +++ b/reciver/lora_reciver/src/config.hpp @@ -3,6 +3,7 @@ #include #include +#include class MqttConfig { public: diff --git a/reciver/lora_reciver/src/display.cpp b/reciver/lora_reciver/src/display.cpp new file mode 100644 index 0000000..3d397d0 --- /dev/null +++ b/reciver/lora_reciver/src/display.cpp @@ -0,0 +1,77 @@ +#include "display.hpp" +#include "config.hpp" +#include "logger.h" + +extern logging::Logger logger; + +// Constructor to initialize the display with I2C parameters +Display::Display() + : display(DISPLAY_ADDRESS, DISPLAY_SDA, DISPLAY_SCL) { +} + +void Display::setup_display() { + 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) { + displayOn = toggle; + if (displayOn) { + display.displayOn(); + } else { + display.displayOff(); + } +} + +void Display::cleanTFT() { + display.clear(); + display.display(); +} +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.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); + display.drawString(0, 12, line1); + display.drawString(0, 24, line2); + display.display(); + delay(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); + display.drawString(0, 12, line1); + display.drawString(0, 24, line2); + display.drawString(0, 36, line3); + display.display(); + delay(wait); +} \ No newline at end of file diff --git a/reciver/lora_reciver/src/display.hpp b/reciver/lora_reciver/src/display.hpp new file mode 100644 index 0000000..11b8c0b --- /dev/null +++ b/reciver/lora_reciver/src/display.hpp @@ -0,0 +1,30 @@ +#ifndef DISPLAY_H +#define DISPLAY_H + +#include +//#include +#include "SSD1306Wire.h" +#define DISPLAY_ADDRESS 0x3c // CS --> NSS +#define DISPLAY_SDA 4 +#define DISPLAY_SCL 15 // IRQ --> DIO0 + + +class Display { +public: + Display(); + + void setup_display(); + void display_toggle(bool toggle); + void cleanTFT(); + + 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); + +private: + SSD1306Wire display; + bool displayOn; +}; + +#endif /* DISPLAY_H */ diff --git a/reciver/lora_reciver/src/lorahandler.cpp b/reciver/lora_reciver/src/lorahandler.cpp new file mode 100644 index 0000000..4481f8f --- /dev/null +++ b/reciver/lora_reciver/src/lorahandler.cpp @@ -0,0 +1,33 @@ +#include "config.hpp" +#include "lorahandler.hpp" +#include "logger.h" + +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)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "LoRa", "Starting LoRa failed!"); + //show_display("ERROR", "Starting LoRa failed!"); + while (true) { + delay(1000); + } + } + LoRa.setSpreadingFactor(config.loraConfig.spreadingFactor); + LoRa.setSignalBandwidth(config.loraConfig.signalBandwidth); + LoRa.setCodingRate4(config.loraConfig.codingRate4); + 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); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", currentLoRainfo.c_str()); + +} \ No newline at end of file diff --git a/reciver/lora_reciver/src/lorahandler.hpp b/reciver/lora_reciver/src/lorahandler.hpp new file mode 100644 index 0000000..3052ff3 --- /dev/null +++ b/reciver/lora_reciver/src/lorahandler.hpp @@ -0,0 +1,22 @@ +#ifndef LORAHANDLER_H +#define LORAHANDLER_H + +#include "config.hpp" +#include +#include + +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_CS 18 // CS --> NSS +#define LORA_RST 14 +#define LORA_IRQ 26 // IRQ --> DIO0 + + +class LoraHandler { + public: + void setup(); + private: +}; + +#endif /* LORAHANDLER_H */ \ No newline at end of file diff --git a/reciver/lora_reciver/src/main.cpp b/reciver/lora_reciver/src/main.cpp index c1cf1e3..233e6a1 100644 --- a/reciver/lora_reciver/src/main.cpp +++ b/reciver/lora_reciver/src/main.cpp @@ -3,19 +3,24 @@ #include "wifi.hpp" #include "mqtt.hpp" #include "config.hpp" - +#include "lorahandler.hpp" +#include "display.hpp" #include Config config; WiFiServer server(80); WiFiClient wclient; Mqtt mqtt(wclient); +LoraHandler lora; +Display display; + #define TRIGGER_PIN 0 void checkButton(); bool portalRunning = false; logging::Logger logger; + void setup() { Serial.begin(115200); @@ -34,14 +39,23 @@ void setup() logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "MAIN", "COnfig not loaded yet..."); } delay(100); + display.setup_display(); + // display.show_display("hej"); setupWifi("island_temp", false); - mqtt.mqttSetup(); - // server.begin(); + //mqtt.mqttSetup(); + //lora.setup(); } 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); mqtt.mqttRun(); if (portalRunning) @@ -50,6 +64,7 @@ void loop() } mqtt.mqttPublish("test/sensor", "123"); checkButton(); + sleep(10);*/ } void checkButton()