Last active
August 6, 2019 14:56
Au moyen d'une page web, on contrôle à distance le message affiché par un LCD branché à un ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************** | |
ESP8266 LCD WiFi | |
Au moyen d'une page web, on contrôle le message affiché par un LCD | |
branché à un ESP8266 ou un ESP32. | |
Instructions complètes: | |
http://electroniqueamateur.blogspot.com/2018/11/esp8266-et-afficheur-lcd-16-x-2.html | |
***********************************************************************/ | |
// inclusion des bibliothèques utiles | |
#if defined ARDUINO_ARCH_ESP8266 // s'il s'agit d'un ESP8266 | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#elif defined ARDUINO_ARCH_ESP32 // s'il s'agit d'un ESP32 | |
#include "WiFi.h" | |
#include <WebServer.h> | |
#endif | |
#include <WiFiClient.h> | |
#include <LiquidCrystal.h> | |
// modifiez ces deux constantes pour qu'elles contiennent les caractéristiques de | |
// votre réseau Wifi | |
#define ssid "**********" // le nom (SSID) de votre réseau WiFi | |
#define password "**********" // votre mot de passe WiFi | |
// définition des broches auxquelles on a branché l'afficheur LCD | |
const int pinRS = 4; // broche 4 (RS) de l'afficheur branchée à GPIO04 de l'ESP8266 | |
const int pinEnable = 5; // broche 6 (Enable) de l'afficheur branchée à GPIO05 de l'ESP8266 | |
const int pinD4 = 12; // broche 11 (D4) de l'afficheur branchée à GPIO12 de l'ESP8266 | |
const int pinD5 = 13; // broche 12 (D5) de l'afficheur branchée à GPIO13 de l'ESP8266 | |
const int pinD6 = 14; // broche 13 (D6) de l'afficheur branchée à GPIO14 de l'ESP8266 | |
const int pinD7 = 15; // broche 14 (D7) de l'afficheur branchée à GPIO15 de l'ESP8266 | |
// Initialisation de la bibliothèque LiquidCrystal en utilisant les broches définies ci-dessus: | |
LiquidCrystal lcd(pinRS, pinEnable, pinD4, pinD5, pinD6, pinD7); | |
#if defined ARDUINO_ARCH_ESP8266 // s'il s'agit d'un ESP8266 | |
ESP8266WebServer server(80); | |
#elif defined ARDUINO_ARCH_ESP32 // s'il s'agit d'un ESP32 | |
WebServer server(80); | |
#endif | |
// Affichage initial sur le LCD | |
String ligne1STR = "Bonjour!"; | |
String ligne2STR = ":)"; | |
/* La fonction construitPage retourne un string qui contient toute notre page web */ | |
String construitPage() { | |
String bouton1Str, bouton2Str; | |
String page = "<html lang=fr-FR><head>"; | |
page += "<title>ESP8266 Afficheur WiFi</title>"; | |
page += "<style> body { background-color: #fffff; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>"; | |
page += "</head><body><h1>ESP8266 Afficheur WiFi</h1>"; | |
page += "<form action='/' method='POST'>"; | |
page += "<h3>Écrivez ci-dessous le message à afficher:</h3>"; | |
page += "<p>Ligne 1:</p>"; | |
page += "<p><INPUT type='text' name='ligne1' value='" + ligne1STR + "'></p>"; | |
page += "<p>Ligne 2:</p>"; | |
page += "<p><INPUT type='text' name='ligne2' value='" + ligne2STR + "'></p>"; | |
page += "<INPUT type='submit' value='Appliquer'><br><br>"; | |
page += "</body></html>"; | |
return page; | |
} | |
/* La fonction gestionPage modifie les caractéristiques du moteur si le bouton | |
Appliquer a été cliqué. */ | |
void gestionPage() { | |
if ( server.hasArg("ligne1") ) { | |
ligne1STR = server.arg("ligne1"); | |
ligne2STR = server.arg("ligne2"); | |
// Affichage sur le moniteur série, pour débogage | |
Serial.print("Commande recue. Message: "); | |
Serial.println(ligne1STR); | |
Serial.println(ligne2STR); | |
// mise à jour de l'afficheur LCD | |
lcd.clear(); | |
lcd.print(ligne1STR); | |
lcd.setCursor(0, 1); // deuxième ligne | |
lcd.print(ligne2STR); | |
} | |
server.send ( 200, "text/html", construitPage() ); | |
} | |
void setup() { | |
lcd.begin(16, 2); | |
lcd.print(ligne1STR); | |
lcd.setCursor(0, 1); // deuxième ligne | |
lcd.print(ligne2STR); | |
// pour affichage dans le moniteur série (utile pour le débogage) | |
Serial.begin ( 115200 ); | |
// initialisation de la communication WiFi | |
WiFi.begin ( ssid, password ); | |
while ( WiFi.status() != WL_CONNECTED ) { | |
delay ( 500 ); Serial.print ( "." ); | |
} | |
Serial.println ( "" ); | |
Serial.print ( "Maintenant connecte a " ); | |
Serial.println ( ssid ); | |
Serial.print ( "Adresse IP: " ); | |
Serial.println ( WiFi.localIP() ); | |
// On indique le nom de la fonction qui gère l'interraction avec la page web | |
server.on ( "/", gestionPage ); | |
server.begin(); | |
Serial.println ( "Serveur HTTP en fonction" ); | |
} | |
void loop() { | |
server.handleClient(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment