Das siebte Türchen

Today is a Microcontroller behind the little door, which not only allows you to read out various sensors and control actuators, but also to establish a WLAN connection. It is the ESP8266 and three of them.

The Breakout Board has, beside the processor with 4MByte flash memory, a CH340 USB to serial converter, so you can easily program the module. Most of the I/O pins are led out. There is also a voltage regulator which provides the 3.3 V supply voltage for the ESP8266 from the USB supply voltage or the voltage at the Vin connector. Here is an important note. Since ESP8266 works with 3.3 V, no voltages higher than 3.3 V must be applied to its inputs!


As the cover picture shows, we want one of the controllers together with one LCD display, as it was introduced in the Advent calendar on December 4th. , to build a clock with a time and date display. The special thing about it is that the time comes from a highly accurate time server on the Internet.

At first a little soldering work is necessary. The I2C adapter should be soldered to the display. I recommend to mount the I2C adapter on the bottom side as shown below. Then you can place it in a case.


The wiring is simply GND and Vcc of the display is connected to G and VU of the controller. The clock line SCL goes to D1 and the data line SDA to D2.

Now it's time to start programming with the Arduino IDE. If support for boards with ESP8266 MCU is not yet installed, we have to do it now. In the Arduino IDE, we go to Preferences via the File menu or simply press "Ctrl+;".

In the form we click on the symbol to the right of the input field under the point “Additional board administrator URLs”. Here we can now enter the URL for the ESP8266 board manager:

http://arduino.esp8266.com/stable/package_esp8266com_index.json


After saving presets, we call the board manager via the menu Tools->Board:->Board manager. As search term we enter "esp8266". We get the following entry, which we install.


Now we can select a board in the Tools-> Board: -> ESP8266 Boards menu. We use NodeMCU 1.0….


Everything is now ready to install the sketch for the clock.

#include <ESP8266WiFi.h> //WLAN Unterstützung
#include <TZ.h> //Zeitzonen Konstanten
//Bibliothek für das LCD Display
#include <LiquidCrystal_I2C.h>
//SSID und PSK vom lokalen WLAN
#define STASSID "****************"
#define STAPSK "****************" //Zeitzone mit Sommer/Winterzeit Information
#define MYTZ TZ_Europe_Berlin

// LCD Adresse = 0x27, Anzahl Zeichen 16 Anzahl Zeilen 2
LiquidCrystal_I2C lcd(0x27,16,2); //Definition der deutschen Wochentags und Monatsnamen als Abkürzung
char weekdays[7][4] = {"Son","Mon","Die","Mit","Don","Fre","Sam"};
//März wird wegen des Umlauts zuerst mit Marz initialisier
//und im Setup auf März geändert
char months[12][6] = {"Jan.","Feb.","Marz","Apr.","Mai","Juni","Juli","Aug.","Sep.", "Okt.","Nov.","Dez."};
//Anzeige der aktuellen Zeit am Display
void showTime() {
time_t now; //aktuelle Zeit
struct tm * s_time; //Zeitstruktur aufgesplittet in Jahr, Monat, Tag, Stunden, Minuten und Sekunden
char line_buffer[20]; //Buffer für Anzeigenzeile
now = time(nullptr); //aktuelle Zeit lesen
s_time = localtime(&now); //für die lokale Zeit in die Zeitstruktur umwandeln
//Zeilenbuffer mit Datum füllen
sprintf(line_buffer,"%s %i.%s %i ",weekdays[s_time->tm_wday],s_time->tm_mday,months[s_time->tm_mon],s_time->tm_year+1900);
//Datum auf die serielle Schnittstelle ausgeben
Serial.println(line_buffer);
//Datum am Display in Zeile 1 anzeigen
lcd.setCursor(0,0);
lcd.print(line_buffer);
//Zeilenbuffer mit Uhrzeit füllen
strftime(line_buffer,16,"%H:%M:%S Uhr",s_time);
//Uhrzeit auf die serielle Schnittstelle ausgeben
Serial.println(line_buffer);
//Uhrzeit am Display in Zeile 2 anzeigen
lcd.setCursor(0,1);
lcd.print(line_buffer);
} //Setup Funktion void setup() { Serial.begin(115200); //Serielle Schnittstelle initialisieren
months[2][1]=0xE1; //Monat März korrigieren  // Initialize the display and turn on the lighting  lcd.init();  lcd.backlight(); //Interne Uhr konfigurieren  configTime(MYTZ, "pool.ntp.org"); //WLAN initialisieren und mit dem WLAN verbinden  WiFi.persistent(false);  WiFi.mode(WIFI_STA);  WiFi.begin(STASSID, STAPSK); } //Hauptschleife
void loop() {
//Zeit anzeigen
showTime();
//1 Sekunde warten
delay(1000);
}

Download the sketch.

Almost everything we need is included in the ESP8266 package. Only the library for the LCD display is missing. It can be installed via the library management in the Arduino IDE.

We enter "I2C LCD" as search term and find, a little further down the list, the library we want.


Before we compile the sketch, we have to enter the access data for the WLAN instead of the asterisks.

Don't be surprised if the following warning is displayed during compilation.

WARNING: Library LiquidCrystal_I2C claims to run on avr architecture(s) and may be incompatible with your current board running on esp8266 architecture(s).

This warning is caused by an incorrect entry in the library specifications. The library works correctly with ESP8266 MCSs.

The special thing about this clock is that it is regularly synchronized from the Internet. It also switches automatically between summer and winter time.

Have fun rebuilding it.

DisplaysEsp-8266Specials

2 comments

Kurt Hennig

Kurt Hennig

Hallo,
Ich habe den Code jetzt an drei verschiedenen ESP8266 (V3 von AZ, Wemos D1 und ESP-07 ausprobiert, funktioniert alles wunderbar. Beim Wemos und ESP-07 SDA Pin D2=GPIO4 und SCL Pin D1=GPIO5 benutzen. Das mit dem lcd.init stimmt bei der neueren libary, bei älteren oder bei parallel Anschluss muss es lcd.begin heißen.

Kurt

Andreas Dürmuth

Andreas Dürmuth

Hallo,
leider hat sich ein Fehler bei der Initialisierung eingeschlichen. Bei mir funktionierte leider der originale Sketsch nicht. Es kam eine Fehlermeldung ‘class LiquidCrystal_I2C’ has no member named ‘init’. Der Fehler steckt bei mir in Zeile 53. Ich habe:
lcd.init(); abgeändert zu:
lcd.begin();
Nun läuft alles.
Viel Spaß allen anderen “Bastlern”
Gruß, Andreas

Leave a comment

All comments are moderated before being published