Hi,
I want to have a couple of sensors in my garden for climate-control (temperature, humidity,...)
So I was doing some tests and I ran into a coding issue. So the plan is to have 2 arduino uno's, one sits in my garden with the sensors and sends the sensor data to the one I'm going to keep in my house. They communicate using nRF24L01. So my issue was that I received the value's of my sensor, but I can't figure out how to separate them, because now I get 1 message with the temperature and humidity, both values are in there but I can't find a way to separate them and put them in a separate variable/float. If you need more specific information then feel free to ask.
Kind regards.
Post both codes... (with code tags)
We have no clue how you send the data
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Adafruit_AM2320.h"
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
Adafruit_AM2320 am2320 = Adafruit_AM2320();
float temp = 0;
float hum = 0;
void setup() {
Serial.begin(9600);
Serial.println("REBOOT");
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
if (!am2320.begin()){
Serial.println("Could not find a valid AM2320 sensor, check wiring!");
} else {
Serial.println("AM2320 sensor valid and connected");
}
}
void loop() {
temp = am2320.readTemperature();
hum = am2320.readHumidity();
Serial.print(temp); Serial.println(" *C");
Serial.print(hum); Serial.println(" %");
radio.write(&temp, sizeof(temp));
radio.write(&hum, sizeof(hum));
delay(1000);
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
float input = 0;
radio.read(&input, sizeof(input));
Serial.println(input);
}
}
Create a struct to hold both values. Then, send / receive it as a whole.
Have a look at this tutorial
Sending data separately won’t help you know what you got on the other side. Send them both in one go as suggested, a struct helps.
Alright, it works! Thank you very much for the help!
For other people:
Transmitter (I used an AM2320 for temp and hum)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Adafruit_AM2320.h"
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
Adafruit_AM2320 am2320 = Adafruit_AM2320();
struct dataStruct {
float temp;
float hum;
}sensorInput;
void setup() {
Serial.begin(9600);
Serial.println("REBOOT");
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
if (!am2320.begin()){
Serial.println("Could not find a valid AM2320 sensor, check wiring!");
} else {
Serial.println("AM2320 sensor valid and connected");
}
}
void loop() {
sensorInput.temp = am2320.readTemperature();
sensorInput.hum = am2320.readHumidity();
Serial.print(sensorInput.temp); Serial.println(" *C");
Serial.print(sensorInput.hum); Serial.println(" %");
radio.write(&sensorInput, sizeof(sensorInput));
delay(1000);
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct dataStruct {
float temp;
float hum;
}sensorInput;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&sensorInput, sizeof(sensorInput));
Serial.print(sensorInput.temp); Serial.println("°C");
Serial.print(sensorInput.hum); Serial.println("%");
}
}
great follow up.
+1 karma for posting your work.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.