Jorge Rancé
Published © GPL3+

Arduino nRF24L01 Wireless Weather Station

An Arduino Nano wireless weather station gathering data from DHT22 and a BME280 sensors and transmitting via nRF24L01 (2.4 GHz).

IntermediateProtip8 hours31,538
Arduino nRF24L01 Wireless Weather Station

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
SparkFun Atmospheric Sensor Breakout - BME280
SparkFun Atmospheric Sensor Breakout - BME280
×1
SparkFun Transceiver Breakout - nRF24L01+
SparkFun Transceiver Breakout - nRF24L01+
×1
Perma-Proto Breadboard Half Size
Perma-Proto Breadboard Half Size
×1
Arduino Mega 2560
Arduino Mega 2560
×1
3.2 inches TFT LCD Ultra HD 320X480 for Arduino MEGA 2560 R3 (optional)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

WirelessWeatherStation_sensors

Fritzing schematics for the sensors + transmitter board.

Code

WirelessWeatherStation_sensors

Arduino
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <DHT.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


// BME280 I2C
Adafruit_BMP280 bme;

// DHT22
#define DHTPIN 7     // DHT22 PIN
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for 16mhz

//DHT22 Variables
int chk;
float dht22_hum;  //Stores humidity value
float dht22_temp; //Stores temperature value

//NRF24L01
const int pinCE = 9;
const int pinCSN = 10;
RF24 radio(pinCE, pinCSN);
byte addresses[][6] = {"0"};
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
struct package
{
  float temperature ;
  float pressure;
  float humidity ;
};
typedef struct package Package;
Package data;


void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setChannel(115);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate( RF24_250KBPS ) ;
  radio.openWritingPipe( addresses[0]);
  delay(1000);

  dht.begin();

  if (!bme.begin())
  {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
}

void loop()
{
  dht22_hum = dht.readHumidity();
  dht22_temp = dht.readTemperature();
  Serial.print("--- Wireless Weather Station ---\n");
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" ºC");
  Serial.print("Pressure = ");
  Serial.print(bme.readPressure() / 100); // 100 Pa = 1 millibar
  Serial.println(" mb");
  Serial.print("Humidity = ");
  Serial.print(dht22_hum);
  Serial.println(" %");
  Serial.print("Approx altitude = ");
  Serial.print(bme.readAltitude(1013.25));
  Serial.println(" m");
  Serial.print("--------------------------------\n\n");
  delay(2000);
  readSensor();
  radio.write(&data, sizeof(data));
}

void readSensor()
{
  data.humidity = dht.readHumidity();
  data.temperature = dht.readTemperature();
  data.pressure = (bme.readPressure() / 100);
}

WirelessWeatherStation_LCD-example

Arduino
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <TFT_HX8357.h>

// tft
TFT_HX8357 tft = TFT_HX8357();

// sensors
float previousRemoteHumidity = 0.1;
float previousRemoteTemperature = 0.1;
float previousRemotePressure = 0.1;

float remoteHumidity = 0.0;
float remoteTemperature = 0.0;
float remotePressure = 0.0;

// trans package
struct package
{
  float temperature;
  float pressure;
  float humidity;
};

// radio
RF24 radio(6, 7);
byte addresses[][6] = {"0"};
typedef struct package Package;
Package data;

void setup(void) {
  // serial
  Serial.begin(9600);

  // radio
  startWirelessCommunication();

  // tft
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  tft.setTextFont(1);
  delay(100);
  printUI();
}

void loop(void) {
  checkForWirelessData();
  printRemoteTemperature();
  printRemoteHumidity();
  printRemotePressure();
}

void startWirelessCommunication()
{
  radio.begin();
  radio.setChannel(115);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate( RF24_250KBPS ) ;
  radio.openReadingPipe(1, addresses[0]);
  radio.startListening();
  delay(100);
}

void checkForWirelessData()
{
  if ( radio.available())
  {
    while (radio.available())
    {
      radio.read( &data, sizeof(data) );
      remoteTemperature = data.temperature;
      remoteHumidity = data.humidity;
      remotePressure = data.pressure;
    }
    Serial.print("\nPackage:");
    Serial.print("\n");
    Serial.println(data.temperature);
    Serial.println(data.humidity);
    Serial.println(data.pressure);
  }
}

void printUI()
{

  tft.drawRoundRect(5, 5, 470, 71, 5, TFT_WHITE);
  tft.drawRoundRect(6, 6, 470, 71, 5, TFT_WHITE);

  tft.drawRoundRect(5, 90, 220, 225, 5, TFT_WHITE);
  tft.drawRoundRect(6, 91, 220, 225, 5, TFT_WHITE);

  tft.drawRoundRect(250, 90, 220, 225, 5, TFT_WHITE);
  tft.drawRoundRect(251, 91, 220, 225, 5, TFT_WHITE);

  tft.fillRect(26, 90, 180, 40, TFT_GREEN);
  tft.fillRect(270, 90, 180, 40, TFT_CYAN);

  tft.setCursor(45, 100);
  tft.setTextColor(TFT_BLACK);
  tft.setTextSize(3);
  tft.print("EXTERIOR");

  tft.setCursor(288, 100);
  tft.setTextColor(TFT_BLACK);
  tft.setTextSize(3);
  tft.print("INTERIOR");

  tft.setCursor(160, 155);
  tft.setTextColor(TFT_GREEN);
  tft.setTextSize(4);
  tft.print("%");

  tft.setCursor(412, 165);
  tft.setTextColor(TFT_CYAN);
  tft.setTextSize(6);
  tft.print("%");

  tft.setCursor(175, 210);
  tft.setTextColor(TFT_GREEN);
  tft.setTextSize(4);
  tft.print("C");

  tft.setCursor(160, 210);
  tft.setTextColor(TFT_GREEN);
  tft.setTextSize(2);
  tft.print("o");

  tft.setCursor(412, 230);
  tft.setTextColor(TFT_CYAN);
  tft.setTextSize(6);
  tft.print("C");

  tft.setCursor(395, 230);
  tft.setTextColor(TFT_CYAN);
  tft.setTextSize(2);
  tft.print("o");

  tft.setCursor(160, 265);
  tft.setTextColor(TFT_GREEN);
  tft.setTextSize(4);
  tft.print("mb");

}

void printRemoteHumidity()
{
  String humidity;
  if (remoteHumidity != previousRemoteHumidity)
  {
    if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
    {
      humidity = "---";
    } else
    {
      humidity = String(remoteHumidity, 1);
    }

    tft.fillRect(35, 152, 120, 40, TFT_BLACK);

    tft.setCursor(35, 152);
    tft.setTextColor(TFT_GREEN);
    tft.setTextSize(4);
    tft.print(humidity);

    previousRemoteHumidity = remoteHumidity;
  }
}

void printRemoteTemperature()
{
  String temperature;
  if (remoteTemperature != previousRemoteTemperature)
  {
    if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
    {
      temperature = "---";
    } else if (remoteTemperature >= 100)
    {
      temperature = String(remoteTemperature, 0);
    } else
    {
      temperature = String(remoteTemperature, 1);
    }

    tft.fillRect(35, 209, 120, 40, TFT_BLACK);

    tft.setCursor(35, 209);
    tft.setTextColor(TFT_GREEN);
    tft.setTextSize(4);
    tft.print(temperature);

    previousRemoteTemperature = remoteTemperature;
  }
}

void printRemotePressure()
{
  String pressure;
  if (remotePressure != previousRemotePressure)
  {
    if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
    {
      pressure = "---";
    } else if (remotePressure >= 100)
    {
      pressure = String(remotePressure, 0);
    } else
    {
      pressure = String(remotePressure, 1);
    }

    tft.fillRect(35, 266, 120, 40, TFT_BLACK);

    tft.setCursor(35, 266);
    tft.setTextColor(TFT_GREEN);
    tft.setTextSize(4);
    tft.print(pressure);

    previousRemotePressure = remotePressure;
  }
}

Credits

Jorge Rancé

Jorge Rancé

6 projects • 45 followers
Linux DevOps interested in hobby electronics and hacking on Arduino, Raspberry Pi and ESP8266 / ESP32.

Comments