#include <Adafruit_ST7735.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <WiFi.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "cn.pool.ntp.org";
const long utcOffsetInSeconds = 8 * 3600;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, utcOffsetInSeconds);
#define TFT_DC 2
#define TFT_CS 15
#define TFT_RES 22
#define TFT_MOSI 23
#define TFT_SCLK 18
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RES);
U8G2_FOR_ADAFRUIT_GFX u8g2;
void setup() {
Serial.begin(115200);
tft.setSPISpeed(16000000L);
tft.initR(INITR_144GREENTAB);
tft.setRotation(2);
u8g2.begin(tft);
u8g2.setFontMode(1);
u8g2.setFontDirection(0);
u8g2.setForegroundColor(ST77XX_GREEN);
u8g2.setFont(u8g2_font_wqy12_t_gb2312);
u8g2.setCursor(0, 20);
WiFi.begin(ssid, password);
u8g2.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
u8g2.print(".");
}
u8g2.println();
u8g2.println("Connected to WiFi");
u8g2.print("Local IP: ");
u8g2.println(WiFi.localIP());
u8g2.println("Connecting to ntpServer");
timeClient.begin();
while (!timeClient.update()) {
timeClient.forceUpdate();
u8g2.print(".");
}
u8g2.println();
// 设置设备时间
setTime(timeClient.getEpochTime());
u8g2.println("Current time: ");
u8g2.println(getFormattedTime());
u8g2.println();
u8g2.println("Hello, ESP32!");
delay(2000);
// 清空表盘区域
tft.fillScreen(ST7735_BLACK);
// 绘制表盘外圈
tft.drawCircle(64, 64, 60, ST7735_WHITE);
}
void loop() {
// 更新一次表盘
showtime();
delay(1000); // this speeds up the simulation
}
void showtime() {
tft.fillCircle(64, 64, 46, ST7735_BLACK);
// 绘制刻度线和数字
for (int i = 0; i < 12; i++) {
float angle = (i - 3) * 30 * DEG_TO_RAD - HALF_PI; // 计算刻度线位置,并将12点钟方向设置为正上方
int x1 = 64 + cos(angle) * 55;
int y1 = 64 + sin(angle) * 55;
int x2 = 64 + cos(angle) * 50;
int y2 = 64 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, ST77XX_WHITE);
}
// 获取当前时间,用于绘制指针位置
time_t currentTime = now();
int currentHour = hour();
int currentMinute = minute();
int currentSecond = second();
// 显示当前时间
Serial.print("Current time: ");
Serial.println(getFormattedTime());
// 绘制时针
float hourAngle = ((currentHour % 12) + currentMinute / 60.0) * 30 * DEG_TO_RAD - HALF_PI; // 将12点钟方向设置为正上方
int hourX = 64 + cos(hourAngle) * 20;
int hourY = 64 + sin(hourAngle) * 20;
tft.drawLine(64, 64, hourX, hourY, ST7735_WHITE);
// 绘制分针
float minuteAngle = (currentMinute + currentSecond / 60.0) * 6 * DEG_TO_RAD - HALF_PI; // 将12点钟方向设置为正上方
int minuteX = 64 + cos(minuteAngle) * 35;
int minuteY = 64 + sin(minuteAngle) * 35;
tft.drawLine(64, 64, minuteX, minuteY, ST7735_WHITE);
// 绘制秒针
float secondAngle = (currentSecond + millis() / 1000.0) * 6 * DEG_TO_RAD - HALF_PI; // 将12点钟方向设置为正上方
int secondX = 64 + cos(secondAngle) * 45;
int secondY = 64 + sin(secondAngle) * 45;
tft.drawLine(64, 64, secondX, secondY, ST7735_RED);
}
String getFormattedTime() {
char buffer[32];
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());
return String(buffer);
}