RUI3 (RAK Unified Interface 3) - RAK4631
P2P Instructions

Data Structures

class  RAKLorawan::nwm
 
class  RAKLorawan::pfreq
 
class  RAKLorawan::psf
 
class  RAKLorawan::pbw
 
class  RAKLorawan::pcr
 
class  RAKLorawan::ppl
 
class  RAKLorawan::ptp
 
class  RAKLorawan::encry
 
class  RAKLorawan::enckey
 
class  RAKLorawan::pbr
 
class  RAKLorawan::pfdev
 

Functions

bool registerPRecvCallback (service_lora_p2p_recv_cb_type callback)
 
bool registerPSendCallback (service_lora_p2p_send_cb_type callback)
 
bool registerPSendCADCallback (service_lora_p2p_send_CAD_cb_type callback)
 
bool precv (uint32_t timeout)
 
bool psend (uint8_t length, uint8_t *payload)
 

Detailed Description

Function Documentation

◆ registerPRecvCallback()

bool registerPRecvCallback ( service_lora_p2p_recv_cb_type  callback)
Description
This API is used to register a callback function, so that application can be notified on receiving P2P data.
Syntax
api.lorawan.registerPRecvCallback(service_lora_p2p_recv_cb_type callback)
Parameters
Thecallback function
Returns
bool
Return values
TRUEfor setting callback function success
FALSEfor setting callback function failure
Example
  void recv_cb(rui_lora_p2p_recv_t data) {
    Serial.println("Receive something");
  }
  
  void send_cb(void) {
    Serial.println("I send something");
  }
  
  long startTime;
  
  void setup()
  {
    Serial.begin(115200);
  
    delay(2000);
    startTime = millis();
  
    Serial.println("P2P Start");
    
    Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
    Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
    Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
    Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
    Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  
    api.lorawan.registerPRecvCallback(recv_cb);
    api.lorawan.registerPSendCallback(send_cb);
  
    randomSeed(millis());
  }
  
  void loop()
  {
    uint8_t payload[] = "payload";
  
    int rxDelay = random(3000, 5000);
  
    // Receive P2P data every 10 seconds
    if(millis() - startTime >= 10*1000) {
      Serial.printf("P2P Rx start for %d millisSconds\r\n", rxDelay);
      startTime = millis();
      Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(rxDelay) ? "Success" : "Fail");
      delay(rxDelay);
    } else {
    
      Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail");
      delay(1000);
    }
  
  }       

◆ registerPSendCallback()

bool registerPSendCallback ( service_lora_p2p_send_cb_type  callback)
Description
This API is used to register a callback function, so that application can be notified when P2P uplink process is done.
Syntax
api.lorawan.registerPSendCallback(service_lora_p2p_send_cb_type callback)
Parameters
Thecallback function
Returns
bool
Return values
TRUEfor setting callback function success
FALSEfor setting callback function failure
Example
  void recv_cb(rui_lora_p2p_recv_t data) {
    Serial.println("Receive something");
  }
  
  void send_cb(void) {
    Serial.println("I send something");
  }
  
  long startTime;
  
  void setup()
  {
    Serial.begin(115200);
  
    delay(2000);
    startTime = millis();
  
    Serial.println("P2P Start");
    
    Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
    Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
    Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
    Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
    Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  
    api.lorawan.registerPRecvCallback(recv_cb);
    api.lorawan.registerPSendCallback(send_cb);
  
    randomSeed(millis());
  }
  
  void loop()
  {
    uint8_t payload[] = "payload";
  
    int rxDelay = random(3000, 5000);
  
    // Receive P2P data every 10 seconds
    if(millis() - startTime >= 10*1000) {
      Serial.printf("P2P Rx start for %d millisSconds\r\n", rxDelay);
      startTime = millis();
      Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(rxDelay) ? "Success" : "Fail");
      delay(rxDelay);
    } else {
    
      Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail");
      delay(1000);
    }
  
  }       

◆ registerPSendCADCallback()

bool registerPSendCADCallback ( service_lora_p2p_send_CAD_cb_type  callback)
Description
This API is used to register a callback function, so that application can be notified when P2P uplink process is done.
Syntax
api.lorawan.registerPSendCallback(service_lora_p2p_send_cb_type callback)
Parameters
Thecallback function
Returns
bool
Return values
TRUEfor setting callback function success
FALSEfor setting callback function failure
Example
  void recv_cb(rui_lora_p2p_recv_t data) {
    Serial.println("Receive something");
  }

  void send_cb(void) {
    Serial.println("I send something");
  }

  void cad_cb(bool detect) {
    if(detect)
      Serial.println("detect Activity");
    else
      Serial.println("no Activity");

  }


  long startTime;

  void setup()
  {
    Serial.begin(115200);

    delay(2000);
    startTime = millis();

    Serial.println("P2P Start");

    Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
    Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
    Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
    Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
    Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
    Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  
    api.lorawan.registerPRecvCallback(recv_cb);
    api.lorawan.registerPSendCallback(send_cb);
    api.lorawan.registerPSendCADCallback(cad_cb);
  
    randomSeed(millis());
  }
  
  void loop()
  {
    uint8_t payload[] = "payload";
  
    int rxDelay = random(3000, 5000);
  
    // Receive P2P data every 10 seconds
    if(millis() - startTime >= 10*1000) {
      Serial.printf("P2P Rx start for %d millisSconds\r\n", rxDelay);
      startTime = millis();
      Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(rxDelay) ? "Success" : "Fail");
      delay(rxDelay);
    } else {
    
      Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail");
      delay(1000);
    }
  
  }       

◆ precv()

bool precv ( uint32_t  timeout)
Description
This API is used to enter P2P RX mode for a specified period.
Syntax
api.lorawan.precv(uint32_t timeout)
Parameters
timeoutthe duration of P2P Rx mode in milli-seconds, except the following magic input:
  • 65535: Stay in RX mode until a packet is received.
  • 65534: Stay in RX mode until api.lorawan.precv(0) is called.
  • 65533: Stay in RX mode, but still can do TX without calling api.lorawan.precv(0).
Returns
bool
Return values
TRUEfor entering P2P Rx mode success
FALSEfor entering P2P Rx mode failure
Example
  void setup()
  {
      Serial.begin(115200);

      Serial.println("P2P Start");
    
      Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
      Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
      Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
      Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
      Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  }

  void loop()
  {
      Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(5000) ? "Success" : "Fail");
  
      delay(6000);
  }

◆ psend()

bool psend ( uint8_t  length,
uint8_t *  payload 
)
Description
This api provides the way to P2P send data
Syntax
api.lorawan.psend(length, payload) api.lorawan.psend(length, payload,cad_enable)
Parameters
lengththe length of the payload
payloadthe data send to the other device
boolChannel Activity Detection enable
Returns
bool
Return values
TRUEfor sending data success
FALSEfor sending data failure
Example
  void setup()
  {
      Serial.begin(115200);

      Serial.println("P2P Start");

      Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
      Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
      Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
      Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
      Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  }

  void loop()
  {
      uint8_t payload[] = "payload";
      Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail"); 
      Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload,true)? "Success" : "Fail"); 

      delay(5000);
  }