SEND DATA DHT22 VIA RF24

DHT22 RF24tr

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/RF24_DHT22.zip

//TRANSMITTER

#include <SPI.h>

#include “nRF24L01.h”

#include “RF24.h”

#include “DHT.h” // DHT & AM2302 library

// Version number

const float fVerNum = 0.03;

// Data pin connected to AM2302

#define DHTPIN 2

#define DHTTYPE DHT22 // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE); // LED pins

//////////////////////////////////////////////////////

RF24 radio(8,7);

void setupRadio()

{

// Initiate the radio

radio.begin();

// Define the number of retries sending a packet

radio.setRetries(15,15);

// Define the radio’s broadcast channel (0 – 127)

radio.setChannel(30);

// Define the radio’s bitrate (using cards lowest bitrate)

radio.setDataRate(RF24_250KBPS);

// Define the radio’s power amplifier level (RF24_PA_MIN for debugging, RF24_PA_HIGH for longest range)

radio.setPALevel(RF24_PA_MIN);

// Enable dynamic payloads for packets

radio.enableDynamicPayloads();

}

struct package

{

float temperature ;

float humidity ;

};

typedef struct package Package;

Package data;

void setup()

{

setupRadio();

dht.begin();

}

void loop()

{

data.temperature = dht.readTemperature();

data.humidity = dht.readHumidity();

// Open a writing pipe on the radio

radio.openWritingPipe(0xE8E8F0F0E1LL);

// Write the temperature to the pipe

radio.write(&data, sizeof(data));

}

DHT22 RF24rcv

//RECEIVER

#include <SPI.h>

#include “nRF24L01.h”

#include “RF24.h”

// — Global variables —

RF24 radio(8,7);

//int packetCounter = 0;

// — Functions —

void setupRadio()

{

// Initiate the radio

radio.begin();

// Define the number of retries sending a packet

radio.setRetries(15,15);

// Define the radio’s broadcast channel (0 – 127)

// Must be the same on the sensor and the master node

radio.setChannel(30);

// Define the radio’s bitrate (using cards lowest bitrate)

radio.setDataRate(RF24_250KBPS);

// Define the radio’s power amplifier level (RF24_PA_MIN for debugging, RF24_PA_HIGH for longest range)

radio.setPALevel(RF24_PA_MIN);

// Enable dynamic payloads for packets

radio.enableDynamicPayloads();

// Open reading pipe

radio.openReadingPipe(1, 0xE8E8F0F0E1LL);

// Start listening for packets

radio.startListening();

}

struct package

{

float temperature ;

float humidity ;

};

typedef struct package Package;

Package data;

void setup()

{

// Initiate serial connection

Serial.begin(9600);

// RF Radio setup

setupRadio();

}

void loop()

{

// Check if the radio has received any data

if(radio.available()) {

//int temperature = 0;

// Read the packet received into the temperature variable

if(!radio.read(&data, sizeof(data))) {

// The sensor did not receive the ACK packet

//Serial.println(“ACK not received by client.”);

}

// Print the temperature

Serial.print(“TEMP: “);

Serial.print(data.temperature,1);

Serial.print(” “);

Serial.print(“HUM: “);

Serial.println(data.humidity,1);

// Wait for serial print to complete

delay(1000);

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s