Table des matières

LCD status

Note d'intention

Il s'agit simplement d'un afficheur LCD branché sur un esp. Un formulaire en php, logé sur un ftp. permet de mettre a jour le texte affiché.

Ainsi avec son telephone sur internet on peut mettre a jour l'ecran à distance.

LCD status

Notes techniques et matériaux

Code pour l'esp :

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
 
// -------------------------------------------------------
// WiFi credentials
// -------------------------------------------------------
const char* ssid = "ESDAMM-Pedagogie";
const char* password = "P3daGog!e";
 
// -------------------------------------------------------
// Grove RGB LCD I2C addresses
// -------------------------------------------------------
#define LCD_TEXT_ADDR 0x3E
#define LCD_RGB_ADDR  0x62
 
// ESP32 I2C pins
#define SDA_PIN 21
#define SCL_PIN 22
 
// -------------------------------------------------------
// LCD low-level functions
// -------------------------------------------------------
void lcdSendCommand(uint8_t cmd) {
  Wire.beginTransmission(LCD_TEXT_ADDR);
  Wire.write(0x80);
  Wire.write(cmd);
  Wire.endTransmission();
}
 
void lcdSendData(uint8_t data) {
  Wire.beginTransmission(LCD_TEXT_ADDR);
  Wire.write(0x40);
  Wire.write(data);
  Wire.endTransmission();
}
 
void i2cWriteRegister(uint8_t addr, uint8_t reg, uint8_t val) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.write(val);
  Wire.endTransmission();
}
 
void setRGB(uint8_t r, uint8_t g, uint8_t b) {
  i2cWriteRegister(LCD_RGB_ADDR, 0x04, r);
  i2cWriteRegister(LCD_RGB_ADDR, 0x03, g);
  i2cWriteRegister(LCD_RGB_ADDR, 0x02, b);
}
 
void lcdSetCursor(uint8_t col, uint8_t row) {
  uint8_t pos = (row == 0) ? 0x00 : 0x40;
  lcdSendCommand(0x80 | (pos + col));
}
 
void lcdInit() {
  lcdSendCommand(0x38);
  lcdSendCommand(0x39);
  lcdSendCommand(0x14);
  lcdSendCommand(0x70);
  lcdSendCommand(0x56);
  lcdSendCommand(0x6C);
  delay(200);
  lcdSendCommand(0x38);
  lcdSendCommand(0x0C);
  lcdSendCommand(0x01);
  lcdSendCommand(0x06);
 
  // Init RGB
  i2cWriteRegister(LCD_RGB_ADDR, 0x00, 0x00);
  i2cWriteRegister(LCD_RGB_ADDR, 0x01, 0x00);
  i2cWriteRegister(LCD_RGB_ADDR, 0x08, 0xAA);
 
  setRGB(255, 255, 255);
}
 
void lcdPrint(const String &msg) {
  lcdSendCommand(0x01);  // clear
  delay(2);
 
  uint8_t col = 0, row = 0;
  lcdSetCursor(0, 0);
 
  for (int i = 0; i < msg.length(); i++) {
    if (msg[i] == '\n') {
      row = 1;
      col = 0;
      lcdSetCursor(col, row);
      continue;
    }
    lcdSendData(msg[i]);
    col++;
    if (col >= 16) {
      if (row == 0) {
        row = 1;
        col = 0;
        lcdSetCursor(col, row);
      } else {
        break;
      }
    }
  }
}
 
// -------------------------------------------------------
// WiFi + HTTP
// -------------------------------------------------------
String downloadText() {
  HTTPClient http;
  http.begin("https://wikibam.com/XP/fichier.txt");
 
  int code = http.GET();
  if (code != 200) {
    Serial.println("Erreur HTTP : " + String(code));
    http.end();
    return "HTTP error";
  }
 
  String payload = http.getString();
  http.end();
  return payload;
}
 
// -------------------------------------------------------
void setup() {
  Serial.begin(115200);
  delay(200);
 
  Wire.begin(SDA_PIN, SCL_PIN);
  lcdInit();
  setRGB(0, 150, 255);
 
  lcdPrint("Connexion\nWiFi...");
  Serial.println("Connexion WiFi...");
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
    Serial.print(".");
  }
  Serial.println("\nWiFi OK !");
  lcdPrint("WiFi OK!\nLecture...");
}
 
// -------------------------------------------------------
void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    String txt = downloadText();
    Serial.println("Mis à jour:");
    Serial.println(txt);
 
    lcdPrint(txt);
  } else {
    lcdPrint("WiFi perdu!\nReconnexion");
    WiFi.begin(ssid, password);
  }
 
  delay(5000); // <<< mise à jour toutes les 5 secondes
}

Coté FTP-php

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Formulaire de mise à jour</title>
    <style>
        body {
            background-color: black;
            color: white;
            font-family: Arial, sans-serif;
            font-size: 18px;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: lightgray;
        }
 
		h5 {
            color: #666;
			font-size: 11px;
        }
        form {
            background-color: #222;
            padding: 20px;
			width: 60%;
            border-radius: 5px;
            margin-top: 20px;
        }
        label {
            display: block;
            margin-bottom: 10px;
        }
        input[type="text"] {
            width: 80%;
            padding: 10px;
            font-size: 16px;
            margin-bottom: 20px;
            border: 1px solid gray;
            border-radius: 4px;
            background-color: #333;
            color: white;
        }
        input[type="submit"] {
            background-color: gray;
            color: white;
			font-size: 20px;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: tomato;
			color:black;
        }
    </style>
</head>
<body>
    <h1>&#9851; Status </h1>
    <form action="" method="post">
        <label for="texte">Entrez votre texte (max 60 caractères) :</label>
        <input type="text" id="texte" name="texte" maxlength="60" required>
        <input type="submit" value="Envoyer">
    </form>
 
    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $texte = htmlspecialchars($_POST['texte']);
 
        // Effacer le contenu du fichier et y écrire le nouveau texte
        file_put_contents('fichier.txt', $texte);
 
        echo "<h5> :) Le fichier a été mis à jour avec succès.</h5>";
    }
    ?>
</body>
</html>

Photos et médias

Code pour afficher les images du projet :