Bonsoir,
voila, j'ai un souci avec un esp8266 equipé d’un oled 1306
https://fr.aliexpress.com/w/wholesale-esp8266-OLED.html?spm=a2g0o.home.search.0
pour le tester j'ai utilise le sketch suivant
// Configuration WiFi
const char* ssid = "yyyyyyyyyyyy";
const char* password = "xxxxxxxxxxxxx";
// Sketch "oled_toggle_web_buttons"
// Includes:
// - A larger clock display on the OLED.
// - A mobile-friendly web page with multiple toggle buttons for "On/Off" functionality.
// - A responsive design adapted for smartphones.
// - Real-time clock display (horodatage) updated every second on the web page.
// - OLED representation of button states with colored points (green/red).
// - Runtime counter displaying uptime since last reset.
// - OTA functionality for firmware updates.
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C
#define OLED_RESET 4 // GPIO4 corresponds to D2
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
// Web server
ESP8266WebServer server(80);
// NTP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);
// Button states
bool button1State = false;
bool button2State = false;
bool button3State = false;
bool button4State = false;
// Uptime counter
unsigned long bootTime;
// Version information
const char* version = "1.0.0";
// Function prototypes
void handleRoot();
void handleToggle1();
void handleToggle2();
void handleToggle3();
void handleToggle4();
void handleGetTime();
void displayTimeOnOLED();
void displayButtonStates();
void setup() {
// Initialize serial
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize OLED reset pin
pinMode(OLED_RESET, OUTPUT);
digitalWrite(OLED_RESET, HIGH);
delay(10);
digitalWrite(OLED_RESET, LOW);
delay(10);
digitalWrite(OLED_RESET, HIGH);
// Initialize OLED
if (!display.begin(OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
Serial.println(F("SSD1306 ok "));
// Start NTP client
timeClient.begin();
// Record boot time
bootTime = millis();
// Configure web server
server.on("/", handleRoot);
server.on("/toggle1", handleToggle1);
server.on("/toggle2", handleToggle2);
server.on("/toggle3", handleToggle3);
server.on("/toggle4", handleToggle4);
server.on("/getTime", handleGetTime);
server.begin();
Serial.println("Web server started");
// Initialize OTA
ArduinoOTA.begin();
Serial.println("OTA ready");
}
void loop() {
server.handleClient();
ArduinoOTA.handle();
timeClient.update();
// Display the time and button states on OLED
display.clearDisplay();
displayTimeOnOLED();
displayButtonStates();
display.display();
}
void handleRoot() {
unsigned long uptimeMillis = millis() - bootTime;
unsigned long uptimeSeconds = uptimeMillis / 1000;
unsigned long hours = uptimeSeconds / 3600;
unsigned long minutes = (uptimeSeconds % 3600) / 60;
unsigned long seconds = uptimeSeconds % 60;
String html = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
html += "<script>function updateTime(){fetch('/getTime').then(response => response.text()).then(time => {document.getElementById('time').innerText = time;})} setInterval(updateTime, 1000);</script></head><body style=\"font-family:Arial; text-align:center;\">";
html += "<h1>ESP8266 Web Interface</h1>";
html += "<h2 id='time'>Current Time: " + timeClient.getFormattedTime() + "</h2>";
html += "<h2>Uptime: " + String(hours) + "h " + String(minutes) + "m " + String(seconds) + "s</h2>";
html += "<div style=\"display:grid;grid-template-columns:repeat(2, 1fr);gap:10px;text-align:center;\">";
html += "<button onclick=\"fetch('/toggle1').then(()=>location.reload())\" style=\"background-color:";
html += (button1State ? "green" : "red");
html += ";color:white;\">Button 1</button>";
html += "<button onclick=\"fetch('/toggle2').then(()=>location.reload())\" style=\"background-color:";
html += (button2State ? "green" : "red");
html += ";color:white;\">Button 2</button>";
html += "<button onclick=\"fetch('/toggle3').then(()=>location.reload())\" style=\"background-color:";
html += (button3State ? "green" : "red");
html += ";color:white;\">Button 3</button>";
html += "<button onclick=\"fetch('/toggle4').then(()=>location.reload())\" style=\"background-color:";
html += (button4State ? "green" : "red");
html += ";color:white;\">Button 4</button>";
html += "</div>";
html += "<footer style=\"margin-top:20px;\">Version: " + String(version) + "</footer>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleToggle1() {
button1State = !button1State;
server.send(200, "text/plain", "OK");
}
void handleToggle2() {
button2State = !button2State;
server.send(200, "text/plain", "OK");
}
void handleToggle3() {
button3State = !button3State;
server.send(200, "text/plain", "OK");
}
void handleToggle4() {
button4State = !button4State;
server.send(200, "text/plain", "OK");
}
void handleGetTime() {
server.send(200, "text/plain", timeClient.getFormattedTime());
}
void displayTimeOnOLED() {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(timeClient.getFormattedTime());
// Display uptime
unsigned long uptimeMillis = millis() - bootTime;
unsigned long uptimeSeconds = uptimeMillis / 1000;
unsigned long hours = uptimeSeconds / 3600;
unsigned long minutes = (uptimeSeconds % 3600) / 60;
display.setTextSize(1);
display.setCursor(0, 20);
display.print("Uptime: ");
display.print(hours);
display.print("h ");
display.print(minutes);
display.print("m");
}
void displayButtonStates() {
// Draw button 1 state
display.fillCircle(20, 50, 5, button1State ? SSD1306_WHITE : SSD1306_BLACK);
display.drawCircle(20, 50, 5, SSD1306_WHITE);
// Draw button 2 state
display.fillCircle(50, 50, 5, button2State ? SSD1306_WHITE : SSD1306_BLACK);
display.drawCircle(50, 50, 5, SSD1306_WHITE);
// Draw button 3 state
display.fillCircle(80, 50, 5, button3State ? SSD1306_WHITE : SSD1306_BLACK);
display.drawCircle(80, 50, 5, SSD1306_WHITE);
// Draw button 4 state
display.fillCircle(110, 50, 5, button4State ? SSD1306_WHITE : SSD1306_BLACK);
display.drawCircle(110, 50, 5, SSD1306_WHITE);
}
tout se passe bien, mais.. après une raz électrique, ou raz avec le bouton rst , ou un nouveau sketch, l'oled n'affiche plus rien .. quelques fois, après plusieurs rst ca refonctionne.
l'esp continue a répondre a la page web, même avec un nouvel téléchargement .
j'ai tenté avec un sketch minimaliste pour tester l oled idem.
une idee? pas de problemes d'adresse, de faux contacts etc..
merci

