diff --git a/platformio.ini b/platformio.ini index aac790c..fbb5eb7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -54,3 +54,8 @@ lib_deps = sstaub/Ticker @ ^4.2.0 adafruit/Adafruit INA219 @ ^1.1.1 robtillaart/I2C_EEPROM @ ^1.5.2 + sandeepmistry/LoRa @ ^0.8.0 + adafruit/Adafruit SSD1306 @ ^2.4.0 + adafruit/Adafruit GFX Library @ ^1.10.1 + adafruit/Adafruit BusIO @ ^1.5.0 + bblanchon/ArduinoJson @ ^6.19.4 \ No newline at end of file diff --git a/src/common.h b/src/common.h index 5f86d01..c0f9cf3 100644 --- a/src/common.h +++ b/src/common.h @@ -5,17 +5,17 @@ #define QUOTE(x) Q(x) // Module connection pins (ESP GPIO-Nums) -#define CLK 16 -#define DIO_FAC_1_7SEG 14 -#define DIO_FAC_2_7SEG 12 -#define DIO_FAC_3_7SEG 13 +#define CLK 12 +#define DIO_FAC_1_7SEG 13 +#define DIO_FAC_2_7SEG 17 +#define DIO_FAC_3_7SEG 21 -#define DIO_FAC_1_TRG 0 +#define DIO_FAC_1_TRG 36 #define FAC_1_TRG_PRESSED LOW -#define DIO_FAC_2_TRG 2 +#define DIO_FAC_2_TRG 37 #define FAC_2_TRG_PRESSED LOW -#define DIO_FAC_3_TRG 15 -#define FAC_3_TRG_PRESSED HIGH +#define DIO_FAC_3_TRG 38 +#define FAC_3_TRG_PRESSED LOW #ifndef HOST_NAME #define HOST_NAME "DE_Timer_%06X" // Use printf-Formatting - Chip-ID (uin32_t) will be added diff --git a/src/lora_net.cpp b/src/lora_net.cpp new file mode 100644 index 0000000..cb51366 --- /dev/null +++ b/src/lora_net.cpp @@ -0,0 +1,20 @@ +#include "lora_net.h" + +uint8_t LoRa_Init() +{ + uint8_t success = false; + // SPI LoRa pins + SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS); + // setup LoRa transceiver module + LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0); + + if (!LoRa.begin(LORA_BAND)) + { + Serial.println("Starting LoRa failed!"); + success = false; + } + Serial.println("LoRa Initializing OK!"); + success = true; + + return success; +} diff --git a/src/lora_net.h b/src/lora_net.h new file mode 100644 index 0000000..7fa36a7 --- /dev/null +++ b/src/lora_net.h @@ -0,0 +1,23 @@ +#ifndef _LORA_NET_H_ +#define _LORA_NET_H_ + +#include +#include +#include + +// define the pins used by the LoRa transceiver module +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_SS 18 +#define LORA_RST 14 +#define LORA_DIO0 26 + +// 433E6 for Asia +// 866E6 for Europe +// 915E6 for North America +#define LORA_BAND 8681E5 + +uint8_t LoRa_Init(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cce9f61..5437d0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include // local includes #include "defaults.h" @@ -18,6 +19,8 @@ #include "globals.h" #include "dtc.h" #include "common.h" +#include "lora_net.h" +#include "oled_display.h" #ifdef WIFI_CLIENT #include @@ -39,6 +42,7 @@ void SystemShutdown(); void SetBatteryType(batteryType_t type); void ProcessKeyCombos(); void OverrideDisplay(const uint8_t *message, uint32_t time); +void LoRaPublish_callback(); #ifdef WIFI_CLIENT void wifiMaintainConnectionTicker_callback(); @@ -62,6 +66,7 @@ Ticker FactionTicker(FactionTicker_callback, 1000, 0, MILLIS); Ticker InputGetterTicker(inputGetterTicker_callback, 250, 0, MILLIS); Ticker PowerMonitorTicker(powerMonitorTicker_callback, 5000, 0, MILLIS); Ticker EEPROMCyclicPDSTicker(EEPROMCyclicPDS_callback, 60000, 0, MILLIS); +Ticker LoRaPublishTicker(LoRaPublish_callback, 60000, 0, MILLIS); uint8_t Faction_1_dot = 0; uint8_t Faction_2_dot = 0; @@ -82,9 +87,9 @@ const uint8_t sevenSeg_file[] = {0x71, 0x30, 0x38, 0x79}; void setup() { setCpuFrequencyMhz(80); - WiFi.setAutoReconnect (false); + WiFi.setAutoReconnect(false); WiFi.persistent(false); - WiFi.disconnect(); + WiFi.disconnect(); Serial.begin(115200); Serial.print("\n\n\n"); @@ -92,9 +97,13 @@ void setup() strcpy(globals.DeviceName, DEVICE_NAME); snprintf(globals.DeviceName_ID, 42, "%s_%08X", globals.DeviceName, getESPChipID()); + if (LoRa_Init()) + LoRaPublishTicker.start(); + OLED_Init(); + pinMode(DIO_FAC_1_TRG, INPUT_PULLUP); pinMode(DIO_FAC_2_TRG, INPUT_PULLUP); - pinMode(DIO_FAC_3_TRG, INPUT); + pinMode(DIO_FAC_3_TRG, INPUT_PULLUP); #ifdef SERIAL_DEBUG Serial.setDebugOutput(true); @@ -186,9 +195,11 @@ void loop() FactionTicker.update(); InputGetterTicker.update(); PowerMonitorTicker.update(); + LoRaPublishTicker.update(); ArduinoOTA.handle(); SevenSeg_Output(); EEPROM_Process(); + OLED_Process(); #ifdef CAPTIVE dnsServer.processNextRequest(); @@ -340,12 +351,12 @@ void powerMonitorTicker_callback() break; } - Serial.printf("Battery Level: %d %%\n", globals.battery_level); - Serial.printf("Bus Voltage: %f V\n", busvoltage); - Serial.printf("Shunt Voltage: %f mV\n", shuntvoltage); - Serial.printf("Load Voltage: %f V\n", globals.loadvoltage); - Serial.printf("Current: %f mA\n", current_mA); - Serial.printf("Power: %f mW\n", power_mW); + // Serial.printf("Battery Level: %d %%\n", globals.battery_level); + // Serial.printf("Bus Voltage: %f V\n", busvoltage); + // Serial.printf("Shunt Voltage: %f mV\n", shuntvoltage); + // Serial.printf("Load Voltage: %f V\n", globals.loadvoltage); + // Serial.printf("Current: %f mA\n", current_mA); + // Serial.printf("Power: %f mW\n", power_mW); } void EEPROMCyclicPDS_callback() @@ -481,4 +492,22 @@ uint32_t getESPChipID() chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; } return chipId; +} +# +void LoRaPublish_callback() +{ + + StaticJsonDocument<200> doc; + + doc["lipo"] = globals.battery_level; + doc["afact"] = PersistenceData.activeFaction; + doc["fac1"] = PersistenceData.faction_1_timer; + doc["fac2"] = PersistenceData.faction_2_timer; + doc["fac3"] = PersistenceData.faction_3_timer; + + LoRa.beginPacket(); + serializeJson(doc, LoRa); + LoRa.endPacket(); + + Serial.printf("Sendet LoRa-Status Package\n"); } \ No newline at end of file diff --git a/src/oled_display.cpp b/src/oled_display.cpp new file mode 100644 index 0000000..7bfc491 --- /dev/null +++ b/src/oled_display.cpp @@ -0,0 +1,42 @@ +#include "oled_display.h" + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST); + +void OLED_Init() +{ + + // reset OLED display via software + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, LOW); + delay(20); + digitalWrite(OLED_RST, HIGH); + + // initialize OLED + Wire.begin(OLED_SDA, OLED_SCL); + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) + { // Address 0x3C for 128x32 + Serial.println(F("SSD1306 allocation failed")); + } + + display.clearDisplay(); + display.setTextColor(WHITE); + display.setTextSize(1); + display.setCursor(0, 0); + display.print("DISPLAY INIT"); + display.display(); +} + +void OLED_Process() +{ + + display.clearDisplay(); + display.setCursor(0, 0); + display.printf("LiPo: %d%%\n", globals.battery_level); + display.print(PersistenceData.activeFaction == FACTION_1 ? "> " : " "); + display.printf("%-5s: %d\n", FACTION_1_NAME, PersistenceData.faction_1_timer); + display.print(PersistenceData.activeFaction == FACTION_2 ? "> " : " "); + display.printf("%-5s: %d\n", FACTION_2_NAME, PersistenceData.faction_2_timer); + display.print(PersistenceData.activeFaction == FACTION_3 ? "> " : " "); + display.printf("%-5s: %d\n", FACTION_3_NAME, PersistenceData.faction_3_timer); + display.display(); +} \ No newline at end of file diff --git a/src/oled_display.h b/src/oled_display.h new file mode 100644 index 0000000..cacaf63 --- /dev/null +++ b/src/oled_display.h @@ -0,0 +1,20 @@ +#ifndef _OLED_DISPLAY_H_ +#define _OLED_DISPLAY_H_ + +#include +#include +#include +#include +#include "globals.h" +#include "config.h" + +#define OLED_SDA 4 +#define OLED_SCL 15 +#define OLED_RST 16 +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels + +void OLED_Init(); +void OLED_Process(); + +#endif \ No newline at end of file