Go Down

Topic: Using Nrf24l01 to send sensor data in integer (Read 1 time) previous topic - next topic

Farhan_tech

I want to sent integer value of a sensor with a Nrf24l01. I can ON/OFF led or relay but dont know how to send sensor value wirelessly with Nrf24l01.


my tx code

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"


#include<dht.h>
dht DHT;
#define DHT11_PIN 3 // if you require to change the pin number, Edit the pin with your arduino pin.

int DataMsg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE14BC8F482LL;

int Button1 = 7;
int Button2 = 2;
void setup()
{
  pinMode(7, INPUT);
  digitalWrite(7,HIGH);
  pinMode(2, INPUT);
  digitalWrite(2,HIGH);

  Serial.begin(9600);
  Serial.println("NRF24L01 Transmitter");

  Serial.println("welcome to TechPonder Humidity and temperature Detector");

  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop()
{
  {
  int chk = DHT.read11(DHT11_PIN);
Serial.println(" Humidity " );
Serial.println(DHT.humidity, 1);
Serial.println(" Temparature ");
Serial.println(DHT.temperature, 1);
 radio.read(&DHT.temperature, sizeof(DHT.temperature));
delay(2000);
  }
   if (digitalRead(Button1) == LOW)
  {
    Serial.println("Button 1 pressed");
    DataMsg[0] = 1;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(Button2) == LOW)
  {
    Serial.println("Button 2 pressed");
    DataMsg[0] = 2;
    radio.write(DataMsg, 1);
  }
while(Serial.available())
  {
  char In=Serial.read();
    if (In=='2' == HIGH)//relay on
    {
      Serial.println("Relay ON");
      DataMsg[0] = 1;
    radio.write(DataMsg, 1);
    DataMsg[0] = 1;
    radio.write(DataMsg, 1);
    }
    if (In=='1' == HIGH)//relay on
    {
      Serial.println("Relay OFF");//relay off
      DataMsg[0] = 2;
    radio.write(DataMsg, 1);
    DataMsg[0] = 2;
    radio.write(DataMsg, 1);
    }

   
    if (In=='4' == HIGH)//relay on
    {
      Serial.println("LED ON");
      DataMsg[0] = 3;
    radio.write(DataMsg, 3);
    DataMsg[0] = 3;
    radio.write(DataMsg, 3);
    }

if (In=='3' == HIGH)// on
    {
      Serial.println("LED OFF");//relay off
      DataMsg[0] = 4;
    radio.write(DataMsg, 3);
    DataMsg[0] = 4;
    radio.write(DataMsg, 3);
    }


if (In=='6' == HIGH)//relay on
    {
      Serial.println("LED ON");
      DataMsg[0] = 5;
    radio.write(DataMsg, 5);
    DataMsg[0] = 5;
    radio.write(DataMsg, 5);
    }

if (In=='5' == HIGH)// on
    {
      Serial.println("LED OFF");//relay off
      DataMsg[0] = 6;
    radio.write(DataMsg, 5);
    DataMsg[0] = 6;
    radio.write(DataMsg, 5);
    }

   
     }
}









My rx code

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

int DataMgs[3];
RF24 radio(9,10);
const uint64_t pipe = 0xE14BC8F482LL;
int Relay1 = 2;
int LED1 = 4;
int LED2 = 3;

void setup()
{
  pinMode(Relay1, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}

void loop()
{
  if (radio.available())
  {
    bool done = false;   
    while (!done)
    {
      done = radio.read(DataMgs, 1);
      Serial.print("NRF24L01 Receiver: ");   
      Serial.println(DataMgs[0]);
     
      if (DataMgs[0] == 1)
      {
        delay(10);
        digitalWrite(Relay1, HIGH);
      }

      if (DataMgs[0] == 2)
      {
        delay(10);
        digitalWrite(Relay1, LOW);
      }
      if (DataMgs[0] == 3)
      {
        delay(10);
        digitalWrite(LED1, HIGH);
      }

      if (DataMgs[0] == 4)
      {
        delay(10);
        digitalWrite(LED1, LOW);
      }
      if (DataMgs[0] == 5)
      {
        delay(10);
        digitalWrite(LED2, HIGH);
      }

      if (DataMgs[0] == 6)
      {
        delay(10);
        digitalWrite(LED2, LOW);
      }     
      delay(100);
    }
  }
  else
  {
  Serial.println("Waiting for signal...");
  }
 }

Go Up