Skip to content

Commit

Permalink
Merge pull request #172 from kadirozdinc/master
Browse files Browse the repository at this point in the history
Some changes and feature additions
  • Loading branch information
wero1414 authored Aug 31, 2023
2 parents b69410f + b01a42f commit 15596e6
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 18 deletions.
125 changes: 125 additions & 0 deletions examples/Class-C-OTAA-ACK/Class-C-OTAA-ACK.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

#include <Arduino.h>
#include <lorawan.h>

// OTAA credentials
const char *devEui = "0";
const char *appEui = "0";
const char *appKey = "0";

const unsigned long interval = 15 * 1000; // 10 s interval to send message
unsigned long previousMillis = 0; // will store last time message sent
unsigned int counter = 0; // message counter

bool sendack = false;

char myStr[50];

const sRFM_pins RFM_pins = {
.CS = 7,
.RST = 3,
.DIO0 = 18,
.DIO1 = 19,
};

void message(sBuffer *msg, bool isConfirmed, uint8_t fPort){

char Buff[255];
int size = msg->Counter;

memset(Buff, 0x00, size + 1);
memcpy(Buff, msg->Data, size);

Serial.println("--------------------");
Serial.print("Msg size as bytes : ");
Serial.println(msg->Counter);
Serial.print("Message :");
Serial.println(Buff);
Serial.print("Port :");
Serial.println(fPort);

if(isConfirmed){

Serial.println("ACK response Should be sent !");
sendack = true;

}

}

void setup() {
// Setup loraid access
Serial.begin(115200);
while(!Serial);
if(!lora.init()){
Serial.println("RFM95 not detected");
delay(10000);
return;
}

// Set Data Rate
lora.setDataRate(SF8BW125);

// set channel to random
lora.setChannel(MULTI);
// Put OTAA Key and DevAddress here
lora.setDevEUI(devEui);
lora.setAppEUI(appEui);
lora.setAppKey(appKey);

// Set Callback function
lora.onMessage(message);

// Join procedure
bool isJoined;
do {
Serial.println("Joining...");
isJoined = lora.join();

//wait for 10s to try again
delay(7000);
}while(!isJoined);
Serial.println("Joined to network");

delay(1000);

sprintf(myStr, "First Message");

Serial.print("Sending: ");
Serial.println(myStr);

lora.sendUplink(myStr, strlen(myStr), 0, 1);
counter++;

}

void loop() {

// Check interval overflow
if (millis() - previousMillis > interval){
delay(1000);

previousMillis = millis();

sprintf(myStr, "Counter-%d", counter);

Serial.print("Sending: ");
Serial.println(myStr);

lora.sendUplink(myStr, strlen(myStr), 1, 1);
counter++;

}

if (sendack) {

lora.sendACK();
sendack = false;

}

lora.update();

if(lora.readAck()) Serial.println("ack received");

}
5 changes: 4 additions & 1 deletion src/arduino-rfm/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
// To define your LoRaWAN frequency band here
//#define AS_923
//#define AS_923_2
//#define EU_868
#define EU_868
//#define US_915
//#define AU_915
#define IN_865

// If you dont define _CLASS_C_, CLASS_A mode will be on
#define _CLASS_C_

// Define max payload size used for this node
#define MAX_UPLINK_PAYLOAD_SIZE 220
#define MAX_DOWNLINK_PAYLOAD_SIZE 220
Expand Down
141 changes: 137 additions & 4 deletions src/arduino-rfm/LoRaMAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "Config.h"
#include "Arduino.h"


/*
*****************************************************************************************
* FUNCTIONS
Expand All @@ -62,13 +63,13 @@
*****************************************************************************************
*/
void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data,
sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings)
sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings, msg_t *upMsg_Type)
{
static const unsigned int Receive_Delay_1 = 1000;
static const unsigned int Receive_Delay_2 = 2000; // Receive_Delay_2 >= Receive_Delay_1 + RX1_Window
static const unsigned int RX1_Window = 1000;
static const unsigned int RX2_Window = 1000;

unsigned long prevTime = 0;
unsigned char rx1_ch = LoRa_Settings->Channel_Rx;
#ifdef US_915
Expand All @@ -89,8 +90,12 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
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);

//Lora send data & ack
if(*upMsg_Type == MSG_UP) LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings);
else if(*upMsg_Type == MSG_ACK) LORA_Send_ACK(Data_Tx, Session_Data, LoRa_Settings);


prevTime = millis();

#if (SAMR34)
Expand Down Expand Up @@ -138,6 +143,11 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
return;
}

//
#ifdef _CLASS_C_
return;
#endif

// Class C open RX2 immediately after first rx window
if(LoRa_Settings->Mote_Class == CLASS_C){
#ifdef US_915
Expand Down Expand Up @@ -328,6 +338,129 @@ void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *Lo
}
}

// send uplink message including ACK

void LORA_Send_ACK(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings)
{
Serial.println("LoraMac send ack");
//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 = 0x00; //set as MAC command
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;
// }
Message.MAC_Header = Message.MAC_Header | 0x40;

//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 | 0x20);

//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_Package.Data[8] = 0;

//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;
}
}
}

/*
*****************************************************************************************
Expand Down
5 changes: 3 additions & 2 deletions src/arduino-rfm/LoRaMAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@
********************************************************************************************
*/

typedef enum {NO_RFM_COMMAND, NEW_RFM_COMMAND, RFM_COMMAND_DONE, JOIN} RFM_command_t;
typedef enum {NO_RFM_COMMAND, NEW_RFM_COMMAND, RFM_COMMAND_DONE, JOIN, NEW_ACK_COMMAND} RFM_command_t;

/*
*****************************************************************************************
* FUNCTION PROTOTYPES
*****************************************************************************************
*/

void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings);
void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings, msg_t *upMsg_Type);
void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings);
void LORA_Send_ACK(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings);
void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings);
bool LORA_join_Accept(sBuffer *Data_Rx,sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings);
void LoRa_Send_JoinReq(sLoRa_OTAA *OTAA_Data, sSettings *LoRa_Settings);
Expand Down
2 changes: 2 additions & 0 deletions src/arduino-rfm/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ typedef enum {NO_RX, NEW_RX} rx_t;

typedef enum {NO_ACK, NEW_ACK} ack_t;

typedef enum {MSG_UP, MSG_ACK} msg_t;

#endif


Loading

0 comments on commit 15596e6

Please sign in to comment.