ESP8266 and NRF24L01 Gateway for Arduino IoT Cloud
Table of Contents
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.
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.
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:
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:
You can see right now the temperature is 30.9C and the Humidity is 41.7%. I am going to apply some heat.
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:
NRF24L01 Radio Transceiver Modules
ESP32 WiFi + Bluetooth Module (Recommended)
Arduino Nano USB C type (Recommended)
Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
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.
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:
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:
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:
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.
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.
Then give a name to the Project and click on the Add variable.
Then add a variable for the temperature the same way as we did for the humidity.
Now, select the data type and under the Variable Permission select Read & Write and then click on the ADD VARIABLE button.
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.
Next, click on the Set up a 3rd Party device.
Next, select the Device type which is ESP8266 and click on the CONTINUE button.
Then give a name to the Device which is NodeMCU and Click on the Next button.
Then download the PDF file which consists of Secret Key and Device ID.
Now, click on the DONE button to complete the device setup.
After that click on the Network button to setup a network.
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.
After this click on the dashboard.
Then click on the BUILD DASHBOARD.
Enter the Dashboard Title Name and click on the ADD button and then click on the Gauge.
Then give a name to the gauge which is temperature and click on the Link Variable.
Then select the temp variable and click on the LINK VARIABLE.
Then click on the done and the temperature gauge will be added.
In a similar way, we will add a gauge for the Humidity.
Now again click on the Things menu and click on the Sketch.
Open code in the full editor mode.
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.
Open the Sketch and install the Arduino IoT Cloud library.
After that write the code for the NRF24L01, add the Wi-Fi credential, and the Device ID.
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#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:
1 2 3 |
#define SECRET_SSID "AndroidAP3DEC" #define SECRET_PASS "electroniclinic" #define SECRET_DEVICE_KEY "QO3F7WMTA8OYOEQWHZXJ" |
thingProperties.h Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
//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.
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?
there were formatting errors in the code. Today, i fixed all the codes.
Is it now working?
Is it now working?
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!