This ESP8266 is very convenient: the display is already on board, no breadboard or wires needed. Just plug it into USB and start programming.
This ESP8266 is very convenient. You don't need a breadboard or wires to connect the display, the display is "on board". Just plug it into your PC's USB and you can program a WiFi network scanner or a clock.
The SCL (serial clock) of the display is already connected to D5 (GPIO14) of the ESP8266, and the SDA (serial data) to D6 (GPIO12).
Here a different library is used for the display, SSD1306.h. In the sketch shown, this library is implicitly started by the second line #include <SSD1306Wire.h>. This library has some useful features, such as automatic centering. There are 3 fonts integrated into the library "SSD1306.h".
Sketch:
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager/
#include <SSD1306Wire.h> // https://github.com/ThingPulse/esp8266-oled-ssd1306
WiFiManager myWiFi;
SSD1306Wire display(0x3c, D6, D5); // ADDRESS, SDA, SCL
struct tm tInfo; // https://cplusplus.com/reference/ctime/tm/
char days[7][10] = { "zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag" };
void setup() {
Serial.begin(115200); // watch serial monitor in case of new WiFi-connection
myWiFi.autoConnect("SSD1306_ESP"); // connect ESP to new wifi via GSM or PC if new WiFi-connection
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "pool.ntp.org");
display.init();
display.setContrast(255);
display.setTextAlignment(TEXT_ALIGN_CENTER);
}
void loop() {
char hourMin[6], second[3], theDate[11];
getLocalTime(&tInfo); // sntp sync at startup & every hour from then on
strftime(hourMin, sizeof(hourMin), "%R", &tInfo); // https://cplusplus.com/reference/ctime/strftime
strftime(second, sizeof(second), "%S", &tInfo);
strftime(theDate, sizeof(theDate), "%d-%m-%G", &tInfo);
display.clear();
display.setFont(ArialMT_Plain_24); // display hour:minute in a larger font
display.drawString(54, 0, hourMin);
display.setFont(ArialMT_Plain_16);
display.drawString(95, 7, second);
display.drawString(64, 26, theDate);
display.drawString(64, 45, days[tInfo.tm_wday]);
display.display();
}
A second version, this time for the black and white version from AliExpress.
We added an extra option to the code: if the "flash" button (next to the USB connector) is pressed for three seconds, the WiFi data should be erased. Of course, we do not use "delay()". The serial monitor was omitted, and a function was added to show the user if no WiFi connection is set up.
Sketch:
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager/
#include <SSD1306Wire.h> // https://github.com/ThingPulse/esp8266-oled-ssd1306
WiFiManager myWiFi;
SSD1306Wire display(0x3c, D1, D2); // ADDRESS, SDA, SCL (D1, D2)
struct tm tInfo; // https://cplusplus.com/reference/ctime/tm/
char days[7][10] = { "zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag" };
unsigned long startC;
bool pressed = false;
void setup() {
display.init();
display.setContrast(255);
display.setTextAlignment(TEXT_ALIGN_CENTER);
myWiFi.setAPCallback(messageNoConnection); // is called when no wifi was set up yet
myWiFi.autoConnect("DUINO"); // connect ESP to new wifi via GSM or PC if new WiFi-connection
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "pool.ntp.org");
}
void loop() {
char hourMin[6], second[3], theDate[11];
getLocalTime(&tInfo); // sntp sync at startup & every hour from then on
strftime(hourMin, sizeof(hourMin), "%R", &tInfo); // https://cplusplus.com/reference/ctime/strftime
strftime(second, sizeof(second), "%S", &tInfo);
strftime(theDate, sizeof(theDate), "%d-%m-%G", &tInfo);
display.clear();
display.setFont(ArialMT_Plain_24); // display hour:minute in a larger font
display.drawString(54, 0, hourMin);
display.setFont(ArialMT_Plain_16);
display.drawString(95, 7, second);
display.drawString(64, 26, theDate);
display.drawString(64, 45, days[tInfo.tm_wday]);
display.display();
buttonFlashPressed_3_Sec();
}
void buttonFlashPressed_3_Sec() { // flash button pressed during 3 seconds: erase WiFi credentials
if (!digitalRead(0) && (pressed) && (millis() - startC > 3000)) myWiFi.resetSettings(), ESP.restart();
if (!digitalRead(0)) {
if (!pressed) startC = millis(); // first time the loop encounters the pressed button: start counter
pressed = true; // Indicate that the button was previously pressed
} else pressed = false; // button released before the 3 seconds expired.
}
void messageNoConnection(WiFiManager* myWiFi) {
display.setFont(ArialMT_Plain_10);
display.clear();
display.drawString(64, 0, "WiFi: no connection.");
display.drawString(64, 10, "Connect to hotspot DUINO");
display.drawString(64, 20, "and open a browser at");
display.drawString(64, 30, "address 192.168.4.1");
display.drawString(64, 40, "to enter network name");
display.drawString(64, 50, "and password");
display.display();
}
Curious about other displays? Check out the full overview, including TM1637, SSD1306, SH1106, ST7735, ST7789, e-paper and integrated displays like the Lilygo T-Display and the Waveshare ESP32-S3.