Radio frecuencia con arduino

Queremos ver la lectura de temperatura y humedad de un sensor DHT11 en un display LCD de 16×2 mediante dos modulos de radiofrequencia NRF24L01. Por lo que utilizaremos:

Material

  • 2 x Modulos NRF24L01
  • 2 x Arduino (Nano, Uno, Mega, etc)
  • 1 x Display 16×2
  • 1 x Sensor DHT11 (Temperatura + Humedad)
  • 1 x Potenciometro 10K
  • Protoboard
  • Cables

Esquema emisor (donde esta el sensor)

Conexión emisor Arduino Nano

Esquema receptor (donde esta el display LCD)

Conexión receptor Arduino Nano

Codigo

//RECEPTOR
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <LiquidCrystal.h> //bliblioteca LCD
LiquidCrystal lcd(3, 4, 5, 6, 7, 8); //Pins para el LCD

// Hardware configuration
//Set up nRF24L01 radio on SPI bus plus pins 9 & 10
//RF24 (cepin, cspin)
RF24 radio(9, 10); //Pins extras para el modulo nRF24L01
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

//Variables globales
int temperatura, humedad;
String stemperatura, shumedad;

void setup(void)
{
  lcd.begin(16, 2); //Iniciamos LCD

  pinMode(10, OUTPUT);
  Serial.begin(9600); // Iniciamos comunicación serie

  // Setup and configure rf radio
  radio.begin(); //Iniciamos radio
  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15, 15);
  // optionally, reduce the payload size.  seems to
  // improve reliability
  //radio.setPayloadSize(8);

  // Start listening
  radio.startListening();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
}

void loop(void)
{
  // if there is data ready
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    unsigned long got_time;
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( &got_time, sizeof(unsigned long) );
      Serial.print("Dato Recibido =");
      Serial.println(got_time);
      // Delay just a little bit to let the other unit
      // make the transition to receiver
      delay(20);
    }

    // First, stop listening so we can talk
    radio.stopListening();

    // Send the final one back.
    radio.write( &got_time, sizeof(unsigned long) );
    Serial.println("Enviando Respuesta");

    int recibido;
    int dato;
    dato = got_time / 10000;
    recibido = got_time - (dato * 10000);
    switch (dato) {
      case 1: // caso de que sea temperatura
        Serial.print(recibido);
        Serial.println(" grados");
        temperatura = recibido;
        stemperatura = "T: " + String(temperatura) +  " *C";
        lcd.setCursor(0, 0);
        lcd.print("                ");  //Limpiar cristal
        lcd.setCursor(0, 0);
        lcd.print(stemperatura);
        break;

      case 2: // caso de que sea humedad
        Serial.print(recibido);
        Serial.println("  Humedad");
        humedad = recibido;
        shumedad =  "H: " + String(humedad) +  " %";
        lcd.setCursor(0, 1);
        lcd.print("                ");  //Limpiar cristal
        lcd.setCursor(0, 1);
        lcd.print(shumedad);
        break;
    }
    // Now, resume listening so we catch the next packets.
    radio.startListening();
  }
}
//EMISOR
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "DHT.h"  //Biblioteca sensor DHT11
#define DHTPIN 2     // Pin donde se conectara el sensor
#define DHTTYPE DHT11   // Sensor que vamos a utilizar 
// Hardware configuration
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9, 10); //Pins extras para el modulo nRF24L01
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
DHT dht(DHTPIN, DHTTYPE);  //Indicamos el pin y el sensor
//Variables globales
int var = 0;

void setup(void)
{
  pinMode(10, OUTPUT);
  Serial.begin(9600); //Iniciamos comunicación serie
  dht.begin(); //Iniciamos el sensor
  radio.begin(); //Iniciamos radio
  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15, 15);
  // optionally, reduce the payload size.  seems to
  // improve reliability
  //radio.setPayloadSize(8);
  // Open pipes to other nodes for communication
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
}

void loop(void)
{
  int envio;
  switch (var) {
    case 1:
      envio = 10000 + dht.readTemperature();
      break;
    case 2:
      envio = 20000 + dht.readHumidity();
      break;
  }
  if (var == 2) {
    var = 1;
  }
  else {
    var++;
  }
  // First, stop listening so we can talk.
  radio.stopListening();
  // Take the time, and send it.  This will block until complete
  //unsigned long time = millis();
  unsigned long time = envio;
  Serial.print("Enviando  ");
  Serial.println(time);
  bool ok = radio.write( &time, sizeof(unsigned long) );
  if (ok) {
    Serial.println("ok...");
  }
  else {
    Serial.println("failed");
  }
  // Now, continue listening
  radio.startListening();
  // Wait here until we get a response, or timeout (250ms)
  unsigned long started_waiting_at = millis();
  bool timeout = false;
  while ( ! radio.available() && ! timeout ) {
    if (millis() - started_waiting_at > 200 ) {
      timeout = true;
    }
  }
  // Describe the results
  if ( timeout )
  {
    Serial.println("Failed, response timed out");
  }
  else
  {
    // Grab the response, compare, and send to debugging spew
    unsigned long got_time;
    radio.read( &got_time, sizeof(unsigned long) );
    // Spew it
    Serial.print("Respuesta = ");
    Serial.println(got_time);
  }
  // Try again 1s later
  delay(500);
}

Galeria de imagenes

Bibliotecas

Fuentes:

Deja un comentario