ESP8266

ESP8266 and NRF24L01 Gateway for Arduino IoT Cloud

ESP8266 & NRF24L01 Gateway:

 

ESP8266 and NRF24L01 Gateway for Arduino IoT Cloud- In this tutorial, you will learn how to make ESP8266 and NRF24L01-based IoT Gateway for monitoring DHT21 Temperature and Humidity sensors using Arduino IoT Cloud.

ESP8266 & NRF24L01

For the short-range communication; within 100 to 500 meters, these tiny low-cost NRF24L01 Transceiver modules are ideal. Because these are fast and easy to use and I am saying this based on my personal experience, as I have been using these NRF24L01 Radio transceiver modules in different projects for quite a long time.

Anyway, the components which I am using in this project, I have already made beginners level videos on each one of these and I have also written many articles. And likewise, I have also a getting started article on the Arduino IoT Cloud which is used for monitoring sensors and controlling devices from any part of the world using the internet.

For demonstration purposes, I am using DHT21 Temperature and Humidity sensor, but if you want you can also use DHT11 or any other sensor as per your requirement.

ESP8266 & NRF24L01

Anyway, here is a prototype model of my ESP8266 and NRF24L01 Gateway. You can see Transmitter on the right side, along which DHT21 sensor and NRF24L01 transceiver module are connected to the Arduino Nano.

And you can see Receiver on the left side. The NRF24L01 Radio Transceiver module is connected with the Nodemcu ESP8266 WiFi module.

Arduino Nano reads the DHT21 sensor and sends the Temperature and Humidity values to the Receiver side IoT gateway using NRF24L01. On the receiver side, the NRF24L01 receives the transmitted data, and gives it to the Nodemcu ESP8266. After some processing, ESP8266 sends the temperature and humidity values to the Arduino IoT Cloud where these values are displayed on the Gauges. Now, let’s go ahead and start a practical demonstration.




JLCPCB Sponsor:

Arduino processing

Feel free to visit their website https://jlcpcb.com/SKL to not only find out what awesome PCB and Assembly services they offer, but also to easily upload your Gerber files and thus order affordable and high-quality PCBs quickly. You will only need to pay 2 dollars for 1- 4 layers PCBs, and 0 dollars for your PCB assembly. Besides this JLCPCB also offers industrial 3D printing services starting at only 1 dollar.

Practical Demonstration:

ESP8266 & NRF24L01

You can see right now the temperature is 30.9C and the Humidity is 41.7%. I am going to apply some heat.

ESP8266 & NRF24L01

After applying some heat, the temperature increase to 41.8C and the Humidity value dropped to 30.9%. Now, with Arduino IoT Cloud I can monitor my DHT21 Temperature and Humidity Sensor from anywhere in the world.

I am sure by now, you might have got an idea of how does this system work. So, without any further delay let’s get started!!!



Amazon Links:

Arduino Nano

DHT21/AM2301 Sensor

NodeMCU ESP8266 WiFi module

NRF24L01 Radio Transceiver Modules

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

DHT21 or AM2301:

AM2301 is a digital temperature and humidity sensor module made using a capacitive humidity sensor, a high-precision temperature sensor, and an 8 bit microcontroller. The former name of this sensor was DHT21. This module provides extremely accurate outputs as it is digitally calibrated using the coefficients stored in the microcontroller. It has a wide operating voltage range, 3.3V ~ 5.2V makes its interface easy with a wide variety of devices. The AM2301 sensor module can be easily interfaced with Arduino Boards, Raspberry Pi, and other microcontroller units with its single bus interface.

ESP8266 & NRF24L01

Features:

  • Ultra low power
  • Fast response
  • Relative humidity and temperature measurement
  • Full range temperature compensation
  • Excellent long term stability
  • Long transmission distance
  • High accuracy
  • No need of extra components
  • Excellent long term stability
  • Calibrated digital output



Pin Out

  • Red VDD Power (3.3V ~ 5.2V)
  • Yellow SDA Serial Data, Dual Port
  • Black GND Ground

Applications:

  • HVAC (Heating, Ventilating and Air Conditioning)
  • Dehumidifier
  • Testing and inspection equipment
  • Home appliances
  • Data loggers
  • Humidity regulator
  • Weather stations

Transmitter Side:

ESP8266 & NRF24L01

VCC and GND pins of the NRF24L01 are connected with the Arduino 3.3V and GND pins. Don’t forget to add a 10uF decoupling capacitor between the VCC and GND pins. Connect the CE Pin of the NRF24L01 with the Arduino pin 9. SCN with Pin 10, SCK with Pin 13, MOSI with Pin 11, and MISO pin of the NRF24L01 with the Arduino pin 12.

Connect the VCC and GND pins of the DHT21 Sensor with the Arduino 5V and GND. Connect the Signal pin or Data pin of the DHT21 Temperature and Humidity Sensor with the Arduino digital pin 2. Now, let’s take a look at the receiver’s side circuit diagram.



Receiver Side:

ESP8266 & NRF24L01

VCC and GND pins of the NRF24L01 are connected with the Nodemcu ESP8266 3.3V and GND pins. Connect the CE Pin of the NRF24L01 with the D4 pin on the Nodemcu module. SCN with D2, SCK with D5, MOSI with D7, and MISO pin of the NRF24L01 with the Nodemcu ESP8266 WiFi module D6 Pin.

On the left side, you can see a 5V regulated power supply based on the LM7805 voltage regulator. You will need this power supply if you want to external power up your Nodemcu module or else you can use your PC or Laptop.

PCBs from JLCPCB:

ESP8266 & NRF24L01

These are the NRF24L01 development boards that I just received from the JLCPCB. I will be using these boards for testing my NRF24L01 transceiver-based projects. On the left and right side you can add different types of sensors and displays. I am really impressed with the PCBs quality. The silkscreen is quite clear and the white color solder mask looks pretty amazing.

ESP8266 & NRF24L01

Next, I connected everything as per the circuit diagrams. And now let’s start working on the Arduino IoT Cloud.




ESP8266 & Arduino IoT Cloud:

First, open the Arduino IoT cloud and click on the CREATE THING.

ESP8266 & NRF24L01

Then give a name to the Project and click on the Add variable.

ESP8266 & NRF24L01

Then add a variable for the temperature the same way as we did for the humidity.

ESP8266 & NRF24L01

Now, select the data type and under the Variable Permission select Read & Write and then click on the ADD VARIABLE button.

ESP8266 & NRF24L01

Similar steps will be used for adding the humidity variable.

Next, we will select the device which we are going to interface with the Arduino IoT Cloud. So, in the Associated Device, click on the Select Device.

ESP8266 & NRF24L01



Next, click on the Set up a 3rd Party device.

ESP8266 & NRF24L01

Next, select the Device type which is ESP8266 and click on the CONTINUE button.

ESP8266 & NRF24L01

Then give a name to the Device which is NodeMCU and Click on the Next button.

ESP8266 & NRF24L01

Then download the PDF file which consists of Secret Key and Device ID.

ESP8266 & NRF24L01

Now, click on the DONE button to complete the device setup.

ESP8266 & NRF24L01

After that click on the Network button to setup a network.

ESP8266 & NRF24L01



Then in the network setup write the Wi-Fi Name, Password, and the Secret Key from the PDF file which we downloaded and click on the save.

ESP8266 & NRF24L01

After this click on the dashboard.

ESP8266 & NRF24L01

Then click on the BUILD DASHBOARD.

ESP8266 & NRF24L01

Enter the Dashboard Title Name and click on the ADD button and then click on the Gauge.

ESP8266 & NRF24L01

Then give a name to the gauge which is temperature and click on the Link Variable.

ESP8266 & NRF24L01

Then select the temp variable and click on the LINK VARIABLE.

ESP8266 & NRF24L01

Then click on the done and the temperature gauge will be added.

ESP8266 & NRF24L01

In a similar way, we will add a gauge for the Humidity.

ESP8266 & NRF24L01



Now again click on the Things menu and click on the Sketch.

ESP8266 & NRF24L01

Open code in the full editor mode.

ESP8266 & NRF24L01

Then click on the three dots and click on the download sketch to download the entire code, because we will modify the code as per our needs. In my case, I will add code for the NRF24L01.

ESP8266 & NRF24L01

Open the Sketch and install the Arduino IoT Cloud library.

ESP8266 & NRF24L01

After that write the code for the NRF24L01, add the Wi-Fi credential, and the Device ID.

ESP8266 & NRF24L01

Now all the process in completed and upload the code to the ESP8266.

Required Libraries:

You will also need to download the NRF24 and NRF24Network libraries.

To install the DHT library, simply click on the Sketch menu, go to include library, and then click on the manage libraries. In the search box type DHT.

ESP8266 & NRF24L01

Select the desired DHT library and install it. As you can see I have already installed this library.

Download the Project entire code




Receiver Side Code:

Main code:

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/34bf5308-2ed6-4b81-a523-e3fdba6ca239 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float humidity;
  float temp;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <DHT.h>;
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Hardware configuration
// Set up nRF24L01 radio on pins D4 & D2 of Nodemcu
RF24 radio(2, 4);//CE, CSN
const byte pipe[6] = "00001";// Radio pipe address for the 2 nodes to communicate.
float data[4];


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  radio.begin();
  radio.openReadingPipe(0, pipe);
  radio.startListening();
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  if (radio.available()) {
    radio.read(&data, sizeof(data));
    temp = data[0];
    humidity = data[1];
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print("%,  Temperature: ");
    Serial.print(temp);
  }

}

 



Arduino_Secrets.h Code:

#define SECRET_SSID "AndroidAP3DEC"
#define SECRET_PASS "electroniclinic"
#define SECRET_DEVICE_KEY "QO3F7WMTA8OYOEQWHZXJ"

thingProperties.h Code:

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "9c46a888-9395-4d7b-a390-cd4ae0902a76";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password


float humidity;
float temp;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(temp, READ, ON_CHANGE, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Note: main arduino code, arduino_secrets.h and thingProperties.h files should be inside the same folder.

NRF24L01 Transmitter Code:

//Transmitter Side Code for Arduino Nano
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>;
#define DHTPIN 2            //what pin we're connected to
#define DHTTYPE DHT21       //DHT 21  (AM2301)
DHT dht(DHTPIN, DHTTYPE);   //Initialize DHT sensor for normal 16mhz Arduino
// Hardware configuration
// Set up nRF24L01 radio on pins D8 & D10 of  Arduino Nano
float data[4];
RF24 radio(8, 10);
const byte pipe[6] = {"00001"};// Radio pipe address for the 2 nodes to communicate.
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
  Serial.begin(9600);
    dht.begin();
  radio.begin();
  radio.openWritingPipe(pipe);
  radio.stopListening();

}

void loop()
{
   hum = dht.readHumidity();
  temp= dht.readTemperature();
  
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print("%,  Temperature: ");
  Serial.print(temp);
  data[0]=temp;
  data[1]=hum;
  radio.write(&data, sizeof(data));
  delay(1000);
}

The purpose of this code is to read the humidity and temperature values from the dht21 sensor; store these values in the data array at locations 0 and 1 and then the Arduino sends the data array to the receiver side.

I didn’t explain things in detail, because I have already made several videos on Arduino IoT Cloud, NRF24L01, and DHT21. So, I am just reusing the code. For a detailed explanation, you can watch my previous videos. Anyway, that’s all for now.



Watch Video Tutorial

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

5 Comments

  1. Hi, I’m trying my best, but I can’t get it to work.
    I can send and receive packets between arduinos (few transmitters with one receiver) but when receiver is changed to ESP, it receives rubbish data.
    When trying to send out this array that you are using, nothing gets received by the ESP.
    I have connected the ESP to IOT Arduino cloud, but would really like to use it with NRF transmitters.
    Is there something obvious that I am missing?

    1. Got it to work!!
      First you need to need to declare these, but that’s just details:
      float humidity; //Stores humidity value
      float temp; //Stores temperature value

      The most important is to switch 0 to 1 in setup of reading pipe:
      radio.openReadingPipe(1, pipe);

      Works like a charm!
      Thanks for this example!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button