// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>
#define DHTPIN 2
//not all #defines used but here for documentation
#define CE_PIN 9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
#define DHTTYPE DHT22
struct data
{
float temperature;
float humidity;
} sentData;
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 2000;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
radio.openWritingPipe(slaveAddress);
dht.begin();
}
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
sentData.temperature = dht.readTemperature();
sentData.humidity = dht.readHumidity();
send();
prevMillis = millis();
}
}
void send()
{
bool rslt;
rslt = radio.write( &sentData, sizeof(sentData) );
if (rslt)
{
Serial.println(" Acknowledge received");
}
else
{
Serial.println(" Tx failed");
}
}
...........................................................................................................................
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#include "DHT.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//not all #defines used but here for documentation
#define CE_PIN 9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
struct data
{
float temperature;
float humidity;
} receivedData;
bool newData = false;
void setup()
{
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C
}
void displayTempHumid(){
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
{
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setFont(&FreeMono9pt7b); //added for font test
display.setCursor(4,13);
display.print("Temp:");
display.print(receivedData.temperature,1);
display.print(" C");
display.setCursor(4,28);
display.print("Humid:");
display.print(receivedData.humidity,1);
display.print("%");
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
}
void loop()
{
getData();
showData();
displayTempHumid();
display.display();
}
void getData()
{
if ( radio.available() )
{
radio.read( &receivedData, sizeof(receivedData) );
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received\ttemperature : ");
Serial.print(receivedData.temperature);
Serial.print("\t");
Serial.print("humidity : ");
Serial.println(receivedData.humidity);
newData = false;
}
}
Comments