Mon code complet:
// #include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
// #include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#define SoilSensor1 A0
#define SoilSensorEnablePin1 6
#define SoilSensorEnablePin2 5
#define SoilSensorEnablePin3 4
#define SoilSensorEnablePin4 3
#define PumpPin 2
//Configure the soil moisture
const int MintSoilMoisture = 250;//Menth
const int ParsleySoilMoisture = 250;//Persil
const int BasilSoilMoisture = 250;//Basilic
const int CorianderSoilMoisture = 250;//Coriande, Cousbara
const int DillSoilMoisture = 250;//Aneth, shamir
const int SageSoilMoisture = 250;//Sauge, Marva
const int rosemarySoilMoisture = 250;//Romarin
const char *host = "serverweb1";
const char *wifi_ssid = "ASUS"; //replace by your value
const char *wifi_password = "Famillejacobi"; //replace by your value
WiFiServer server(80); //listen on port 80
//from now on the server is started and will serve your content!
const byte maxHTTPLine = 100;
char httpLine[maxHTTPLine + 1]; // +1 pour avoir la place du '\0'
const String CONFIG_HTML = "/html/config.html";
const String UPDATE_HTML = "/html/Update.html";
const String DISPLAY_DATA_HTML = "/html/display_data.html";
const char *configUsername = "admin";
const char *configPwd = "admin";
// String varname; // read data from the text box
String PlantSensor1,PlantSensor2,PlantSensor3,PlantSensor4; //Read from the Web Form
int NewPlantSensor1,NewPlantSensor2,NewPlantSensor3,NewPlantSensor4; //Convert string receive from web to int
int Web_Soil_Sensor1,Web_Soil_Sensor2,Web_Soil_Sensor3,Web_Soil_Sensor4; //Convert to the correct target Soil Sensor
int electric_tap_motor1=7,electric_tap_motor2=8,electric_tap_motor3=9,electric_tap_motor4=10; //pin to drive the motor's electric tap
/**
* racine du site.
*/
void handleRoot(){
server.send(200, "text/plain", "Welcome to my Irrigation System"); //Send web page
}
/**
* 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);
}
}
/**
* se connecte en client wifi.
*/
bool connectWifiClient()
{
WiFi.mode(WIFI_OFF);
delay(200);
Serial.println("Connecting to...");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
if(WiFi.waitForConnectResult() == WL_CONNECTED){
Serial.println("WiFi connected");
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
return true;
}
else{
Serial.println("WiFi Failed");
return false;
}
}
void initWebserver()
{
/* --- configure the root --- */
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
}
});
/* --- configure the update page --- */
server.on("/update", [&](){
if(!server.authenticate(configUsername, configPwd)){
return server.requestAuthentication();
} else {
handleUpdate(); // display the page
}
});
server.on("display_data", [&](){
if(!server.authenticate(configUsername, configPwd)){
return server.requestAuthentication();
} else {
DisplayData(); // display the page
}
});
/* --- After press update button --- */
/*server.on("/save", [&](){
handleSubmit(); // receive form's data from the form tag where action=save
}); */
/* --- After press submit button for sensor --- */
server.on("/postindex", [&](){
handleReadUserPlantSelected(); // receive form's data from the form tag where action=postindex
});
server.serveStatic("/css", SPIFFS, "/html/css");
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");
}