Hi, I am slightly new here and therefore hoping for some help.
I am trying to build a small weather station. At the one side there is a DHT 22 and a light sensor. At the other side is an oled display. Now I want to transfer the sensor data wireless to the display via 2 NRF24 modules.
The wired communication works fine, now I wanted to add the NRF24, but it seems like I am not receiving data. I was hoping, that someone might have a clue where the problem is.
Here is my Code.
Transmitter:
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
RF24 myRadio(7,8);
const uint64_t pipe=0xE8E8F0F0E1LL;
int input = A0;
struct package // sending out a struct
{
float humidity;
float temperature;
int sensorVal;
};
typedef struct package Package;
Package data;
void setup() {
delay(1000);
Serial.begin(9600);
myRadio.begin();
dht.begin();
myRadio.setChannel(115); // RF24 supports 126 different channels
myRadio.setPALevel(RF24_PA_MAX); // maximum transmission power
myRadio.setDataRate(RF24_250KBPS); // transmission data rate
myRadio.openWritingPipe(pipe);
}
void loop() {
myRadio.write(&data,sizeof(data));
data.humidity = dht.readHumidity();
data.temperature = dht.readTemperature();
data.sensorVal=analogRead(input);
Serial.print("\nPackage write");
Serial.println(data.humidity);
Serial.print("\n");
Serial.println(data.temperature);
Serial.println(data.sensorVal);
}
receiver
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define I2C_ADDRESS 0x3c
SSD1306AsciiWire oled;
RF24 myRadio(7,8);
const uint64_t pipe=0xE8E8F0F0E1LL;
int sensorWert = 0;
struct package
{
float humidity;
float temperature;
int sensorVal;
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(9600);
myRadio.begin();
myRadio.setChannel(115);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate(RF24_250KBPS);
myRadio.openReadingPipe(1,pipe);
myRadio.startListening();
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x64, I2C_ADDRESS);
delay(1000);
}
void loop() {
delay(2000);
// wait for data
if (myRadio.available()){
while(myRadio.available()){
myRadio.read(&data, sizeof(data));
}
}
oled.setFont(System5x7);
oled.clear();
oled.println("humidity:");
oled.print(data.humidity);
oled.println("%");
oled.println("temperature:");
oled.print(data.temperature);
oled.println(" degree");
setValue(data.sensorVal);
Serial.print("\nPackage read");
Serial.println(data.humidity);
Serial.print("\n");
Serial.println(data.temperature);
Serial.println(data.sensorVal);
}
void setValue(int value)
{
int calcvalue = map(value,0,1023,0,2);
switch(calcvalue){
case 0:oled.println(" sunny"); break;
case 1:oled.println(" cloudy"); break;
case 2:oled.println(" night");break;
}
}
Thanks for the help.