本站改版新增arduino频道
arduino SSD1306 OLED时钟u8g2库
#include <WiFi.h> //EPS8266: <EPS8266WiFi.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <time.h>
#include "images.h"
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define SDA 21
#define SCL 22
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *NTPServer = "time1.aliyun.com";
char szTime[9];
char szData[20];
char szWeek[10];
uint8_t hour,minute,second,month,day,week;
uint16_t year;
// 连接WiFi
void connectWiFi(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
configTime(28800, 0, NTPServer); //NTP服务
}
void setup() {
Serial.begin(115200);
Wire.begin(SDA,SCL);
u8g2.begin();
delay(200);
u8g2.firstPage();
do{
u8g2.drawXBMP(34,14,WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
u8g2.sendBuffer();
}
while(u8g2.nextPage());
delay(2000);
u8g2.enableUTF8Print();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_unifont_t_chinese3);
u8g2.setCursor(14, 14);
u8g2.print("开始连接WiFi");
u8g2.sendBuffer();
connectWiFi();
}
void getTime() {
time_t now = time(nullptr); // 获取当前时间戳
struct tm *timeinfo = localtime(&now); // 转为本地时间
hour = timeinfo->tm_hour;
minute = timeinfo->tm_min;
second = timeinfo->tm_sec;
week = timeinfo->tm_wday;
month = timeinfo->tm_mon + 1;
day = timeinfo->tm_mday;
year = timeinfo->tm_year + 1900;
u8g2.clearBuffer();
// Date
u8g2.setFont(u8g2_font_unifont_t_chinese3);
strftime(szData,sizeof(szData),"%Y-%m-%d",timeinfo);
u8g2.drawStr(2, 12, szData);
// Weekday
strftime(szWeek,sizeof(szWeek),"%a",timeinfo);
u8g2.drawStr(100, 12, szWeek);
//Line
u8g2.drawHLine(0, 14, 128);
// Time
u8g2.setFont(u8g2_font_logisoso24_tr);
u8g2.drawStr(8, 44, szTime);
strftime(szTime,sizeof(szTime),"%X", timeinfo);
//Line
u8g2.drawHLine(0, 48, 128);
//IP address
String ipAddress = WiFi.localIP().toString();
u8g2.setFont(u8g2_font_pixzillav1_tr);
u8g2.drawStr(19, 61,ipAddress.c_str());
u8g2.setFont(u8g2_font_siji_t_6x10); // icon字体
u8g2.drawGlyph(113, 61, 0x0e200); // 电源图标
u8g2.drawGlyph(2, 61, 0x0e21a); // WiFi图标
//空心矩形
u8g2.drawFrame(0,0,128,64);
// Send buffer to display
u8g2.sendBuffer();
delay(1000);
}
void loop() {
getTime();
}
images.h
#define WiFi_Logo_width 60
#define WiFi_Logo_height 36
const uint8_t WiFi_Logo_bits[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF,
0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00,
0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C,
0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00,
0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C,
0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00,
0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C,
0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00,
0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C,
0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00,
0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F,
0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00,
0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF,
0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00,
0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0093009471893311 seconds