From 3fb84ac27b6061ae64498cbcfd33c2ce9cd40925 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Sun, 20 Jun 2021 12:40:18 +0200 Subject: [PATCH] Add Second Keyset, Save Settings in EEPROM --- src/config.example.h | 13 +++++-- src/main.cpp | 92 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/src/config.example.h b/src/config.example.h index f41db2d..769716c 100644 --- a/src/config.example.h +++ b/src/config.example.h @@ -1,6 +1,13 @@ //******************************************************************* // LoRa Config - Put Your Keys Here! MSB Left for all three //******************************************************************* -unsigned char NwkSkey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -unsigned char AppSkey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -unsigned char DevAddr[4] = { 0x00, 0x00, 0x00, 0x00 }; \ No newline at end of file + +// Primary Keyset, will be used on Startup +unsigned char NwkSkey_1[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +unsigned char AppSkey_1[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +unsigned char DevAddr_1[4] = { 0x00, 0x00, 0x00, 0x00 }; + +// Secondary ABP Keyset, can be selected via Setup +unsigned char NwkSkey_2[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +unsigned char AppSkey_2[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +unsigned char DevAddr_2[4] = { 0x00, 0x00, 0x00, 0x00 }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 76e27c0..e776db9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include // OLED @@ -26,8 +27,20 @@ enum Modes { M_SETUP }; +enum Settings { + S_NONE, + S_INTERVAL, + S_KEYSET, + S_EXIT +}; + Modes amode = M_NORMAL; // Active Mode Modes omode = M_NORMAL; // Previous Mode +Settings cset = S_INTERVAL; // Current Setting +Settings aset = S_NONE; // Selected Setting + +#define EEPROM_KEYSET_ADR 0 +#define EEPROM_INTERVAL_ADR 1 // LoRa #include "config.h" // Contains LoRa ABP Keys @@ -35,11 +48,12 @@ Modes omode = M_NORMAL; // Previous Mode #define NSS PIN_PA5 RFM95 rfm(DIO0, NSS); LoRaWAN lora = LoRaWAN(rfm); -uint16_t Frame_Counter_Tx = 0x0000; +uint16_t Frame_Counter_Tx = 0x0001; // Some Status Variables uint8_t interval = 20; // Sending Interval in Settings uint8_t packets = 0; // Sent LoRa Packets +uint8_t keyset = 1; // LoRa KeySet String statusmsg = ""; uint8_t loraBuffer[9]; // Lora Data Packet @@ -60,14 +74,40 @@ uint16_t readSupplyVoltage() { return reading; } +void loraKeySetup(uint8_t keyset) { + if (keyset == 1) + lora.setKeys(NwkSkey_1, AppSkey_1, DevAddr_1); + if (keyset == 2) + lora.setKeys(NwkSkey_2, AppSkey_2, DevAddr_2); +} + // Button Handler Function void handler(Button2& btn) { switch (btn.getClickType()) { case SINGLE_CLICK: if (amode == M_SETUP) { - interval = interval += 10; - if (interval > 40) - interval = 10; + if (aset == S_INTERVAL) { + interval = interval += 10; + if (interval > 30) + interval = 10; + } else if (aset == S_KEYSET) { + keyset = keyset + 1; + if (keyset > 2) + keyset = 1; + loraKeySetup(keyset); + } else { + switch (cset) { + case S_INTERVAL: + cset = S_KEYSET; + break; + case S_KEYSET: + cset = S_EXIT; + break; + case S_EXIT: + cset = S_INTERVAL; + break; + } + } } else if (amode == M_NORMAL) { statusmsg = "Adhoc Request"; adhocsend = true; @@ -77,9 +117,19 @@ void handler(Button2& btn) { switch (amode) { case M_NORMAL: amode = M_SETUP; + cset = S_INTERVAL; break; case M_SETUP: - amode = M_NORMAL; + if (cset == S_EXIT) { + aset = S_NONE; + amode = M_NORMAL; + EEPROM.update(EEPROM_KEYSET_ADR, keyset); + EEPROM.update(EEPROM_INTERVAL_ADR, interval); + } else if (aset == S_NONE) { + aset = cset; + } else { + aset = S_NONE; + } break; } break; @@ -102,8 +152,25 @@ void updateDisplay() { if (amode == M_SETUP) { oled.println ("<< SETUP >>"); oled.println (""); + char tmp = ' '; + + tmp = (cset == S_INTERVAL) ? '>' : ' '; + oled.print(tmp); + tmp = (aset == S_INTERVAL) ? '>' : ' '; + oled.print(tmp); oled.print("Interval: "); oled.println(interval); + + tmp = (cset == S_KEYSET) ? '>' : ' '; + oled.print(tmp); + tmp = (aset == S_KEYSET) ? '>' : ' '; + oled.print(tmp); + oled.print("ABP KeySet: "); + oled.println(keyset); + tmp = (cset == S_EXIT) ? '>' : ' '; + oled.print(tmp); + oled.println(" Exit Setup"); + } else if (amode == M_NORMAL) { if (hasFix) { if (hasFix != oldFix){ @@ -119,7 +186,7 @@ void updateDisplay() { oled.print("Int: "); oled.println(interval); oled.print("Packet: "); - oled.println(Frame_Counter_Tx); + oled.println(packets); oled.print("Alt: "); oled.println(buffer[3]); oled.print("Speed: "); @@ -139,7 +206,7 @@ void updateDisplay() { oled.print("Int: "); oled.println(interval); oled.print("Packet: "); - oled.println(Frame_Counter_Tx); + oled.println(packets); oled.print("Bat: "); oled.println((float)readSupplyVoltage()/1000); } @@ -166,9 +233,17 @@ void setup() { oled.setFlipMode(1); oled.clear(); + // Get Settings from EEPROM + + EEPROM.get(EEPROM_KEYSET_ADR, keyset); + if (keyset > 2) keyset = 1; + if (keyset < 1) keyset = 1; + EEPROM.get(EEPROM_INTERVAL_ADR, interval); + if (interval < 10 || interval > 30) interval = 20; + // Lora Initialization rfm.init(); - lora.setKeys(NwkSkey, AppSkey, DevAddr); + loraKeySetup(keyset); } // Main Loop @@ -186,6 +261,7 @@ void loop() { gps.buildPacket(loraBuffer); lora.Send_Data((unsigned char *)&loraBuffer, sizeof(loraBuffer), Frame_Counter_Tx, SF7BW125, 0x01); + packets++; Frame_Counter_Tx++; lastmillis = millis(); } else {