#include <SPI.h>
#include "RF24.h"
#include "DHT.h"
#include <Sleep_n0m1.h>
RF24 radio(7,8);
int a=0,b=20;
// SLEEP
Sleep sleep;
unsigned long sleepTime = 600000; //how long you want the arduino to sleep 600000ms = 10min
// DHT
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float temp, hum;
// Address of the TX modules
// 1Node = fuori_cucina -> 0
// 2Node = cucina -> 1 (also RX)
// 3Node = bagno -> 2
// 4Node = camera -> 3
byte addresses[][6] = {"1Node", "2Node", "3Node", "4Node"};
// Acknowledge variable
bool rslt;
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Trasmitter");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW); // Massimo
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS); // Più basso
pinMode(5, INPUT);
// Start DHT library
//dht.begin();
}
void loop() {
// Read sensor value
delay(2000);
temp = a++;
hum = b++;
/*
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
*/
// Send the value with the addres of this module
// 3Node = bagno -> 2
send(2, temp);
send(2, hum);
Serial.print("Data Transmitted = ");
Serial.print(temp);
Serial.print(" ");
Serial.println(hum);
// Put the AtMega to sleep
//sleep.pwrDownMode(); //set sleep mode to Power Down
//sleep.sleepDelay(sleepTime); //sleep for sleepTime
delay(10000);
}
void send(int address, float value) {
int error = 0;
// Open connection to the RX module with this address
radio.openWritingPipe(addresses[address]);
Serial.println("Sending...");
// Send the value
do {
rslt = radio.write(&temp, sizeof(temp));
error++;
Serial.println("Trying to send...");
} while (rslt != true || error <= 15);
if(error >= 15){
Serial.println("Error while send, NOT SEND");
}
Serial.println("Send!");
}
But when I try to send something the code apparently freeze. If you see the serial monitor, the last message is "Sending".
I try to modify the send() function and this code work (hoping that this can help):
#include <SPI.h>
#include "RF24.h"
#include "DHT.h"
#include <Sleep_n0m1.h>
RF24 radio(7,8);
int a=0,b=20;
// SLEEP
Sleep sleep;
unsigned long sleepTime = 600000; //how long you want the arduino to sleep 600000ms = 10min
// DHT
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float temp, hum;
// Address of the TX modules
// 1Node = fuori_cucina -> 0
// 2Node = cucina -> 1 (also RX)
// 3Node = bagno -> 2
// 4Node = camera -> 3
byte addresses[][6] = {"1Node", "2Node", "3Node", "4Node"};
// Acknowledge variable
bool rslt;
int dataToSend;
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Trasmitter");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW); // Massimo
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS); // Più basso
pinMode(5, INPUT);
// Start DHT library
//dht.begin();
}
void loop() {
// Read sensor value
delay(2000);
dataToSend = a++;
hum = b++;
/*
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
*/
// Send the value with the addres of this module
// 3Node = bagno -> 2
send(2);
//send(2, hum);
Serial.print("Data Transmitted = ");
Serial.print(temp);
Serial.print(" ");
Serial.println(hum);
// Put the AtMega to sleep
//sleep.pwrDownMode(); //set sleep mode to Power Down
//sleep.sleepDelay(sleepTime); //sleep for sleepTime
delay(10000);
}
void send(int address) {
bool rslt;
radio.openWritingPipe(addresses[address]);
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt == false) {
Serial.println(" Acknowledge but no data ");
}
}
/*
void send(int address, float& value) {
int error = 0;
int cocco = 1;
// Open connection to the RX module with this address
radio.openWritingPipe(addresses[address]);
Serial.println("Trying to send...");
// Send the value
do {
Serial.println("sending");
rslt = radio.write(&cocco, sizeof(cocco));
error++;
delay(1000);
} while (rslt != true || error <= 15);
if(error >= 15){
Serial.println("Error while send, NOT SEND");
}
Serial.println("Send!");
}*/
Why do you print that when the transmission failed?
It's just a very fast try that I made for seeing if I can get it work. I put the first Serial.println() that I had in the paste history. I put the second sketch just for help because it sends the message, but it's not very well made.
Eternyt:
because it sends the message, but it's not very well made.
Why not go back to the original tutorial example and extend it piece by piece to get the functionality you need. Test each change and don't move on until that change works properly. Keep copies of the working program at each stage of development.
Just without the capacitor, and I use D7 and D8 instead of D9 and D10.
I know that you don't want a fritzing-like diagram, so I'm trying to make a CAD diagram. But I have a big problem... where are the GND and VCC pin in the image I attached?
I attached also an image of my project, just ignore the button.
It looks like a couple of components are missing from your module,
I don't have one of those extra cheapos, but the frontend looks like others, only without components.
I doubt that his nearly naked blobbed NRF24L01+ (clone?) will send anything ever.
Thanks for showing up this, but I reached pretty good results in term of distance with this modules (so yes, they send and receive data) and I still don't know why there are missing components. The problem is with this new sketch
In that case get the first example in my Simple nRF24L01+ Tutorial working
Your programme works pretty well, the problems comes out with the first sketch I uploaded in reply#1.
Eternyt:
Your programme works pretty well, the problems comes out with the first sketch I uploaded in reply#1.
Then ditch the first sketch and start again using my working program as a starting point. Test each change as you make it so you can immediately see if something breaks.
I find out an interesting thing: if I pass the value that I will send through the call of the function than the value will not be sent. If instead I use a global value (always inside the function) that it will work.
I attach the code. But I still want to say at the function what value it must send during the call of the function in the loop, inside the parenthesis (so in this format: Send(toWichDevice, Value))
#include <SPI.h>
#include "RF24.h"
#include "DHT.h"
#include <Sleep_n0m1.h>
RF24 radio(7,8);
int a=0,b=20;
// SLEEP
Sleep sleep;
unsigned long sleepTime = 600000; //how long you want the arduino to sleep 600000ms = 10min
// DHT
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float temp, hum;
// Address of the TX modules
// 1Node = fuori_cucina -> 0
// 2Node = cucina -> 1 (also RX)
// 3Node = bagno -> 2
// 4Node = camera -> 3
byte addresses[][6] = {"1Node", "2Node", "3Node", "4Node"};
// Acknowledge variable
bool rslt;
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Trasmitter");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW); // Massimo
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS); // Più basso
pinMode(5, INPUT);
// Start DHT library
//dht.begin();
}
void loop() {
// Read sensor value
delay(2000);
temp = a++;
hum = b++;
/*
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
*/
// Send the value with the addres of this module
// 3Node = bagno -> 2
send(2, temp);
send(2, hum);
Serial.print("Data Transmitted = ");
Serial.print(temp);
Serial.print(" ");
Serial.println(hum);
// Put the AtMega to sleep
//sleep.pwrDownMode(); //set sleep mode to Power Down
//sleep.sleepDelay(sleepTime); //sleep for sleepTime
delay(10000);
}
void send(int address, float value) {
int error = 0;
// Open connection to the RX module with this address
radio.openWritingPipe(addresses[0]); // Put 0 for debug only
Serial.println("Sending...");
// Send the value
do {
rslt = radio.write(&temp, sizeof(temp));
delay(1000);
error++;
Serial.println("Trying to send...");
} while (rslt != true && error <= 15);
if(error >= 15){
Serial.println("Error while send, NOT SEND");
}
Serial.println("Send!");
}
Update to my previous post:
with that method, the acknowledge is received correctly, but the receiver module receives only 0, no matter what is the real number in the variable I send.