salman_code

mail@pastecode.io avatar
unknown
abap
3 years ago
4.8 kB
9
Indexable
 #include <ESP8266WiFi.h>
 #include "DHT.h"        // DHT11 temperature and humidity sensor Predefined library
 
#define DHTTYPE DHT11   // DHT 11
#define dht_dpin D5     //GPIO-0 D3 pin of nodemcu
#define rainDigital D0
int Raw       = A0;      //Analog channel A0 as used to measure temperature
int threshold = D1;      //Nodemcu digital pin water sensor read-GPIO16---D0 of NodeMCU
int Solenoid = D7;       // GPIO13---D7 of NodeMCU--Motor connection

const char* ssid = "Tenda_68A6C8";
const char* password = "awse2313";


DHT dht(dht_dpin, DHTTYPE); 
WiFiServer server(80);

void setup(void)
{ 
  dht.begin();
  Serial.begin(9600);
  delay(10);
  pinMode(rainDigital,INPUT);
  pinMode(threshold,INPUT_PULLUP); //Pin#13 as output-Activate pullup at pin 13
  pinMode(Solenoid, OUTPUT);       //D7 as output
  digitalWrite(Solenoid, LOW);     //Deactivate Solenoid
  // Connect to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);     //Begin WiFi
  
 while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address on serial monitor
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");    //URL IP to be typed in mobile/desktop browser
  Serial.print(WiFi.localIP());
  Serial.println("/");
}



void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  } 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  int rainDigitalVal = digitalRead(rainDigital);

 float h =0.0;  //Humidity level
 float t =0.0;  //Temperature in celcius 
 float f =0.0;  //Temperature in fahrenheit
 float percentage = 0.0; // Calculating percentage of moisture
 float reading    = 0.0; //Analog channel moisture read
 
  // Match the request
  int value = LOW;
  if (request.indexOf("/Up=ON") != -1)  {
     h = dht.readHumidity();    //Read humidity level
     t = dht.readTemperature(); //Read temperature in celcius
     f = (h * 1.8) + 32;        //Temperature converted to Fahrenheit
     reading = analogRead(Raw); //Analog pin reading output voltage by water moisture rain sensor
     percentage = (reading/1024) * 100;   //Converting the raw value in percentage

    if (reading<=110){  // If less mositure in soil start the motor otherwise stop
    digitalWrite(Solenoid, HIGH);
    value = HIGH;
    }
    else {
    digitalWrite(Solenoid, LOW);
    value = LOW;
    }
    
  }

  if (request.indexOf("/Solenoid=ON") != -1)  {   //Motor ON
    digitalWrite(Solenoid, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/Solenoid=OFF") != -1)  {  //Motor OFF
    digitalWrite(Solenoid, LOW);
    value = LOW;
  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<h1 align=center>Smart Erigation System</h1><br><br>");
  client.print("Temperature in Celsius =");
  client.println(t);
  client.println("<br>");
  client.print("Temperature in Fahrenheit =");
  client.println(f);
  client.println("<br>");
  client.print("Humidity =");
  client.println(h);
  client.print(" %");   
  client.println("<br>");  
  client.println();
  client.print("Moisture Level Percentage =");
  client.print(percentage);
  client.print("%");
 
  if(digitalRead(threshold)==HIGH){ // Read digital output of soil sensor
  client.println("Threshold Reached = Rain detected / Moisture exceeded / Water detected");
  }
  
  client.println("<br><br>");
  if(value == HIGH) 
    client.println("Motor/Pump Operational");
   else 
    client.print("Motor/Pump at Halt");
  client.println("<br>");
  if(digitalRead(rainDigital)==LOW){ // Read digital output of soil sensor
  client.println("Raining>>>>>>>>>>>>>>>>>>>");}
   else 
    client.print("Not Raining");
  
  client.println("<br><br>");
  client.println("<a href=\"/Up=ON\"\"><button>Update = Temperature  Humidity Moisture Values</button></a><br />"); 
  client.println("<a href=\"/Solenoid=ON\"\"><button>Motor Pump On </button></a>");
  client.println("<a href=\"/Solenoid=OFF\"\"><button>Motor Pump Off </button></a><br />"); 
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}