【零知ESP8266教程】快速入门21 世界时钟demo
[复制链接]
上次分享了一个本地时钟,小伙伴觉得不fashion,所以思前想后,做一个世界时钟,来撑撑场面,也算是一个小拓展。这次,我们一起制作世界时钟!
一、硬件
电脑,windows系统
零知ESP8266开发板
OLED SSD1306模块
micro-usb线
杜邦线若干
二、连线
三、软件库的查找安装
(1)查找:可以在零知实验室查看“无线”下载安装,也可在github查找安装。
(亦可评论留言,私发给你)
OLED和WeatherStation
(2)安装:可以在零知实验室搜索:本地库,即可出现安装教程。
(3)安装完成后,打开零知开源软件,选择对应的开发板,如图:
(4)我们新建工程,烧写以下代码:
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <Ticker.h>
- #include <JsonListener.h>
- #include <SSD1306Wire.h>
- #include <OLEDDisplayUi.h>
- #include <Wire.h>
- #include <WorldClockClient.h>
- #include "icons.h"
- #include "fonts.h"
-
-
-
-
-
- const char* WIFI_SSID = "xx";
- const char* WIFI_PWD = "xx";
-
-
- const int UPDATE_INTERVAL_SECS = 10 * 60;
-
-
- const int I2C_DISPLAY_ADDRESS = 0x3c;
- const int SDA_PIN = D3;
- const int SDC_PIN = D4;
-
-
-
-
-
-
-
- SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
- OLEDDisplayUi ui ( &display );
-
-
-
-
-
- String timeZoneIds [] = {"America/New_York", "Europe/London", "Europe/Paris", "Australia/Sydney", "Asia/Shanghai"};
- WorldClockClient worldClockClient("zh", "CN", "E, dd. MMMMM yyyy", 5, timeZoneIds);
-
-
- bool readyForUpdate = false;
-
- String lastUpdate = "--";
-
- Ticker ticker;
-
-
- void updateData(OLEDDisplay *display) {
- drawProgress(display, 50, "Updating Time...");
- worldClockClient.updateTime();
- drawProgress(display, 100, "Done...");
- readyForUpdate = false;
- delay(1000);
- }
-
- void drawProgress(OLEDDisplay *display, int percentage, String label) {
- display->clear();
- display->setTextAlignment(TEXT_ALIGN_CENTER);
- display->setFont(ArialMT_Plain_10);
- display->drawString(64, 10, label);
- display->drawProgressBar(10, 28, 108, 12, percentage);
- display->display();
- }
-
- void drawClock(OLEDDisplay *display, int x, int y, int timeZoneIndex, String city, const uint8_t* icon) {
- display->setTextAlignment(TEXT_ALIGN_LEFT);
- display->setFont(ArialMT_Plain_10);
- display->drawString(x + 60, y + 5, city);
- display->setFont(Crushed_Plain_36);
- display->drawXbm(x, y, 60, 60, icon);
- display->drawString(x + 60, y + 15, worldClockClient.getHours(timeZoneIndex) + ":" + worldClockClient.getMinutes(timeZoneIndex));
-
- }
-
- void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
- drawClock(display, x, y, 0, "New York", new_york_bits);
- }
-
- void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
- drawClock(display, x, y, 1, "London", london_bits);
- }
-
- void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
- drawClock(display, x, y, 2, "Paris", paris_bits);
- }
-
- void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
- drawClock(display, x, y, 3, "Sydney", sydney_bits);
- }
-
- void drawFrame5(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
- drawClock(display, x, y, 4, "Beijing", beijing_bits);
- }
-
- void setReadyForWeatherUpdate() {
- Serial.println("Setting readyForUpdate to true");
- readyForUpdate = true;
- }
-
-
-
- FrameCallback frames[] = { drawFrame1, drawFrame2, drawFrame3, drawFrame4, drawFrame5};
- int numberOfFrames = 5;
-
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println();
-
-
- display.init();
- display.clear();
- display.display();
-
-
- display.setFont(ArialMT_Plain_10);
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.setContrast(255);
-
- WiFi.begin(WIFI_SSID, WIFI_PWD);
-
- int counter = 0;
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- display.clear();
- display.drawString(64, 10, "Connecting to WiFi");
- display.drawXbm(46, 30, 8, 8, counter % 3 == 0 ? activeSymbol : inactiveSymbol);
- display.drawXbm(60, 30, 8, 8, counter % 3 == 1 ? activeSymbol : inactiveSymbol);
- display.drawXbm(74, 30, 8, 8, counter % 3 == 2 ? activeSymbol : inactiveSymbol);
- display.display();
-
- counter++;
- }
-
- ui.setTargetFPS(30);
-
-
-
- ui.setIndicatorPosition(BOTTOM);
-
-
- ui.setIndicatorDirection(LEFT_RIGHT);
-
-
-
- ui.setFrameAnimation(SLIDE_LEFT);
-
-
- ui.setFrames(frames, numberOfFrames);
-
- ui.setTimePerFrame(2*1000);
-
-
- ui.init();
-
- Serial.println("");
-
- updateData(&display);
-
- ticker.attach(UPDATE_INTERVAL_SECS, setReadyForWeatherUpdate);
-
- }
-
- void loop() {
-
- if (readyForUpdate && ui.getUiState()->frameState == FIXED) {
- updateData(&display);
- }
-
- int remainingTimeBudget = ui.update();
-
- if (remainingTimeBudget > 0) {
-
-
-
- delay(remainingTimeBudget);
- }
-
- }
-
(5)验证代码,然后上传
四、结果
同时OLED就可以显示世界时钟的效果:
也有视频效果,可以一览哦
|