NodeMCU
NodeMCU bietet eine einfache und günstige Möglichkeit eine Arduino-ähnliche Umgebung mit integriertem WiFi-Zugang zu benutzen. Testweise habe ich eine Uhr gebaut, die die Uhrzeit über einen Time-Server bekommt und diese auf dem OLED darstellt. Die Zweit wird sekundenweise angezeigt, sie wird alle 5 Minuten mit dem Server synchronisiert. Hardware: NodeMCU-ESP8266 OLED-I2C-Modul Breadboard + Kabel Software: Die Arduino-IDE muss zuerst auf das Board NodeMCU umgestellt werden. Für den Zugruff auf den Time-Server habe ich die Bibliothek NTPClient verwendet. Das Display wird mit der Bibliothek Adafruit_SSD1306 angesteuert.
NTPClient:
#include <NTPClient.h> #include <ESP8266WiFi.h> #include <WiFiUdp.h> const char *ssid = "xxxxxxxxxx"; const char *password = "xxxxxxxxxxxxxxxxxx"; // time-server, offset (in s), update (in ms) WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 300000); void setup(){ Serial.begin(115200); WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } timeClient.begin(); } void loop() { timeClient.update(); Serial.println(timeClient.getFullFormattedTime()); delay(1000); }
OLED-Display:
Offenbar gibt es unterschiedliche Boards. Ich habe hier ein Board mit 30 PINs verwendet (ESP8266), neuere Boards haben 38 PINs (ESP-32S). Das Display wird folgendermaßen verkabelt:
NodeMCU (30 PIN) DISPLAY 3.3V (PIN 16) <---> VCC GND (PIN 17) <---> GND D1 (PIN 29) <---> SCL D2 (PIN 28) <---> SDA```
Software:
#include <SPI.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET LED_BUILTIN // 4 Adafruit_SSD1306 display(OLED_RESET); #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif // Array für Wochentagsnamen char *wochentage[] = {"Sonntag","Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}; // in setup(): display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Definition einer Anzeigefunktion (4-Zeilen) // obere Zeile (gelb): txt1 // darunter die Zeilen "", txt2, txt3 void drawText(String txt1, String txt2, String txt3) { display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(10,0); display.clearDisplay(); display.println(txt1); display.println(""); display.println(txt2); display.print(txt3); display.display(); } // Aufruf von drawText in loop() String wtag = wochentage[timeClient.getDay()]; String zeit = timeClient.getFormattedTime(); String datum = timeClient.getFormattedDate(); drawText(zeit,wtag,datum);