Sodaq One peer-to-peer example

I’ve just purchased a Sodaq One v3 board for a GPS tracking project in Australia (SODAQ ONE-US-RN2903-V3). The setup was easy and I’ve worked through examples with the GPS tracker. However I can’t find any examples of how to set up a simple peer-to-peer configuration that will allow a Sodaq One to talk directly to another Sodaq One.

The code should be simple, but I just can’t figure it out. Can someone please help.

Regards
Edmond

1 Like

Dear @edmond0167,

Under the explorer pages we has a P2P example:
https://support.sodaq.com/Boards/ExpLoRer/Examples/lora_p2p/

It should work similar for the One.

Best regards,
Jan

Hi Jan,

Thanks for your suggestion. I can program in Python, just struggling with C/C++. I’ve working with other Arduino LoRa shields and also with Pycom modules without issues.

I tried the example you mentioned and I get an error “Serial2 was not declared in this scope”.

I’ve included the code below, which is a combination of two files (Explorer_P2P TX and RN2483_P2P).

I remember reading that the configuration of Serial coms were different between the Explorer and the One units. My device is the US version (used in Australia - 915MHz) using RN2903 rather than RN2483. Should I be importing an additional libraries in the code?

I have included all the code below for reference.:

#include <Arduino.h>
#include <Sodaq_UBlox_GPS.h>
#define MySerial SERIAL_PORT_MONITOR
int8_t trPower = 1; // Transreceiver power ( can be -3 to 15)
String SprFactor = “sf12”; // Spreadingsfactor (can be sf7 to sf12)
uint8_t max_dataSize = 100; // Maximum charcount to avoid writing outside of string
unsigned long readDelay = 60000; // Time to read for messages in ms (max 4294967295 ms, 0 to disable)

const char CR = ‘\r’;
const char LF = ‘\n’;

//#define DEBUG

// Configuring the RN2483 for P2P
void LoraP2P_Setup()
{

Serial2.print(“sys reset\r\n”);
delay(200);
Serial2.print(“radio set pwr “);
Serial2.print(trPower);
Serial2.print(”\r\n”);
delay(100);
Serial2.print(“radio set sf “);
Serial2.print(SprFactor);
Serial2.print(”\r\n”);
delay(100);
Serial2.print(“radio set wdt “);
Serial2.print(readDelay);
Serial2.print(”\r\n”);
delay(100);
Serial2.print(“mac pause\r\n”);
delay(100);

FlushSerialBufferIn();
}

// Send Data array (in HEX)
void LORA_Write(char* Data)
{
Serial2.print(“radio tx “);
Serial2.print(Data);
Serial2.print(”\r\n”);
Serial2.flush();

waitTillMessageGone();

}

// Waits until the data transmit is done
void waitTillMessageGone()
{
while (!Serial2.available());
delay(10);
while (Serial2.available() > 0)
Serial2.read();

while (!Serial2.available());
delay(10);
while (Serial2.available() > 0)
{
#ifdef DEBUG
SerialUSB.write(Serial2.read());
#else
Serial2.read();
#endif
}
}

// Setting up the receiver to read for incomming messages
void StartLoraRead()
{
Serial2.print(“radio rx 0\r\n”);
delay(100);

FlushSerialBufferIn();
}

//////////////////////////////////////
// Read message from P2P TX module //
// Returns 1 if there is a message //
// Returns 2 if there is no message //
//////////////////////////////////////
int LORA_Read(char* Data)
{
int messageFlag = 0;
String dataStr = "radio_rx ";
String errorStr = “radio_err”;
String Buffer = “”;

StartLoraRead();

while (messageFlag == 0) // As long as there is no message
{
while (!Serial2.available());

delay(50);  // Some time for the buffer to fill

// Read message from RN2483 LORA chip
while (Serial2.available() > 0 && Serial2.peek() != LF)
{
  Buffer += (char)Serial2.read();
}

// If there is an incoming message
if (Buffer.startsWith(dataStr, 0)) // if there is a message in the buffer
{
  int i = 10;  // Incoming data starts at the 11th character

  // Seperate message from string till end of datastring
  while (Buffer[i] != CR && i - 10 < max_dataSize)
  {
    Data[i - 10] = Buffer[i];
    i++;
  }
  messageFlag = 1; // Message received
}
else if (Buffer.startsWith(errorStr, 0))
{
  messageFlag = 2; // Read error or Watchdogtimer timeout
}

}

#ifdef DEBUG
SerialUSB.println(Buffer);
#endif

return (messageFlag);
}

// Flushes any message available
void FlushSerialBufferIn()
{
while (Serial2.available() > 0)
{
#ifdef DEBUG
SerialUSB.write(Serial2.read());
#else
Serial2.read();
#endif
}
}

void setup() {
// put your setup code here, to run once:
SerialUSB.begin(57600);
Serial2.begin(57600);

pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_GREEN, OUTPUT) ;

while (!SerialUSB && millis() < 1000);

LoraP2P_Setup();
digitalWrite(LED_BUILTIN, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:

// Some data to send
char Data[100] = “48656c6c6f20576f726c6421”;

LORA_Write(Data);
digitalWrite(LED_GREEN, LOW); // To let us know when the data is send
delay(100);
digitalWrite(LED_GREEN, HIGH);

}

Hi @edmond0167,

The RN module is connected on the explorer to Serial2 and on the One on Serial1.

Best regards,
Jan

@edmond0167

After your question I have added a Hardware Serial section on the overview page of the SODAQ One.
https://support.sodaq.com/Boards/One/#hardware-serials

Explorer:
https://support.sodaq.com/Boards/ExpLoRer/#hardware-serials

Let me know if you need more information.

Best regards,
Jan

Hi Jan,

Thanks for your reply.

I changed all the Serial2 instructions to Serial1. Bug fixed.

There was an issue with the - pinMode(LED_BUILTIN, OUTPUT); statement for the Sodaq One.

I changed this to - pinMode(LED_GREEN, OUTPUT); and pinMode(LED_RED, OUTPUT); respectively, I don’t think that the Sodaq One recognises the LED_BUILTIN constant.

I also needed to change the frequency of communication for Australia. There was no frequency setting to be changed in the code - so I added the following statements to the LoraP2P_Setup() method:

// Set LoRa frequency in Australia 915 MHz
Serial1.print(“radio set freq 915000000”);
Serial1.print("\r\n");
delay(100);

Lastly I also added in a suite of header files just to be sure I wasn;t leaving anything out:

// NB. The hash and <> symbols removed from header statements below
include Arduino.h
include Sodaq_AT_Device.h
include Sodaq_nbIOT.h
include Sodaq_OnOffBee.h
include Sodaq_UBlox_GPS.h
include Sodaq_RN2483.h

I noticed that the Sodaq_RN2483.h is really for Europe, but in Australia I’m using the SODAQ ONE-US-RN2903-V3 device - which I assume uses the Sodaq_RN2903 header file. However when I added this it didn’t seem to be recognised. Not sure if this header file is required at all for the code to work.

The code compiles well - and it looks like the Sodaq One is working. I left the LoRa aerial off for a few minutes (by mistake) and I’m worried I may have damaged the unit.

Any further advice would be appreciated.

Thanks again for your help to date.

Regards
Edmond

Sodaq_RN2483 is just a name, since we are in Europe we made first a library for the RN2483.
A proper name would have been Sodaq_RN2XX3. The library should work also for the RN2903.

You can do a complete power cycle to see if it comes back alive. For me every RN module I tested came back alive after a complete power cycle.

Best regards,
Jan

Hi Jan,

Almost there. The code seems to be working ok - but I don’t seem to be able to receive a clear message when I send a signal from the Sodaq One to a Pycom4 microcontroller.

I bought two more Sodaq One units and they will take 2 weeks to get here. I would like to test LoRa communication from one Sodaq One unit to another Sodaq One unit.

When I try to receive LoRa node-to-node signals using a Pycom unit, I can see that a LoRa signal is received, but the message appears to be lost in translation.

I think this is because the signal send from the Sodaq One is sent as hexadecimal from the Sodaq unit and the Pycom unit has difficulties translating this.

// Some data to send from Sodaq example
char Data[100] = “48656c6c6f20576f726c6421”;

Is there another way to send characters without them being in hexadecimal format? Or is this fundamental to the way data is transmitted using LoRa.

Please keep this post open so I can write about the solution. Perhaps Sodaq to Sodaq is the best result. I’ll write again soon to this post as soon as the new Sodaq One unit arrives and I can test node-to-node communication

Regards
Edmond

Hi Edmond,

I can remember that peer-to-peer isn’t LoRaWAN, but some LoRa protocol of Microchip on the 868 frequency.

Note that there is difference between LoRa and LoRaWAN, LoRaWAN is a STAR network where you need a gateway, hereby peer-to-peer isn’t available.

Here you can find all commands available of the RN mdule:

Best regards,
Jan