Currently I am trying to send data (battery status, humidity, Arduino ID, ...) with the RD24 transceiver to a gateway (Raspberry Pi).
It already works with another temperature sensor.
The problem with the whole thing is that the sensor sends back 'NaN'.
My Hardware:
Arduino pro Mini: https://www.antratek.de/arduino-pro-mini-328-3-3v-8mhz
DHT22: %product-title% kaufen
NRF24L01: https://www.ebay.de/itm/142897030856
Battery case: %product-title% kaufen
Battery: https://www.amazon.de/dp/B07PCDSBZM/
Jumper Wires
My Arduino is wired as follows:
GND – GND (RF24)
2 - out (Humidity Sensor)
7 - CE (RF24)
8 - CS (RF24)
11 – MOSI (RF24)
12 – MISO (RF24)
13 – SCK (RF24)
A0 – Battery (red wire)
VCC – VCC (RF24)
GND - Battery (black wire)
VCC – VCC (Humidity Sensor)
GND – GND (Humidity Sensor)
Here is the code for it:
#include <DHT.h>
#include <DHT_U.h>
#include <OneWire.h>
#include <printf.h>
#include <SPI.h>
#include <RF24.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#define TIMERSECONDS 30
DHT dht(DHTPIN, DHTTYPE);
RF24 radio(7, 8); // using pin 7 for the CE pin, and pin 8 for the CSN pin
uint8_t address[][6] = {"1Node", "2Node"}; // Let these addresses be used for the pair
float batteryPercent;
int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
float batteryVoltage;
float humidity;
long lastTimestamp = -1;
float payload[4];
bool radioNumber = 2; //to use different addresses on a pair of radios, we need a variable to uniquely identify which address this radio will use to transmit
int valueA0 = 0;
void setup() {
payload[0] = 2; // ID
payload[1] = 1234; // API - Key
Serial.begin(115200);
// initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
dht.begin();
delay(2000);
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
// save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
radio.stopListening(); // put radio in TX mode
} // setup
void loop() {
//executes the code every TIMERSECONDS
if (now()/TIMERSECONDS != lastTimestamp)
{
lastTimestamp=now()/TIMERSECONDS;
delay(2000);
humidity = dht.readHumidity();
if (isnan(humidity)) {
humidity = 99.0;
}
// calculates volts and battery level
valueA0 = analogRead(A0);
batteryVoltage = valueA0 * (3.3/1024.0);
batteryPercent = (batteryVoltage - 1.0) * 100 / (3.3 - 1.0); // map(): (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
// battery calculation isnt accurate
if (batteryPercent > 100.0){
batteryPercent = 100.0;
}else if(batteryPercent < 0.0){
batteryPercent = 0.0;
}
// data to transfer
payload[2] = humidity;
payload[3] = batteryPercent;
bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report
if (report) {
Serial.println(F("Transmission successful! ")); // payload was delivered
} else {
Serial.println(F("Transmission failed or timed out")); // payload was not delivered
}
// to make this example readable in the serial monitor
delay(1000); // slow transmissions down by 1 second
}
} // loop
unsigned long now(){
static unsigned long prevMillis;
static unsigned long sysTime;
while( millis() - prevMillis >= 1000){
sysTime++;
prevMillis += 1000;
}
return sysTime;
}
I tried the following things:
1:
I connect it to the breadboard and connect the battery cables to the upper VCC and GND, instead of A0 and GND. The problem here is that I don't want my Arduino on the breadboard. If I take it off there, I have one VCC pin too less.
2:
If I connect the Arduino on the breadboard as described above and also connect the PC, the sensor only sends correct data if the battery is also switched on at the same time.