I have been working on a project lately and I keep bumping into trouble. The Setup is pretty basic there will be multiple sensors connected to different places in our farm and they will send information to a monitor located inside my house. But hey lets test each module to check whether or not the connection is working or not...And they don,t! I am using the nrf24l01 wireless com module, the code is pretty self explanatory. I used to use an older version of the nrf24 library but with a little bit of digging I thought that upgrading to tmrh20 library... and now it does not even complete the loop. My connections are all right. last time I did not have a receiver while I was using the autoACK() and obviously It did not work anyway I still cant make it work and I have made some fixes to the setup...it still does not work.
// The transmitter code is as follows just ignore the parts about temperature readings
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <OneWire.h>
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error //This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__
int DS18S20_Pin = 2; // sets data pin to pin2
OneWire ds(DS18S20_Pin);
// wifi info
const int CSN = 10;
const int CE = 9;
byte LED1 = 3;
byte LED2 = 4;
byte counter = 1; //used to count the packets sent
bool done = false;
RF24 radio(CE,CSN); //CE, CSN
const uint64_t rAdress = 0xE8E8F0F0E1LL;
void setup(void) {
Serial.begin(9600);
fdevopen( &serial_putc, 0 );
radio.begin(); //Start the nRF24 module
radio.setAutoAck(1); // / Ensure autoACK is enabled so rec sends ack packet to let you know it got the packet
radio.enableAckPayload();
radio.openWritingPipe(rAdress); //open writing pipe
radio.setPALevel(RF24_PA_MAX);
radio.stopListening(); //go into transmit mode
radio.printDetails();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop(void) {
// gets the temperaute in celcius from the ds18s20
float temperature = getTemp();
Serial.print("Temperature: "); // shows the temperature value
Serial.println(temperature);
Serial.print("Now send packet: ");
Serial.println(counter); //serial print the packet number that is being sent
unsigned long time1 = micros(); //start timer to measure round trip
Serial.print("The payload size is: ");
Serial.println(radio.getDynamicPayloadSize());
//send or write the packet to the rec nRF24 module. Arguments are the payload / variable address and size
if (!radio.write(&temperature,sizeof(float))){ //if the send fails let the user know over serial monitor
Serial.println("packet delivery failed");
digitalWrite(LED2, HIGH); // leds are good manual tools to check if everything is working well
digitalWrite(LED1,LOW);
}
else { //if the send was successful
unsigned long time2 = micros(); //get time new time
time2 = time2 - time1; //calculate round trip time to send packet
Serial.print("Time from message sent to recieve Ack packet: ");
Serial.print(time2); //print the time to the serial monitor
Serial.println(" microseconds");
counter++; //up the packet count
if (temperature < -273) { // when the sensor is not connceted the getTemp() function prints -1000 and -273C is the coldest it can can in this universe...
Serial.println("ERROR: TEMPERATURE SENSOR NOT CONNECTED!");
digitalWrite(LED2, HIGH);
digitalWrite(LED1,LOW);}
else if ( radio.write(&temperature,sizeof(temperature)) && temperature < -273 ) {
digitalWrite(LED2, LOW);
digitalWrite(LED1,HIGH);}
}
delay(100);
}
float getTemp(){ //returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
// Serial output for this is as follows and neither LEDs light
STATUS = 0x0e RX_DR=0 TX_DS=0 STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xe8e8f0f0e1 0xc2c2c2c2c2
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xe8e8f0f0e1
RX_PW_P0-6 = 0x20 0x00 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x03
RF_CH = 0x4c
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x03 0x06
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
Temperature: 26.19
Now send packet: 1
The payload size is: 0
// this means the code has some trouble with the if(radio.write()) part as it just stops there
//The receiver code is as follows
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <OneWire.h>
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error //This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__
// wifi info
const int CSN = 10;
const int CE = 9;
byte LED1 = 3;
byte LED2 = 4;
byte counter = 1; //used to count the packets sent
RF24 radio(CE,CSN); //CE, CSN
const uint64_t tAdress = 0xE8E8F0F0E1LL;
float temperature = -1000;
int payload_size;
void setup() {
Serial.begin(9600);
fdevopen( &serial_putc, 0 );
radio.begin(); //Start the nRF24 module
radio.setAutoAck(1); // / Ensure autoACK is enabled so rec sends ack packet to let you know it got the packet
radio.enableAckPayload();
radio.openReadingPipe(1,tAdress); //open reading pipe
radio.setPALevel(RF24_PA_MAX);
radio.startListening(); //go into receiving mode
radio.printDetails();
}
void loop() {
while(radio.available()){
Serial.println("comms available");
radio.read( &temperature, sizeof(float) ); //read size of data and store it in tempersture variable
Serial.print("Temperature: "); //payload counts packet number
Serial.println(temperature); //print payload / packet number
}
Serial.println("no comms available");
Serial.print("The payload size is: ");
payload_size = radio.getDynamicPayloadSize();
Serial.println(payload_size);
delay(1000);
}
The serial output for this part is as follows
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xe7e7e7e7e7 0xe8e8f0f0e1
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xe7e7e7e7e7
RX_PW_P0-6 = 0x00 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x4c
RF_SETUP = 0x07
CONFIG = 0x0f
DYNPD/FEATURE = 0x03 0x06
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
no comms available
The payload size is: 0
// I guess no signal ever leaves the transmitter what may be the problem ? I know these nrf24l01 modules work properly, it either has something to do with my code or I had done something wrong uploading the library or something...can you please help.