Created
March 22, 2026 01:24
-
-
Save adlerweb/ce5ab429b01084097c9e59071fc23286 to your computer and use it in GitHub Desktop.
TFT Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <SPI.h> | |
| #include <Wire.h> | |
| #include "time.h" | |
| #include <WiFi.h> | |
| #include <Adafruit_GFX.h> | |
| #include <Adafruit_ST7735.h> | |
| #include <Adafruit_INA219.h> | |
| #include "myimage.h" | |
| const char* ssid = "freifunk-myk.de"; | |
| const char* password = ""; | |
| const char* ntpServer = "de.pool.ntp.org"; | |
| const long gmtOffset_sec = 3600; | |
| const int daylightOffset_sec = 3600; | |
| #define TFT_LED 2 | |
| #define TFT_CS 10 | |
| #define TFT_RST 11 | |
| #define TFT_DC 9 // A0 | |
| #define TFT_MOSI 13 // SDA | |
| #define TFT_SCLK 12 // SCK | |
| Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); | |
| String getCurrentDateTime() { | |
| struct tm timeinfo; | |
| if (!getLocalTime(&timeinfo)) { | |
| return ""; // Or handle error as needed | |
| } | |
| char timeStringBuff[20]; | |
| strftime(timeStringBuff, sizeof(timeStringBuff), "%Y-%m-%d %H:%M:%S", &timeinfo); | |
| return String(timeStringBuff); | |
| } | |
| uint8_t getCurrentSeconds() { | |
| struct tm timeinfo; | |
| if (!getLocalTime(&timeinfo)) { | |
| return 0; // Error fallback | |
| } | |
| return static_cast<uint8_t>(timeinfo.tm_sec); | |
| } | |
| void WiFiConnect() { | |
| tft.setTextSize(1); //6x8 | |
| tft.setCursor(0, 22); | |
| tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK); | |
| tft.println("Connecting WiFi..."); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| tft.print("SSID: "); | |
| tft.println(ssid); | |
| WiFi.begin(ssid, password); | |
| uint8_t dot = 1; | |
| uint8_t i = 0; | |
| while (WiFi.status() != WL_CONNECTED) { | |
| tft.setCursor(0, 38); | |
| for(i=0; i<dot; i++) tft.print("."); | |
| dot++; | |
| if(dot > 3) dot = 1; | |
| Serial.print("."); | |
| delay(500); | |
| } | |
| tft.setCursor(0, 38); | |
| tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); | |
| tft.print("OK!"); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| tft.print(" - IP: "); | |
| tft.println(WiFi.localIP()); | |
| Serial.println("\nWiFi connected!"); | |
| Serial.print("IP: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| void ntpConnect() { | |
| tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK); | |
| tft.print("NTP"); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| tft.print(" - "); | |
| configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | |
| time_t now = time(nullptr); | |
| uint8_t dot = 1; | |
| uint8_t i = 0; | |
| while (now < 8 * 3600 * 2) { // Check if year is still roughly 1970 | |
| delay(500); | |
| tft.setCursor(42, 55); | |
| for(i=0; i<dot; i++) tft.print("."); | |
| dot++; | |
| if(dot > 3) dot = 1; | |
| Serial.print("."); | |
| now = time(nullptr); | |
| } | |
| Serial.println("Time synced!"); | |
| tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); | |
| tft.println("OK!"); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| Serial.print("ST77xx TFT"); | |
| //TFT Backlight | |
| pinMode(TFT_LED, OUTPUT); | |
| digitalWrite(TFT_LED, HIGH); | |
| SPI.begin(TFT_SCLK, -1, TFT_MOSI, TFT_CS); | |
| tft.initR(INITR_BLACKTAB); | |
| //tft.setSPISpeed(27000000); | |
| tft.setRotation(1); | |
| tft.fillScreen(ST77XX_BLACK); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| tft.setTextSize(2); //16x18 | |
| tft.setCursor(0, 2); | |
| tft.print("TFT TEST START"); | |
| WiFiConnect(); | |
| ntpConnect(); | |
| delay(2000); | |
| tft.fillScreen(ST77XX_BLACK); | |
| tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); | |
| tft.setTextSize(2); //16x18 | |
| tft.drawRGBBitmap(0, 0, image_data_160x128x16, 160, 128); | |
| tft.setCursor(0, 2); | |
| tft.print("TFT TEST"); | |
| tft.setTextSize(1); //6x8 | |
| } | |
| void updateStatus() { | |
| tft.setCursor(23, 121); | |
| uint8_t sec = getCurrentSeconds(); | |
| if(sec < 5 || (sec >= 30 && sec < 35)) { | |
| tft.print("IPv4: "); | |
| tft.print(WiFi.localIP()); | |
| }else{ | |
| tft.println(getCurrentDateTime()); | |
| } | |
| } | |
| void loop() { | |
| updateStatus(); | |
| delay(500); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment