> For the complete documentation index, see [llms.txt](https://docs.antares.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.antares.id/en/code-and-library-examples/stm32-lora-arduino-ide/sending-simple-data-to-antares.md).

# Sending Simple Data to Antares

In this project you will be directed to send dummy data from the **STM32 LoRa Discovery Kit** to the **Antares IoT Platform** using LoRa connectivity with the LoRaWAN protocol.

The LoRaWAN protocol is divided into two classes, Class A and Class C, each class has its own advantages and disadvantages, for more details please visit the following page.

{% content-ref url="/pages/2drjc2iLUddplq9EcqUq" %}
[LoRaWAN Class](/en/code-and-library-examples/esp32-lora/lorawan-class.md)
{% endcontent-ref %}

### Prerequisites

The required materials follow the **General Prerequisites** on the previous page. If you have not prepared the requirements in that time, then you can visit the following page

{% content-ref url="/pages/B8JPcIc5ffMuFEAZPBC3" %}
[General Prerequisites STM32 (LoRa) Arduino IDE](/en/code-and-library-examples/stm32-lora-arduino-ide/general-prerequisites-stm32-lora-arduino-ide.md)
{% endcontent-ref %}

## Follow These Steps

### 1. Launch the Arduino IDE Application

### 2. Open Sample Programme

{% hint style="info" %}
You can open the programme code in the Arduino IDE via **File > Examples > Antares LoRaWAN > send-class-A-ABP.**
{% endhint %}

Here is the programme code of the **send-class-A-ABP example.**

```
#include <lorawan.h>

//ABP Credentials
/*
  Notes:
  - select ABP Activation on ANTARES
  - select inherit to generate your keys
  - nwkSKey: 32 digit hex, you can put 16 first digits by first 16 digits your access key and add 16 digits with 0 (ex : abcdef01234567890000000000000000)
  - appSKey: 32 digit hex, put 16 first digits by 0 and put last 16 digits by last 16 digit your access key (ex : 0000000000000000abcdef0123456789)
*/
const char *devAddr = "Lora-Device-Address";      // Replace with the Device Address that you have in the Antares console
const char *nwkSKey = "Network-Session-Key";      // Replace with the Network Session Key that you have in the Antares console
const char *appSKey = "Application-Session-Key";  // Replace with the Application Session Key that you have in the Antares console

const unsigned long interval = 10000;  // 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;
int channel;

#if defined(ARDUINO_ESP32_DEV)
const sRFM_pins RFM_pins = {
  .CS = 5,     //LYNX32 to RFM NSS
  .RST = 0,    //LYNX32 to RFM RST
  .DIO0 = 27,  //LYNX32 to RFM DIO0
  .DIO1 = 2,   //LYNX32 to RFM DIO1
};
#elif defined(ARDUINO_B_L072Z_LRWAN1)
const sRFM_pins RFM_pins = {
  .CS = PA15,   //STM32L0 to NSS
  .RST = PC0,   //STM32L0 to RST
  .DIO0 = PB4,  //STM32L0 to DIO0
  .DIO1 = PB1,  //STM32L0 to DIO1
};
#else
#error *** ONLY SUPPORTS LYNX32 AND B-L072Z-LRWAN1 ***
#endif

void setup() {

#if defined(ARDUINO_B_L072Z_LRWAN1)
  SPI.setMOSI(PA7);
  SPI.setMISO(PA6);
  SPI.setSCLK(PB3);
  SPI.begin();
#elif !defined(ARDUINO_ESP32_DEV)
#error *** ONLY SUPPORTS LYNX32 AND B-L072Z-LRWAN1 ***
#endif

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

  // Set LoRaWAN Class change CLASS_A or CLASS_C
  lora.setDeviceClass(CLASS_A);

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

  // set channel to random
  lora.setChannel(MULTI);

  // Set TxPower to 15 dBi (max)
  lora.setTxPower1(15);

  // 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, "Lora Counter-%d", counter);

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

    lora.sendUplink(myStr, strlen(myStr), 0, 5);
    counter++;
    channel = lora.getChannel();
    Serial.print(F("Ch : "));
    Serial.print(channel);
    Serial.println(" ");
  }

  // Check Lora RX
  lora.update();

  recvStatus = lora.readData(outStr);
  if (recvStatus) {
    int counter = 0;

    for (int i = 0; i < recvStatus; i++) {
      if (((outStr[i] >= 32) && (outStr[i] <= 126)) || (outStr[i] == 10) || (outStr[i] == 13))
        counter++;
    }
    if (counter == recvStatus) {
      Serial.print(F("Received String : "));
      for (int i = 0; i < recvStatus; i++) {
        Serial.print(char(outStr[i]));
      }
    } else {
      Serial.print(F("Received Hex : "));
      for (int i = 0; i < recvStatus; i++) {
        Serial.print(outStr[i], HEX);
        Serial.print(" ");
      }
    }
    Serial.println();
  }
}
```

### 3. Set LoRaWAN Parameters in Antares

On the Antares Device console page, **set LoRa** by pressing the Set LoRa button as shown below.

<figure><img src="/files/oZH5uGhb2NkIjpkq08Uz" alt=""><figcaption></figcaption></figure>

{% tabs %}
{% tab title="Class A" %}
Input LoRaWAN parameters with **Lora Device Class A, Activation Mode ABP, ABP Parameters Inherit** as shown below.

{% hint style="info" %}
When selecting **ABP Parameters Inherit**, the LoRa parameters will be generated by Antares. From the device side, the **Lynx32 Development Board** needs to adjust the LoRa parameters.
{% endhint %}

{% hint style="warning" %}
Don't forget to save (copy) the **Network Session Key** and **Application Session Key** parameters before clicking Set LoRa to facilitate the next process.
{% endhint %}

<figure><img src="/files/fQCwOJT8emT5tvnUy7iG" alt=""><figcaption><p>The Form image contains the LoRa Class A Parameter Set.</p></figcaption></figure>

{% hint style="danger" %}
Make sure your antares account has an active LoRa package.
{% endhint %}
{% endtab %}

{% tab title="Class C" %}
Input LoRaWAN parameters with **Lora Device Class C, Activation Mode ABP, ABP Parameters Inherit** as shown below.

{% hint style="info" %}
When selecting **ABP Parameters Inherit**, the LoRa parameters will be generated by Antares. From the device side, the **Lynx32 Development Board** needs to adjust the LoRa parameters.
{% endhint %}

{% hint style="warning" %}
Don't forget to save (copy) the **Network Session Key** and **Application Session Key** parameters before clicking Set LoRa to facilitate the next process.
{% endhint %}

<figure><img src="/files/mQDVQmgoTC4hZdX4G1hV" alt=""><figcaption><p>The Form image contains the LoRa Class C Parameter Set.</p></figcaption></figure>

{% hint style="danger" %}
Make sure your antares account has an active LoRa package.
{% endhint %}
{% endtab %}
{% endtabs %}

### 4. Set LoRaWAN Parameters in Programme Code

Change the LoRaWAN ABP parameters in the following variables \*devAddr, \*nwkSkey, and \*appsKey. Adjust to the parameters in the Antares console.

```
const char *devAddr = "Lora-Device-Address";
const char *nwkSKey = "Network-Session-Key";
const char *appSKey = "Application-Session-Key"
```

{% hint style="info" %}
The **\*devAddr** parameter that has been generated by Antares can be seen on the device page after completing the **LoRa Set.**
{% endhint %}

{% hint style="info" %}
The parameters **\*nwkSKey** and **\*appSKey** are obtained during **Set LoRa** in the previous step.
{% endhint %}

<figure><img src="/files/rRUg5Awo3piHPE9bYQzm" alt=""><figcaption><p>Figure Antares Console Page After successful LoRa Set.</p></figcaption></figure>

{% hint style="info" %}
If you forgot to save **\*nwkSkey** and **\*appSKey** in the previous step then look at the accesskey in your antares account then follow the following format.

```
Example Accesskey = "aaaaaaaaaaaaaaaa:bbbbbbbbbbbbbbbb"; //32 digit accesskey
const char *nwkSKey = "aaaaaaaaaaaaaaaa0000000000000000"; //16 digit first accesskey plus 16 digit zero
const char *appSKey = "0000000000000000bbbbbbbbbbbbbbbb"; //16 digit zero plus 16 digit last acesskey
```

{% endhint %}

### 5. Compile and Upload Program

Connect the **STM32 LoRa Discovery Kit** with your computer and make sure the **Communication Port** is read.

{% hint style="info" %}
On Windows operating systems, checking can be done through the **Device Manager**. If your **STM32 LoRa Discovery Kit** is read, the **STMicroelectronics STLink Virtual COM Port** appears with the port adjusting the port availability (in this case it reads **COM5**).
{% endhint %}

<figure><img src="/files/26sp7UWDkKoe12mGhfMt" alt=""><figcaption><p>Device Manager image on Windows.</p></figcaption></figure>

Set up the **STM32 LoRa Discovery Kit** board by clicking **Tools > Board > STM32 MCU Based Board** in the Arduino IDE, then make sure the one used is **Discovery**. Change the Board Part Number to **B-L072Z-LRWAN1**. Change Upload Method to **STM32CUBEProgrammer (SWD)** Select the port according to the communication port that is read (in this case COM5). The result will look like the following image.

<figure><img src="/files/EDqfFDMSxbew8Qr6kah0" alt=""><figcaption><p>Image of Tools Menu on Arduino IDE</p></figcaption></figure>

After all the setup is complete, upload the programme by pressing the arrow icon as shown below. Wait for the compile and upload process to finish

{% hint style="info" %}
**The Checklist icon** in the Arduino IDE is just the verify process. Usually used to **Compile** the programme to find out if there are errors or not.&#x20;

**The Arrow icon** on the Arduino IDE is the verify and upload process. Usually used to **Compile** the programme as well as flash the programme on the target board.
{% endhint %}

<figure><img src="/files/LKIOvsV6ZHPsZF86ueBP" alt=""><figcaption><p>Image of the Verify and Upload icons in the Arduino IDE.</p></figcaption></figure>

If the programme upload is successful, it will look like the following image.

<figure><img src="/files/3K9T0g3piKlMXGOnJuzZ" alt=""><figcaption><p>Arduino IDE page image after successful upload.</p></figcaption></figure>

After uploading the programme, you can view the **serial monitor** to debug the programme. The **serial monitor** icon is shown in the following image.

<figure><img src="/files/kBYRA819Unpbga7NN4oN" alt=""><figcaption><p>Image of the Serial Monitor Icon in the Arduino IDE.</p></figcaption></figure>

Set the **serial baud rate** to 115200 and select BothNL & CR. The result will look like the following image.

{% hint style="danger" %}
Make sure the **serial baud rate** matches the value defined in the programme code. If the **serial baud rate** is not the same between the **programme code** and the **serial monitor**, the ASCII characters will not be read properly.
{% endhint %}

<figure><img src="/files/1MvVa4ToS9oUB3qLhC5D" alt=""><figcaption><p>Serial Monitor Display</p></figcaption></figure>

### **5. Check Data in Antares**

After uploading the programme successfully, then open the device antares page and see if the data has been successfully sent.

<figure><img src="/files/UxPB0t7ZSNFL5IPkjtEm" alt=""><figcaption><p>Image of the Antares Console Page When Data is Successfully Received.</p></figcaption></figure>

The form of the data received by the antares will be JSON in the following format

```
{
  "type": "uplink",
  "port": 5,
  "data": "Lora Counter-0",
  "counter": 0,
  "devEui": "b9dc778f871ed781",
  "radio": {
    "gps_time": null,
    "hardware": {
      "snr": 9.3,
      "rssi": -53
    },
    "datarate": 2,
    "modulation": {
      "bandwidth": 125000,
      "spreading": 10
    },
    "delay": 0.0010647773742675781,
    "freq": 922,
    "size": 27
  }
}
```

{% hint style="info" %}
The data sent from the **STM32 LoRa Discovery Kit** is in the form of **"counter", "port"**, and message in the JSON field **"data"**. While other parameters are supporting parameters generated by the Antares LoRaWAN Infrastructure.
{% endhint %}
