Hello,
I have idea to use with Arduino Uno R3 radio NRF24L01 and DHT22 module for measuring of temperature and humidity.
I have tried to create dual communication channels, but I have had many problems with it. On the end I have found here in forum simply usíng of NRF24L01 radio module as only for one side to second one transmission as a type of master - slave system without any acknolige message based on SimplyTx and SimplyRx by Robin2 here on forum.
So, there are two schetches for this purpose and I hope they will be interesting for users of this forum.
//Arduino-NRF-DHT-SimpleTx telemetry sender
//from NRF24L01 + temperature and humidity modul DHT22
//created by PavelOu on the base of Simple one way transmission by Robin2
//from Arduino Forum
// thank you very much for Dratek.cz for their lectures (in the czech language)
//The following pair of programs sends a simple telemetry message from the TX to the RX
//on the base of SimpleTx-Ronin2.ino
//
//SimpleTx - the master sender or the transmitter
// libraries for NRF24L01 (in original used as radio)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7 // set your pins on the right ones in your schema
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','X','S','0','1'}; //RX Slave 01
RF24 nRF(CE_PIN, CSN_PIN); // Create a nRF
//example of a sended message with numbers 0 to 10 (the teen byte is as char 0)
//char dataToSend[11] = {'0','1','2','3','4','5','6','7','8','9','0'};
//final example of the sended message
char dataToSend[11] = {'$','1','2','3',',','5','6','7','*','P','P'};
//where is :
//123 is temperature multiplied by 10
//567 is humidity multiplied by 10
//library DHT
#include "DHT.h"
//number of pin coupled with DHT sensore
#define pinDHT 5
//set the right type of DHT sensor
//#define typeDHT11 DHT11 // DHT 11
#define typeDHT22 DHT22 // DHT 22 (AM2302)
// inicialiyation of DHT sensor with pin and type of sensor
DHT myDHT(pinDHT, typeDHT22);
byte dataMode = 0;
char endOfMessage = '*';
byte typedMark = 0;
byte lengthOfMessage = 0;
byte pause = 3;
unsigned int timeOfpause = 1000;
int j = 0;
int i = 0;
//examples for the float outputs from sensors
float analogInputA0 = 23.9;
float temp = 21.7;
float hum = 54.3;
//table for transcoding the float numbers to characters
char nrToCharBuf[] = {'0','1','2','3','4','5','6','7','8','9'};
String sendedText = "";
void setup()
{
Serial.begin(9600);
Serial.println("SimpleTx Starting send messages !");
nRF.begin();
nRF.setDataRate( RF24_250KBPS );
nRF.setRetries(3,5); // delay, count
nRF.openWritingPipe(slaveAddress);
//set communication with module DHT
myDHT.begin();
dataMode = 4;
}
//====================
void loop()
{
if (dataMode == 5 )
{
Senddata();
dataMode = 4;
timeOfpause = pause * 1000;
delay(timeOfpause);
}
Setdata();
}
//====================
void Senddata()
{
bool rslt;
rslt = nRF.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
//Serial.print("Data Sent ");
Serial.println(dataToSend);
nRF.flush_tx();
}
//================
void Setdata()
{
//Servis part of this sketch is in final ino not used
if (dataMode == 1)
{
Serial.println(" ");
Serial.println("Sensor TXM01 is ready for NRF communication !");
Serial.print("End mark of message: ");
Serial.println(endOfMessage);
Serial.println("For sending messages type T (as TX) !");
Serial.println("For set of message delays type P (as pause) !");
Serial.println("For end of all actions type B (as Break) !");
dataMode = 2;
} // end of dataMode = 1
if (dataMode == 2)
{
if (Serial.available() )
{
typedMark = Serial.read();
Serial.print("Typed: ");
Serial.println(typedMark);
if (typedMark == 66) // Break
{
//ServisOn = true;
Serial.println("Break and return to basic state !");
dataMode = 1;
}
if (typedMark == 84) // T = transmission
{
dataMode = 4;
Serial.println("TX - Station will send messages !");
}
if ( typedMark == 80 ) // P
{
String Pstring = Serial.readString();
Pstring.trim();
pause = Pstring.toInt();
if (pause > 10)
{
Serial.println("Length of pause is limited to 10 sec !");
pause = 10;
}
Serial.print("Pause between sending of messages : ");
Serial.print(pause);
Serial.println(" sec !");
}
Serial.flush();
}
}
if (dataMode == 4)
{
//analogInputA0 = analogRead(A0) / 10;
//analogInputA0 = 87.9;
/*
if (analogInputA0 > 100)
{
analogInputA0 = 99.9 ;
}
for (j = 0; j<10; j++)
{
dataToSend[j] = 0;
}
*/
dataToSend[0] = '$';
temp = myDHT.readTemperature();
// test of temp validity by function isnan
if (!isnan(temp))
{
int X = temp*10; // example 231
int W = X / 100; // 2
dataToSend[1] = nrToCharBuf[W];
int V = X - (W*100); // 31
int U = V/10; // 3
dataToSend[2] = nrToCharBuf[U];
int Q = V - (U*10); // 1
dataToSend[3] = nrToCharBuf[Q];
}
else
{
dataToSend[1] = 'E';
dataToSend[2] = 'r';
dataToSend[3] = 'r';
}
dataToSend[4] = ',';
hum = myDHT.readHumidity();
if (!isnan(hum))
{
int X = hum*10; // example 56.8
int W = X / 100; // 5
dataToSend[5] = nrToCharBuf[W];
int V = X - (W*100); // 68
int U = V/10; // 6
dataToSend[6] = nrToCharBuf[U];
int Q = V - (U*10); // 8
dataToSend[7] = nrToCharBuf[Q];
}
else
{
dataToSend[5] = 'E';
dataToSend[6] = 'r';
dataToSend[7] = 'r';
}
dataToSend[8] = '*';
//dataToSend[9] = '9'; //this will be simpliest LRC test parity nr one
//dataToSend[10] = '0'; //this will be simpliest LRC test parity nr twoo of 1 byte
byte parPom = 0;
byte pLR = 0;
for (j = 1; j<8; j++)
{
parPom = dataToSend[j];
pLR = pLR + parPom;
}
parPom = pLR + 1;
pLR = parPom;
int X = pLR; // example 041
int W = X / 100; // 0 not used
int V = X - (W*100); // 41 this byte is simpliest LRC parity test
int U = V/10; // 4
dataToSend[9] = nrToCharBuf[U];
int Q = V - (U*10); // 1
dataToSend[10] = nrToCharBuf[Q];
lengthOfMessage = 10;
dataMode = 5;
} // end of dataMode = 4
if (dataMode == 5)
{
i = 0;
sendedText = "";
for (j = 0; j<11; j++)
{
typedMark = dataToSend[j];
sendedText = sendedText + dataToSend[j];
i = i + 1;
}
lengthOfMessage = i;
Serial.print("Length of message: ");
Serial.println(lengthOfMessage);
Serial.print("Sended data (text): ");
Serial.println(sendedText);
delay(1000);
} //end of dataMode = 5
}
//====================
//Arduino-NRF-SimpleRx telemetry receiver
//from NRF24L01 and temperature and humidity sensor DHT22 for example
//created by PavelOu on the base of Simple one way transmitter and receiver
//by Robin2 from Arduino Forum
// thank you very much for Dratek.cz for their lectures (in the czech language)
//The following pair of programs can send a simple telemetry message from the TX to the RX one.
//
//SimpleRx-Ronin2.ino
// SimpleRx - the slave as the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte thisSlaveAddress[5] = {'R','X','S','0','1'};
String receiverAddress = "RXS01"; // RX Slave 01
RF24 nRF(CE_PIN, CSN_PIN);
char dataReceived[11]; // this need match to dataToSend length in the TX, but it is not very critical
bool newData = false;
String reseivedData = "";
int j = 0;
//===========
void setup()
{
Serial.begin(9600);
Serial.println("SimpleRx Starting");
nRF.begin();
nRF.setDataRate( RF24_250KBPS );
nRF.openReadingPipe(1, thisSlaveAddress);
Serial.print("Receiver ");
Serial.println(receiverAddress);
Serial.println(" is on air ... ");
nRF.startListening();
//delay(3000);
Serial.println(" and ready to receive new messagees !");
nRF.flush_rx();
}
//=============
void loop()
{
Getdata();
Showdata();
}
//==============
void Getdata()
{
dataReceived[0] = 0;
newData = false;
if ( nRF.available() ) {
nRF.read( &dataReceived, sizeof(dataReceived) );
reseivedData = "";
for (j = 0; dataReceived[j]>0; j++)
{
reseivedData = reseivedData + dataReceived[j];
}
//Serial.print("Reseived data: ");
//Serial.println(reseivedData);
if (dataReceived[0]>0)
{
newData = true;
}
else
{
nRF.flush_rx();
}
}
}
void Showdata()
{
if (newData == true)
{
//Serial.print("Data received ");
Serial.println(dataReceived);
}
newData = false;
}