# Uplink DHT11 Data Periodically

***

In this project, you will use the **Antares Workshop Shield** on the **Lynx-32 Antares Development Board** module. The **Antares Workshop Shield** contains temperature, humidity (DHT11), relay, LED and push button sensors. You will monitor the temperature and humidity according to the specified interval period. The results of the data sent by the sensor can be monitored through the Antares console.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/RyosWuwJ3C3N3cn3WOEX/workshop%20shield.png" alt=""><figcaption><p>Image of Development Board Lynx-32 and Shield Workshop Antares.</p></figcaption></figure>

## Prerequisites

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

{% content-ref url="../general-prerequisites-esp32-lora" %}
[general-prerequisites-esp32-lora](https://docs.antares.id/en/code-and-library-examples/esp32-lora/general-prerequisites-esp32-lora)
{% endcontent-ref %}

The additional materials specific to this project are as follows.

1. Shield Workshop Antares
2. DHT11 Library. This documentation uses **DHT11 Sensor Library version 1.4.4.**

{% hint style="info" %}
If you have not installed the **DHT11 Sensor Library version 1.4.4**, you can follow the steps at the following link.

[dht11-sensor-library](https://docs.antares.id/en/getting-started/arduino-library-installation/dht11-sensor-library "mention")
{% endhint %}

## Follow These Steps

### 1.Launch the Arduino IDE application

### 2.Opening Sample Programme

{% tabs %}
{% tab title="Class A" %}
{% hint style="info" %}
You can open the programme code in the Arduino IDE via**File > Examples > Antares LoRaWAN >  Lynx32-Simple-Project** > **Class A > UPLINK\_DHT11\_CLASS\_A.**
{% endhint %}

Here is the programme code of the **UPLINK\_DHT11\_CLASS\_A** example.

```arduino
#include <lorawan.h>
#include "DHT.h"

#define DHTTYPE  DHT11
#define SENSOR_DHT  14

DHT dht(SENSOR_DHT, DHTTYPE);
//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 = 60000;    // 60 s interval to send message
unsigned long previousMillis = 0;  // will store last time message sent
unsigned int counter = 0;     // message counter
String dataSend = "";

char myStr[50];
char outStr[255];
byte recvStatus = 0;
int channel;

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

// get temperature and humidity from DHT11
float getTemperature()
{
  float t = dht.readTemperature();
  if (isnan(t)) return 0;
  return t;
}

float getHumidity()
{
  float h = dht.readHumidity();
  if (isnan(h)) return 0;
  return h;
}

void setup() {
  // Setup loraid access
  Serial.begin(115200);
  dht.begin();

  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();

    int t, h;
    t = getTemperature();
    h = getHumidity();
    Serial.println("Temperature : " + (String)t + " C");
    Serial.println("Humidity    : " + (String)h + " %");

    dataSend = "{\"Temp\": " + (String)t + ", \"Humd\": " + (String)h + "}";
    dataSend.toCharArray(myStr, 50);

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

    lora.sendUplink(myStr, strlen(myStr), 0, 5);
    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();
  }

}
```

{% endtab %}

{% tab title="Class C" %}
{% hint style="info" %}
You can open the programme code in the Arduino IDE via **File > Examples > Antares LoRaWAN >  Lynx32-Simple-Project** > **Class C > UPLINK\_DHT11\_CLASS\_C.**
{% endhint %}

Here is the programme code of the **UPLINK\_DHT11\_CLASS\_C** example.

```arduino
#include <lorawan.h>
#include "DHT.h"

#define DHTTYPE  DHT11
#define SENSOR_DHT  14

DHT dht(SENSOR_DHT, DHTTYPE);
//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 = 60000;    // 60 s interval to send message
unsigned long previousMillis = 0;  // will store last time message sent
unsigned int counter = 0;     // message counter
String dataSend = "";

char myStr[50];
char outStr[255];
byte recvStatus = 0;
int channel;

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

// get temperature and humidity from DHT11
float getTemperature()
{
  float t = dht.readTemperature();
  if (isnan(t)) return 0;
  return t;
}

float getHumidity()
{
  float h = dht.readHumidity();
  if (isnan(h)) return 0;
  return h;
}

void setup() {
  // Setup loraid access
  Serial.begin(115200);
  dht.begin();

  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_C);

  // 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();

    int t, h;
    t = getTemperature();
    h = getHumidity();
    Serial.println("Temperature : " + (String)t + " C");
    Serial.println("Humidity    : " + (String)h + " %");

    dataSend = "{\"Temp\": " + (String)t + ", \"Humd\": " + (String)h + "}";
    dataSend.toCharArray(myStr, 50);

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

    lora.sendUplink(myStr, strlen(myStr), 0, 5);
    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();
  }

}
```

{% endtab %}
{% endtabs %}

### 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="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FTnDGkJ31vPQBSlqKIgR5%2Fimage.png?alt=media&#x26;token=63061b56-05ae-460e-a94a-4a80f774daab" alt=""><figcaption><p>Image of Antares Console Page for LoRa Set.</p></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="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/UQ12XloVfe7Qsw3FaFmM/kotak%20setlora.png" 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="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/RVtEL5Tk6XYaZJ7RHKDl/CLASS%20C%20kotak%20setlora.png" 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.

```arduino
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="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2Fp2eMXXcUiu6MioD8j05d%2Fimage.png?alt=media&#x26;token=12c8e850-2350-4ccb-82d5-e06ddbad0a37" alt=""><figcaption><p>Figure Antares Console Page After successful LoRa Set.</p></figcaption></figure>

{% hint style="danger" %}
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.

{% code overflow="wrap" %}

```arduino
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
```

{% endcode %}
{% endhint %}

### 5. Compile and Upload Program

Connect the **Lynx-32 Development Board** to your computer and make sure the **Communication Port** is read.

{% hint style="info" %}
On Windows operating systems the check can be done via **Device Manager**. If your **Lynx-32 Development Board** is read, the **USB-Serial CH340** will appear with the port adjusting the port availability (in this case it reads **COM4**).
{% endhint %}

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/9WinViTOAYYBwZEJpxnk/comport.png" alt=""><figcaption><p>Device Manager image on Windows.</p></figcaption></figure>

Set up the **Lynx-32 Board** by clicking **Tools > Board > ESP32 Arduino** in the Arduino IDE, then make sure the board used is the **ESP32 Dev Module**. Select the port according to the communicaion port that is read (in this case COM4). The result will look like the following picture.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/eYau7taIMBAlqVFHaO0Y/4_Board%20Setup.png" alt=""><figcaption><p>Image of the Tools Menu in the 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.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/Esv8XSbvX64vIg6oq0Qi/5_Upload%20and%20Compile.png" alt=""><figcaption><p>Image of the Verify and Upload Icon in the Arduino IDE.</p></figcaption></figure>

{% hint style="info" %}
**The Tick icon** on the Arduino IDE is just the verify process. Usually used to **Compile** the programme to find out whether there are errors or not. \
**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 to the target board.
{% endhint %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/kXODJ2TgYDGN0OzJjlsq/doneupload.png" 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="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/b9BAIlt3rZGnEaZRFLTO/8_Serial%20Monitor.png" 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.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/asIbtdugBHa7UVGKD6IX/9_Serial%20Communication.png" alt=""><figcaption><p>Serial Monitor Image</p></figcaption></figure>

{% 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 %}

### 6. Check Data in Antares

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

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FCsiKTcWBZke0OazPbbqT%2Fimage.png?alt=media&#x26;token=60f1ec40-3bbf-45fd-8308-b3e12ed59600" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FFEAswOEZfc5qi0WwvPRL%2Fimage.png?alt=media&#x26;token=ee0055ea-4bfd-469c-ac35-59e1490f5bed" alt=""><figcaption></figcaption></figure>

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

{% hint style="info" %}
The data sent from the **Lynx-32 Development Board i**s in the form of "**counter", "port",** and message in the JSON field "**data"**. DHT11 temperature monitoring results are in the JSON field "data" which includes the fields **"Temp"** and **"Humd"**. While other parameters are supporting parameters generated by the LoRaWAN Antares Infrastructure.
{% endhint %}
