Example Code for Arduino-Uploading Temperature and Humidity Data to LoRaWAN Gateway

This sample code connects to the LoRaWAN network in OTAA mode. It reads data from the SHT31 temperature and humidity sensor (I2C address: 0x45) every 30 seconds, displays the data on the screen, sends the data to the gateway, and then enters sleep mode. The main controller can be proactively woken up via a button (connected to IO18) to collect and send data.

Hardware Preparation

Software Preparation

Note:

  • LoRaWAN protocol stack version: 1.0.3
  • You need to set the data parsing format to "text" on the gateway.

Sample Code

#include "DFRobot_LoRaWAN.h"
#include <DFRobot_SHT3x.h>

// Button pin
#define BTN_PIN 18  // GPIO2, 3, 11, 12, 13 can all trigger external wake-up

LCD_OnBoard screen;

#define BG_COLOR        COLOR_RGB565_BLACK      // Screen background color
#define TEXT_COLOR      COLOR_RGB565_GREEN      // Screen font color

#define TEXT_FONT       &FreeMono9pt7b          // font
#define TEXT_SIZE       1                       // Screen font size
#define LINE_HEIGHT     18                      // Line height
#define POX_X           0                       // Screen print position X coordinate
#define POX_Y           15                      // Screen print position Y coordinate
#define LINE_1          0                       // Line number
#define LINE_2          1
#define LINE_3          2
#define LINE_4          3

// Data packet transmission interval
#define APP_INTERVAL_MS 30000
const uint8_t DevEUI[8] = {0xDF, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
const uint8_t AppEUI[8] = {0xDF, 0xB7, 0xB7, 0xB7, 0xB7, 0x00, 0x00, 0x00};
const uint8_t AppKey[16] = {
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
  0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};

uint8_t buffer[255];
uint8_t port = 2;
LoRaWAN_Node node(DevEUI, AppEUI, AppKey, CLASS_A);

DFRobot_SHT3x sht3x;

// Rejoin count
RTC_DATA_ATTR uint8_t CurReJoinTimes = 0;

// Join network callback function
void joinCb(bool isOk, int16_t rssi, int8_t snr)
{
    screen.fillScreen(BG_COLOR);    
    screen.setTextColor(TEXT_COLOR);
    screen.setFont(TEXT_FONT);
    screen.setTextSize(TEXT_SIZE);

    if(isOk){
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
        screen.printf("JOIN SUCCESS");
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_2);
        screen.printf("Accept Packet");
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_3);
        screen.printf("Rssi = %d", rssi);
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_4);
        screen.printf("Snr = %d", snr);
        delay(1000);
        node.deepSleepMs(APP_INTERVAL_MS);  // Deep sleep after successful network join        
    }else{
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
        screen.printf("OTAA join Err!");
        delay(2000);
        // Backoff join procedure
        CurReJoinTimes++; 
        uint64_t backoff_time_ms = 5000 * (1ULL << (CurReJoinTimes - 1));
        backoff_time_ms = (backoff_time_ms > 300000) ? 300000 : backoff_time_ms;
        node.deepSleepMs(backoff_time_ms);
    }  
}

void userSendConfirmedPacket(void)
{    
    float temperature = sht3x.getTemperatureC();
    float humidity = sht3x.getHumidityRH();
    screen.fillScreen(BG_COLOR);    
    screen.setTextColor(TEXT_COLOR);
    screen.setFont(TEXT_FONT);
    screen.setTextSize(TEXT_SIZE);
    screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
    screen.printf("Temp: %.2f C", temperature);
    screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_2);
    screen.printf("Humi: %.2f %%", humidity);
    char data[64];
    snprintf((char*)data, sizeof(data), "T:%.2f,H:%.2f", temperature, humidity);
    uint32_t datalen = strlen(data);
    memcpy(buffer, data, datalen);
    node.sendUnconfirmedPacket(port, buffer, datalen);
    delay(4000);  //A minimum delay of 4 seconds is required
    node.deepSleepMs(APP_INTERVAL_MS);
}

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

    screen.begin(); 
    screen.fillScreen(BG_COLOR);    
    screen.setTextColor(TEXT_COLOR);
    screen.setFont(TEXT_FONT);
    screen.setTextSize(TEXT_SIZE);
    while (sht3x.begin() != 0) {
        screen.fillScreen(BG_COLOR);  
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
        screen.printf("SHT3x Err");
        delay(1000);
    }
    delay(1000);

    // Set to wake up using a button press
    esp_sleep_enable_ext0_wakeup((gpio_num_t )BTN_PIN, LOW);
    esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
    if(!(node.init(/*dataRate=*/DR_4, /*txEirp=*/16))){     // Initialize the LoRaWAN node, set the data rate and Tx Eirp
        screen.fillScreen(BG_COLOR);        
        screen.setTextColor(TEXT_COLOR);
        screen.setFont(TEXT_FONT);
        screen.setTextSize(TEXT_SIZE);
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
        screen.printf("LoRaWAN Init Failed!");
        while(1);
    }

    if(!node.isJoined()) {
        screen.fillScreen(BG_COLOR);        
        screen.setTextColor(TEXT_COLOR);
        screen.setFont(TEXT_FONT);
        screen.setTextSize(TEXT_SIZE);
        screen.setCursor(POX_X, POX_Y + LINE_HEIGHT * LINE_1);
        screen.printf("Join Request");
        node.join(joinCb);                                  // Join the LoRaWAN network 
    } else {
        userSendConfirmedPacket();                          // Send data
    }    
}

void loop()
{
    delay(1000);
}

Was this article helpful?

TOP