上次分享了一個本地時鐘,小伙伴覺得不fashion,所以思前想后,做一個世界時鐘,來撐撐場面,也算是一個小拓展。這次,我們一起制作世界時鐘!
一、硬件
電腦,windows系統
零知ESP8266開發板
OLED SSD1306模塊
micro-usb線
杜邦線若干
二、連線
1.jpg (70.84 KB, 下載次數: 20)
下載附件
2019-11-5 16:00 上傳
2.jpg (72.17 KB, 下載次數: 19)
下載附件
2019-11-5 16:00 上傳
三、軟件庫的查找安裝
(1)查找:可以在零知實驗室查看“無線”下載安裝,也可在github查找安裝。
(亦可評論留言,私發給你) OLED和WeatherStation (2)安裝:可以在零知實驗室搜索:本地庫,即可出現安裝教程。
(3)安裝完成后,打開零知開源軟件,選擇對應的開發板,如圖:
3.png (115.16 KB, 下載次數: 23)
下載附件
2019-11-5 16:00 上傳
(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"
-
-
-
- /***************************
- * Begin Settings
- **************************/
- // WIFI
- const char* WIFI_SSID = "xx";//在這里寫入你的WIFI名字
- const char* WIFI_PWD = "xx"; //寫WIFI密碼
-
- // Setup
- const int UPDATE_INTERVAL_SECS = 10 * 60; // Update every 10 minutes
-
- // Display Settings
- const int I2C_DISPLAY_ADDRESS = 0x3c;
- const int SDA_PIN = D3; //
- const int SDC_PIN = D4;
-
- // TimeClient settings
-
-
- // Initialize the oled display for address 0x3c
- // sda-pin=14 and sdc-pin=12
-
- SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
- OLEDDisplayUi ui ( &display );
-
- /***************************
- * End Settings
- **************************/
- //String timeZoneIds [] = {"America/New_York", "Europe/London", "Europe/Paris", "Australia/Sydney", ""};
- //WorldClockClient worldClockClient("de", "CH", "E, dd. MMMMM yyyy", 4, timeZoneIds);
- //
- String timeZoneIds [] = {"America/New_York", "Europe/London", "Europe/Paris", "Australia/Sydney", "Asia/Shanghai"};
- WorldClockClient worldClockClient("zh", "CN", "E, dd. MMMMM yyyy", 5, timeZoneIds);
-
- // flag changed in the ticker function every 10 minutes
- 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;
- }
-
- // this array keeps function pointers to all frames
- // frames are the single views that slide from right to left
- FrameCallback frames[] = { drawFrame1, drawFrame2, drawFrame3, drawFrame4, drawFrame5};
- int numberOfFrames = 5;
-
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println();
-
- // initialize dispaly
- display.init();
- display.clear();
- display.display();
-
- //display.flipScreenVertically();
- 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);
-
- // You can change this to
- // TOP, LEFT, BOTTOM, RIGHT
- ui.setIndicatorPosition(BOTTOM);
-
- // Defines where the first frame is located in the bar.
- ui.setIndicatorDirection(LEFT_RIGHT);
-
- // You can change the transition that is used
- // SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN
- ui.setFrameAnimation(SLIDE_LEFT);
-
- // Add frames
- ui.setFrames(frames, numberOfFrames);
-
- ui.setTimePerFrame(2*1000); // Setup frame display time to 10 sec
-
- // Inital UI takes care of initalising the display too.
- 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) {
- // You can do some work here
- // Don't do stuff if you are below your
- // time budget.
- delay(remainingTimeBudget);
- }
-
- }
復制代碼
(5)驗證代碼,然后上傳 四、結果
4.jpg (71.13 KB, 下載次數: 26)
下載附件
2019-11-5 16:00 上傳
同時OLED就可以顯示世界時鐘的效果:
5.jpg (78.95 KB, 下載次數: 25)
下載附件
2019-11-5 16:00 上傳
還有效果視頻哦! |