From 195e078c3f3ec3d4d6fa26a651047b43b805c7a5 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 15:32:33 -0600 Subject: [PATCH 01/29] change timming and mesasage readings --- src/arduino-rfm/Config.h | 4 +- src/arduino-rfm/LoRaMAC.cpp | 124 +++++++++++++----------- src/arduino-rfm/RFM95.cpp | 11 +-- src/arduino-rfm/lorawan-arduino-rfm.cpp | 13 +-- 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/src/arduino-rfm/Config.h b/src/arduino-rfm/Config.h index 305cef8..f0607d6 100644 --- a/src/arduino-rfm/Config.h +++ b/src/arduino-rfm/Config.h @@ -14,8 +14,8 @@ #ifdef US_915 //Select the subband youre working on // make sure your gateway is working in one of these bands - //#define SUBND_0 // 902.3 - 903.7 Mhz - #define SUBND_1 // 903.9 - 905.3 Mhz TTN + #define SUBND_0 // 902.3 - 903.7 Mhz + //#define SUBND_1 // 903.9 - 905.3 Mhz TTN //#define SUBND_2 // 905.5 - 906.9 Mhz //#define SUBND_3 // 907.1 - 908.5 Mhz //#define SUBND_4 // 908.7 - 910.1 Mhz diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index b4e5961..859b417 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -67,56 +67,64 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_1 = 1000; static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; - - //Transmit - if(*RFM_Command == NEW_RFM_COMMAND) - { - //Lora send data - LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); - prevTime = millis(); + unsigned char rx1_ch = LoRa_Settings->Channel_Rx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Rx; + //Transmit + if(*RFM_Command == NEW_RFM_COMMAND){ + //Lora send data + LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); + prevTime = millis(); + // Class C open RX2 immediately after sending data - if(LoRa_Settings->Mote_Class == 0x01) - { - // RX2 window - //LoRa_Settings->Channel_Rx = Channel_Rx_2; // set Rx2 channel - //LoRa_Settings->Datarate_Rx = Datarate_Rx_2; //set data rate Rx2 - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - if(Data_Rx->Counter > 0) { - Serial.print((char *)Data_Rx->Data); - } else { - Serial.println("No Data RX2 Class C"); - } - } - - *RFM_Command = NO_RFM_COMMAND; - } - - // wait rx1 window - while((digitalRead(RFM_pins.DIO0) != HIGH) && (millis() - prevTime < Receive_Delay_1)); - - //Get data - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - *RFM_Command = NO_RFM_COMMAND; - - if (Data_Rx->Counter==0) - { - // wait rx2 window - while((digitalRead(RFM_pins.DIO0) != HIGH) && (millis() - prevTime < Receive_Delay_2)); - - //Get data - // TODO - // The RX2 receive window uses a fixed frequency and data rate. The default parameters are - // 869.525 MHz / DR0 (SF12, 125 kHz) - - #ifdef EU_868 - unsigned char previousChannelRX=LoRa_Settings->Channel_Rx; - unsigned char previousDatarateRX=LoRa_Settings->Datarate_Rx; - LoRa_Settings->Channel_Rx=CHRX2; - LoRa_Settings->Datarate_Rx=SF9BW125; - LoRa_Settings->Channel_Rx=previousChannelRX; - LoRa_Settings->Datarate_Rx=previousDatarateRX; - #endif + if(LoRa_Settings->Mote_Class == CLASS_C){ + // RX2 window + LoRa_Settings->Channel_Rx = 0; // set Rx2 channel 923.3 MHZ + LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate 10 + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + } + Serial.println("RX2"); + //Wait rx1 window delay + //Receive on RX2 if countinous mode is available + //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated + do{ + Serial.print("."); + if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + }while(millis() - prevTime < Receive_Delay_1); + Serial.println(); + //Return if message on RX2 + if (Data_Rx->Counter>0)return; + + //Update time for counting 1 sec more + prevTime = millis(); + //Return to datarate and channel for RX1 + LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel + LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate + + //Receive Data RX1 + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + Serial.println("RX1"); + //Wait rx2 window delay + do{ + //Poll Rx done for getting message + //DIO0 flag will only be active while class C + Serial.print("."); + if(digitalRead(RFM_pins.DIO0)) + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + }while(millis() - prevTime < Receive_Delay_2); + Serial.println(); + //Return if message on RX1 + if (Data_Rx->Counter>0)return; + + //Configure datarate and channel for RX2 + LoRa_Settings->Channel_Rx = 0; // set RX2 channel + LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate + + //Receive Data RX2 + //If class A timeout will apply + //If class C continous Rx will happen LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + Serial.println("RX2"); *RFM_Command = NO_RFM_COMMAND; } } @@ -290,7 +298,6 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA { //Switch RFM to standby RFM_Switch_Mode(RFM_MODE_STANDBY); - Message_Status = NEW_MESSAGE; } @@ -417,16 +424,15 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Data_Rx->Data[i] = RFM_Data[Data_Location + i]; } - //Check frame port fiels. When zero it is a mac command message encrypted with NwkSKey - if(Message->Frame_Port == 0x00) - { - Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); - } - else - { - Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); - } - + //Check frame port fiels. When zero it is a mac command message encrypted with NwkSKey + if(Message->Frame_Port == 0x00) + { + Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); + } + else + { + Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); + } Message_Status = MESSAGE_DONE; } } diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 8c3b779..6c1c4a2 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -331,6 +331,7 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) static void RFM_Change_Datarate(unsigned char Datarate) { #if defined(US_915) + Serial.println("Change to Datarate "+String(Datarate)); switch (Datarate) { case 0x00: // SF10BW125 RFM_change_SF_BW(10,0x07); @@ -623,13 +624,6 @@ void RFM_Send_Package(sBuffer *RFM_Tx_Package, sSettings *LoRa_Settings) //Clear interrupt RFM_Write(RFM_REG_IRQ_FLAGS,0x08); - - //Switch RFM back to receive if it is a type C mote - if(LoRa_Settings->Mote_Class == CLASS_C) - { - //Switch Back to Continuous receive - RFM_Continuous_Receive(LoRa_Settings); - } } /* @@ -706,6 +700,9 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) RFM_Change_Datarate(SF12BW125); RFM_Change_Channel(CHRX2); #else + //Datarate for downlink should be 8 but testing on 10 + //LoRa_Settings->Datarate_Rx=10; + //LoRa_Settings->Channel_Rx=0; RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); #endif diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 0c83a44..0aedd5d 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -383,7 +383,7 @@ void LoRaWANClass::update(void) //Type A mote transmit receive cycle if((RFM_Command_Status == NEW_RFM_COMMAND || RFM_Command_Status == JOIN) && LoRa_Settings.Mote_Class == CLASS_A) { - //LoRa cycle + //LoRaWAN TX/RX cycle LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if ((Message_Rx.Frame_Control & 0x20) > 0) @@ -403,18 +403,19 @@ void LoRaWANClass::update(void) //Transmit if(RFM_Command_Status == NEW_RFM_COMMAND) { - //Lora send data - LORA_Send_Data(&Buffer_Tx, &Session_Data, &LoRa_Settings); - + //LoRaWAN TX/RX cycle + LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); + if(Buffer_Rx.Counter != 0x00){ + Rx_Status = NEW_RX; + } RFM_Command_Status = NO_RFM_COMMAND; } //Receive if(digitalRead(RFM_pins.DIO0) == HIGH) { - //Get data + Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); - if(Buffer_Rx.Counter != 0x00) { Rx_Status = NEW_RX; From c80b50f8934a12bec58a0f3e9b24897e5d2af941 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 17:11:00 -0600 Subject: [PATCH 02/29] receiving on class C --- src/arduino-rfm/LoRaMAC.cpp | 17 +++++++++-------- src/arduino-rfm/RFM95.cpp | 3 +++ src/arduino-rfm/lorawan-arduino-rfm.cpp | 11 ++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 859b417..e055d56 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -68,7 +68,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; unsigned char rx1_ch = LoRa_Settings->Channel_Rx; - unsigned char rx1_dr = LoRa_Settings->Datarate_Rx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ //Lora send data @@ -78,8 +78,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window - LoRa_Settings->Channel_Rx = 0; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate 10 + LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ + LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate 10 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } Serial.println("RX2"); @@ -87,7 +87,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Receive on RX2 if countinous mode is available //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated do{ - Serial.print("."); if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_1); @@ -108,7 +107,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, do{ //Poll Rx done for getting message //DIO0 flag will only be active while class C - Serial.print("."); if(digitalRead(RFM_pins.DIO0)) LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_2); @@ -117,8 +115,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if (Data_Rx->Counter>0)return; //Configure datarate and channel for RX2 - LoRa_Settings->Channel_Rx = 0; // set RX2 channel - LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate + LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel + LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply @@ -366,16 +364,19 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; //Check address + Serial.println("Check DevAddr"); if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { + Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { - Address_Check++; + Address_Check++; } } } + Serial.println(); if(Address_Check == 0x04) { diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 6c1c4a2..e11f40d 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -703,6 +703,9 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) //Datarate for downlink should be 8 but testing on 10 //LoRa_Settings->Datarate_Rx=10; //LoRa_Settings->Channel_Rx=0; + Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); + Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); + RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); #endif diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 0aedd5d..b03fefe 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -94,7 +94,7 @@ bool LoRaWANClass::init(void) #else //US_915 or AU_915 LoRa_Settings.Datarate_Rx = 0x0C; //set to SF8 BW 500 kHz #endif - LoRa_Settings.Channel_Rx = 0x0A; // set to recv channel + LoRa_Settings.Channel_Rx = 0x08; // set to recv channel // Tx #if defined(US_915) @@ -401,8 +401,7 @@ void LoRaWANClass::update(void) if(LoRa_Settings.Mote_Class == CLASS_C) { //Transmit - if(RFM_Command_Status == NEW_RFM_COMMAND) - { + if(RFM_Command_Status == NEW_RFM_COMMAND){ //LoRaWAN TX/RX cycle LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ @@ -412,12 +411,10 @@ void LoRaWANClass::update(void) } //Receive - if(digitalRead(RFM_pins.DIO0) == HIGH) - { + if(digitalRead(RFM_pins.DIO0) == HIGH){ Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); - if(Buffer_Rx.Counter != 0x00) - { + if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; } } From dba266c1f1513a2cf357e8b3cb7e5fff8b03101f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 17:54:59 -0600 Subject: [PATCH 03/29] erase debug --- src/arduino-rfm/LoRaMAC.cpp | 19 +++++-------------- src/arduino-rfm/RFM95.cpp | 5 ++--- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index e055d56..53086c4 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -68,7 +68,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; unsigned char rx1_ch = LoRa_Settings->Channel_Rx; - unsigned char rx1_dr = LoRa_Settings->Datarate_Tx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ //Lora send data @@ -79,10 +79,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate 10 + LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate 10 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } - Serial.println("RX2"); //Wait rx1 window delay //Receive on RX2 if countinous mode is available //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated @@ -90,7 +89,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_1); - Serial.println(); //Return if message on RX2 if (Data_Rx->Counter>0)return; @@ -102,7 +100,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - Serial.println("RX1"); //Wait rx2 window delay do{ //Poll Rx done for getting message @@ -110,19 +107,17 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(digitalRead(RFM_pins.DIO0)) LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_2); - Serial.println(); //Return if message on RX1 if (Data_Rx->Counter>0)return; //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel - LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate + LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - Serial.println("RX2"); *RFM_Command = NO_RFM_COMMAND; } } @@ -311,10 +306,9 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA RFM_Continuous_Receive(LoRa_Settings); } } - //if CRC ok breakdown package if(Message_Status == CRC_OK) - { + { //Get MAC_Header Message->MAC_Header = RFM_Data[0]; @@ -358,25 +352,22 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA } else { - Message_Status = WRONG_MESSAGE; + Message_Status = WRONG_MESSAGE; } Address_Check = 0; //Check address - Serial.println("Check DevAddr"); if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } - Serial.println(); if(Address_Check == 0x04) { diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index e11f40d..9ac7c4f 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -331,7 +331,6 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) static void RFM_Change_Datarate(unsigned char Datarate) { #if defined(US_915) - Serial.println("Change to Datarate "+String(Datarate)); switch (Datarate) { case 0x00: // SF10BW125 RFM_change_SF_BW(10,0x07); @@ -703,8 +702,8 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) //Datarate for downlink should be 8 but testing on 10 //LoRa_Settings->Datarate_Rx=10; //LoRa_Settings->Channel_Rx=0; - Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); - Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); + //Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); + //Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); From fbb7ea47511879790495a1846f29e0537d4c9fa1 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 18:09:28 -0600 Subject: [PATCH 04/29] Fix CRC error --- src/arduino-rfm/LoRaMAC.cpp | 4 ++-- src/arduino-rfm/RFM95.cpp | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 53086c4..bef17c2 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -79,7 +79,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate 10 + LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } //Wait rx1 window delay @@ -112,7 +112,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel - LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate + LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 9ac7c4f..ee6bdc2 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -315,10 +315,12 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) RFM_Write(RFM_REG_MODEM_CONFIG2, (_SF << 4) | 0b0100); //SFx CRC On RFM_Write(RFM_REG_MODEM_CONFIG1,(_BW << 4) | 0x02); //x kHz 4/5 coding rate explicit header mode - if(_SF==12 || _SF==11) - RFM_Write(RFM_REG_MODEM_CONFIG3, 0x0C); //Low datarate optimization on AGC auto on - else + if(_SF==12 || _SF==11){ + RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Low datarate optimization on AGC auto on (disable not working on US915) + }else{ RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + } + } /* ***************************************************************************************** From b8f6d4d64681a2494e581d5c3f670dcb6589da67 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 18:11:23 -0600 Subject: [PATCH 05/29] mix minor debug --- src/arduino-rfm/lorawan-arduino-rfm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index b03fefe..e8ac7ee 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -412,7 +412,7 @@ void LoRaWANClass::update(void) //Receive if(digitalRead(RFM_pins.DIO0) == HIGH){ - Serial.println("Get data"); + //Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; From 5ea420362074c78705a9310b7068dbf129ead4f5 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 19:48:50 -0600 Subject: [PATCH 06/29] update comments and tabs instead of spaces --- src/arduino-rfm/LoRaMAC.cpp | 322 ++++++++++++------------ src/arduino-rfm/lorawan-arduino-rfm.cpp | 1 - 2 files changed, 164 insertions(+), 159 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index bef17c2..69694e6 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -80,7 +80,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE) + Serial.println("Data RX counter "+String(Data_Rx->Counter)); } //Wait rx1 window delay //Receive on RX2 if countinous mode is available @@ -122,6 +123,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, } } + + /* ***************************************************************************************** * Description : Function that is used to build a LoRaWAN data message and then tranmit it. @@ -133,122 +136,122 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, */ void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings) { - //Define variables - unsigned char i; - - //Initialise RFM buffer - unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65]; - sBuffer RFM_Package = {&RFM_Data[0], 0x00}; - - //Initialise Message struct for a transmit message - sLoRa_Message Message; - - Message.MAC_Header = 0x00; - Message.Frame_Port = 0x01; //Frame port always 1 for now - Message.Frame_Control = 0x00; - - //Load device address from session data into the message - Message.DevAddr[0] = Session_Data->DevAddr[0]; - Message.DevAddr[1] = Session_Data->DevAddr[1]; - Message.DevAddr[2] = Session_Data->DevAddr[2]; - Message.DevAddr[3] = Session_Data->DevAddr[3]; - - //Set up direction - Message.Direction = 0x00; - - //Load the frame counter from the session data into the message - Message.Frame_Counter = *Session_Data->Frame_Counter; - - //Set confirmation - //Unconfirmed - if(LoRa_Settings->Confirm == 0x00) - { - Message.MAC_Header = Message.MAC_Header | 0x40; - } - //Confirmed - else - { - Message.MAC_Header = Message.MAC_Header | 0x80; - } - - //Build the Radio Package - //Load mac header - RFM_Package.Data[0] = Message.MAC_Header; - - //Load device address - RFM_Package.Data[1] = Message.DevAddr[3]; - RFM_Package.Data[2] = Message.DevAddr[2]; - RFM_Package.Data[3] = Message.DevAddr[1]; - RFM_Package.Data[4] = Message.DevAddr[0]; - - //Load frame control - RFM_Package.Data[5] = Message.Frame_Control; - - //Load frame counter - RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF); - RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF); - - //Set data counter to 8 - RFM_Package.Counter = 8; - - //If there is data load the Frame_Port field - //Encrypt the data and load the data - if(Data_Tx->Counter > 0x00) - { + //Define variables + unsigned char i; + + //Initialise RFM buffer + unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65]; + sBuffer RFM_Package = {&RFM_Data[0], 0x00}; + + //Initialise Message struct for a transmit message + sLoRa_Message Message; + + Message.MAC_Header = 0x00; + Message.Frame_Port = 0x01; //Frame port always 1 for now + Message.Frame_Control = 0x00; + + //Load device address from session data into the message + Message.DevAddr[0] = Session_Data->DevAddr[0]; + Message.DevAddr[1] = Session_Data->DevAddr[1]; + Message.DevAddr[2] = Session_Data->DevAddr[2]; + Message.DevAddr[3] = Session_Data->DevAddr[3]; + + //Set up direction + Message.Direction = 0x00; + + //Load the frame counter from the session data into the message + Message.Frame_Counter = *Session_Data->Frame_Counter; + + //Set confirmation + //Unconfirmed + if(LoRa_Settings->Confirm == 0x00) + { + Message.MAC_Header = Message.MAC_Header | 0x40; + } + //Confirmed + else + { + Message.MAC_Header = Message.MAC_Header | 0x80; + } + + //Build the Radio Package + //Load mac header + RFM_Package.Data[0] = Message.MAC_Header; + + //Load device address + RFM_Package.Data[1] = Message.DevAddr[3]; + RFM_Package.Data[2] = Message.DevAddr[2]; + RFM_Package.Data[3] = Message.DevAddr[1]; + RFM_Package.Data[4] = Message.DevAddr[0]; + + //Load frame control + RFM_Package.Data[5] = Message.Frame_Control; + + //Load frame counter + RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF); + RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF); + + //Set data counter to 8 + RFM_Package.Counter = 8; + + //If there is data load the Frame_Port field + //Encrypt the data and load the data + if(Data_Tx->Counter > 0x00) + { //Load Frame port field - //RFM_Data[8] = Message.Frame_Port; + //RFM_Data[8] = Message.Frame_Port; RFM_Package.Data[8] = LoRa_Settings->Mport; - //Raise package counter - RFM_Package.Counter++; - - //Encrypt the data - Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message); - - //Load Data - for(i = 0; i < Data_Tx->Counter; i++) - { - RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i]; - } - - - } - - //Calculate MIC - Construct_Data_MIC(&RFM_Package, Session_Data, &Message); - - //Load MIC in package - for(i = 0; i < 4; i++) - { - RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i]; - } - - //Send Package - RFM_Send_Package(&RFM_Package, LoRa_Settings); - - //Raise Frame counter - if(*Session_Data->Frame_Counter != 0xFFFF) - { - //Raise frame counter - *Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1; - } - else - { - *Session_Data->Frame_Counter = 0x0000; - } - - //Change channel for next message if hopping is activated - if(LoRa_Settings->Channel_Hopping == 0x01) - { - if(LoRa_Settings->Channel_Tx < 0x07) - { - LoRa_Settings->Channel_Tx++; - } - else - { - LoRa_Settings->Channel_Tx = 0x00; - } - } + //Raise package counter + RFM_Package.Counter++; + + //Encrypt the data + Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message); + + //Load Data + for(i = 0; i < Data_Tx->Counter; i++) + { + RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i]; + } + + + } + + //Calculate MIC + Construct_Data_MIC(&RFM_Package, Session_Data, &Message); + + //Load MIC in package + for(i = 0; i < 4; i++) + { + RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i]; + } + + //Send Package + RFM_Send_Package(&RFM_Package, LoRa_Settings); + + //Raise Frame counter + if(*Session_Data->Frame_Counter != 0xFFFF) + { + //Raise frame counter + *Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1; + } + else + { + *Session_Data->Frame_Counter = 0x0000; + } + + //Change channel for next message if hopping is activated + if(LoRa_Settings->Channel_Hopping == 0x01) + { + if(LoRa_Settings->Channel_Tx < 0x07) + { + LoRa_Settings->Channel_Tx++; + } + else + { + LoRa_Settings->Channel_Tx = 0x00; + } + } } @@ -348,7 +351,7 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA //Check MIC if(MIC_Check == 0x04) { - Message_Status = MIC_OK; + Message_Status = MIC_OK; } else { @@ -357,18 +360,21 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; + Serial.print("DevAddr "); //Check address if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - if(Session_Data->DevAddr[i] == Message->DevAddr[i]) + Serial.print(Message->DevAddr[i]); + if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } + Serial.println(); if(Address_Check == 0x04) { Message_Status = ADDRESS_OK; @@ -446,12 +452,12 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA */ static void Generate_DevNonce(unsigned char *DevNonce) { - unsigned int RandNumber; + unsigned int RandNumber; - RandNumber = random(0xFFFF); + RandNumber = random(0xFFFF); - DevNonce[0] = RandNumber & 0x00FF; - DevNonce[1] = (RandNumber >> 8) & 0x00FF; + DevNonce[0] = RandNumber & 0x00FF; + DevNonce[1] = (RandNumber >> 8) & 0x00FF; } /* ***************************************************************************************** @@ -463,58 +469,58 @@ static void Generate_DevNonce(unsigned char *DevNonce) */ void LoRa_Send_JoinReq(sLoRa_OTAA *OTAA_Data, sSettings *LoRa_Settings) { - unsigned char i; + unsigned char i; //Initialise RFM data buffer - unsigned char RFM_Data[23]; - sBuffer RFM_Package = { &RFM_Data[0], 0x00}; + unsigned char RFM_Data[23]; + sBuffer RFM_Package = { &RFM_Data[0], 0x00}; - //Initialise message sturct - sLoRa_Message Message; + //Initialise message sturct + sLoRa_Message Message; - Message.MAC_Header = 0x00; //Join request - Message.Direction = 0x00; //Set up Direction + Message.MAC_Header = 0x00; //Join request + Message.Direction = 0x00; //Set up Direction - //Construct OTAA Request message - //Load Header in package - RFM_Data[0] = Message.MAC_Header; + //Construct OTAA Request message + //Load Header in package + RFM_Data[0] = Message.MAC_Header; - //Load AppEUI in package - for(i = 0x00; i < 8; i++) - { - RFM_Data[i+1] = OTAA_Data->AppEUI[7-i]; - } + //Load AppEUI in package + for(i = 0x00; i < 8; i++) + { + RFM_Data[i+1] = OTAA_Data->AppEUI[7-i]; + } - //Load DevEUI in package - for(i= 0x00; i < 8; i++) - { - RFM_Data[i+9] = OTAA_Data->DevEUI[7-i]; - } + //Load DevEUI in package + for(i= 0x00; i < 8; i++) + { + RFM_Data[i+9] = OTAA_Data->DevEUI[7-i]; + } - //Generate DevNonce - Generate_DevNonce(OTAA_Data->DevNonce); + //Generate DevNonce + Generate_DevNonce(OTAA_Data->DevNonce); - //Load DevNonce in package - RFM_Data[17] = OTAA_Data->DevNonce[0]; - RFM_Data[18] = OTAA_Data->DevNonce[1]; + //Load DevNonce in package + RFM_Data[17] = OTAA_Data->DevNonce[0]; + RFM_Data[18] = OTAA_Data->DevNonce[1]; - //Set length of package - RFM_Package.Counter = 19; + //Set length of package + RFM_Package.Counter = 19; - //Get MIC - Calculate_MIC(&RFM_Package, OTAA_Data->AppKey, &Message); + //Get MIC + Calculate_MIC(&RFM_Package, OTAA_Data->AppKey, &Message); - //Load MIC in package - RFM_Data[19] = Message.MIC[0]; - RFM_Data[20] = Message.MIC[1]; - RFM_Data[21] = Message.MIC[2]; - RFM_Data[22] = Message.MIC[3]; + //Load MIC in package + RFM_Data[19] = Message.MIC[0]; + RFM_Data[20] = Message.MIC[1]; + RFM_Data[21] = Message.MIC[2]; + RFM_Data[22] = Message.MIC[3]; - //Set length of package to the right length - RFM_Package.Counter = 23; + //Set length of package to the right length + RFM_Package.Counter = 23; - //Send Package - RFM_Send_Package(&RFM_Package, LoRa_Settings); + //Send Package + RFM_Send_Package(&RFM_Package, LoRa_Settings); } bool LORA_join_Accept(sBuffer *Data_Rx,sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings) { diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index e8ac7ee..d94fca4 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -412,7 +412,6 @@ void LoRaWANClass::update(void) //Receive if(digitalRead(RFM_pins.DIO0) == HIGH){ - //Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; From ec0b718f832e93797a77c367440bef7126835dcc Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:13:32 -0600 Subject: [PATCH 07/29] erase debug --- .../serialToLoRaWAN.ino} | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) rename examples/class-C-ABP/{class_c_ABP.ino => serialToLoRaWAN/serialToLoRaWAN.ino} (54%) diff --git a/examples/class-C-ABP/class_c_ABP.ino b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino similarity index 54% rename from examples/class-C-ABP/class_c_ABP.ino rename to examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino index ef50ad9..b722620 100644 --- a/examples/class-C-ABP/class_c_ABP.ino +++ b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino @@ -1,32 +1,27 @@ #include //ABP Credentials -const char *devAddr = "00000000"; -const char *nwkSKey = "00000000000000000000000000000000"; -const char *appSKey = "00000000000000000000000000000000"; +const char *devAddr = "01addd38"; +const char *nwkSKey = "b2ef58d25e9523ab8618c5e6ce1d1b66"; +const char *appSKey = "02e06f9ccccd4f0a4376a89954be606c"; -const unsigned long interval = 20000; // 10 s interval to send message -unsigned long previousMillis = 0; // will store last time message sent -unsigned int counter = 0; // message counter - -char myStr[50]; +char myStr[240]; char outStr[255]; byte recvStatus = 0; const sRFM_pins RFM_pins = { - .CS = 6, - .RST = 7, - .DIO0 = 8, - .DIO1 = 9, - .DIO2 = -1, - .DIO5 = -1, + .CS = SS, + .RST = RFM_RST, + .DIO0 = RFM_DIO0, + .DIO1 = RFM_DIO1, + .DIO2 = RFM_DIO2, + .DIO5 = RFM_DIO5, }; void setup() { // Setup loraid access Serial.begin(115200); - delay(5000); Serial.println("Start.."); if(!lora.init()){ Serial.println("RFM95 not detected"); @@ -50,26 +45,27 @@ void setup() { } void loop() { - - // Check interval overflow - if(millis() - previousMillis > interval) { - previousMillis = millis(); - sprintf(myStr, "Counter-%d", counter); - - Serial.print("Sending: "); + if(Serial.available()){ + uint8_t i = 0; + while(Serial.available()>0){ + myStr[i++]=Serial.read(); + if(i>240){ + Serial.println("[ERROR] payload maximo alcanzado"); + break; + } + } + Serial.print("Uplink: "); Serial.println(myStr); - lora.sendUplink(myStr, strlen(myStr), 0, 1); - counter++; } recvStatus = lora.readData(outStr); if(recvStatus) { - Serial.print("====>> "); + Serial.print("Downlink: "); Serial.println(outStr); } // Check Lora RX lora.update(); -} \ No newline at end of file +} From 7db1d8889eac56008d2c2813f168fc60adedfb9f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:13:40 -0600 Subject: [PATCH 08/29] erase debug --- src/arduino-rfm/LoRaMAC.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 69694e6..f580ce2 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -81,7 +81,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE) - Serial.println("Data RX counter "+String(Data_Rx->Counter)); } //Wait rx1 window delay //Receive on RX2 if countinous mode is available @@ -360,21 +359,17 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; - Serial.print("DevAddr "); //Check address if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } - - Serial.println(); if(Address_Check == 0x04) { Message_Status = ADDRESS_OK; From f0c9792f1714fb4694ba8361f9a8eb1bb4be1adf Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:17:37 -0600 Subject: [PATCH 09/29] fix example --- .../serialToLoRaWAN/serialToLoRaWAN.ino | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino index b722620..2f815f6 100644 --- a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino +++ b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino @@ -1,21 +1,21 @@ #include //ABP Credentials -const char *devAddr = "01addd38"; -const char *nwkSKey = "b2ef58d25e9523ab8618c5e6ce1d1b66"; -const char *appSKey = "02e06f9ccccd4f0a4376a89954be606c"; +const char *devAddr = "00000000"; +const char *nwkSKey = "00000000000000000000000000000000"; +const char *appSKey = "00000000000000000000000000000000"; char myStr[240]; char outStr[255]; byte recvStatus = 0; const sRFM_pins RFM_pins = { - .CS = SS, - .RST = RFM_RST, - .DIO0 = RFM_DIO0, - .DIO1 = RFM_DIO1, - .DIO2 = RFM_DIO2, - .DIO5 = RFM_DIO5, + .CS = 6, + .RST = 7, + .DIO0 = 8, + .DIO1 = 9, + .DIO2 = -1, + .DIO5 = -1, }; From 92c15483103f4d1c6a3459b348a29a913a4052e1 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 16:40:06 -0600 Subject: [PATCH 10/29] add bastwan support for RF switch --- src/arduino-rfm/LoRaMAC.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index f580ce2..e407c9f 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -71,12 +71,20 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ - //Lora send data + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx + #elif + //Lora send data LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); prevTime = millis(); // Class C open RX2 immediately after sending data - if(LoRa_Settings->Mote_Class == CLASS_C){ + if(LoRa_Settings->Mote_Class == CLASS_C){ + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 @@ -97,7 +105,10 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Return to datarate and channel for RX1 LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate - + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //Wait rx2 window delay @@ -113,7 +124,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate - + #if defined(SAMR34) + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen From 255329d6ecb8c62514324bf9070c15716fffd4cc Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 16:46:01 -0600 Subject: [PATCH 11/29] fix define macros --- src/arduino-rfm/LoRaMAC.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index e407c9f..f4d0f4f 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -74,17 +74,17 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, #if defined(SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx - #elif + #endif //Lora send data LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); prevTime = millis(); // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ - #if defined(SAMR34) + #ifdef SAMR34 pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 @@ -108,7 +108,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, #if defined(SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //Wait rx2 window delay @@ -126,7 +126,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate #if defined(SAMR34) digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen From 45c5387d45d9fa83df1a43c36eb0a3445c002a5b Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 18:58:24 -0600 Subject: [PATCH 12/29] update macros --- src/arduino-rfm/LoRaMAC.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index f4d0f4f..dba0bae 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -71,7 +71,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ - #if defined(SAMR34) + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx #endif @@ -81,7 +81,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ - #ifdef SAMR34 + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif @@ -105,7 +105,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Return to datarate and channel for RX1 LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate - #if defined(SAMR34) + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif @@ -124,7 +124,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate - #if defined(SAMR34) + #if (SAMR34) digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif //Receive Data RX2 From c55d84afd3f76136e8534bdc1a8db33c2650763a Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 15:32:33 -0600 Subject: [PATCH 13/29] change timming and mesasage readings --- src/arduino-rfm/Config.h | 4 +- src/arduino-rfm/LoRaMAC.cpp | 129 ++++++++++++------------ src/arduino-rfm/RFM95.cpp | 11 +- src/arduino-rfm/lorawan-arduino-rfm.cpp | 13 +-- 4 files changed, 78 insertions(+), 79 deletions(-) diff --git a/src/arduino-rfm/Config.h b/src/arduino-rfm/Config.h index 1b2dd7c..4ff1121 100644 --- a/src/arduino-rfm/Config.h +++ b/src/arduino-rfm/Config.h @@ -14,8 +14,8 @@ #ifdef US_915 //Select the subband youre working on // make sure your gateway is working in one of these bands - //#define SUBND_0 // 902.3 - 903.7 Mhz - #define SUBND_1 // 903.9 - 905.3 Mhz TTN + #define SUBND_0 // 902.3 - 903.7 Mhz + //#define SUBND_1 // 903.9 - 905.3 Mhz TTN //#define SUBND_2 // 905.5 - 906.9 Mhz //#define SUBND_3 // 907.1 - 908.5 Mhz //#define SUBND_4 // 908.7 - 910.1 Mhz diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 7b3fe42..859b417 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -67,61 +67,64 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_1 = 1000; static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; - - //Transmit - if(*RFM_Command == NEW_RFM_COMMAND) - { - //Lora send data - LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); - prevTime = millis(); + unsigned char rx1_ch = LoRa_Settings->Channel_Rx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Rx; + //Transmit + if(*RFM_Command == NEW_RFM_COMMAND){ + //Lora send data + LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); + prevTime = millis(); + // Class C open RX2 immediately after sending data - if(LoRa_Settings->Mote_Class == 0x01) - { - // RX2 window - //LoRa_Settings->Channel_Rx = Channel_Rx_2; // set Rx2 channel - //LoRa_Settings->Datarate_Rx = Datarate_Rx_2; //set data rate Rx2 - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - if(Data_Rx->Counter > 0) { - Serial.print((char *)Data_Rx->Data); - } else { - Serial.println("No Data RX2 Class C"); - } - } - - *RFM_Command = NO_RFM_COMMAND; - } - - // wait rx1 window - while((digitalRead(RFM_pins.DIO0) != HIGH) && (millis() - prevTime < Receive_Delay_1)) - { - #if defined(ESP8266) || defined(ESP32) - yield(); - #endif - }; - - //Get data - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - *RFM_Command = NO_RFM_COMMAND; - - if (Data_Rx->Counter==0) - { - // wait rx2 window - while((digitalRead(RFM_pins.DIO0) != HIGH) && (millis() - prevTime < Receive_Delay_2)); - - //Get data - // TODO - // The RX2 receive window uses a fixed frequency and data rate. The default parameters are - // 869.525 MHz / DR0 (SF12, 125 kHz) - - #ifdef EU_868 - unsigned char previousChannelRX=LoRa_Settings->Channel_Rx; - unsigned char previousDatarateRX=LoRa_Settings->Datarate_Rx; - LoRa_Settings->Channel_Rx=CHRX2; - LoRa_Settings->Datarate_Rx=SF9BW125; - LoRa_Settings->Channel_Rx=previousChannelRX; - LoRa_Settings->Datarate_Rx=previousDatarateRX; - #endif + if(LoRa_Settings->Mote_Class == CLASS_C){ + // RX2 window + LoRa_Settings->Channel_Rx = 0; // set Rx2 channel 923.3 MHZ + LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate 10 + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + } + Serial.println("RX2"); + //Wait rx1 window delay + //Receive on RX2 if countinous mode is available + //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated + do{ + Serial.print("."); + if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + }while(millis() - prevTime < Receive_Delay_1); + Serial.println(); + //Return if message on RX2 + if (Data_Rx->Counter>0)return; + + //Update time for counting 1 sec more + prevTime = millis(); + //Return to datarate and channel for RX1 + LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel + LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate + + //Receive Data RX1 + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + Serial.println("RX1"); + //Wait rx2 window delay + do{ + //Poll Rx done for getting message + //DIO0 flag will only be active while class C + Serial.print("."); + if(digitalRead(RFM_pins.DIO0)) + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + }while(millis() - prevTime < Receive_Delay_2); + Serial.println(); + //Return if message on RX1 + if (Data_Rx->Counter>0)return; + + //Configure datarate and channel for RX2 + LoRa_Settings->Channel_Rx = 0; // set RX2 channel + LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate + + //Receive Data RX2 + //If class A timeout will apply + //If class C continous Rx will happen LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + Serial.println("RX2"); *RFM_Command = NO_RFM_COMMAND; } } @@ -295,7 +298,6 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA { //Switch RFM to standby RFM_Switch_Mode(RFM_MODE_STANDBY); - Message_Status = NEW_MESSAGE; } @@ -422,16 +424,15 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Data_Rx->Data[i] = RFM_Data[Data_Location + i]; } - //Check frame port fiels. When zero it is a mac command message encrypted with NwkSKey - if(Message->Frame_Port == 0x00) - { - Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); - } - else - { - Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); - } - + //Check frame port fiels. When zero it is a mac command message encrypted with NwkSKey + if(Message->Frame_Port == 0x00) + { + Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); + } + else + { + Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); + } Message_Status = MESSAGE_DONE; } } diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 8c3b779..6c1c4a2 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -331,6 +331,7 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) static void RFM_Change_Datarate(unsigned char Datarate) { #if defined(US_915) + Serial.println("Change to Datarate "+String(Datarate)); switch (Datarate) { case 0x00: // SF10BW125 RFM_change_SF_BW(10,0x07); @@ -623,13 +624,6 @@ void RFM_Send_Package(sBuffer *RFM_Tx_Package, sSettings *LoRa_Settings) //Clear interrupt RFM_Write(RFM_REG_IRQ_FLAGS,0x08); - - //Switch RFM back to receive if it is a type C mote - if(LoRa_Settings->Mote_Class == CLASS_C) - { - //Switch Back to Continuous receive - RFM_Continuous_Receive(LoRa_Settings); - } } /* @@ -706,6 +700,9 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) RFM_Change_Datarate(SF12BW125); RFM_Change_Channel(CHRX2); #else + //Datarate for downlink should be 8 but testing on 10 + //LoRa_Settings->Datarate_Rx=10; + //LoRa_Settings->Channel_Rx=0; RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); #endif diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 0c83a44..0aedd5d 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -383,7 +383,7 @@ void LoRaWANClass::update(void) //Type A mote transmit receive cycle if((RFM_Command_Status == NEW_RFM_COMMAND || RFM_Command_Status == JOIN) && LoRa_Settings.Mote_Class == CLASS_A) { - //LoRa cycle + //LoRaWAN TX/RX cycle LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if ((Message_Rx.Frame_Control & 0x20) > 0) @@ -403,18 +403,19 @@ void LoRaWANClass::update(void) //Transmit if(RFM_Command_Status == NEW_RFM_COMMAND) { - //Lora send data - LORA_Send_Data(&Buffer_Tx, &Session_Data, &LoRa_Settings); - + //LoRaWAN TX/RX cycle + LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); + if(Buffer_Rx.Counter != 0x00){ + Rx_Status = NEW_RX; + } RFM_Command_Status = NO_RFM_COMMAND; } //Receive if(digitalRead(RFM_pins.DIO0) == HIGH) { - //Get data + Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); - if(Buffer_Rx.Counter != 0x00) { Rx_Status = NEW_RX; From 48d0bad7982d0fb883379dd0ccb17c8b07b9d4c8 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 17:11:00 -0600 Subject: [PATCH 14/29] receiving on class C --- src/arduino-rfm/LoRaMAC.cpp | 17 +++++++++-------- src/arduino-rfm/RFM95.cpp | 3 +++ src/arduino-rfm/lorawan-arduino-rfm.cpp | 11 ++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 859b417..e055d56 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -68,7 +68,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; unsigned char rx1_ch = LoRa_Settings->Channel_Rx; - unsigned char rx1_dr = LoRa_Settings->Datarate_Rx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ //Lora send data @@ -78,8 +78,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window - LoRa_Settings->Channel_Rx = 0; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate 10 + LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ + LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate 10 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } Serial.println("RX2"); @@ -87,7 +87,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Receive on RX2 if countinous mode is available //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated do{ - Serial.print("."); if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_1); @@ -108,7 +107,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, do{ //Poll Rx done for getting message //DIO0 flag will only be active while class C - Serial.print("."); if(digitalRead(RFM_pins.DIO0)) LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_2); @@ -117,8 +115,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if (Data_Rx->Counter>0)return; //Configure datarate and channel for RX2 - LoRa_Settings->Channel_Rx = 0; // set RX2 channel - LoRa_Settings->Datarate_Rx = 10; //set RX2 datarate + LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel + LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply @@ -366,16 +364,19 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; //Check address + Serial.println("Check DevAddr"); if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { + Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { - Address_Check++; + Address_Check++; } } } + Serial.println(); if(Address_Check == 0x04) { diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 6c1c4a2..e11f40d 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -703,6 +703,9 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) //Datarate for downlink should be 8 but testing on 10 //LoRa_Settings->Datarate_Rx=10; //LoRa_Settings->Channel_Rx=0; + Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); + Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); + RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); #endif diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 0aedd5d..b03fefe 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -94,7 +94,7 @@ bool LoRaWANClass::init(void) #else //US_915 or AU_915 LoRa_Settings.Datarate_Rx = 0x0C; //set to SF8 BW 500 kHz #endif - LoRa_Settings.Channel_Rx = 0x0A; // set to recv channel + LoRa_Settings.Channel_Rx = 0x08; // set to recv channel // Tx #if defined(US_915) @@ -401,8 +401,7 @@ void LoRaWANClass::update(void) if(LoRa_Settings.Mote_Class == CLASS_C) { //Transmit - if(RFM_Command_Status == NEW_RFM_COMMAND) - { + if(RFM_Command_Status == NEW_RFM_COMMAND){ //LoRaWAN TX/RX cycle LORA_Cycle(&Buffer_Tx, &Buffer_Rx, &RFM_Command_Status, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ @@ -412,12 +411,10 @@ void LoRaWANClass::update(void) } //Receive - if(digitalRead(RFM_pins.DIO0) == HIGH) - { + if(digitalRead(RFM_pins.DIO0) == HIGH){ Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); - if(Buffer_Rx.Counter != 0x00) - { + if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; } } From 75ae4158068ea68e22813f11cff73f05339dfac8 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 17:54:59 -0600 Subject: [PATCH 15/29] erase debug --- src/arduino-rfm/LoRaMAC.cpp | 19 +++++-------------- src/arduino-rfm/RFM95.cpp | 5 ++--- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index e055d56..53086c4 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -68,7 +68,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; unsigned char rx1_ch = LoRa_Settings->Channel_Rx; - unsigned char rx1_dr = LoRa_Settings->Datarate_Tx; + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ //Lora send data @@ -79,10 +79,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate 10 + LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate 10 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } - Serial.println("RX2"); //Wait rx1 window delay //Receive on RX2 if countinous mode is available //check if anything if coming on class C RX2 window in class A no DIO0 flag will be activated @@ -90,7 +89,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(digitalRead(RFM_pins.DIO0)) //Poll Rx done for getting message LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_1); - Serial.println(); //Return if message on RX2 if (Data_Rx->Counter>0)return; @@ -102,7 +100,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - Serial.println("RX1"); //Wait rx2 window delay do{ //Poll Rx done for getting message @@ -110,19 +107,17 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(digitalRead(RFM_pins.DIO0)) LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); }while(millis() - prevTime < Receive_Delay_2); - Serial.println(); //Return if message on RX1 if (Data_Rx->Counter>0)return; //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel - LoRa_Settings->Datarate_Rx = 12; //set RX2 datarate + LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); - Serial.println("RX2"); *RFM_Command = NO_RFM_COMMAND; } } @@ -311,10 +306,9 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA RFM_Continuous_Receive(LoRa_Settings); } } - //if CRC ok breakdown package if(Message_Status == CRC_OK) - { + { //Get MAC_Header Message->MAC_Header = RFM_Data[0]; @@ -358,25 +352,22 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA } else { - Message_Status = WRONG_MESSAGE; + Message_Status = WRONG_MESSAGE; } Address_Check = 0; //Check address - Serial.println("Check DevAddr"); if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } - Serial.println(); if(Address_Check == 0x04) { diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index e11f40d..9ac7c4f 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -331,7 +331,6 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) static void RFM_Change_Datarate(unsigned char Datarate) { #if defined(US_915) - Serial.println("Change to Datarate "+String(Datarate)); switch (Datarate) { case 0x00: // SF10BW125 RFM_change_SF_BW(10,0x07); @@ -703,8 +702,8 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings) //Datarate for downlink should be 8 but testing on 10 //LoRa_Settings->Datarate_Rx=10; //LoRa_Settings->Channel_Rx=0; - Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); - Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); + //Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx)); + //Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx)); RFM_Change_Datarate(LoRa_Settings->Datarate_Rx); RFM_Change_Channel(LoRa_Settings->Channel_Rx); From f1f01fc80f5fce3b682a76a9dff8a1e728532e1a Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 18:09:28 -0600 Subject: [PATCH 16/29] Fix CRC error --- src/arduino-rfm/LoRaMAC.cpp | 4 ++-- src/arduino-rfm/RFM95.cpp | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 53086c4..bef17c2 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -79,7 +79,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, if(LoRa_Settings->Mote_Class == CLASS_C){ // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate 10 + LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); } //Wait rx1 window delay @@ -112,7 +112,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel - LoRa_Settings->Datarate_Rx = 0x0C; //set RX2 datarate + LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate //Receive Data RX2 //If class A timeout will apply diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 9ac7c4f..ee6bdc2 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -315,10 +315,12 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) RFM_Write(RFM_REG_MODEM_CONFIG2, (_SF << 4) | 0b0100); //SFx CRC On RFM_Write(RFM_REG_MODEM_CONFIG1,(_BW << 4) | 0x02); //x kHz 4/5 coding rate explicit header mode - if(_SF==12 || _SF==11) - RFM_Write(RFM_REG_MODEM_CONFIG3, 0x0C); //Low datarate optimization on AGC auto on - else + if(_SF==12 || _SF==11){ + RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Low datarate optimization on AGC auto on (disable not working on US915) + }else{ RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + } + } /* ***************************************************************************************** From 649a1b472e02a602816fb555ad918dc1d13fa24c Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 18:11:23 -0600 Subject: [PATCH 17/29] mix minor debug --- src/arduino-rfm/lorawan-arduino-rfm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index b03fefe..e8ac7ee 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -412,7 +412,7 @@ void LoRaWANClass::update(void) //Receive if(digitalRead(RFM_pins.DIO0) == HIGH){ - Serial.println("Get data"); + //Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; From 44406aa6e393c5170b3f659c9d437348dc4890d6 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Oct 2020 19:48:50 -0600 Subject: [PATCH 18/29] update comments and tabs instead of spaces --- src/arduino-rfm/LoRaMAC.cpp | 322 ++++++++++++------------ src/arduino-rfm/lorawan-arduino-rfm.cpp | 1 - 2 files changed, 164 insertions(+), 159 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index bef17c2..69694e6 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -80,7 +80,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 - LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE) + Serial.println("Data RX counter "+String(Data_Rx->Counter)); } //Wait rx1 window delay //Receive on RX2 if countinous mode is available @@ -122,6 +123,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, } } + + /* ***************************************************************************************** * Description : Function that is used to build a LoRaWAN data message and then tranmit it. @@ -133,122 +136,122 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, */ void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings) { - //Define variables - unsigned char i; - - //Initialise RFM buffer - unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65]; - sBuffer RFM_Package = {&RFM_Data[0], 0x00}; - - //Initialise Message struct for a transmit message - sLoRa_Message Message; - - Message.MAC_Header = 0x00; - Message.Frame_Port = 0x01; //Frame port always 1 for now - Message.Frame_Control = 0x00; - - //Load device address from session data into the message - Message.DevAddr[0] = Session_Data->DevAddr[0]; - Message.DevAddr[1] = Session_Data->DevAddr[1]; - Message.DevAddr[2] = Session_Data->DevAddr[2]; - Message.DevAddr[3] = Session_Data->DevAddr[3]; - - //Set up direction - Message.Direction = 0x00; - - //Load the frame counter from the session data into the message - Message.Frame_Counter = *Session_Data->Frame_Counter; - - //Set confirmation - //Unconfirmed - if(LoRa_Settings->Confirm == 0x00) - { - Message.MAC_Header = Message.MAC_Header | 0x40; - } - //Confirmed - else - { - Message.MAC_Header = Message.MAC_Header | 0x80; - } - - //Build the Radio Package - //Load mac header - RFM_Package.Data[0] = Message.MAC_Header; - - //Load device address - RFM_Package.Data[1] = Message.DevAddr[3]; - RFM_Package.Data[2] = Message.DevAddr[2]; - RFM_Package.Data[3] = Message.DevAddr[1]; - RFM_Package.Data[4] = Message.DevAddr[0]; - - //Load frame control - RFM_Package.Data[5] = Message.Frame_Control; - - //Load frame counter - RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF); - RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF); - - //Set data counter to 8 - RFM_Package.Counter = 8; - - //If there is data load the Frame_Port field - //Encrypt the data and load the data - if(Data_Tx->Counter > 0x00) - { + //Define variables + unsigned char i; + + //Initialise RFM buffer + unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65]; + sBuffer RFM_Package = {&RFM_Data[0], 0x00}; + + //Initialise Message struct for a transmit message + sLoRa_Message Message; + + Message.MAC_Header = 0x00; + Message.Frame_Port = 0x01; //Frame port always 1 for now + Message.Frame_Control = 0x00; + + //Load device address from session data into the message + Message.DevAddr[0] = Session_Data->DevAddr[0]; + Message.DevAddr[1] = Session_Data->DevAddr[1]; + Message.DevAddr[2] = Session_Data->DevAddr[2]; + Message.DevAddr[3] = Session_Data->DevAddr[3]; + + //Set up direction + Message.Direction = 0x00; + + //Load the frame counter from the session data into the message + Message.Frame_Counter = *Session_Data->Frame_Counter; + + //Set confirmation + //Unconfirmed + if(LoRa_Settings->Confirm == 0x00) + { + Message.MAC_Header = Message.MAC_Header | 0x40; + } + //Confirmed + else + { + Message.MAC_Header = Message.MAC_Header | 0x80; + } + + //Build the Radio Package + //Load mac header + RFM_Package.Data[0] = Message.MAC_Header; + + //Load device address + RFM_Package.Data[1] = Message.DevAddr[3]; + RFM_Package.Data[2] = Message.DevAddr[2]; + RFM_Package.Data[3] = Message.DevAddr[1]; + RFM_Package.Data[4] = Message.DevAddr[0]; + + //Load frame control + RFM_Package.Data[5] = Message.Frame_Control; + + //Load frame counter + RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF); + RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF); + + //Set data counter to 8 + RFM_Package.Counter = 8; + + //If there is data load the Frame_Port field + //Encrypt the data and load the data + if(Data_Tx->Counter > 0x00) + { //Load Frame port field - //RFM_Data[8] = Message.Frame_Port; + //RFM_Data[8] = Message.Frame_Port; RFM_Package.Data[8] = LoRa_Settings->Mport; - //Raise package counter - RFM_Package.Counter++; - - //Encrypt the data - Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message); - - //Load Data - for(i = 0; i < Data_Tx->Counter; i++) - { - RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i]; - } - - - } - - //Calculate MIC - Construct_Data_MIC(&RFM_Package, Session_Data, &Message); - - //Load MIC in package - for(i = 0; i < 4; i++) - { - RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i]; - } - - //Send Package - RFM_Send_Package(&RFM_Package, LoRa_Settings); - - //Raise Frame counter - if(*Session_Data->Frame_Counter != 0xFFFF) - { - //Raise frame counter - *Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1; - } - else - { - *Session_Data->Frame_Counter = 0x0000; - } - - //Change channel for next message if hopping is activated - if(LoRa_Settings->Channel_Hopping == 0x01) - { - if(LoRa_Settings->Channel_Tx < 0x07) - { - LoRa_Settings->Channel_Tx++; - } - else - { - LoRa_Settings->Channel_Tx = 0x00; - } - } + //Raise package counter + RFM_Package.Counter++; + + //Encrypt the data + Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message); + + //Load Data + for(i = 0; i < Data_Tx->Counter; i++) + { + RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i]; + } + + + } + + //Calculate MIC + Construct_Data_MIC(&RFM_Package, Session_Data, &Message); + + //Load MIC in package + for(i = 0; i < 4; i++) + { + RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i]; + } + + //Send Package + RFM_Send_Package(&RFM_Package, LoRa_Settings); + + //Raise Frame counter + if(*Session_Data->Frame_Counter != 0xFFFF) + { + //Raise frame counter + *Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1; + } + else + { + *Session_Data->Frame_Counter = 0x0000; + } + + //Change channel for next message if hopping is activated + if(LoRa_Settings->Channel_Hopping == 0x01) + { + if(LoRa_Settings->Channel_Tx < 0x07) + { + LoRa_Settings->Channel_Tx++; + } + else + { + LoRa_Settings->Channel_Tx = 0x00; + } + } } @@ -348,7 +351,7 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA //Check MIC if(MIC_Check == 0x04) { - Message_Status = MIC_OK; + Message_Status = MIC_OK; } else { @@ -357,18 +360,21 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; + Serial.print("DevAddr "); //Check address if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - if(Session_Data->DevAddr[i] == Message->DevAddr[i]) + Serial.print(Message->DevAddr[i]); + if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } + Serial.println(); if(Address_Check == 0x04) { Message_Status = ADDRESS_OK; @@ -446,12 +452,12 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA */ static void Generate_DevNonce(unsigned char *DevNonce) { - unsigned int RandNumber; + unsigned int RandNumber; - RandNumber = random(0xFFFF); + RandNumber = random(0xFFFF); - DevNonce[0] = RandNumber & 0x00FF; - DevNonce[1] = (RandNumber >> 8) & 0x00FF; + DevNonce[0] = RandNumber & 0x00FF; + DevNonce[1] = (RandNumber >> 8) & 0x00FF; } /* ***************************************************************************************** @@ -463,58 +469,58 @@ static void Generate_DevNonce(unsigned char *DevNonce) */ void LoRa_Send_JoinReq(sLoRa_OTAA *OTAA_Data, sSettings *LoRa_Settings) { - unsigned char i; + unsigned char i; //Initialise RFM data buffer - unsigned char RFM_Data[23]; - sBuffer RFM_Package = { &RFM_Data[0], 0x00}; + unsigned char RFM_Data[23]; + sBuffer RFM_Package = { &RFM_Data[0], 0x00}; - //Initialise message sturct - sLoRa_Message Message; + //Initialise message sturct + sLoRa_Message Message; - Message.MAC_Header = 0x00; //Join request - Message.Direction = 0x00; //Set up Direction + Message.MAC_Header = 0x00; //Join request + Message.Direction = 0x00; //Set up Direction - //Construct OTAA Request message - //Load Header in package - RFM_Data[0] = Message.MAC_Header; + //Construct OTAA Request message + //Load Header in package + RFM_Data[0] = Message.MAC_Header; - //Load AppEUI in package - for(i = 0x00; i < 8; i++) - { - RFM_Data[i+1] = OTAA_Data->AppEUI[7-i]; - } + //Load AppEUI in package + for(i = 0x00; i < 8; i++) + { + RFM_Data[i+1] = OTAA_Data->AppEUI[7-i]; + } - //Load DevEUI in package - for(i= 0x00; i < 8; i++) - { - RFM_Data[i+9] = OTAA_Data->DevEUI[7-i]; - } + //Load DevEUI in package + for(i= 0x00; i < 8; i++) + { + RFM_Data[i+9] = OTAA_Data->DevEUI[7-i]; + } - //Generate DevNonce - Generate_DevNonce(OTAA_Data->DevNonce); + //Generate DevNonce + Generate_DevNonce(OTAA_Data->DevNonce); - //Load DevNonce in package - RFM_Data[17] = OTAA_Data->DevNonce[0]; - RFM_Data[18] = OTAA_Data->DevNonce[1]; + //Load DevNonce in package + RFM_Data[17] = OTAA_Data->DevNonce[0]; + RFM_Data[18] = OTAA_Data->DevNonce[1]; - //Set length of package - RFM_Package.Counter = 19; + //Set length of package + RFM_Package.Counter = 19; - //Get MIC - Calculate_MIC(&RFM_Package, OTAA_Data->AppKey, &Message); + //Get MIC + Calculate_MIC(&RFM_Package, OTAA_Data->AppKey, &Message); - //Load MIC in package - RFM_Data[19] = Message.MIC[0]; - RFM_Data[20] = Message.MIC[1]; - RFM_Data[21] = Message.MIC[2]; - RFM_Data[22] = Message.MIC[3]; + //Load MIC in package + RFM_Data[19] = Message.MIC[0]; + RFM_Data[20] = Message.MIC[1]; + RFM_Data[21] = Message.MIC[2]; + RFM_Data[22] = Message.MIC[3]; - //Set length of package to the right length - RFM_Package.Counter = 23; + //Set length of package to the right length + RFM_Package.Counter = 23; - //Send Package - RFM_Send_Package(&RFM_Package, LoRa_Settings); + //Send Package + RFM_Send_Package(&RFM_Package, LoRa_Settings); } bool LORA_join_Accept(sBuffer *Data_Rx,sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings) { diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index e8ac7ee..d94fca4 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -412,7 +412,6 @@ void LoRaWANClass::update(void) //Receive if(digitalRead(RFM_pins.DIO0) == HIGH){ - //Serial.println("Get data"); LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if(Buffer_Rx.Counter != 0x00){ Rx_Status = NEW_RX; From e1f46a9d19a48f56d1913e7b1a229c3334a5a58e Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:13:32 -0600 Subject: [PATCH 19/29] erase debug --- .../serialToLoRaWAN/serialToLoRaWAN.ino | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino diff --git a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino new file mode 100644 index 0000000..b722620 --- /dev/null +++ b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino @@ -0,0 +1,71 @@ +#include + +//ABP Credentials +const char *devAddr = "01addd38"; +const char *nwkSKey = "b2ef58d25e9523ab8618c5e6ce1d1b66"; +const char *appSKey = "02e06f9ccccd4f0a4376a89954be606c"; + +char myStr[240]; +char outStr[255]; +byte recvStatus = 0; + +const sRFM_pins RFM_pins = { + .CS = SS, + .RST = RFM_RST, + .DIO0 = RFM_DIO0, + .DIO1 = RFM_DIO1, + .DIO2 = RFM_DIO2, + .DIO5 = RFM_DIO5, +}; + + +void setup() { + // Setup loraid access + Serial.begin(115200); + Serial.println("Start.."); + if(!lora.init()){ + Serial.println("RFM95 not detected"); + delay(5000); + return; + } + + // Set LoRaWAN Class change CLASS_A or CLASS_C + lora.setDeviceClass(CLASS_C); + + // Set Data Rate + lora.setDataRate(SF7BW125); + + // set channel to random + lora.setChannel(MULTI); + + // Put ABP Key and DevAddress here + lora.setNwkSKey(nwkSKey); + lora.setAppSKey(appSKey); + lora.setDevAddr(devAddr); +} + +void loop() { + + if(Serial.available()){ + uint8_t i = 0; + while(Serial.available()>0){ + myStr[i++]=Serial.read(); + if(i>240){ + Serial.println("[ERROR] payload maximo alcanzado"); + break; + } + } + Serial.print("Uplink: "); + Serial.println(myStr); + lora.sendUplink(myStr, strlen(myStr), 0, 1); + } + + recvStatus = lora.readData(outStr); + if(recvStatus) { + Serial.print("Downlink: "); + Serial.println(outStr); + } + + // Check Lora RX + lora.update(); +} From e49a1fdd89d894188e746ca6a4ec5e75a5cce067 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:13:40 -0600 Subject: [PATCH 20/29] erase debug --- src/arduino-rfm/LoRaMAC.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index 69694e6..f580ce2 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -81,7 +81,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE) - Serial.println("Data RX counter "+String(Data_Rx->Counter)); } //Wait rx1 window delay //Receive on RX2 if countinous mode is available @@ -360,21 +359,17 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA Address_Check = 0; - Serial.print("DevAddr "); //Check address if(MIC_Check == 0x04) { for(i = 0x00; i < 4; i++) { - Serial.print(Message->DevAddr[i]); if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { Address_Check++; } } } - - Serial.println(); if(Address_Check == 0x04) { Message_Status = ADDRESS_OK; From 8df9f82744729cb58bac4d6a215859acef198a19 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 3 Nov 2020 18:17:37 -0600 Subject: [PATCH 21/29] fix example --- .../serialToLoRaWAN/serialToLoRaWAN.ino | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino index b722620..2f815f6 100644 --- a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino +++ b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino @@ -1,21 +1,21 @@ #include //ABP Credentials -const char *devAddr = "01addd38"; -const char *nwkSKey = "b2ef58d25e9523ab8618c5e6ce1d1b66"; -const char *appSKey = "02e06f9ccccd4f0a4376a89954be606c"; +const char *devAddr = "00000000"; +const char *nwkSKey = "00000000000000000000000000000000"; +const char *appSKey = "00000000000000000000000000000000"; char myStr[240]; char outStr[255]; byte recvStatus = 0; const sRFM_pins RFM_pins = { - .CS = SS, - .RST = RFM_RST, - .DIO0 = RFM_DIO0, - .DIO1 = RFM_DIO1, - .DIO2 = RFM_DIO2, - .DIO5 = RFM_DIO5, + .CS = 6, + .RST = 7, + .DIO0 = 8, + .DIO1 = 9, + .DIO2 = -1, + .DIO5 = -1, }; From 62d5bb918b5a630dda6be2f536880b2be00e1707 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 16:40:06 -0600 Subject: [PATCH 22/29] add bastwan support for RF switch --- src/arduino-rfm/LoRaMAC.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index f580ce2..e407c9f 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -71,12 +71,20 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ - //Lora send data + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx + #elif + //Lora send data LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); prevTime = millis(); // Class C open RX2 immediately after sending data - if(LoRa_Settings->Mote_Class == CLASS_C){ + if(LoRa_Settings->Mote_Class == CLASS_C){ + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 @@ -97,7 +105,10 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Return to datarate and channel for RX1 LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate - + #if defined(SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //Wait rx2 window delay @@ -113,7 +124,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate - + #if defined(SAMR34) + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #elif //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen From 63b29757baaaab5be69b16ecdc87474ba22b3c1f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 16:46:01 -0600 Subject: [PATCH 23/29] fix define macros --- src/arduino-rfm/LoRaMAC.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index e407c9f..f4d0f4f 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -74,17 +74,17 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, #if defined(SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx - #elif + #endif //Lora send data LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); prevTime = millis(); // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ - #if defined(SAMR34) + #ifdef SAMR34 pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif // RX2 window LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 @@ -108,7 +108,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, #if defined(SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //Wait rx2 window delay @@ -126,7 +126,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate #if defined(SAMR34) digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #elif + #endif //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen From db11bd0799fd2fccbae5231478a4d649dc928f06 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 10 Dec 2020 18:58:24 -0600 Subject: [PATCH 24/29] update macros --- src/arduino-rfm/LoRaMAC.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index f4d0f4f..dba0bae 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -71,7 +71,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ - #if defined(SAMR34) + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx #endif @@ -81,7 +81,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ - #ifdef SAMR34 + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif @@ -105,7 +105,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Return to datarate and channel for RX1 LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate - #if defined(SAMR34) + #if (SAMR34) pinMode(RFM_SWITCH,OUTPUT); digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif @@ -124,7 +124,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate - #if defined(SAMR34) + #if (SAMR34) digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif //Receive Data RX2 From 980f12579fe8ea8de7a44d688a290d5b718b0537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Roman=C3=AD?= Date: Mon, 1 Feb 2021 20:26:07 +0100 Subject: [PATCH 25/29] EU868 changes --- src/arduino-rfm/LoRaMAC.cpp | 18 ++++++++++++++---- src/arduino-rfm/RFM95.cpp | 13 ++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index dba0bae..bc7c7a3 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -68,7 +68,12 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, static const unsigned int Receive_Delay_2 = 1000; unsigned long prevTime = 0; unsigned char rx1_ch = LoRa_Settings->Channel_Rx; - unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; + #ifdef US_915 + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10; + #elif defined(EU_868) + unsigned char rx1_dr = LoRa_Settings->Datarate_Tx; + #endif + //Transmit if(*RFM_Command == NEW_RFM_COMMAND){ #if (SAMR34) @@ -86,8 +91,13 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx #endif // RX2 window - LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ + #ifdef US_915 + LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 + #elif defined(EU_868) + LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ + LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12 + #endif LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE) } //Wait rx1 window delay @@ -433,11 +443,11 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA //Check frame port fiels. When zero it is a mac command message encrypted with NwkSKey if(Message->Frame_Port == 0x00) { - Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); + Encrypt_Payload(Data_Rx, Session_Data->NwkSKey, Message); } else { - Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); + Encrypt_Payload(Data_Rx, Session_Data->AppSKey, Message); } Message_Status = MESSAGE_DONE; } diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index ee6bdc2..367e075 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -315,11 +315,14 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) RFM_Write(RFM_REG_MODEM_CONFIG2, (_SF << 4) | 0b0100); //SFx CRC On RFM_Write(RFM_REG_MODEM_CONFIG1,(_BW << 4) | 0x02); //x kHz 4/5 coding rate explicit header mode - if(_SF==12 || _SF==11){ - RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Low datarate optimization on AGC auto on (disable not working on US915) - }else{ - RFM_Write(RFM_REG_MODEM_CONFIG3, 0x04); //Mobile node, low datarate optimization on AGC acorging to register LnaGain - } + #ifdef EU_868 + if(_SF>10) + RFM_Write(RFM_REG_MODEM_CONFIG3, 0b1100); //Low datarate optimization on AGC auto on + else + RFM_Write(RFM_REG_MODEM_CONFIG3, 0b0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + #else + RFM_Write(RFM_REG_MODEM_CONFIG3, 0xb0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + #endif } /* From 84f4174b2c9d75ef1043e24e636ad7255bf608b2 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 9 Feb 2021 17:25:44 -0600 Subject: [PATCH 26/29] fix typo for binary --- src/arduino-rfm/RFM95.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 367e075..18566f3 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -321,7 +321,7 @@ static void RFM_change_SF_BW(unsigned char _SF, unsigned char _BW) else RFM_Write(RFM_REG_MODEM_CONFIG3, 0b0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain #else - RFM_Write(RFM_REG_MODEM_CONFIG3, 0xb0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + RFM_Write(RFM_REG_MODEM_CONFIG3, 0b0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain #endif } From c2be9a9dbda1f6e5cd7718a5f612838f17ecdd47 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 9 Feb 2021 17:25:54 -0600 Subject: [PATCH 27/29] change number for enum --- src/arduino-rfm/LoRaMAC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index bc7c7a3..a3c07d2 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -93,7 +93,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, // RX2 window #ifdef US_915 LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ - LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate 12 + LoRa_Settings->Datarate_Rx = SF12BW500; //set RX2 datarate 12 #elif defined(EU_868) LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12 From 3c309cc24e81a212995601412376b3bbc7c08b3a Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 20 Apr 2021 17:36:55 -0500 Subject: [PATCH 28/29] change rf switch behave --- src/arduino-rfm/LoRaMAC.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/arduino-rfm/LoRaMAC.cpp b/src/arduino-rfm/LoRaMAC.cpp index a3c07d2..1d40662 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -84,13 +84,12 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); prevTime = millis(); + #if (SAMR34) + digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx + #endif + // Class C open RX2 immediately after sending data if(LoRa_Settings->Mote_Class == CLASS_C){ - #if (SAMR34) - pinMode(RFM_SWITCH,OUTPUT); - digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #endif - // RX2 window #ifdef US_915 LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ LoRa_Settings->Datarate_Rx = SF12BW500; //set RX2 datarate 12 @@ -115,10 +114,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Return to datarate and channel for RX1 LoRa_Settings->Channel_Rx = rx1_ch; // set RX1 channel LoRa_Settings->Datarate_Rx = rx1_dr; //set RX1 datarate - #if (SAMR34) - pinMode(RFM_SWITCH,OUTPUT); - digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #endif //Receive Data RX1 LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //Wait rx2 window delay @@ -134,9 +129,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, //Configure datarate and channel for RX2 LoRa_Settings->Channel_Rx = 0x08; // set RX2 channel LoRa_Settings->Datarate_Rx = 0x08; //set RX2 datarate - #if (SAMR34) - digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Rx - #endif //Receive Data RX2 //If class A timeout will apply //If class C continous Rx will happen From 61591819e360ef94d2223ff0f5cc0085ece58182 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Fri, 9 Jul 2021 16:29:20 -0500 Subject: [PATCH 29/29] fix donwlink bug --- src/arduino-rfm/RFM95.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 18566f3..7328d05 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -737,18 +737,19 @@ message_t RFM_Get_Package(sBuffer *RFM_Rx_Package) message_t Message_Status; //Get interrupt register - RFM_Interrupts = RFM_Read(RFM_REG_IRQ_FLAGS); + RFM_Interrupts = RFM_Read(0x12); - //Check CRC - if((RFM_Interrupts & 0x20) != 0x20) - { - Message_Status = CRC_OK; - } - else - { - Message_Status = WRONG_MESSAGE; + + if((RFM_Interrupts & 0x40)){ //IRQ_RX_DONE_MASK + if((RFM_Interrupts & 0x20) != 0x20) //Check CRC + { + Message_Status = CRC_OK; + } + else + { + Message_Status = WRONG_MESSAGE; + } } - RFM_Package_Location = RFM_Read(0x10); /*Read start position of received package*/ RFM_Rx_Package->Counter = RFM_Read(0x13); /*Read length of received package*/ @@ -760,7 +761,7 @@ message_t RFM_Get_Package(sBuffer *RFM_Rx_Package) } //Clear interrupt register - RFM_Write(RFM_REG_IRQ_FLAGS,0xE0); + RFM_Write(RFM_REG_IRQ_FLAGS,RFM_Interrupts); return Message_Status; }