From 2b76107ff599276bdd1287be864bd144f54f4eea Mon Sep 17 00:00:00 2001 From: Julian Flake Date: Thu, 5 Mar 2020 14:07:57 +0100 Subject: [PATCH] Implemented ABP configuration --- README.md | 2 +- src/loraconf.sample.h | 53 +++++++++++++++++++++++++++++++++++++- src/loraconf_abp.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ src/lorawan.cpp | 45 ++++++++++++++++++++++---------- 4 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 src/loraconf_abp.cpp diff --git a/README.md b/README.md index ca90f791..525adfb0 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Before compiling the code, - **create file ota.conf in your local /src directory** using the template [ota.sample.conf](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/ota.sample.conf) and enter your WIFI network&key. These settings are used for downloading updates. If you want to push own OTA updates you need a Bintray account. Enter your Bintray user account data in ota.conf. If you don't need wireless firmware updates just rename ota.sample.conf to ota.conf. -To join the network only method OTAA is supported, not ABP. The DEVEUI for OTAA will be derived from the device's MAC adress during device startup and is shown on the device's display (if it has one). It is also printed on the serial console for copying it, if you set *verbose 1* in paxcounter.conf and *debug_level 3* in platformio.ini. +To join the network you have to configure either the preferred OTAA method or the ABP method. The DEVEUI for OTAA will be derived from the device's MAC adress during device startup and is shown on the device's display (if it has one). It is also printed on the serial console for copying it, if you set *verbose 1* in paxcounter.conf and *debug_level 3* in platformio.ini. If your device has a fixed DEVEUI enter this in your local loraconf.h file. During compile time this DEVEUI will be grabbed from loraconf.h and inserted in the code. diff --git a/src/loraconf.sample.h b/src/loraconf.sample.h index 5e60c0a5..3eacd18e 100644 --- a/src/loraconf.sample.h +++ b/src/loraconf.sample.h @@ -1,3 +1,6 @@ +#ifndef __LORACONF_H__ +#define __LORACONF_H__ + #if(HAS_LORA) /************************************************************ @@ -6,6 +9,17 @@ * Read the values from TTN console (or whatever applies), insert them here, * and rename this file to src/loraconf.h * + * You can configure you PaxCounter to activate via OTAA (recommended) or ABP. + * In order to use ABP, uncomment (enable) the following line, + * otherwise, leave the line commented (disabled). + * + *************************************************************/ + +//#define LORA_ABP + +/************************************************************ + * OTAA configuration + * * Note that DEVEUI, APPEUI and APPKEY should all be specified in MSB format. * (This is different from standard LMIC-Arduino which expects DEVEUI and APPEUI * in LSB format.) @@ -22,6 +36,7 @@ * the DEVEUI will be overwriten by the one contained in the Microchip module * ************************************************************/ +#ifndef LORA_ABP static const u1_t DEVEUI[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; @@ -29,5 +44,41 @@ static const u1_t APPEUI[8] = {0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00}; static const u1_t APPKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +#endif -#endif // HAS_LORA \ No newline at end of file + +/************************************************************ + * ABP configuration (for development) + * + * Get your + * - Network Session Key (NWKSKEY) + * - App Session Key and your (APPSKEY) + * - Device Address (DEVADDR) + * from TTN console and replace the example values below. + * + * NOTE: Use MSB format (as displayed in TTN console, so you can cut & paste + * from there) + * + ************************************************************/ +#ifdef LORA_ABP + +// ID of LoRaAlliance assigned Network (for a list, see e.g. here https://www.thethingsnetwork.org/docs/lorawan/prefix-assignments.html) +static const u1_t NETID = 0x13; // TTN + +static const u1_t NWKSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +static const u1_t APPSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +static const u4_t DEVADDR = 0x00000000; // <-- Change this address for every node! + +// set additional ABP parameters in loraconf_abp.cpp +void setABPParamaters(); + +#endif + +#endif // HAS_LORA + +#endif // __LORACONF_H__ \ No newline at end of file diff --git a/src/loraconf_abp.cpp b/src/loraconf_abp.cpp new file mode 100644 index 00000000..4a4971bf --- /dev/null +++ b/src/loraconf_abp.cpp @@ -0,0 +1,60 @@ +#include "lorawan.h" +#include "loraconf.h" + +#ifdef LORA_ABP + +/** ****************************************************************** + * + * See LoRaWAN Regional Parameters Document, or here: + * https://www.thethingsnetwork.org/docs/lorawan/frequency-plans.html + * + * Parameters fro TTN and comments below are taken + * from MCCI LoRaWAN LMIC library example 'ttn-abp.ino' + * + *********************************************************************/ + +void setABPParamaters() { + + /** ************************************************************** + * ************************************************************* */ + #if defined(CFG_eu868) + // Set up the channels used by the Things Network, which corresponds + // to the defaults of most gateways. Without this, only three base + // channels from the LoRaWAN specification are used, which certainly + // works, so it is good for debugging, but can overload those + // frequencies, so be sure to configure the full frequency range of + // your network here (unless your network autoconfigures them). + // Setting up channels should happen after LMIC_setSession, as that + // configures the minimal channel set. + LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band + LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band + LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band + // TTN defines an additional channel at 869.525Mhz using SF9 for class B + // devices' ping slots. LMIC does not have an easy way to define set this + // frequency and support for class B is spotty and untested, so this + // frequency is not configured here. + #elif defined(CFG_us915) + // NA-US channels 0-71 are configured automatically + // but only one group of 8 should (a subband) should be active + // TTN recommends the second sub band, 1 in a zero based count. + // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json + LMIC_selectSubBand(1); + #endif + + // Disable link check validation + LMIC_setLinkCheckMode(0); + + // TTN uses SF9 for its RX2 window. + LMIC.dn2Dr = DR_SF9; + + // Set data rate and transmit power for uplink + LMIC_setDrTxpow(DR_SF7,14); +} + +#endif \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 853832aa..196cff04 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -159,14 +159,21 @@ void RevBytes(unsigned char *b, size_t c) { } // LMIC callback functions -void os_getDevKey(u1_t *buf) { memcpy(buf, APPKEY, 16); } +void os_getDevKey(u1_t *buf) { +#ifndef LORA_ABP + memcpy(buf, APPKEY, 16); +#endif +} void os_getArtEui(u1_t *buf) { +#ifndef LORA_ABP memcpy(buf, APPEUI, 8); RevBytes(buf, 8); // TTN requires it in LSB First order, so we swap bytes +#endif } void os_getDevEui(u1_t *buf) { +#ifndef LORA_ABP int i = 0, k = 0; memcpy(buf, DEVEUI, 8); // get fixed DEVEUI from loraconf.h for (i = 0; i < 8; i++) { @@ -183,6 +190,7 @@ void os_getDevEui(u1_t *buf) { get_hard_deveui(buf); RevBytes(buf, 8); // swap bytes to LSB format #endif +#endif } void get_hard_deveui(uint8_t *pdeveui) { @@ -323,19 +331,30 @@ esp_err_t lora_stack_init(bool do_join) { &lmicTask, // task handle 1); // CPU core - // Start join procedure if not already joined, - // lora_setupForNetwork(true) is called by eventhandler when joined - // else continue current session - if (do_join) { - if (!LMIC_startJoining()) - ESP_LOGI(TAG, "Already joined"); - } else { + #ifdef LORA_ABP + // Pass ABP parameters to LMIC_setSession LMIC_reset(); - LMIC_setSession(RTCnetid, RTCdevaddr, RTCnwkKey, RTCartKey); - LMIC.seqnoUp = RTCseqnoUp; - LMIC.seqnoDn = RTCseqnoDn; - } - + uint8_t appskey[sizeof(APPSKEY)]; + uint8_t nwkskey[sizeof(NWKSKEY)]; + memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); + memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); + LMIC_setSession (NETID, DEVADDR, nwkskey, appskey); + // These parameters are defined as macro in loraconf.h + setABPParamaters(); + #else + // Start join procedure if not already joined, + // lora_setupForNetwork(true) is called by eventhandler when joined + // else continue current session + if (do_join) { + if (!LMIC_startJoining()) + ESP_LOGI(TAG, "Already joined"); + } else { + LMIC_reset(); + LMIC_setSession(RTCnetid, RTCdevaddr, RTCnwkKey, RTCartKey); + LMIC.seqnoUp = RTCseqnoUp; + LMIC.seqnoDn = RTCseqnoDn; + } + #endif // start lmic send task xTaskCreatePinnedToCore(lora_send, // task function "lorasendtask", // name of task