//ESP 32_AJAX_Demo_210119 #include #include #include //SSID and Password of your WiFi router const char* ssid = "xxx"; // Identifiant WiFi const char* password = "xxx"; WebServer server(80); //Server on port 80 //PROGMEM to store webpage HTML data ASTUCIEUX! peut aussi être mis en .h const char MAIN_page[] PROGMEM = R"=====(

ESP 32 -- AJAX

Page automatically updated without having to Refresh the page

LEDState is : NA

)====="; //****************************Functions void handleRoot() { String s = MAIN_page; //Read HTML contents server.send(200, "text/html", s); //Send web page Une seule fois, noter html } void handleLED() { //Lit l'argument de LEDState passé ds la chaine GET String monEtat = "OFF"; //Sera la chaine envoyée au client String t_state = server.arg("LEDState"); //Lecture de l'argument de la chaine GET, 0 ou 1 Serial.println(t_state); //Sur le moniteur if(t_state == "1")monEtat = "ON"; //Feedback parameter else monEtat = "OFF"; //Feedback parameter server.send(200, "text/plain", monEtat); //Send led value only, noter plane et non html } //**************************************************** void setup(void){ Serial.begin(115200); WiFi.begin(ssid, password); //Connect to your WiFi router Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //IP address assigned to your ESP //******************Connected server.on("/", handleRoot); //Une seule fois au demarrage.Envoi la page Web server.on("/setLED", handleLED);// A chaque clic, envoi une seule valeur server.begin(); //Start server Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); //Handle client requests }