Carte ESP8266
- niko
- 6 févr. 2019
- 2 min de lecture

Code :
const char* ssid = "xxxx"; //Identifiant WiFi const char* password = "xxxx"; // Mot de passe WiFi
ESP8266WebServer server(80); // On instancie un serveur qui écoute sur le port 80
int led = D0; String etatD0="Off";
// FONCTION SERVANT A GENERER LA PAGE HTML (par l'envoi d'une chaîne de caractères)
String getPage(){ // LA PAGE S'AUTO-ACTUALISE String page="<html lang=fr-FR><head><meta http-equiv='refresh' content='10'/>"; // TITRE DE LA BARRE DU NAVIGATEUR page += "<title>ESP8266 Web Server demo - www.projetsdiy.fr</title>";
// STYLE DE LA PAGE page += "<style> body { background-color: #fffff; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>";
// TITRE DE LA PAGE page += "</head><body><h1>ESP8266 Web Server Demo</h1>"; // SOUS-TITRE H3 page += "<h3>GPIO</h3>"; // LE FORMULAIRE SERA ENVOYE AVEC UNE REQUETE DE TYPE POST page += "<form action='/' method='POST'>"; // PREMIERE LIGNE DE LA LISTE (ul) AVEC DO0 page += "<ul><li>D0 (etat: "; page += etatD0; page += ") ";
// BOUTON POUR ACTIVER D0 page += "<INPUT type='radio' name='D0' value='1'>ON"; // BOUTON POUR DESACTIVER D0 page += "<INPUT type='radio' name='D0' value='0'>OFF</li></ul>";
// BOUTON D'ACTUALISATION page += "<INPUT type='submit' value='Actualiser'>";
page += "<br><br><p><a href='https://www.projetsdiy.fr'>www.projetsdiy.fr</p>";
page += "</body></html>";
return page; }
void handleRoot(){ if (server.hasArg("D0")){ handleSubmit(); } else { server.send(200,"text/html", getPage()); } }
void handleSubmit(){ // Actualise le GPIO String LEDValue; LEDValue = server.arg("D0"); Serial.print("Set GPIO to "); Serial.println(LEDValue); if (LEDValue == "1"){ digitalWrite(led,1); etatD0 ="On"; server.send(200,"text/html",getPage()); } else if (LEDValue =="0") { digitalWrite(led,0); etatD0 ="Off"; server.send(200,"text/html",getPage()); } else { Serial.println("Err Led Value"); } }
void setup() { Serial.begin(115200); // On ouvre le port série à 115200 bauds WiFi.begin(ssid, password); // L'ESP8266 se connecte au réseau WiFi
Serial.println(""); // on attend d'etre connecte au WiFi avant de continuer while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // on affiche l'adresse IP attribuee pour le serveur DSN Serial.println(""); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // on definit les points d'entree (les URL a saisir dans le navigateur web) et on affiche un simple texte server.on("/", handleRoot); // on demarre le serveur web server.begin(); pinMode(led, OUTPUT); // Initialise la broche "led" comme une sortie - Initialize the "LED" pin as an output }
// Cette boucle s'exécute à l'infini - the loop function runs over and over again forever void loop() { //A chaque iteration, la fonction handleClient traite les requetes server.handleClient(); }
Liens utiles :
https://projetsdiy.fr/programmer-esp8266-ide-arduino-librairies-gpio-web-serveur-client/ [ premiers programmes avec ESP8266 ]
https://www.youtube.com/watch?v=drZQStOjpgI [si erreur : espcomm_upload_mem failed ]