ESP8266网络时钟
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP8266网络时钟相关的知识,希望对你有一定的参考价值。
ESP8266网络时钟
- 效果演示
ESP8266网络时钟
以上视频中硬件包含:esp8266,1.3寸IIC OLED屏幕
- 个人自作的话,一块NodeMCU或者wemos都可以,iic屏幕,0.96的也可以,只要是包含esp8266模块即可制作。
- 代码
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
#include <sys/time.h>
#include <coredecls.h>
#include "SH1106Wire.h" //1.3寸用这个
//#include "SSD1306Wire.h" //0.96寸用这个
#include "OLEDDisplayUi.h"
#include "font.c"
#if defined(ESP8266) //OLED引脚定义
const int SDA_PIN = D2; //对应nodemcu接D5或者D1,,wemosD1mini的D2
const int SDC_PIN = D1; //对应nodemcu接D6或者D2,,wemosD1mini的D5
#else
const int SDA_PIN = D1; //对应nodemcu接D5或者D1
const int SCL = D2; //对应nodemcu接D6或者D2
#endif
const int I2C_DISPLAY_ADDRESS = 0x3c; //I2c地址默认
SH1106Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN); // 1.3寸用这个
//SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN); // 0.96寸用这个
const char *ssid = "MERCURY_D268G";
const char *password = "pba5ayzk";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp2.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);
#define TZ 8 // 中国时区为8
#define DST_MN 0 // 默认为0
#define TZ_MN ((TZ)*60) //时间换算
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)
time_t now; //实例化时间
//const String WDAY_NAMES[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; //星期
const String MONTH_NAMES[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; //月份
int clockCenterX=31;
int clockCenterY=31;
int oldsec=0;
OLEDDisplayUi ui ( &display );
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
Serial.printf("WiFi name:%s\\n IP Address: %s\\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
randomSeed(analogRead(0));
display.init();
display.clear();
display.display();
// display.flipScreenVertically(); //屏幕翻转
// display.mirrorScreen();//使用分光棱镜显示需要用到此函数
display.setContrast(240); //屏幕亮度
// delay(1000);
ui.setTargetFPS(80); //刷新频率
ui.disableAllIndicators(); //不显示页码小点。
ui.enableAutoTransition();
ui.setFrameAnimation(SLIDE_LEFT); //切屏方向
// ui.disableAutoTransition();
ui.enableAutoTransition();
ui.setAutoTransitionForwards();//设置自动过渡方向,
// ui.setFrames(frames, numberOfFrames); // 设置框架和显示屏幕内容数
ui.setTimePerFrame(5000); //设置切换时间
ui.setTimePerTransition(500);//设置转场大约所需要时间
// ui.setOverlays(overlays, numberOfOverlays); //设置覆盖的画面数
ui.init();// UI负责初始化显示
configTime(TZ_SEC, DST_SEC, "ntp.ntsc.ac.cn", "ntp5.aliyun.com"); //ntp获取时间,你也可用其他"pool.ntp.org","0.cn.pool.ntp.org","1.cn.pool.ntp.org","ntp1.aliyun.com"
}
void drawDisplay()
{
display.clear();
// Draw Clockface
for (int i=0; i<2; i++)
{
display.drawCircle(clockCenterX, clockCenterY, 31-i);
}
for (int i=0; i<3; i++)
{
display.drawCircle(clockCenterX, clockCenterY, i);
}
// Draw a small mark for every hour
for (int i=0; i<12; i++)
{
drawMark(i);
}
timeClient.update();
}
void drawMark(int h)
{
float x1, y1, x2, y2;
h=h*30;
h=h+270;
x1=29*cos(h*0.0175);
y1=29*sin(h*0.0175);
x2=26*cos(h*0.0175);
y2=26*sin(h*0.0175);
display.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}
void drawSec(int s)
{
float x1, y1, x2, y2;
s=s*6;
s=s+270;
x1=29*cos(s*0.0175);
y1=29*sin(s*0.0175);
x2=26*cos(s*0.0175);
y2=26*sin(s*0.0175);
if ((s % 5) == 0)
display.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
// else
// display.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}
void drawMin(int m)
{
float x1, y1, x2, y2, x3, y3, x4, y4;
m=m*6;
m=m+270;
x1=25*cos(m*0.0175);
y1=25*sin(m*0.0175);
x2=3*cos(m*0.0175);
y2=3*sin(m*0.0175);
x3=10*cos((m+8)*0.0175);
y3=10*sin((m+8)*0.0175);
x4=10*cos((m-8)*0.0175);
y4=10*sin((m-8)*0.0175);
display.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
display.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
display.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
display.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}
void drawHour(int h, int m)
{
float x1, y1, x2, y2, x3, y3, x4, y4;
h=(h*30)+(m/2);
h=h+270;
x1=20*cos(h*0.0175);
y1=20*sin(h*0.0175);
x2=3*cos(h*0.0175);
y2=3*sin(h*0.0175);
x3=8*cos((h+12)*0.0175);
y3=8*sin((h+12)*0.0175);
x4=8*cos((h-12)*0.0175);
y4=8*sin((h-12)*0.0175);
display.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
display.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
display.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
display.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}
void drawDateTime(){
now = time(nullptr);
struct tm* timeInfo;
timeInfo = localtime(&now);
char buff[16];
timeClient.update();
display.setFont(ArialMT_Plain_16); //显示字体大小
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawXbm(42+28, 1, 16, 16, xing);
display.drawXbm(59+28, 1, 16, 16, qi);
display.drawXbm(76+28, 1, 16, 16, Week[timeClient.getDay()]);
display.drawHorizontalLine(70, 18, 128); //画水平线
display.drawString(65 , 26 , timeClient.getFormattedTime()); //显示位置
display.drawHorizontalLine(68, 43, 128); //画水平线
display.setFont(ArialMT_Plain_14); //显示字体大小
sprintf_P(buff, PSTR("%04d-%02d-%02d"), timeInfo->tm_year + 1900, timeInfo->tm_mon + 1, timeInfo->tm_mday);
display.drawString(54 , 49 , String(buff));
// display.drawHorizontalLine(56, 64, 128); //画水平线
}
void loop()
{
timeClient.update();
drawDisplay();
drawSec(timeClient.getSeconds());
drawMin(timeClient.getMinutes());
drawHour( timeClient.getHours(),timeClient.getMinutes());
drawDateTime();
display.display();
}
- font.c文件
#include <Arduino.h>
const unsigned char xing[] PROGMEM= {0x00,0x00,0xF8,0x0F,0x08,0x08,0xF8,0x0F,0x08,0x08,0xF8,0x0F,0x80,0x00,0x88,0x00,0xF8,0x1F,0x84,0x00,0x82,0x00,0xF8,0x0F,0x80,0x00,0x80,0x00,0xFE,0x3F,0x00,0x00};
const unsigned char qi[] PROGMEM= {0x44,0x00,0x44,0x3E,0xFE,0x22,0x44,0x22,0x44,0x22,0x7C,0x3E,0x44,0x22,0x44,0x22,0x7C,0x22,0x44,0x3E,0x44,0x22,0xFF,0x22,0x20,0x21,0x44,0x21,0x82,0x28,0x41,0x10};
const unsigned char Week[7][32] PROGMEM = {
{0x00, 0x00, 0xF8, 0x1F, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xF8, 0x1F, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xF8, 0x1F, 0x18, 0x18},/*"日"*/
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*\\"一\\"*/
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*\\"二\\",0*/
{0x00, 0x00, 0x00, 0x00, 0xFE, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, ESP8266便携式物联网时钟(软件篇) 代号:喵
ESP8266 + STC15+ I2C OLED带网络校时功能的定时器时钟