Deze ESP8266 is zeer handig: het display zit al aan boord, geen bord of draden nodig. Gewoon op USB aansluiten en programmeren.
Deze ESP8266 is zeer handig. Je hebt geen bord of draden nodig om het display te verbinden, het display is "aan boord". Gewoon op de USB van je PC aansluiten en je kan een WiFi-netwerkscanner of een klokje programmeren.
De SCL (Serial clock) van het display is reeds verbonden met D5 (GPIO14) van de ESP8266, en de SDA (Serial data) met D6 (GPIO12).
We gebruiken hier een andere bibliotheek voor het display, SSD1306.h. In de getoonde sketch wordt deze bibliotheek impliciet opgestart door de tweede lijn #include <SSD1306Wire.h>. Deze bibliotheek heeft enkele handige mogelijkheden, zoals automatisch centreren. Er zijn 3 fonts geïntegreerd in "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();
}
Een tweede versie, ditmaal voor de zwart-witte versie van AliExpress.
We hebben een optie toegevoegd aan de code: als de knop "flash" (naast de USB-aansluiting) drie seconden ingedrukt wordt, moeten de WiFi-gegevens gewist worden. Uiteraard gebruiken we geen "delay()". De seriële monitor werd weggelaten, en er werd een functie bijgezet om aan de gebruiker te tonen als er geen WiFi-verbinding ingesteld is.
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();
}
Benieuwd naar andere displays? Bekijk het volledige overzicht met o.a. TM1637, SSD1306, SH1106, ST7735, ST7789, e-paper en geïntegreerde displays zoals de Lilygo T-Display en de Waveshare ESP32-S3.