# LoRaWAN Keys Konfiguration Dieses Dokument zeigt dir, wie die LoRaWAN Keys in ChirpStack und in der main.cpp eingetragen werden müssen. ## Generierte Keys für dieses Projekt ### Option 1: Zufällige Keys #### In ChirpStack / TTN eintragen (MSB Format): ``` DevEUI: 70B3D57ED005A1B2 AppEUI: 70B3D57ED0000001 AppKey: A1B2C3D4E5F6071829384756ABCDEF12 ``` #### In main.cpp eintragen (LSB Format): ```cpp // LoRaWAN Keys - WICHTIG: DevEUI und AppEUI in LSB Format! static const u1_t PROGMEM APPEUI[8] = { 0x01, 0x00, 0x00, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }; static const u1_t PROGMEM DEVEUI[8] = { 0xB2, 0xA1, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }; static const u1_t PROGMEM APPKEY[16] = { 0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07, 0x18, 0x29, 0x38, 0x47, 0x56, 0xAB, 0xCD, 0xEF, 0x12 }; ``` --- ### Option 2: Einfache Keys (für Testing) #### In ChirpStack / TTN eintragen (MSB Format): ``` DevEUI: 0000000000000001 AppEUI: 0000000000000001 AppKey: 00112233445566778899AABBCCDDEEFF ``` #### In main.cpp eintragen (LSB Format): ```cpp // LoRaWAN Keys - WICHTIG: DevEUI und AppEUI in LSB Format! static const u1_t PROGMEM APPEUI[8] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const u1_t PROGMEM DEVEUI[8] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const u1_t PROGMEM APPKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; ``` --- ## Wichtige Hinweise zur Konvertierung ### DevEUI und AppEUI: LSB (Least Significant Byte First) **Die Byte-Reihenfolge muss umgedreht werden!** ChirpStack/TTN zeigt: `70B3D57ED005A1B2` - Teile in Bytes: `70 B3 D5 7E D0 05 A1 B2` - **Umdrehen:** `B2 A1 05 D0 7E D5 B3 70` - In main.cpp: `{ 0xB2, 0xA1, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }` ### AppKey: MSB (Most Significant Byte First) **Die Byte-Reihenfolge bleibt gleich!** ChirpStack/TTN zeigt: `A1B2C3D4E5F6071829384756ABCDEF12` - Teile in Bytes: `A1 B2 C3 D4 E5 F6 07 18 29 38 47 56 AB CD EF 12` - **Nicht umdrehen!** - In main.cpp: `{ 0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07, 0x18, 0x29, 0x38, 0x47, 0x56, 0xAB, 0xCD, 0xEF, 0x12 }` --- ## Schritt-für-Schritt Anleitung ### 1. ChirpStack Konfiguration #### Device Profile erstellen (falls noch nicht vorhanden) 1. Gehe zu **Device Profiles** → **Create** 2. Einstellungen: - Name: `OTAA-EU868` - Region: `EU868` (oder deine Region) - MAC Version: `1.0.3` - Regional Parameters Revision: `A` - Supports OTAA: ✅ **aktiviert** - Supports Class B: ❌ - Supports Class C: ❌ #### Application erstellen 1. Gehe zu **Applications** → **Create** 2. Name: `HC-SR04-Sensors` (oder einen anderen Namen) #### Device hinzufügen 1. Öffne deine Application 2. Klicke auf **Create Device** 3. Fülle aus: - **Device name:** `HC-SR04-Sensor-01` - **Device description:** `Ultraschallsensor HC-SR04 mit TTGO LoRa32` - **Device EUI:** `70B3D57ED005A1B2` (oder eine eigene) - **Device profile:** Wähle dein OTAA Profil 4. Nach dem Erstellen → Gehe zu **Keys (OTAA)** 5. Trage ein: - **Application key:** `A1B2C3D4E5F6071829384756ABCDEF12` - **Network key:** (leer lassen oder gleicher Wert für LoRaWAN 1.0.x) 6. **Application EUI** wird automatisch von der Application übernommen oder kann manuell gesetzt werden: - Nutze: `70B3D57ED0000001` --- ### 2. The Things Network (TTN) Konfiguration #### Application erstellen 1. Gehe zu [console.thethingsnetwork.org](https://console.thethingsnetwork.org) 2. **Applications** → **Add application** 3. Application ID: `hc-sr04-sensors` #### Device registrieren 1. Öffne deine Application 2. **Register end device** 3. Einstellungen: - **Activation mode:** Over the air activation (OTAA) - **LoRaWAN version:** MAC V1.0.3 - **Frequency plan:** Europe 863-870 MHz (SF9 for RX2) 4. **Provisioning information:** - **JoinEUI (AppEUI):** `70B3D57ED0000001` - **DevEUI:** `70B3D57ED005A1B2` (oder generieren lassen) - **AppKey:** `A1B2C3D4E5F6071829384756ABCDEF12` (oder generieren lassen) 5. **End device ID:** `hc-sr04-sensor-01` 6. **Register end device** --- ### 3. main.cpp anpassen Öffne [src/main.cpp](src/main.cpp) und ersetze die Keys in Zeile 22-24: ```cpp // LoRaWAN Keys - Kopiere die umgewandelten Werte hier ein static const u1_t PROGMEM APPEUI[8] = { 0x01, 0x00, 0x00, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }; // LSB! static const u1_t PROGMEM DEVEUI[8] = { 0xB2, 0xA1, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }; // LSB! static const u1_t PROGMEM APPKEY[16] = { 0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07, 0x18, 0x29, 0x38, 0x47, 0x56, 0xAB, 0xCD, 0xEF, 0x12 }; // MSB ``` **Wichtig:** Achte darauf, dass DevEUI und AppEUI in **LSB** (umgedreht) sind! --- ## Konvertierungs-Tool ### Online-Konverter Du kannst auch einen Hex-String online umkehren: - [Hex String Reverser](https://www.browserling.com/tools/reverse-hex) - Oder nutze Python: ```python def convert_to_lsb(hex_string): # Entferne Leerzeichen und teile in Bytes hex_clean = hex_string.replace(" ", "") bytes_list = [hex_clean[i:i+2] for i in range(0, len(hex_clean), 2)] # Umdrehen bytes_reversed = bytes_list[::-1] # Als C-Array formatieren c_array = "{ " + ", ".join([f"0x{b}" for b in bytes_reversed]) + " }" return c_array # Beispiel deveui_msb = "70B3D57ED005A1B2" print("DevEUI LSB:", convert_to_lsb(deveui_msb)) # Ausgabe: { 0xB2, 0xA1, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 } ``` --- ## Überprüfung ### Im Serial Monitor Nach dem Upload solltest du sehen: ``` Starte HC-SR04 + OLED + LoRa Beitritt läuft... EV_JOINING ``` Nach 1-10 Sekunden (wenn das Gateway erreichbar ist): ``` EV_JOINED Netzwerkbeitritt erfolgreich Distanz gesendet: Distanz: 123.4 cm ``` ### In ChirpStack / TTN 1. Gehe zu deinem Device 2. Öffne **LoRaWAN frames** oder **Live data** 3. Du solltest **Join Request** und **Join Accept** sehen 4. Danach sollten **Uplink** Nachrichten mit den Sensordaten ankommen --- ## Troubleshooting ### "EV_JOINING" wiederholt sich, aber kein "EV_JOINED" **Mögliche Ursachen:** 1. ❌ **Keys falsch** - Überprüfe LSB/MSB Format 2. ❌ **Kein Gateway in Reichweite** 3. ❌ **Falsche Frequenz** - EU868 vs. US915 etc. 4. ❌ **Device nicht in ChirpStack/TTN registriert** **Lösung:** - Überprüfe die Keys noch einmal - Stelle sicher, dass DevEUI und AppEUI umgedreht sind (LSB) - Prüfe, ob ein Gateway in der Nähe ist (ChirpStack → Gateways) ### "EV_JOIN_FAILED" oder "EV_REJOIN_FAILED" **Ursache:** - AppKey stimmt nicht überein **Lösung:** - Überprüfe den AppKey in ChirpStack und main.cpp - AppKey wird **nicht** umgedreht (MSB Format) --- ## Zusammenfassung der Keys | Key | Format in ChirpStack/TTN | Format in main.cpp | Umdrehen? | |-----|--------------------------|-------------------|-----------| | DevEUI | `70B3D57ED005A1B2` | `{ 0xB2, 0xA1, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }` | ✅ Ja (LSB) | | AppEUI | `70B3D57ED0000001` | `{ 0x01, 0x00, 0x00, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }` | ✅ Ja (LSB) | | AppKey | `A1B2C3D4E5F6071829384756ABCDEF12` | `{ 0xA1, 0xB2, 0xC3, 0xD4, ..., 0x12 }` | ❌ Nein (MSB) | --- ## Eigene Keys generieren ### Mit OpenSSL (Linux/Mac): ```bash # AppKey (16 Bytes = 128 Bit) openssl rand -hex 16 ``` ### Mit Python: ```python import secrets # AppKey (16 Bytes) appkey = secrets.token_hex(16) print(f"AppKey: {appkey.upper()}") # DevEUI (8 Bytes) - optional mit Prefix deveui = "70B3D57E" + secrets.token_hex(4) print(f"DevEUI: {deveui.upper()}") ``` ### Online: - [Random Hex Generator](https://www.random.org/bytes/) - Wähle 16 Bytes für AppKey - Wähle 8 Bytes für DevEUI/AppEUI --- **Tipp:** Speichere deine Keys sicher ab! Du brauchst sie, wenn du das Device neu flashen musst oder ein zweites Device hinzufügen möchtest.