I used a nodemcu(Esp8266) and nrf24L01 tranceiver to get data from arduino to a webpage.
I use this for an irrigation system. It also contains two pump on off buttons. There is a problem displaying data in the web page. Often after pressing the pump button several times, the data is displayed once.
tranceiver code arduino uno
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>
const uint64_t pipes[2] = {0xE8E8F0F0E1LL,0xE8E8F0F0E2LL};//0xE8E8F0F0E1LL,0xE8E8F0F0E2LL
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
RF24 radio(7, 8); // CN and CSN pins of nrf
int sensorPin = A0; //soil moister pin
struct sensor {
uint32_t hum; // humidity
uint32_t tem; // tempereture
uint32_t soil; // soil moisture
uint32_t sensorNum;
};
sensor amila;
void setup()
{
Serial.begin(115200);
dht.begin();
radio.begin();
//radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipes[1]);
//amila.sensorNum=1;
}
void loop()
{
delay(1000);
amila.hum =10;// dht.readHumidity();
amila.tem = 20;//dht.readTemperature();
amila.soil = 30;//analogRead(sensorPin);
amila.sensorNum=1;
// data1.h1 = 90;
// data1.t1 = 30;
if (isnan(amila.hum) || isnan(amila.tem)){
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Humidity: ");
Serial.print(amila.hum);
Serial.println("Temperature: ");
Serial.print(amila.tem);
Serial.println("moisture");
Serial.print(amila.soil);
radio.write(&amila, sizeof(amila));
delay(1000);
}
nodemcu code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ESP8266WiFi.h> //esp 2.7.4 board manager
int newhum;
int newtemp;
int newsoil;
int newhum2;
int newtemp2;
int newsoil2;
int hum1;
int temp1;
int hum2;
int temp2;
int soil1;
int soil2;
const char* ssid = "redmi_9";
const char* password = "amila1234";
WiFiServer server(80);
String header;
String pump1_State = "off";
String pump2_State = "off";
const int pump_1 = 0; //d3
const int pump_2 = 2; //d4
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
//String hu = "";
const uint64_t pipes[2] ={0xE8E8F0F0E1LL,0xE8E8F0F0E2LL};
RF24 radio(2, 15);
struct sensor {
uint32_t hum; // humidity
uint32_t tem; // tempereture
uint32_t soil; // soil moisture
uint32_t sensorNum;
};
sensor sensorData;
void setup() {
Serial.begin(115200);
radio.begin();
//radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipes[1]);
radio.openReadingPipe(2, pipes[2]);
radio.startListening();
pinMode(pump_1, OUTPUT);
pinMode(pump_2, OUTPUT);
digitalWrite(pump_1, LOW);
digitalWrite(pump_2, LOW);
// 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(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
server.begin();
//Firebase.begin (FIREBASE_HOST, FIREBASE_AUTH);
}
void loop(){
if ( radio.available() )
{
radio.read(&sensorData, sizeof(sensorData));
disp_sensor_data();
}
hum1 = newhum; //dht.readHumidity();// make a String to hold incoming data from the client
temp1 = newtemp; // dht.readTemperature();
soil1 = newsoil;
hum2 = newhum2;
temp2 = newtemp2;
soil2 = newsoil2;
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client"); // print a message out in the serial port
String currentLine = "";
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the LEDs on and off
if (header.indexOf("GET /1/on") >= 0) {
Serial.println("LED 1 on");
pump1_State = "on";
digitalWrite(pump_1, HIGH);
} else if (header.indexOf("GET /1/off") >= 0) {
Serial.println("pump 1 off");
pump1_State = "off";
digitalWrite(pump_1, LOW);
} else if (header.indexOf("GET /2/on") >= 0) {
Serial.println("pump 2 on");
pump2_State = "on";
digitalWrite(pump_2, HIGH);
} else if (header.indexOf("GET /2/off") >= 0) {
Serial.println("pump 2 off");
pump2_State = "off";
digitalWrite(pump_2, LOW);
}
// Read the first line of the request
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".head { position: fixed; left: 0; top: 0; width: 100%; background-color: #34495e; font-size:35px; font-weight: bold; color: white; text-align: center; }");
client.println(".footer { position: fixed; left: 0; bottom: 0; background-color: #ffff; width: 100%; color: white; text-align: center;}");
client.println(".button { background-color: #1b33d1; border: none; color: #191b26; padding: 16px 40px; width: 150px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #e0b1bb; width: 150px;}");
//client.println("thead { font-size: 35px;background-color: #33ABF4 ;border-style: solid;border-color: red;font-weight: bold;");
//client.println("tbody { font-size: 35px;background-color: #33ABF4 ;border-style: solid;border-color: red;font-weight: bold;");
client.println("table{ font-size: 30px;background-color: #49EFCE ;border-style: solid;border-color: red;font-weight: bold;border-collapse: collapse;align:center;width: 100%;text-align: left;</style></head>");
// Web Page Heading
client.println("<div class=\"head\"> <p>Smart irrigation system</p> </div></br></br></br></br></br></br></br>");
client.println("<pre>");
client.println("<table border =1>");
client.println("<thead >");
client.println("<tr>");//row 1 start
client.println("<th colspan = 2 >");
client.println("FIELD 1 DATA");
client.println("</th>");
client.println("<th colspan = 2 >");
client.println("FIELD 2 DATA");
client.println("</th>");
client.println("</tr>");
client.println("</thead>");
client.println("<tbody>");
client.println("<tr>");//
client.println("<td>");
client.print("Humidity 1(%) : ");
client.println("</td>");
client.println("<td>");
client.println(hum1);
client.println("</td>");
client.println("<td>");
client.print("Humidity 2 (%) : ");
client.println("</td>");
client.println("<td>");
client.println(hum2);
client.println("</td>");
client.println("</tr>");//row 2 end
client.println("<tr>");//row 3 start
client.println("<td>");
client.print("Temperature 1 (°C) : ");
client.println("</td>");
client.println("<td>");
client.println(temp1);
client.println("</td>");
client.println("<td>");
client.print("Temperature 2 (°C) : ");
client.println("</td>");
client.println("<td>");
client.println(temp2);
client.println("</td>");
client.println("</tr>");//row3 end
client.println("<tr>");
client.println("<td>");
client.print("Soil moisture 1 : ");
client.println("</td>");
client.println("<td>");
client.println(soil1);
client.println("</td>");
client.println("<td>");
client.print("Soil moisture 2 : ");
client.println("</td>");
client.println("<td>");
client.println(soil2);
client.println("</td>");
client.println("</tr>");
client.println("</tbody>");
client.println("</table>");
client.println("</pre>");
client.println("<h3>pump 1</h3>");
// If the pump1_State is off, it displays the ON button
if (pump1_State=="off") {
client.println("<p><a href=\"/1/on\"><button class=\"button button2\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/1/off\"><button class=\"button\">ON </button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 4
client.println("<h3>pump 2</h3>");
// If the pump2_State is off, it displays the ON button
if (pump2_State=="off") {
client.println("<p><a href=\"/2/on\"><button class=\"button button2\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/2/off\"><button class=\"button \">ON </button></a></p>");
}
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
void disp_sensor_data()
{
if(sensorData.sensorNum == 1)
{
newhum = sensorData.hum;
newtemp = sensorData.tem;
newsoil = sensorData.soil;
}
if(sensorData.sensorNum == 2)
{
newhum2 = sensorData.hum;
newtemp2 = sensorData.tem;
newsoil2 = sensorData.soil;
}
}