LORA shield arduino

How to send sensor value using LORA with arduino

This is tutorial on how to send sensor value using LORA shield with an arduino. I will demonstrate and explain how to send sensor value from an arduino using LORA and the data will received by another LORA as a server.

So we need two arduinos with LORA shield, an DHT11 sensor, and OLED display to display the sensor at LORA server.

You can watch the full tutorial step by step in video below.

The sensor that we will use in this demo is DHT11. So let’s connect the DHT11 sensor to arduino.

Arduino with DHT11 sensor
Arduino with DHT11 sensor wiring

Test the sensor use the sketch below :

/*---------------------
Read sensor data from dht11
Sketch made by miliohm.com
-----------------------*/

#include "DHT.h"
#define DHTPIN 5     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  delay(500);
}

Upload the sketch above and open the serial monitor. You should see the Humidity and Temperature printed on serial monitor. Once it’s done, we can continue to send this sensor data using LORA.

And here’s the sketch to send sensor value using LORA.

/*---------------------
Send sensor data from dht11 to LORA server demo
Sketch made by miliohm.com
-----------------------*/
#include "DHT.h"
#define DHTPIN 5     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
#include <SPI.h>
#include <RH_RF95.h>

RH_RF95 rf95;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  if (!rf95.init())
    Serial.println("init failed");
  rf95.setFrequency(433.0);
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  String data = String(h) + "-" + String(t);
  
  int dataLength = data.length(); dataLength++;
  uint8_t total[dataLength]; //variable for data to send
  data.toCharArray(total, dataLength); //change type data from string ke uint8_t
  Serial.println(data);
  rf95.send(total, dataLength); //send data
  rf95.waitPacketSent();
  delay(500);
}

If you want explanation about the code, you can watch the video.

Upload the sketch again and now we can move to second arduino that is server.

Use the sketch below to second arduino as a server. Upload and let’s test that.

#include <SPI.h>
#include <RH_RF95.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold12pt7b.h>

RH_RF95 rf95;
int led = 13;
unsigned long int millisBefore;

int turn = 0;
int h, t;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  rf95.setFrequency(433.0);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  printText();
  delay(1500);
}

void loop()
{
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);
  if (rf95.waitAvailableTimeout(500))
  {
    if (rf95.recv(buf, &len))
    {
      Serial.print("Received at Server: ");
      Serial.println((char*)buf);
      String dataTotal = (char*)buf;
      Serial.println(dataTotal);
      explode(dataTotal);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  printText();
  display.display();
  delay(300);
  display.clearDisplay();
}

void explode(String req) {
  char str[20];
  req.toCharArray(str, 20);

  char * pch;
  pch = strtok (str, "-");
  while (pch != NULL)
  {
    String sementara = pch;
    turn++;
    if (turn == 1) {
      Serial.print("Humidity : ");
      Serial.println(pch);
      h = sementara.toFloat();
    }
    if (turn == 2) {
      Serial.print("Temperature : ");
      Serial.println(pch);
      t = sementara.toFloat();
    }
    pch = strtok (NULL, "-");
    delay(100);
  }
  turn = 0;
}

void printText() {
  display.setFont(&FreeMonoBold12pt7b);
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0, 28);            // Start at top-left corner
  display.print(t);
  display.drawCircle(34, 8, 3, WHITE);
  display.setCursor(40, 27);
  display.print("C");
  display.setCursor(80, 28);
  display.print(h);
  display.print("%");
}

After upload the server sketch, now you should see the data from first arduino (client with sensor) will displayed at second arduino (the server).