Skip to content

Commit

Permalink
OTAA joins after 3-4 tries.
Browse files Browse the repository at this point in the history
Signed-off-by: Brady <brady.aiello@gmail.com>
  • Loading branch information
brady-aiello committed Jan 1, 2018
1 parent 4010b09 commit 2c32dc4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 86 deletions.
98 changes: 46 additions & 52 deletions LoRaWan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ void LoRaWanClass::setDataRate(_data_rate_t dataRate, _physical_type_t physicalT
if(physicalType == EU434)sendCommand("AT+DR=EU433\r\n");
else if(physicalType == EU868)sendCommand("AT+DR=EU868\r\n");
else if(physicalType == US915)sendCommand("AT+DR=US915\r\n");
else if(physicalType == AU920)sendCommand("AT+DR=AU920\r\n");
else if(physicalType == US915HYBRID)sendCommand("AT+DR=US915HYBRID\r\n");
else if(physicalType == US915HYBRID)sendCommand("AT+DR=US915HYBRID\r\n");
else if(physicalType == AU915)sendCommand("AT+DR=AU915\r\n");
else if(physicalType == AU915OLD)sendCommand("AT+DR=AU915OLD\r\n");
else if(physicalType == CN470)sendCommand("AT+DR=CN470\r\n");
else if(physicalType == CN779)sendCommand("AT+DR=CN779\r\n");
else if(physicalType == AS923)sendCommand("AT+DR=AS923\r\n");
else if(physicalType == KR920)sendCommand("AT+DR=KR920\r\n");
else if(physicalType == IN865)sendCommand("AT+DR=IN865\r\n");

#if _DEBUG_SERIAL_
loraDebugPrint(DEFAULT_DEBUGTIME);
Expand Down Expand Up @@ -322,15 +328,20 @@ short LoRaWanClass::receivePacket(char *buffer, short length, short *rssi)

ptr = strstr(_buffer, "RX: \"");
if(ptr)
{
{
ptr += 5;

uint8_t bitStep = 0;
if(*(ptr + 2) == ' ')bitStep = 3; // Firmware version 2.0.10
else bitStep = 2; // Firmware version 2.1.15

for(short i = 0; ; i ++)
{
char temp[2] = {0};
unsigned char tmp, result = 0;

temp[0] = *(ptr + i * 3);
temp[1] = *(ptr + i * 3 + 1);
temp[0] = *(ptr + i * bitStep);
temp[1] = *(ptr + i * bitStep + 1);

for(unsigned char j = 0; j < 2; j ++)
{
Expand All @@ -345,57 +356,15 @@ short LoRaWanClass::receivePacket(char *buffer, short length, short *rssi)
}

if(i < length)buffer[i] = result;

if(*(ptr + i * 3 + 3) == '\"' && *(ptr + i * 3 + 4) == '\r' && *(ptr + i * 3 + 5) == '\n')
{
number = i + 1;
break;
}
}
}

ptr = strstr(_buffer, "MACCMD: \"");
if(ptr)
{
buffer[0] = 'M';
buffer[1] = 'A';
buffer[2] = 'C';
buffer[3] = 'C';
buffer[4] = 'M';
buffer[5] = 'D';
buffer[6] = ':';

ptr += 9;
for(short i = 0; ; i ++)
{
char temp[2] = {0};
unsigned char tmp, result = 0;

temp[0] = *(ptr + i * 3);
temp[1] = *(ptr + i * 3 + 1);

for(unsigned char j = 0; j < 2; j ++)
{
if((temp[j] >= '0') && (temp[j] <= '9'))
tmp = temp[j] - '0';
else if((temp[j] >= 'A') && (temp[j] <= 'F'))
tmp = temp[j] - 'A' + 10;
else if((temp[j] >= 'a') && (temp[j] <= 'f'))
tmp = temp[j] - 'a' + 10;

result = result * 16 + tmp;
}

if((i + 7) < length)buffer[i + 7] = result;

if(*(ptr + i * 3 + 3) == '\"' && *(ptr + i * 3 + 4) == '\r' && *(ptr + i * 3 + 5) == '\n')
if(*(ptr + (i + 1) * bitStep) == '\"' && *(ptr + (i + 1) * bitStep + 1) == '\r' && *(ptr + (i + 1) * bitStep + 2) == '\n')
{
number = i + 1 + 7;
number = i + 1;
break;
}
}
}

memset(_buffer, 0, BEFFER_LENGTH_MAX);

return number;
Expand Down Expand Up @@ -525,6 +494,26 @@ void LoRaWanClass::setReceiceWindowSecond(float frequency, _spreading_factor_t s
delay(DEFAULT_TIMEWAIT);
}

void LoRaWanClass::setDutyCycle(bool command)
{
if(command)sendCommand("AT+LW=DC, ON\r\n");
else sendCommand("AT+LW=DC, OFF\r\n");
#if _DEBUG_SERIAL_
loraDebugPrint(DEFAULT_DEBUGTIME);
#endif
delay(DEFAULT_TIMEWAIT);
}

void LoRaWanClass::setJoinDutyCycle(bool command)
{
if(command)sendCommand("AT+LW=JDC,ON\r\n");
else sendCommand("AT+LW=JDC,OFF\r\n");
#if _DEBUG_SERIAL_
loraDebugPrint(DEFAULT_DEBUGTIME);
#endif
delay(DEFAULT_TIMEWAIT);
}

void LoRaWanClass::setReceiceWindowDelay(_window_delay_t command, unsigned short _delay)
{
char cmd[32];
Expand Down Expand Up @@ -691,13 +680,18 @@ short LoRaWanClass::receivePacketP2PMode(unsigned char *buffer, short length, sh
if(ptr)
{
ptr += 4;

uint8_t bitStep = 0;
if(*(ptr + 2) == ' ')bitStep = 3; // Firmware version 2.0.10
else bitStep = 2; // Firmware version 2.1.15

for(short i = 0; i < number; i ++)
{
char temp[2] = {0};
unsigned char tmp, result = 0;

temp[0] = *(ptr + i * 3);
temp[1] = *(ptr + i * 3 + 1);
temp[0] = *(ptr + i * bitStep);
temp[1] = *(ptr + i * bitStep + 1);

for(unsigned char j = 0; j < 2; j ++)
{
Expand Down
36 changes: 21 additions & 15 deletions LoRaWan.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@

#define BEFFER_LENGTH_MAX 256

#define MAC_COMMAND_FLAG "MACCMD:"


enum _class_type_t { CLASS_A = 0, CLASS_C };
enum _physical_type_t { EU434 = 0, EU868, US915, AU920, US915HYBRID };
enum _physical_type_t { EU434 = 0, EU868, US915, US915HYBRID, AU915, AU915OLD, CN470, CN779, AS923, KR920, IN865 };
enum _device_mode_t { LWABP = 0, LWOTAA, TEST };
enum _otaa_join_cmd_t { JOIN = 0, FORCE };
enum _window_delay_t { RECEIVE_DELAY1 = 0, RECEIVE_DELAY2, JOIN_ACCEPT_DELAY1, JOIN_ACCEPT_DELAY2 };
Expand Down Expand Up @@ -97,18 +95,7 @@ US915 0 SF10/125 kHz 980 | 0 30dBm
12 SF8 /500 kHz 12500 | 10 10dBm
13 SF7 /500 kHz 21900 | 11:15 RFU
14:15 RFU |
*******************************************************************
Type DataRate Configuration BitRate| TxPower Configuration
CN780 0 SF12/125 kHz 250 | 0 10dBm
1 SF11/125 kHz 440 | 1 7 dBm
2 SF10/125 kHz 980 | 2 4 dBm
3 SF9 /125 kHz 1760 | 3 1 dBm
4 SF8 /125 kHz 3125 | 4 -2dBm
5 SF7 /125 kHz 5470 | 5 -5dBm
6 SF7 /250 kHz 11000 | 6:15 RFU
7 FSK:50 kbps 50000 |
8:15 RFU |
******************************************************************/
*******************************************************************/


class LoRaWanClass
Expand Down Expand Up @@ -370,6 +357,7 @@ class LoRaWanClass
* \return Return null
*/
void setReceiceWindowSecond(float frequency, _data_rate_t dataRate);

/**
* \brief Set receice window 2 channel mapping
*
Expand All @@ -381,6 +369,24 @@ class LoRaWanClass
*/
void setReceiceWindowSecond(float frequency, _spreading_factor_t spreadingFactor, _band_width_t bandwidth);

/**
* \brief ON/OFF duty cycle limitation
*
* \param [in] command The true : ON, false OFF
*
* \return Return null
*/
void setDutyCycle(bool command);

/**
* \brief ON/OFF join duty cycle limitation
*
* \param [in] command The true : ON, false OFF
*
* \return Return null
*/
void setJoinDutyCycle(bool command);

/**
* \brief Set receice window delay
*
Expand Down
30 changes: 18 additions & 12 deletions Seeeduino-LoRaWAN-OTAA/Seeeduino-LoRaWAN-OTAA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const float AS2_hybrid_channels[8] = {923.2, 923.4, 923.6, 923.8, 924.0, 924.2,

#define DEFAULT_RESPONSE_TIMEOUT 5
unsigned char frame_counter = 1;
int loopcount = 0;

char buffer[256];


Expand All @@ -72,7 +74,6 @@ void setup(void)
while(!SerialUSB);

lora.init();
//lora.setDeviceDefault();
lora.setDeviceReset();

memset(buffer, 0, 256);
Expand All @@ -83,28 +84,33 @@ void setup(void)
lora.getId(buffer, 256, 1);
SerialUSB.print(buffer);

//void setId(char *DevAddr, char *DevEUI, char *AppEUI);
//void setKey(char *NwkSKey, char *AppSKey, char *AppKey);

//lora.setId(DEV_ADDR, DEV_EUI, APP_EUI); // NOT USED IN OTAA
lora.setId(NULL, DEV_EUI, APP_EUI);
// lora.setKey(NWK_S_KEY, APP_S_KEY, APP_KEY);
lora.setKey(NULL, NULL, APP_KEY);

lora.setDeciveMode(LWOTAA);
lora.setDataRate(DR0, US915HYBRID);
//lora.setAdaptiveDataRate(true);
setHybridForTTN(US_hybrid_channels);

//lora.setReceiceWindowFirst(0, 903.9);
lora.setReceiceWindowFirst(1);
lora.setReceiceWindowFirst(0, US_hybrid_channels[0]);
lora.setReceiceWindowSecond(FREQ_RX_WNDW_SCND_US, DOWNLINK_DATA_RATE_US);

lora.setDutyCycle(false);
lora.setJoinDutyCycle(true);
lora.setJoinDutyCycle(false);
lora.setPower(MAX_EIRP_NDX_US);

// SerialUSB.print("Starting OTTA Join.\n");
// loopcount = 0;
// while(true) {
// loopcount++;
// if (lora.setOTAAJoin(JOIN))
// break;
// }
// SerialUSB.print("Took ");
// SerialUSB.print(loopcount);
// SerialUSB.println(" tries to join.");
while(!lora.setOTAAJoin(JOIN));
lora.loraDebug();
//lora.loraDebug();
}

void setHybridForTTN(const float* channels){
Expand All @@ -118,9 +124,9 @@ void setHybridForTTN(const float* channels){

void loop(void)
{
//while(!lora.setOTAAJoin(JOIN));
while(!lora.setOTAAJoin(JOIN));
bool result = lora.transferPacket(&frame_counter, 1, DEFAULT_RESPONSE_TIMEOUT);

if(result)
{
delay(50);
Expand Down
13 changes: 6 additions & 7 deletions Seeeduino_LoRaWAN_ABP/Seeeduino_LoRaWAN_ABP.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "LoRaWan.h"
#include <LoRaWan.h>
#include "keys.h"

#define FREQ_RX_WNDW_SCND_US 923.3
Expand Down Expand Up @@ -67,11 +67,11 @@ char buffer[256];

void setup(void)
{
// SerialUSB.begin(115200);
// while(!SerialUSB);
SerialUSB.begin(115200);
while(!SerialUSB);

lora.init();
//lora.setDeviceDefault();
lora.setDeviceDefault();

memset(buffer, 0, 256);
lora.getVersion(buffer, 256, 1);
Expand All @@ -93,7 +93,6 @@ void setup(void)
setHybridForTTN(US_hybrid_channels);
lora.setReceiceWindowFirst(1);
lora.setReceiceWindowSecond(FREQ_RX_WNDW_SCND_US, DOWNLINK_DATA_RATE_US);

}

void setHybridForTTN(const float* channels){
Expand All @@ -110,7 +109,7 @@ void setHybridForTTN(const float* channels){
void loop(void)
{
bool result = lora.transferPacket(&frame_counter, 1, DEFAULT_RESPONSE_TIMEOUT);
//lora.loraDebug();
lora.loraDebug();

if(result)
{
Expand Down Expand Up @@ -139,6 +138,6 @@ void loop(void)
}
}

//lora.loraDebug();
lora.loraDebug();
delay(1000);
}

0 comments on commit 2c32dc4

Please sign in to comment.