EDIT - Solved with the final code posted in post #31. May help in the future for somebody else
Hello
First off, I've changed this thread from the original post, to save doing a new post as I've progressed further along
This is a project which I want to build with the intention to montitor my sons bedroom as he is like his mum and enjoys the room being at a warm temperature. Also, he has asthma, which is another reason I want to keep an eye on the humidity.
I had the project working without the rf24, which worked perfectly. I can get the rf24 modules to work on their own but when the two projects are merged, the serial monitor on the Transmitter shows NAN and the display of the Receiver displays NAN on the oled.
I just can't figure out why this is happening, looking online this appears to be a common problem but nothing seems to work.
The dht library is by adafruit and the rf24 is the TMRH20 library.
The DHT22 in use is on a breakout board with the pullup resistor in place.
Has anybody got any tips?
Thank you in advance
Tx
#include "DHT.h"
#include <SPI.h>
#include "RF24.h"
#define DHTPIN 2
#define DHTTYPE DHT22
//#define _pin
RF24 myRadio (7, 8);
byte addresses[][6] = {"0"};
const int led_pin = 13;
struct package
{
float temperature ;
float humidity ;
};
typedef struct package Package;
Package data;
DHT dht(DHTPIN, DHTTYPE, 15);
void setup()
{
Serial.begin(9600);
pinMode(led_pin, OUTPUT);
dht.begin();
myRadio.begin();
myRadio.setChannel(115);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate( RF24_250KBPS ) ;
myRadio.openWritingPipe( addresses[0]);
delay(1000);
}
void loop()
{
// digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
readSensor();
Serial.println(data.humidity);
Serial.println(data.temperature);
myRadio.write(&data, sizeof(data));
//digitalWrite(led_pin, LOW);
digitalWrite(DHTPIN, HIGH);// added
delayMicroseconds(50);// added
// delay(2000);
}
void readSensor()
{
data.humidity = dht.readHumidity();
data.temperature = dht.readTemperature();
}
Rx
#include "DHT.h" //https://github.com/adafruit/DHT-sensor-library
#include "RF24.h" //https://github.com/TMRh20/RF24
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//#define DHTPIN 8
struct package
{
float temperature ;
float humidity ;
};
typedef struct package Package;
Package data;
//#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
RF24 myRadio (7, 8);
byte addresses[][6] = {"0"};
float remoteHumidity = 0.0;
float remoteTemperature = 0.0;
void setup() {
Serial.begin(9600);
Serial.begin(9600);
dht.begin();
myRadio.begin();
myRadio.setChannel(115);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate( RF24_250KBPS ) ;
myRadio.openWritingPipe( addresses[0]);
delay(2000);
Wire.begin();
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C
}
void displayTempHumid(){
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
{
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setFont(&FreeMono9pt7b); //added for font test
//display.setFont(&FreeSans9pt7b); //added for font test
//display.setTextSize(1);
display.setCursor(4,13);
display.print("Temp:");
display.print(t,1);
display.print(" C");
display.setCursor(4,28);
display.print("Humid:");
display.print(h,1);
display.print("%");
//void displaytemp(float temp, char C_F) // function to display temp, takes temperature and character C or F from calling function void dispTemp(void)
//{
display.drawRect(1, 1, display.width()-1, display.height()-1, WHITE); // draws the outer rectangular boundary on the screen
display.setTextColor(WHITE); // i have white OLED display, you can use other colors in case you have multicolored display
display.setTextSize(1); // i have used large font to display temperature, it can be varied as per your taste
display.setFont(); //added for font test
display.setCursor(107,2);
display.print("o"); // this prints the "o" symbol to show Degree
// display.setTextSize(2);
//display.setCursor(112,10);
//display.print(C_F);
}
void loop() {
displayTempHumid();
display.display();
}