Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <WiFiClient.h>
- #include <ArduinoJson.h>
- #include <FS.h>
- #include <dampermotor.h>
- // const char *host = "serverweb1";
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- const char* CONFIG_HTML = "/html/config.html";
- const char* UPDATE_HTML = "/html/Update.html";
- const char* DISPLAY_DATA_HTML = "/html/display_data.html";
- const char* configUsername = "admin";
- const char* configPwd = "admin";
- ESP8266WebServer server(80);
- #define SoilSensor A0
- #define SoilSensorEnablePin1 D0
- #define SoilSensorEnablePin2 D1
- #define SoilSensorEnablePin3 D2
- #define SoilSensorEnablePin4 D3
- #define tap_solenoid_timeout 1500 // delay for 3 sec.
- #define PumpPin 2
- //Configure the soil moisture
- int MintSoilMoisture = 250;//Menth
- const int ParsleySoilMoisture = 230;//Persil
- const int BasilSoilMoisture = 260;//Basilic
- const int CorianderSoilMoisture = 300;//Coriande, Cousbara
- const int DillSoilMoisture = 2700;//Aneth, shamir
- const int SageSoilMoisture = 310;//Sauge, Marva
- const int rosemarySoilMoisture = 200;//Romarin
- int PlantSensor1,PlantSensor2,PlantSensor3,PlantSensor4; //User final target after converted from WebForm
- int read_sensor_value1,read_sensor_value2,read_sensor_value3,read_sensor_value4; //read data from sensor
- boolean electric_tap_motor1,electric_tap_motor2,electric_tap_motor3,electric_tap_motor4; //Off or On motor's electric tap
- const int onTime = 1000; // in ms
- const int offTime = 5000; // in ms
- boolean currentlyOn = false;
- unsigned long startTime;
- char WebTemp_itoa[10],*TargetTempChar;
- String XML;
- dampermotor tap_motor_sensor1(D4,D5,tap_solenoid_timeout); // define pin 4,5 to drive the tap_motor_sensor1
- dampermotor tap_motor_sensor2(D6,D7,tap_solenoid_timeout); // define pin 6,7 to drive the tap_motor_sensor1
- dampermotor tap_motor_sensor3(D8,D9,tap_solenoid_timeout); // define pin 8,9 to drive the tap_motor_sensor1
- //dampermotor tap_motor_sensor4(D10,11,tap_solenoid_timeout); // define pin 10,11 to drive the tap_motor_sensor1
- /**
- * racine du site.
- */
- void handleRoot() {
- server.send(200, "text/plain", "Welcome to my Irrigation System!");
- }
- /**
- * Affiche la page de configuration.
- */
- void handleConfig()
- {
- String form = "";
- File f = SPIFFS.open(CONFIG_HTML, "r");
- if (!f){
- Serial.println("Can't open config html file");
- server.send(404, "text/html", "File not found");
- }
- else{
- char buf[1024];
- int siz = f.size();
- while(siz > 0) {
- size_t len = std::min((int)(sizeof(buf) - 1), siz);
- f.read((uint8_t *)buf, len);
- buf[len] = 0;
- form += buf;
- siz -= sizeof(buf) - 1;
- }
- f.close();
- server.send(200, "text/html", form);
- }
- }
- /**
- * Affiche la page Update.
- */
- void handleUpdate()
- {
- String form = "";
- File f = SPIFFS.open(UPDATE_HTML, "r");
- if (!f){
- Serial.println("Can't open update html file");
- server.send(404, "text/html", "File not found");
- }
- else{
- char buf[1024];
- int siz = f.size();
- while(siz > 0) {
- size_t len = std::min((int)(sizeof(buf) - 1), siz);
- f.read((uint8_t *)buf, len);
- buf[len] = 0;
- form += buf;
- siz -= sizeof(buf) - 1;
- }
- f.close();
- server.send(200, "text/html", form);
- }
- }
- /**
- * Affiche la page display data.
- */
- void DisplayData()
- {
- String form = "";
- File f = SPIFFS.open(DISPLAY_DATA_HTML, "r");
- if (!f){
- Serial.println("Can't open update html file");
- server.send(404, "text/html", "File not found");
- }
- else{
- char buf[1024];
- int siz = f.size();
- while(siz > 0) {
- size_t len = std::min((int)(sizeof(buf) - 1), siz);
- f.read((uint8_t *)buf, len);
- buf[len] = 0;
- form += buf;
- siz -= sizeof(buf) - 1;
- }
- f.close();
- server.send(200, "text/html", form);
- }
- }
- void handleXML(){
- XML="<?xml version='1.0'?>";
- XML+="<xml>";
- XML+="<sens1>";
- XML+= read_sensor_value1;
- XML+="</sens1>";
- XML+="<sens2>";
- XML+= read_sensor_value2;
- XML+="</sens2>";
- XML+="<sens3>";
- XML+= read_sensor_value3;
- XML+="</sens3>";
- XML+="<sens4>";
- XML+= read_sensor_value4;
- XML+="</sens4>";
- XML+="</xml>";
- server.send(200,"text/xml",XML);
- }
- /*void upgrade()
- {
- // update the file on SPIFFS
- }*/
- void handleReadUserPlantSelected(){
- // Read Value from the User selected from the Html WebForm to String
- PlantSensor1 = server.arg("sensor1").toInt();
- PlantSensor2 = server.arg("sensor2").toInt();
- PlantSensor3 = server.arg("sensor3").toInt();
- PlantSensor4 = server.arg("sensor4").toInt();
- /*
- Serial.println("Before Convert");
- Serial.println(PlantSensor1);
- Serial.println(PlantSensor2);
- Serial.println(PlantSensor3);
- Serial.println(PlantSensor4);
- */
- // Assign the correct Soil Target
- PlantSensor1 = SoilSensorWebUser(PlantSensor1);
- PlantSensor2 = SoilSensorWebUser(PlantSensor2);
- PlantSensor3 = SoilSensorWebUser(PlantSensor3);
- PlantSensor4 = SoilSensorWebUser(PlantSensor4);
- /*
- Serial.println("The Final Target Soil After switch case");
- Serial.println(PlantSensor1);
- Serial.println(PlantSensor2);
- Serial.println(PlantSensor3);
- Serial.println(PlantSensor4);
- */
- server.sendHeader("Location","/config");
- server.send(303);
- if (!saveConfig())
- {Serial.println("Failed to save config");}
- else
- {Serial.println("Config saved");}
- }
- void initWebserver()
- {
- server.on("/", handleRoot);
- /* --- configure the config page, main page --- */
- /*server.on("/config", [](){
- if(!server.authenticate(configUsername, configPwd)){
- return server.requestAuthentication();
- }else{
- handleConfig(); // display the page
- }
- });*/
- server.on("/config",handleConfig);
- /* --- configure the update page --- */
- /*server.on("/update", [](){
- if(!server.authenticate(configUsername, configPwd)){
- return server.requestAuthentication();
- } else {
- handleUpdate(); // display the page
- }
- });*/
- server.on("/update",handleUpdate);
- /* --- After press update on system page run upgrade function --- */
- //server.on("/save", upgrade);
- /* --- After press submit button for sensor --- */
- /*server.on("/postindex", [](){
- handleReadUserPlantSelected(); // receive form's data from the form tag where action=postindex
- });*/
- server.on("/postindex",handleReadUserPlantSelected);
- /* --- After press display data on Menu run DisplayData()--- */
- /*server.on("/display_data", [](){
- if(!server.authenticate(configUsername, configPwd)){
- return server.requestAuthentication();
- } else {
- DisplayData(); // display the page
- }
- });*/
- server.on("/display_data",DisplayData);
- server.on("/reqEtatVariables",handleXML);
- server.serveStatic("/css", SPIFFS, "/html/css");
- server.serveStatic("/config.html", SPIFFS, "/html/config.html");
- server.serveStatic("/Update.html", SPIFFS, "/html/Update.html");
- server.serveStatic("/display_data.html", SPIFFS, "/html/display_data.html");
- /*server.serveStatic("/images/1.png", SPIFFS, "/html/images/1.png");
- server.serveStatic("/images/2.png", SPIFFS, "/html/images/2.png");
- server.serveStatic("/images/3.png", SPIFFS, "/html/images/3.png");
- server.serveStatic("/images/4.png", SPIFFS, "/html/images/4.png");
- server.serveStatic("/images/5.png", SPIFFS, "/html/images/5.png");
- server.serveStatic("/images/bg.png", SPIFFS, "/html/images/bg.png");*/
- }
- /**
- * Assign the correct Soil Target for the Plant Sensor connected after user choice from the WebForm
- */
- int SoilSensorWebUser(int WebSoilSensor)
- {
- switch(WebSoilSensor)
- {
- case 0:
- return(0);
- break;
- case 1:
- return(MintSoilMoisture);
- break;
- case 2:
- return(ParsleySoilMoisture);
- break;
- case 3:
- return(BasilSoilMoisture);
- break;
- case 4:
- return(CorianderSoilMoisture);
- break;
- case 5:
- return(DillSoilMoisture);
- break;
- case 6:
- return(SageSoilMoisture);
- break;
- case 7:
- return(rosemarySoilMoisture);
- break;
- }
- }
- /**
- * Read Datas from all Sensors
- */
- int read_Sensors_Values(int EnableSoilPin)
- {
- int ReadSensor;
- digitalWrite(EnableSoilPin,HIGH);
- for(int SensorReading = 0;SensorReading < 5;SensorReading++)
- {
- ReadSensor = analogRead(SoilSensor);
- }
- digitalWrite(EnableSoilPin,LOW);
- return ReadSensor;
- }
- void WaterController()
- {
- if(read_sensor_value1 < PlantSensor1)
- {
- tap_motor_sensor1.open_damper(); //turn one side solenoid with motortimeout delay
- electric_tap_motor1 = HIGH ;
- if(digitalRead(PumpPin) == LOW);
- {
- digitalWrite(PumpPin,HIGH);
- }
- }
- else
- {
- tap_motor_sensor1.close_damper(); //turn other side solenoid with motortimeout delay
- electric_tap_motor1 = LOW ;
- }
- if(read_sensor_value2 < PlantSensor2)
- {
- tap_motor_sensor2.open_damper(); //turn one side solenoid with motortimeout delay
- electric_tap_motor2 = HIGH ;
- if(digitalRead(PumpPin) == LOW)
- {
- digitalWrite(PumpPin,HIGH);
- }
- }
- else
- {
- tap_motor_sensor2.close_damper(); //turn other side solenoid with motortimeout delay
- electric_tap_motor2 = LOW ;
- }
- if(read_sensor_value3 < PlantSensor3)
- {
- tap_motor_sensor3.open_damper(); //turn one side solenoid with motortimeout delay
- electric_tap_motor3 = HIGH ;
- if(digitalRead(PumpPin) == LOW)
- {
- digitalWrite(PumpPin,HIGH);
- }
- }
- else
- {
- tap_motor_sensor3.close_damper(); //turn other side solenoid with motortimeout delay
- electric_tap_motor3 = LOW ;
- }
- if(read_sensor_value4 < PlantSensor4)
- {
- //tap_motor_sensor4.open_damper(); //turn one side solenoid with motortimeout delay
- electric_tap_motor4 = HIGH ;
- if(digitalRead(PumpPin) == LOW)
- {
- digitalWrite(PumpPin,HIGH);
- }
- }
- else
- {
- // tap_motor_sensor4.close_damper(); //turn other side solenoid with motortimeout delay
- electric_tap_motor4 = LOW ;
- }
- // check if all Tap is OFF then turn OFF the Pump
- if(electric_tap_motor1 == LOW && electric_tap_motor2 == LOW && electric_tap_motor3 == LOW && electric_tap_motor4 == LOW)
- {
- digitalWrite(PumpPin,LOW);
- }
- }
- bool loadConfig()
- {
- File configFile = SPIFFS.open("/config.json", "r");
- if (!configFile)
- {
- Serial.println("Failed to open config file");
- return false;
- }
- size_t size = configFile.size();
- if (size > 1024)
- {
- Serial.println("Config file size is too large");
- return false;
- }
- // Allocate a buffer to store contents of the file.
- std::unique_ptr<char[]> buf(new char[size]);
- // We don't use String here because ArduinoJson library requires the input
- // buffer to be mutable. If you don't use ArduinoJson, you may as well
- // use configFile.readString instead.
- configFile.readBytes(buf.get(), size);
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& json = jsonBuffer.parseObject(buf.get());
- if (!json.success())
- {
- Serial.println("Failed to parse config file");
- return false;
- }
- *TargetTempChar = json["TargetTempChar"];
- // Real world application would store these values in some variables for
- // later use.
- Serial.print("Loaded TargetTempChar: ");
- Serial.println(TargetTempChar);
- MintSoilMoisture = *TargetTempChar;
- Serial.print("The MintSoilMoisture: ");
- Serial.println(MintSoilMoisture);
- return true;
- }
- bool saveConfig()
- {
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- itoa(MintSoilMoisture,WebTemp_itoa,10);
- json["TargetTempChar"] = WebTemp_itoa;
- File configFile = SPIFFS.open("/config.json", "w");
- if (!configFile)
- {
- Serial.println("Failed to open config file for writing");
- return false;
- }
- json.printTo(configFile);
- return true;
- }
- void setup(){
- Serial.begin(115200);
- pinMode(A0, INPUT);
- pinMode(PumpPin, INPUT);
- pinMode(SoilSensorEnablePin1, OUTPUT);
- pinMode(SoilSensorEnablePin2, OUTPUT);
- pinMode(SoilSensorEnablePin3, OUTPUT);
- pinMode(SoilSensorEnablePin4, OUTPUT);
- Serial.println("...");
- Serial.println("Mounting FS...");
- if (!SPIFFS.begin()) {
- Serial.println("Failed to mount file system");
- return;
- }
- else
- {Serial.println("Mounting FS Successfully");}
- // Read Data From SPIFFS File or EEPROM
- /*if (!loadConfig())
- {Serial.println("Failed to load config");}
- else
- {Serial.println("Config loaded");}*/
- WiFi.begin(ssid, password);
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("TCP server started");
- /*if (!MDNS.begin(host)){
- Serial.println("Error setting up MDNS responder!");
- while(1){
- delay(1000);
- }
- }*/
- //Serial.println("mDNS responder started");
- initWebserver();
- // Start TCP (HTTP) server
- server.begin();
- // Add service to MDNS-SD
- //MDNS.addService("http", "tcp", 80);
- startTime=millis(); // Initialisation
- }
- void loop()
- {
- server.handleClient();
- // --- read sensors value Every 5sec for 1sec --- //
- if (currentlyOn && millis()>startTime+onTime) // Switch resistor off
- {
- currentlyOn = false;
- startTime=millis(); // Reset timer
- }
- if (!currentlyOn && millis()>startTime+offTime) // Switch resistor on
- {
- read_sensor_value1 = read_Sensors_Values(SoilSensorEnablePin1);
- read_sensor_value2 = read_Sensors_Values(SoilSensorEnablePin2);
- read_sensor_value3 = read_Sensors_Values(SoilSensorEnablePin3);
- read_sensor_value4 = read_Sensors_Values(SoilSensorEnablePin4);
- currentlyOn = true;
- startTime=millis(); // Reset timer
- }
- // --- drive motor for stop after running --- //
- tap_motor_sensor1.damper_motor_driver();
- tap_motor_sensor2.damper_motor_driver();
- tap_motor_sensor3.damper_motor_driver();
- //tap_motor_sensor4.damper_motor_driver();
- // --- water the plant turn on/off tap and pump --- //
- //WaterController();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement