Hello everyone,
(Question in bold below, this is for context)
Once again I have problems with the 433 MHz radio modules. I have made a weather station (although I like to see it as a planet explorer, like the Perseverance rover or the InSight Mars lander), and I would like to send the data of the sensors to a reciever via a 433Mhz radio link. I was clueless on how to do it, and the only viable help I found on the internet was DroneBot's workshop video about 433MHz modules. He uses the Radio Ahead ASK library.
In the video, the author sends the data of a DHT 22 sensor (Humidity and Temperature) via radio using a comma delimited strings. He writes in a single string the temperature reading, then in another string the humidity reading, and then combines both in an output string with a comma in the midle to separate both values. Later in the reciever code he separates both values using the comma as reference point.
The problem is that my project consists of 5 sensors, not just two. It uses two termistors for temperature reading, a DHT 11 for humidity readings (I know it has a temperature reading module, but the termistors have prooven more accurate), an LDR module for light intensity readings and a resistive soil moisture sensor, to read the humidity of the soil. All sensors work, and for now they display their values on an LCD screen with I2C comunication. All but the DHT use analog readings.
AND, using the raw code provided by DroneBot workshop (below), I have managed to send different kinds of values in pairs, both digital and analog. But I need to send the 5 values at the same time.
QUESTION
I don't understand how he separates the two values of the string using commas. In the video, that part is not explained. The auhtor said that more sensors could be added, so it is possible to put 5.
Does anyone know how to send the values via a single comma divided string, or any way to send 5 different values with the module?
Thank you for your time.
Codes:
TRANSMITTER
ad(LDR1);
LDRp100 = map(LDRV, 0, 1023, 0, 100); //turns values into percentages
delay(5);
}/*
433 MHz RF Module Transmitter Demonstration 2
RF-Xmit-Demo-2.ino
Demonstrates 433 MHz RF Transmitter Module with DHT-22 Sensor
Use with Receiver Demonstration 2
DroneBot Workshop 2018
https://dronebotworkshop.com
Slight modification Arduinomaker 1897 June '23
*/
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
#include <DHT.h> //Dht library
// Define Variables
float hum; // Stores humidity value in percent
float temp; // Stores temperature value in Celcius
#define DHTPIN 2
#define DHTTYPE DHT11 //DHT
#define TRM1 A0 //Termistor 1
#define TRM2 A1 //Termistor 2
#define LDR1 A2 //LDR
#define MOST A3 //Moisture sensor
int Vo; //required for the termistor equation
int Vo3; //for second termistor
float R1 = 10000;
float R3 = 10000;
float logR2, R2, tKelvin, tCelsius;
float logR23, R23, tKelvin3, tCelsius3;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
int LDRV;//ldr value variable
int LDRp100; //to calculate percentage
int SMoistV = 0; //for the moisture sensor
int per100 = 0; //to calculate percentage
// Define output strings
String str_humid;
String str_ldr;
String str_tp1;
String str_tp2;
String str_smt;
String str_out;
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize ASK Object
rf_driver.init();
// Start DHT Sensor
dht.begin();
}
void loop()
{
delay(2000); // Delay so DHT-11 sensor can stabalize
hum = dht.readHumidity(); // Get Humidity value
ldr(); //ldr function
readTemp(); //temperature function
moisture(); //Soil moisture function
// Convert Humidity to string
str_humid = String(hum);
// Convert LDR to string
str_ldr = String(LDRp100);
//Convert temp1 to string
str_tp1 = String(tCelsius);
//temp2 to string
str_tp2 = String(tCelsius3);
//soil moisture to string
str_smt = String(per100);
// Combine Humidity and Temperature
str_out = str_humid + "," + str_ldr + str_tp1 + "," + str_tp2 + " + str_smt;
// Compose output character
static char *msg = str_out.c_str();
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
}
void readTemp(){ //reads termistor value using the following equation
Vo = analogRead(TRM1);
R2 = R1 * (1023.0 / (float)Vo - 1.0); // resistance of the Thermistor
logR2 = log(R2);
tKelvin = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
tCelsius = tKelvin - 273.15;
Vo3 = analogRead(TRM2);
R23 = R3 * (1023.0 / (float)Vo3 - 1.0); // resistance of the Thermistor
logR23 = log(R23);
tKelvin3 = (1.0 / (c1 + c2 * logR23 + c3 * logR23 * logR23 * logR23));
tCelsius3 = tKelvin3 - 273.15;
delay(5);
}
void moisture(){ //reads soil moisture
SMoistV = analogRead(A3);
per100 = map(SMoistV, 0, 1023, 100, 0); //turns values into percentages
delay(5);
}
void ldr(){ //reads light intensity
LDRV = analogRead(LDR1);
LDRp100 = map(LDRV, 0, 1023, 0, 100); //turns values into percentages
delay(5);
}
RECIEVER (Incomplete because I don't understand the code provided):
/*
433 MHz RF Module Receiver Demonstration 2
RF-Rcv-Demo-2.ino
Demonstrates 433 MHz RF Receiver Module with DHT-22 Sensor
Use with Transmitter Demonstration 2
DroneBot Workshop 2018
https://dronebotworkshop.com
Slight modification by Arduinomaker 1897 June '23
*/
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Define output strings
String str_humid; //air humidity
String str_ldr; //ldr value
String str_tp1; //temperature 1
String str_tp2; //temperature 2
String str_smt; //soil moisture
String str_out; //output string
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
void setup()
{
// Initialize ASK Object
rf_driver.init();
// Setup Serial Monitor
Serial.begin(9600);
}
void loop()
{
// Set buffer to character size of expected message
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
// Check if received packet is correct size
if (rf_driver.recv(buf, &buflen))
{
// Message received with valid checksum
// Get values from string
// Convert received data into string
str_out = String((char*)buf);
// Split string into two values
//This is the part I don't understand
for (int i = 0; i < str_out.length(); i++) {
if (str_out.substring(i, i+1) == ",") {
str_humid = str_out.substring(0, i);
str_ldr = str_out.substring(i+1);
break;
}
}
// Print values to Serial Monitor
Serial.print("Humidity: ");
Serial.print(str_humid);
Serial.print(" - LDR: ");
Serial.println(str_ldr);
}
delay(1500);
}
The part I don't understand
for (int i = 0; i < str_out.length(); i++) {
if (str_out.substring(i, i+1) == ",") {
str_humid = str_out.substring(0, i);
str_ldr = str_out.substring(i+1);
break;
}
Again, thank you very much. Any help is appreciated.
Kind regards,
Arduino 1897