Arduino DHT22 & LoRa Project for Beginners: Send Data to LoRaWAN

Arduino DHT22 & LoRa Project for Beginners: Send Data to LoRaWAN

Project Overview

This beginner-friendly project demonstrates how to use a DHT22 sensor with Arduino and LoRa to send temperature and humidity data to a LoRaWAN network such as The Things Network (TTN). This setup is useful for remote environmental monitoring applications.

Project Goals for Arduino DHT22 & LoRa

  • Learn how to connect a DHT22 sensor to Arduino for temperature and humidity measurements.
  • Use the LMIC library to send data to a LoRaWAN network.
  • Transmit temperature and humidity data to TTN using the LoRa module.

Required Components for Arduino DHT22 & LoRa Project

Here’s a list of components needed to build this project:

ComponentDescriptionLink
Arduino UnoMain microcontroller boardBuy on Amazon
DHT22 SensorMeasures temperature and humidityCheck Availability
LoRa Module (e.g., RFM95)Sends data via LoRaWANCheck Availability
Jumper WiresConnects components to ArduinoBuy on Amazon
BreadboardFor prototyping connectionsBuy on Amazon

What is the DHT22 Sensor?

The DHT22 sensor is a digital sensor used to measure temperature and humidity. It provides accurate readings and is commonly used in weather monitoring, home automation, and environmental sensing projects.

DHT22 Sensor Pinout Overview

PinDescription
VCCPower supply (3.3V or 5V)
GNDGround connection
DataOutputs temperature and humidity data

Circuit Connection for DHT22 & LoRa with Arduino

Follow these connections to set up the DHT22 sensor and LoRa module with Arduino:

ComponentArduino PinDetails
DHT22 VCC5VPower supply for the sensor
DHT22 GNDGNDGround connection
DHT22 DataD5Data output to Arduino
LoRa NSSD9Chip select for SPI communication
LoRa RSTD10Reset pin
LoRa DIO0D8Interrupt pin for LoRa
LoRa DIO1D7Interrupt pin for LoRa

How the Circuit Works

The DHT22 sensor measures temperature and humidity and sends the data to the Arduino. The LoRa module is configured to transmit the data over LoRaWAN to a network like The Things Network (TTN). The LMIC library is used to manage LoRa communication and packet transmission.

Arduino Code for DHT22 & LoRa

This code reads temperature and humidity from the DHT22 sensor and sends the data to TTN using the LoRa module. Copy and upload this code to your Arduino Uno.


#include 
#include 
#include 
#include "DHT.h"

#define DHTPIN 5
#define DHTTYPE DHT22

static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }

static const u1_t PROGMEM DEVEUI[8] = { 0xF5, 0x33, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }

static const u1_t PROGMEM APPKEY[16] = { 0xB8, 0x2B, 0xEA, 0xC6, 0x49, 0x5A, 0xAC, 0xBC, 0x36, 0xAC, 0x22, 0x4A, 0xF2, 0x78, 0x33, 0x72 };
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }

static uint8_t payload[5];
static osjob_t sendjob;
const unsigned TX_INTERVAL = 30;

const lmic_pinmap lmic_pins = {
  .nss = 9,
  .rxtx = LMIC_UNUSED_PIN,
  .rst = 10,
  .dio = {8, 7, LMIC_UNUSED_PIN},
};

DHT dht(DHTPIN, DHTTYPE);

void onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    switch(ev) {
        case EV_JOINING:
            Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            Serial.println(F("EV_JOINED"));
            LMIC_setLinkCheckMode(0);
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE"));
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
        default:
            Serial.print(F("Unknown event: "));
            Serial.println((unsigned) ev);
            break;
    }
}

void do_send(osjob_t* j) {
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        float temperature = dht.readTemperature();
        float rHumidity = dht.readHumidity();
        uint16_t payloadTemp = LMIC_f2sflt16(temperature / 100);
        uint16_t payloadHumid = LMIC_f2sflt16(rHumidity / 100);
        payload[0] = lowByte(payloadTemp);
        payload[1] = highByte(payloadTemp);
        payload[2] = lowByte(payloadHumid);
        payload[3] = highByte(payloadHumid);
        LMIC_setTxData2(1, payload, sizeof(payload) - 1, 0);
    }
}

void setup() {
    delay(5000);
    Serial.begin(9600);
    dht.begin();
    os_init();
    LMIC_reset();
    do_send(&sendjob);
}

void loop() {
  os_runloop_once();
}

Steps to Upload Arduino Code

  • Connect your Arduino to the computer using a USB cable.
  • Open the Arduino IDE and paste the code into a new sketch.
  • Install the LMIC and DHT libraries from the Library Manager.
  • Select the correct board (e.g., Arduino Uno) and port from the "Tools" menu.
  • Click the "Upload" button to transfer the code to the Arduino.

Check Output for DHT22 & LoRa

  • Monitor the Serial Monitor (Ctrl + Shift + M) to see the temperature and humidity readings.
  • Data will be transmitted to the LoRaWAN network (TTN), and you can check it on the TTN console.

Troubleshooting Tips for DHT22 & LoRa

  • No Readings in Serial Monitor: Check the wiring, especially the DHT22 data pin connection.
  • Data Not Sent to TTN: Verify that the LoRa module is configured with the correct keys and connected properly.
  • Inconsistent Readings: Ensure stable power supply and proper connections.

Suggestions for Beginners

Start by testing the DHT22 sensor and LoRa module separately before integrating them into one project. This will help you understand their behavior and troubleshoot issues effectively.

Recommended Book for Learning Arduino

Arduino Programming for Absolute Beginners - This book provides easy, step-by-step instructions, making it ideal for beginners who want to learn Arduino programming.

For more Arduino tutorials, visit MechatronicsLab.net, where you’ll find resources on Arduino, ESP8266, ESP32, and Raspberry Pi.

No comments

Theme images by Dizzo. Powered by Blogger.