diff --git a/examples/class-C-ABP/class_c_ABP/class_c_ABP.ino b/examples/class-C-ABP/class_c_ABP/class_c_ABP.ino new file mode 100644 index 0000000..ef50ad9 --- /dev/null +++ b/examples/class-C-ABP/class_c_ABP/class_c_ABP.ino @@ -0,0 +1,75 @@ +#include + +//ABP Credentials +const char *devAddr = "00000000"; +const char *nwkSKey = "00000000000000000000000000000000"; +const char *appSKey = "00000000000000000000000000000000"; + +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 outStr[255]; +byte recvStatus = 0; + +const sRFM_pins RFM_pins = { + .CS = 6, + .RST = 7, + .DIO0 = 8, + .DIO1 = 9, + .DIO2 = -1, + .DIO5 = -1, +}; + + +void setup() { + // Setup loraid access + Serial.begin(115200); + delay(5000); + 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() { + + // Check interval overflow + if(millis() - previousMillis > interval) { + previousMillis = millis(); + + sprintf(myStr, "Counter-%d", counter); + + Serial.print("Sending: "); + Serial.println(myStr); + + lora.sendUplink(myStr, strlen(myStr), 0, 1); + counter++; + } + + recvStatus = lora.readData(outStr); + if(recvStatus) { + Serial.print("====>> "); + Serial.println(outStr); + } + + // Check Lora RX + lora.update(); +} \ No newline at end of file diff --git a/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino new file mode 100644 index 0000000..e35c794 --- /dev/null +++ b/examples/class-C-ABP/serialToLoRaWAN/serialToLoRaWAN.ino @@ -0,0 +1,71 @@ +#include + +//ABP Credentials +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 = 6, + .RST = 7, + .DIO0 = 8, + .DIO1 = 9, + .DIO2 = -1, + .DIO5 = -1, +}; + + +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(); +} diff --git a/src/arduino-rfm/Config.h b/src/arduino-rfm/Config.h index 24c42ab..30b1c7c 100644 --- a/src/arduino-rfm/Config.h +++ b/src/arduino-rfm/Config.h @@ -16,8 +16,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 5792c6a..1d40662 100644 --- a/src/arduino-rfm/LoRaMAC.cpp +++ b/src/arduino-rfm/LoRaMAC.cpp @@ -67,73 +67,78 @@ 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) - { - #if (SAMR34) - pinMode(RFM_SWITCH,OUTPUT); - digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx - #endif - //Lora send data - LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings); - prevTime = millis(); - #if (SAMR34) - //pinMode(RFM_SWITCH,OUTPUT); - digitalWrite(RFM_SWITCH,1); //Rf switch inside RAK module change to Tx - #endif - // 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(); + unsigned char rx1_ch = LoRa_Settings->Channel_Rx; + #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 - }; - //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 + //Transmit + if(*RFM_Command == NEW_RFM_COMMAND){ + #if (SAMR34) + pinMode(RFM_SWITCH,OUTPUT); + digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx + #endif + //Lora send data + 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){ + #ifdef US_915 + LoRa_Settings->Channel_Rx = 0x08; // set Rx2 channel 923.3 MHZ + 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 + #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 + //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{ + 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); + //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); + //Wait rx2 window delay + do{ + //Poll Rx done for getting message + //DIO0 flag will only be active while class C + if(digitalRead(RFM_pins.DIO0)) + LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); + }while(millis() - prevTime < Receive_Delay_2); + //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 = 0x08; //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); *RFM_Command = NO_RFM_COMMAND; } } + + /* ***************************************************************************************** * Description : Function that is used to build a LoRaWAN data message and then tranmit it. @@ -145,122 +150,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; + } + } } @@ -303,7 +308,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; } @@ -319,10 +323,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]; @@ -362,11 +365,11 @@ 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 { - Message_Status = WRONG_MESSAGE; + Message_Status = WRONG_MESSAGE; } Address_Check = 0; @@ -376,13 +379,12 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA { for(i = 0x00; i < 4; i++) { - if(Session_Data->DevAddr[i] == Message->DevAddr[i]) + if(Session_Data->DevAddr[i] == Message->DevAddr[i]) { - Address_Check++; + Address_Check++; } } } - if(Address_Check == 0x04) { Message_Status = ADDRESS_OK; @@ -430,16 +432,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; } } @@ -461,12 +462,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; } /* ***************************************************************************************** @@ -478,58 +479,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/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 1ba1e00..133258b 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -315,10 +315,15 @@ 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 + #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, 0x04); //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 + #else + RFM_Write(RFM_REG_MODEM_CONFIG3, 0b0100); //Mobile node, low datarate optimization on AGC acorging to register LnaGain + #endif + } /* ***************************************************************************************** @@ -623,13 +628,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 +704,12 @@ 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; + //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 @@ -733,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*/ @@ -756,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; } diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 4004a11..420d992 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) @@ -389,7 +389,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) @@ -407,22 +407,19 @@ void LoRaWANClass::update(void) if(LoRa_Settings.Mote_Class == CLASS_C) { //Transmit - if(RFM_Command_Status == NEW_RFM_COMMAND) - { - //Lora send data - LORA_Send_Data(&Buffer_Tx, &Session_Data, &LoRa_Settings); - + 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){ + Rx_Status = NEW_RX; + } RFM_Command_Status = NO_RFM_COMMAND; } //Receive - if(digitalRead(RFM_pins.DIO0) == HIGH) - { - //Get data + if(digitalRead(RFM_pins.DIO0) == HIGH){ 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; } }