//Libraries for NRF24L01+ module. #include <SPI.h> #include <RF24.h> //RF24 object with two pins defined with arguments. CE: 9, CSN: 10 RF24 radio(9, 10); //Address of the pipe. 40 bit long, you can choose this freely. //Remember to use different address in different projects. long long address = 0x1234ABCDEFLL; int sensorPin = 0; float temperature; //Variable that selects whether the circuit is sender (1) or receiver (0) bool sender = 0; void setup() { //Start the radio Serial.begin(9600); radio.begin(); if (sender) { //If sender radio.openWritingPipe(address); //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX //NRF24L01: -18dBm, -12dBm,-6dBM, and 0dBm radio.setPALevel(RF24_PA_LOW); radio.stopListening(); } //If sender else { //If receiver //Open reading pipe with given address and start listening for incoming data radio.openReadingPipe(1, address); radio.startListening(); } //If receiver } void loop() { if (sender) { //If sender //Get temperature from the sensor int reading = analogRead(sensorPin); float voltage = reading * 5.0; voltage /= 1024.0; temperature = (voltage - 0.5) * 100 ; //Send the temperature wirelessly, print error if failed if (!radio.write(&temperature, sizeof(temperature))) { Serial.println(F("Sending failed")); } delay(1000); } //If sender else { //If receiver //If data is available in radio's buffer if(radio.available()) { //While the data is available... while (radio.available()) { //Update temperature -variable with data from the radio module radio.read(&temperature, sizeof(temperature)); } Serial.println(temperature); } } //If receiver } |