Problemes affichage Avec Oled SSD1306

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

L'écran est bien alimenté en 5V ? Normalement il supporte aussi 3,3V mais c'est peut-être mieux en 5V.
As-tu essayé de l'alimenter non par l'esp8266 mais par une alimentation externe ?

Bonjour, bonnes fêtes de Noël à tous.
C'est un module hybride.. l olesd est soudé sur l'esp8266 pas de faux contacts, et pas possible de l'alimenter en 5v.
J'en ai 3 modules, tous le même souci... soit un problème de conception, soit un défaut de bibliothèque inadapté... j'ai contacté le vendeur... j'attends la réponse..

Bonsoir

Tu as testé avec la bibliothèque ug8 et l'exemple donnée en bas de cette page d'un des vendeurs ?
https://fr.aliexpress.com/item/1005005242283189.html

Dans ton code , j'ai peut être mal regardé mais je ne vois pas à quel endroit tu définies GPIO12 et 14 pour SDA et SCL, les 2 GPIOs choisis par le concepteur de la carte pour l'afficheur OLED

Salut al1fch,
D'après ce que j'ai lu, les pin de i2c sont par défaut.. effectivement, je ne suit pas posé de question car l'afficheur fonctionnait..
Là, je ne peux pas tester mais dès la semaine prochaine je ferai le test de la bibliothèque ug8.
Merci. Eg bonnes fêtes à tous

Oui, et bien si tu regardes les fichiers de définitions des cartes à base de 8266 tu vas voir que ce sont les broches 4 et 5 qui sont définies par défaut.

#define PIN_WIRE_SDA (4)
#define PIN_WIRE_SCL (5)

Donc, si ta carte utilise des broches différentes, il faut ouvrir un objet Wire avec les bonnes broches.

Salut al1fch !!!
Tu as Gagné c'était bien un problème de bibliothèque.. ca m'étonnait aussi que sur 3 spécimen le même soucis.. la ça a l'aire de fonctionner impeccable.. je regarderai plus en profondeur.. Merci en tout cas... ca m'arrange bien..

Pas vraiment. C'est que tu ne définissais pas les bonnes broches pour l'I2C.

même avis que fdufnews

le positionnement par défaut de SDA et SCL ne convient pas à ta carte , @Arzou

A la ligne 6 de l'exemple pour ug8 donné par le vendeur de ta carte le nécessaire est fait pour piloter l'afficheur par les bons GPIOs, différents de ceux souvent rencontrés sur les autres cartes avec des ESP8266
image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.