找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1383|回复: 0
收起左侧

【零知ESP8266教程】快速入门21 世界时钟demo

[复制链接]
ID:349555 发表于 2019-11-5 16:03 | 显示全部楼层 |阅读模式
上次分享了一个本地时钟,小伙伴觉得不fashion,所以思前想后,做一个世界时钟,来撑撑场面,也算是一个小拓展。这次,我们一起制作世界时钟!
一、硬件
电脑,windows系统
零知ESP8266开发板
OLED SSD1306模块
micro-usb线
杜邦线若干
二、连线

1.jpg



2.jpg



三、软件库的查找安装
(1)查找:可以在零知实验室查看“无线”下载安装,也可在github查找安装。
(亦可评论留言,私发给你)

OLED和WeatherStation

(2)安装:可以在零知实验室搜索:本地库,即可出现安装教程。
(3)安装完成后,打开零知开源软件,选择对应的开发板,如图:

3.png

(4)我们新建工程,烧写以下代码:



  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <Ticker.h>
  4. #include <JsonListener.h>
  5. #include <SSD1306Wire.h>
  6. #include <OLEDDisplayUi.h>
  7. #include <Wire.h>
  8. #include <WorldClockClient.h>
  9. #include "icons.h"
  10. #include "fonts.h"



  11. /***************************
  12. * Begin Settings
  13. **************************/
  14. // WIFI
  15. const char* WIFI_SSID = "xx";//在这里写入你的WIFI名字
  16. const char* WIFI_PWD = "xx"; //写WIFI密码

  17. // Setup
  18. const int UPDATE_INTERVAL_SECS = 10 * 60; // Update every 10 minutes

  19. // Display Settings
  20. const int I2C_DISPLAY_ADDRESS = 0x3c;
  21. const int SDA_PIN = D3; //
  22. const int SDC_PIN = D4;

  23. // TimeClient settings


  24. // Initialize the oled display for address 0x3c
  25. // sda-pin=14 and sdc-pin=12

  26. SSD1306Wire  display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
  27. OLEDDisplayUi ui     ( &display );

  28. /***************************
  29. * End Settings
  30. **************************/
  31. //String timeZoneIds [] = {"America/New_York", "Europe/London", "Europe/Paris", "Australia/Sydney", ""};
  32. //WorldClockClient worldClockClient("de", "CH", "E, dd. MMMMM yyyy", 4, timeZoneIds);
  33. //
  34. String timeZoneIds [] = {"America/New_York", "Europe/London", "Europe/Paris", "Australia/Sydney", "Asia/Shanghai"};
  35. WorldClockClient worldClockClient("zh", "CN", "E, dd. MMMMM yyyy", 5, timeZoneIds);

  36. // flag changed in the ticker function every 10 minutes
  37. bool readyForUpdate = false;

  38. String lastUpdate = "--";

  39. Ticker ticker;


  40. void updateData(OLEDDisplay *display) {
  41.   drawProgress(display, 50, "Updating Time...");
  42.   worldClockClient.updateTime();
  43.   drawProgress(display, 100, "Done...");
  44.   readyForUpdate = false;
  45.   delay(1000);
  46. }

  47. void drawProgress(OLEDDisplay *display, int percentage, String label) {
  48.   display->clear();
  49.   display->setTextAlignment(TEXT_ALIGN_CENTER);
  50.   display->setFont(ArialMT_Plain_10);
  51.   display->drawString(64, 10, label);
  52.   display->drawProgressBar(10, 28, 108, 12, percentage);
  53.   display->display();
  54. }

  55. void drawClock(OLEDDisplay *display, int x, int y, int timeZoneIndex, String city, const uint8_t* icon) {
  56.   display->setTextAlignment(TEXT_ALIGN_LEFT);
  57.   display->setFont(ArialMT_Plain_10);
  58.   display->drawString(x + 60, y + 5, city);
  59.   display->setFont(Crushed_Plain_36);
  60.   display->drawXbm(x, y, 60, 60, icon);
  61.   display->drawString(x + 60, y + 15, worldClockClient.getHours(timeZoneIndex) + ":" + worldClockClient.getMinutes(timeZoneIndex));

  62. }

  63. void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  64.   drawClock(display, x, y, 0, "New York",  new_york_bits); //纽约
  65. }

  66. void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  67.   drawClock(display, x, y, 1, "London",  london_bits); //伦敦
  68. }

  69. void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  70.   drawClock(display, x, y, 2, "Paris",  paris_bits);  //巴黎
  71. }

  72. void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  73.   drawClock(display, x, y, 3, "Sydney",  sydney_bits); //悉尼
  74. }

  75. void drawFrame5(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  76.   drawClock(display, x, y, 4, "Beijing",  beijing_bits); //北京
  77. }

  78. void setReadyForWeatherUpdate() {
  79.   Serial.println("Setting readyForUpdate to true");
  80.   readyForUpdate = true;
  81. }

  82. // this array keeps function pointers to all frames
  83. // frames are the single views that slide from right to left
  84. FrameCallback frames[] = { drawFrame1, drawFrame2, drawFrame3, drawFrame4, drawFrame5};
  85. int numberOfFrames = 5;

  86. void setup() {
  87.   Serial.begin(115200);
  88.   Serial.println();
  89.   Serial.println();

  90.   // initialize dispaly
  91.   display.init();
  92.   display.clear();
  93.   display.display();

  94.   //display.flipScreenVertically();
  95.   display.setFont(ArialMT_Plain_10);
  96.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  97.   display.setContrast(255);

  98.   WiFi.begin(WIFI_SSID, WIFI_PWD);

  99.   int counter = 0;
  100.   while (WiFi.status() != WL_CONNECTED) {
  101.     delay(500);
  102.     Serial.print(".");
  103.     display.clear();
  104.     display.drawString(64, 10, "Connecting to WiFi");
  105.     display.drawXbm(46, 30, 8, 8, counter % 3 == 0 ? activeSymbol : inactiveSymbol);
  106.     display.drawXbm(60, 30, 8, 8, counter % 3 == 1 ? activeSymbol : inactiveSymbol);
  107.     display.drawXbm(74, 30, 8, 8, counter % 3 == 2 ? activeSymbol : inactiveSymbol);
  108.     display.display();

  109.     counter++;
  110.   }

  111.   ui.setTargetFPS(30);

  112.   // You can change this to
  113.   // TOP, LEFT, BOTTOM, RIGHT
  114.   ui.setIndicatorPosition(BOTTOM);

  115.   // Defines where the first frame is located in the bar.
  116.   ui.setIndicatorDirection(LEFT_RIGHT);

  117.   // You can change the transition that is used
  118.   // SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN
  119.   ui.setFrameAnimation(SLIDE_LEFT);

  120.   // Add frames
  121.   ui.setFrames(frames, numberOfFrames);
  122.    
  123.   ui.setTimePerFrame(2*1000); // Setup frame display time to 10 sec

  124.   // Inital UI takes care of initalising the display too.
  125.   ui.init();

  126.   Serial.println("");

  127.   updateData(&display);

  128.   ticker.attach(UPDATE_INTERVAL_SECS, setReadyForWeatherUpdate);

  129. }

  130. void loop() {

  131.   if (readyForUpdate && ui.getUiState()->frameState == FIXED) {
  132.     updateData(&display);
  133.   }

  134.   int remainingTimeBudget = ui.update();

  135.   if (remainingTimeBudget > 0) {
  136.     // You can do some work here
  137.     // Don't do stuff if you are below your
  138.     // time budget.
  139.     delay(remainingTimeBudget);
  140.   }

  141. }
复制代码

(5)验证代码,然后上传

四、结果

4.jpg


同时OLED就可以显示世界时钟的效果:

5.jpg

还有效果视频哦!

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表