Arduino codes with LoRa communication

Good evening to all. Well, I have some questions about two codes I found on youtube that are related to Dragino LoRa shield. Before I ask you my questions I must tell you a few things. I used them and just added some commands (to be precise, those that have to do with temperature, humidity and water level, and therefore the sensors of these variables). The meaning is that these codes communicate with each other via LoRa and the transmitter sends temperature and humidity (with the help of the DHT22 sensor), water level with the help of a water level sensor and RSSI indicator. This data is sent to Node-red serially. I will send you both codes below.

Client Code (transmitter) LoRa

#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
#include <SPI.h>
#include <RH_RF95.h>
RH_RF95 rf95;
DHT dht(DHTPIN, DHTTYPE);
const int waterSensor = A0;
int waterValue = 0;
int mmwaterValue = 0;

void setup() {
Serial.begin(115200);
dht.begin();
if (!rf95.init()) //
Serial.println("init failed");
rf95.setFrequency(868.0);
}
void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
//float a = dht.readHumidity(true);
int waterValue = analogRead(waterSensor);
mmwaterValue = map(waterValue, 0, 1023, 0, 40);
//Serial.println(mmwaterValue);
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 ");
Serial.print("this is water depth: ");
Serial.print(mmwaterValue);
Serial.println("mm"); // σε mm
String data = String(h) + "-" + String(t)+ "-" +String(mmwaterValue);
//String json = "{"humidity": " + String(h) + ", "temperature": " + String(t)+ ", "water": " +String(mmwaterValue) + "}";
//Serial.println(json);
**int dataLength = data.length(); dataLength++;
uint8_t total[dataLength];
data.toCharArray(total, dataLength);
Serial.println(data);
rf95.send(total, dataLength);
rf95.waitPacketSent();
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);

if (rf95.waitAvailableTimeout(500))
{

if (rf95.recv(buf, &len))
{
  //digitalWrite(led, HIGH);
  Serial.print("got reply: ");
  Serial.println((char*)buf);
  Serial.print("RSSI: ");
  Serial.println(rf95.lastRssi(), DEC);

delay(10000);
}
}
}

My questions are:

a) why is a buffer used in this case?
b) I would like you to explain to me what the following commands do:
int dataLength = data.length (); dataLength ++;
uint8_t total [dataLength];
data.toCharArray (total, dataLength);
uint8_t buf [RH_RF95_MAX_MESSAGE_LEN];
uint8_t len ​​= sizeof (buf);
if (rf95.recv (buf, & len))

By the way I could not write the other code (server code of LoRa). I will send it in the next post.

start by reading this:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.