Merge pull request #501 from nuthub/development

Implemented ABP configuration (on development branch)
This commit is contained in:
Verkehrsrot 2020-03-15 22:11:05 +01:00 committed by GitHub
commit 0375185f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 145 additions and 15 deletions

View File

@ -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 <A HREF="https://bintray.com/JFrog">Bintray account</A>. 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.

View File

@ -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};
@ -30,4 +45,40 @@ 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
/************************************************************
* 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__

60
src/loraconf_abp.cpp Normal file
View File

@ -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

View File

@ -136,14 +136,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++) {
@ -160,6 +167,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) {
@ -298,6 +306,17 @@ esp_err_t lora_stack_init(bool do_join) {
&lmicTask, // task handle
1); // CPU core
#ifdef LORA_ABP
// Pass ABP parameters to LMIC_setSession
LMIC_reset();
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
@ -310,7 +329,7 @@ esp_err_t lora_stack_init(bool do_join) {
LMIC.seqnoUp = RTCseqnoUp;
LMIC.seqnoDn = RTCseqnoDn;
}
#endif
// start lmic send task
xTaskCreatePinnedToCore(lora_send, // task function
"lorasendtask", // name of task