WouoUI/esp32UI/UI/新建文本文档 (2).txt

80 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-08-05 18:18:30 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ESP32<EFBFBD><EFBFBD>U8g2<EFBFBD><EFBFBD><EFBFBD><EFBFBD>OLED<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```c++
#include <U8g2lib.h>
#include <WiFi.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Define OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
// Define OLED object
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, OLED_SCL, OLED_SDA, OLED_RST);
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to Wi-Fi");
// Initialize time
configTime(0, 0, "pool.ntp.org");
Serial.println("Waiting for time");
while (!time(nullptr)) {
delay(1000);
Serial.println("Waiting...");
}
Serial.println("Time synchronized");
// Initialize OLED display
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);
}
void loop() {
// Get current time
time_t now = time(nullptr);
struct tm* timeinfo = localtime(&now);
// Format date and time
char date[20];
strftime(date, sizeof(date), "%Y-%m-%d", timeinfo);
char time[20];
strftime(time, sizeof(time), "%H:%M:%S", timeinfo);
// Clear OLED display
u8g2.clearBuffer();
// Display date and time
u8g2.setCursor(0, 20);
u8g2.print(date);
u8g2.setCursor(0, 40);
u8g2.print(time);
// Send display buffer to OLED
u8g2.sendBuffer();
// Wait for 1 second
delay(1000);
}
```
<EFBFBD>ڴ<EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Wi-Fi<46><69><EFBFBD>磬Ȼ<E7A3AC><C8BB>ʹ<EFBFBD><CAB9>NTP<54><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>ʱ<EFBFBD>䡣Ȼ<E4A1A3><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD>ʼ<EFBFBD><CABC>OLED<45><44>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>л<EFBFBD>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><EFBFBD><E4B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>Ϊ<EFBFBD><CEAA><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><E4A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OLED<45><44><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><E4A3AC><EFBFBD>ȴ<EFBFBD>1<EFBFBD><31><EFBFBD>ӡ<EFBFBD>