In this project, we will make ESP32 LoRa Thingspeak Gateway with LoRa Sensor Node. The project is basically a sensor monitoring system on Thingspeak Server using an ESP32 board & LoRa Module SX1278 or SX1276.
Overview
In the last couple of years, there is a number of communication technologies available for interaction between IoT devices. The most popular ones are the Wi-Fi Technology and Bluetooth Module. But they have few limitations like limited range, limited access points & high power consumption. So LoRa technology is introduced by Semtech to fix all these issue. Using a single battery the device operates over a year.
So, in this IoT Project, we will design ESP32 LoRa Gateway & also ESP32 LoRa Sensor Node to monitor the sensor reading wirelessly from a few kilometer distances. The sender will read the humidity and temperature data using DHT11 Sensor. Then it transmits the data via LoRa Radio. The data is received by the receiver module. The receiver will then send the data to Thingspeak Server after certain interval.
Before getting started, you can visit the following posts:
1. Interfacing SX1278 (Ra-02) LORA Module with Arduino: Check Here
2. Sending Sensor Data Wirelessly with LoRa SX1278 & Arduino: Check Here
3. ESP8266 & LoRa SX1278 Transmitter Receiver with DHT11: Check Here
4. ESP32 & LoRa SX1278/76 Transmitter Receiver with OLED: Check Here
5. ESP32 LoRa Sensor Data Monitoring on Web Server: Check Here
Bill of Materials
Following are the components required for making this project. All the components can easily be purchased from Amazon. The components purchase link is given below.
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | ESP32 Board | 2 | Amazon | AliExpress |
2 | LoRa Module SX1278 | 2 | Amazon | AliExpress |
3 | DHT11 Sensor | 1 | Amazon | AliExpress |
4 | Connecting Wires | 10 | Amazon | AliExpress |
5 | Breadboard | 1 | Amazon | AliExpress |
Installing the Required Libraries
Several Libraries are required for the project to program the ESP32 Board with Arduino IDE.
1. DHT11 Library
To read the temperature and humidity we need any temperature humidity sensor. For that DHT11 is the best and cheap sensor. We need to install DHT11 Library for that. Download the library from below and add to the Arduino IDE.
2. LoRa Library
We need an Arduino library for sending and receiving data using LoRa radios. The library supports module like Semtech SX1276, SX1277, SX1278, SX1279 and also HopeRF RFM95W, RFM96W and RFM98W.
Circuit: ESP32 LoRa Thingspeak Gateway with LoRa Sensor Node
Now let us see the sender and receiver circuit for building ESP32 LoRa Gateway & Sensor Node. I assembled both the circuit on breadboard. You can make it on PCB if you want.
ESP32 LoRa Sensor Node
Here is an ESP32 LoRa Sensor Node Circuit with DHT11 Sensor. This part work as a transmitter. The humidity and temperature data is read by DHT11 Humidity Temperature Sensor and transmitted using LoRa Radio.
The connection is fairly simple. Simlarly connect the Lora SX1278 & ESP32 as follows.
ESP32 Pins | SX1278 Pins |
---|---|
GND | GND |
3.3V | VCC |
D5 | NSS |
D23 | MOSI |
D19 | MISO |
D18 | SCK |
D14 | RST |
D2 | DIO0 |
Similarly connect the DHT11 digital input pin to GPIO4 of ESP32. Connect its VCC to 3.3V & GND to GND of ESP32.
ESP32 LoRa Thingspeak Gateway
Here is an ESP32 LoRa Module SX1278 Gateway Circuit. This part work as a Receiver. The humidity and temperature data is received using LoRa Radio & uploaded to Thingspeak Server.
The ESP32 & LoRa SX1278 connection same as above.
Setting Up Thingspeak
In order to Monitor the Sensor Data on Thingspeak Server, you first need to Setup the Thingspeak. To set up the Thingspeak Server, visit https://thingspeak.com/. Create an account or simply sign in if you created the account earlier. Then create a new channel with following details.
ESP32 LoRa Sensor Node Code
After assembling the circuit on breadboard for Sensor Node Circuit. Copy the following code and upload it to the ESP32 Board. Before uploading make sure to do the changes in the LoRa frequency according to region.
1 |
#define BAND 433E6 //433E6 for Asia, 866E6 for Europe, 915E6 for North America |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
//Libraries for LoRa #include <SPI.h> #include <LoRa.h> //Libraries for LoRa #include "DHT.h" #define DHTPIN 4 //pin where the dht11 is connected DHT dht(DHTPIN, DHT11); //define the pins used by the LoRa transceiver module #define ss 5 #define rst 14 #define dio0 2 #define BAND 433E6 //433E6 for Asia, 866E6 for Europe, 915E6 for North America //packet counter int readingID = 0; int counter = 0; String LoRaMessage = ""; float temperature = 0; float humidity = 0; //Initialize LoRa module void startLoRA() { LoRa.setPins(ss, rst, dio0); //setup LoRa transceiver module while (!LoRa.begin(BAND) && counter < 10) { Serial.print("."); counter++; delay(500); } if (counter == 10) { // Increment readingID on every new reading readingID++; Serial.println("Starting LoRa failed!"); } Serial.println("LoRa Initialization OK!"); delay(2000); } void startDHT() { if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } } void getReadings(){ humidity = dht.readHumidity(); temperature = dht.readTemperature(); Serial.print(F("Humidity: ")); Serial.print(humidity); Serial.print(F("% Temperature: ")); Serial.print(temperature); Serial.println(F("°C ")); } void sendReadings() { LoRaMessage = String(readingID) + "/" + String(temperature) + "&" + String(humidity) ; //Send LoRa packet to receiver LoRa.beginPacket(); LoRa.print(LoRaMessage); LoRa.endPacket(); Serial.print("Sending packet: "); Serial.println(readingID); readingID++; Serial.println(LoRaMessage); } void setup() { //initialize Serial Monitor Serial.begin(115200); dht.begin(); startDHT(); startLoRA(); } void loop() { getReadings(); sendReadings(); delay(500); } |
ESP32 LoRa Gateway Code
You can now copy the below code and upload the Gateway code to ESP32 Board. In the below code make changes to WiFi SSID, Password & Thingspeak API Key.
1 2 3 |
String apiKey = "14K8UL2QEK8BTHN6"; // Enter your Write API key from ThingSpeak const char *ssid = "Alexahome"; // replace with your wifi ssid and wpa2 key const char *password = "12345678"; |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
// Import Wi-Fi library #include <WiFi.h> //Libraries for LoRa #include <SPI.h> #include <LoRa.h> //define the pins used by the LoRa transceiver module #define ss 5 #define rst 14 #define dio0 2 #define BAND 433E6 //433E6 for Asia, 866E6 for Europe, 915E6 for North America // Replace with your network credentials String apiKey = "14K8UL2QEK8BTHN6"; // Enter your Write API key from ThingSpeak const char *ssid = "Alexahome"; // replace with your wifi ssid and wpa2 key const char *password = "12345678"; const char* server = "api.thingspeak.com"; WiFiClient client; // Initialize variables to get and save LoRa data int rssi; String loRaMessage; String temperature; String humidity; String readingID; // Replaces placeholder with DHT values String processor(const String& var){ //Serial.println(var); if(var == "TEMPERATURE") { return temperature; } else if(var == "HUMIDITY") { return humidity; } else if (var == "RRSI") { return String(rssi); } return String(); } void setup() { Serial.begin(115200); int counter; //setup LoRa transceiver module LoRa.setPins(ss, rst, dio0); //setup LoRa transceiver module while (!LoRa.begin(BAND) && counter < 10) { Serial.print("."); counter++; delay(2000); } if (counter == 10) { // Increment readingID on every new reading Serial.println("Starting LoRa failed!"); } Serial.println("LoRa Initialization OK!"); delay(2000); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(2000); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } // Read LoRa packet and get the sensor readings void loop() { int packetSize = LoRa.parsePacket(); if (packetSize) { Serial.print("Lora packet received: "); while (LoRa.available()) // Read packet { String LoRaData = LoRa.readString(); Serial.print(LoRaData); int pos1 = LoRaData.indexOf('/'); int pos2 = LoRaData.indexOf('&'); readingID = LoRaData.substring(0, pos1); // Get readingID temperature = LoRaData.substring(pos1 +1, pos2); // Get temperature humidity = LoRaData.substring(pos2+1, LoRaData.length()); // Get humidity } rssi = LoRa.packetRssi(); // Get RSSI Serial.print(" with RSSI "); Serial.println(rssi); } if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com { String postStr = apiKey; postStr += "&field1="; postStr += String(readingID); postStr += "&field2="; postStr += String(temperature); postStr += "&field3="; postStr += String(humidity); postStr += "&field4="; postStr += String(rssi); postStr += "\r\n\r\n\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } //delay(30000); } |
Monitoring Sensor Data on Thingspeak Server
Once the code is uploaded, you can open the Serial Monitor on both the Gateway & Sensor Node Circuit. You will see the following output if the code is connections are correct.
Now you can visit Thingspeak Private View. There you can see the data for Packet Number, Temperature, Humidity & Gateway is uploaded after the interval of 15 seconds. You can modify the delay function in the code to change the timing of the data retrieval.
2 Comments
Hello, good afternoon, I have a question
When I execute your code, the receiver node connects to my internet network but does not print the message of the received packets, the temperature, humidity and the rssi. Maybe you know why it is?
sir i am not able to receive the data on lora gateway code is correct everything is correct can you tell me