Hi guys, i have an idea to build a wireless thermostat for my rooms in my house. The function goes as this.
1 arduino in the living room houses a tempsensor and an graphic controller of some kind. This sensor sends data to another arduino in the same room that is attached to a relay controlling my heating system. The slave continually sends it´s state to the master telling the master if it´s on or not.
I have cobbled together some code for this purpose down below.
The master sends a number to the slave and the slave reads it and then sends it back. The master verifies it. BUT i imagine with the nrf24L01 there are alot more that goes into this. Do i need to think about what happens if no responce comes? How would you guys do it?
Master
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" // Download and Install (See above)
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataTransmitted; // Data that will be Transmitted from the transmitter
int dataReceived; // Data that will be received from the transmitter
void setup()
{
Serial.begin(115200);
delay(1000);
dataTransmitted = 100;
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openWritingPipe( addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
}
void loop()
{
myRadio.stopListening();
myRadio.write( &dataTransmitted, sizeof(dataTransmitted) ); // Transmit the data
Serial.println(dataTransmitted);
dataTransmitted = dataTransmitted + 1; // Send different data next time
myRadio.startListening();
delay(1000);
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
}
// DO something with the data, like print it
Serial.print("Data received = ");
Serial.println(dataReceived);
}
}
Slave
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" // Download and Install (See above)
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataTransmitted; // Data that will be Transmitted from the transmitter
int dataReceived; // Data that will be received from the transmitter
void setup()
{
Serial.begin(115200);
delay(1000);
dataTransmitted = 100;
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openWritingPipe( addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.startListening();
delay(1000);
}
void loop()
{
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
}
// DO something with the data, like print it
Serial.print("Data received = ");
Serial.println(dataReceived);
myRadio.stopListening();
myRadio.write( &dataReceived, sizeof(dataReceived) ); // Transmit the data
Serial.print("Sending data back = "); Serial.println(dataReceived);
myRadio.startListening();
delay(1000);
}
}