【零知ESP8266项目篇】1 OLED天气时钟
[复制链接]
引述:
我们一起学了这么久的零知ESP8266的教程,是不是该检验一下自己了呢?在前面的分享中,有小伙伴留言:太简单!那我就顺水推舟,拔高一下,实践搞个小项目——天气时钟。
到现在为止,作为一个基本的开发者,咱们已经对零知ESP8266有了最基本的了解了,可能你早就按耐不住要做项目了吧!那咱今儿个就做个小项目瞧瞧。继续给我们电子世界的轮廓加一个点。
一、硬件准备
电脑,windows系统
零知ESP8266开发板
OLED SSD1306模块
micro-usb线
二、连线
三、软件库
①打开零知开发工具最新版,选中开发板,如图所示:
②点击库,然后安装以下两个库,如图:
安装完成后,就可以啦。
四、打开零知开发软件,新建工程,命名weather—station。然后烧写如下代码(已经做好了中文注释,复制粘贴即可):
-
-
- #include <ESPWiFi.h>
- #include <ESPHTTPClient.h>
- #include <JsonListener.h>
-
-
- #include <time.h>
- #include <sys/time.h>
- #include <coredecls.h>
-
- #include "SSD1306Wire.h"
- #include "OLEDDisplayUi.h"
- #include "Wire.h"
- #include "OpenWeatherMapCurrent.h"
- #include "OpenWeatherMapForecast.h"
- #include "WeatherStationFonts.h"
- #include "WeatherStationImages.h"
-
-
-
-
-
- const char* WIFI_SSID = "WiFi名";
- const char* WIFI_PWD = "WiFi密码";
-
- #define TZ 8
- #define DST_MN 60
-
-
- const int UPDATE_INTERVAL_SECS = 20 * 60;
-
-
- const int I2C_DISPLAY_ADDRESS = 0x3c;
-
- const int SDA_PIN = D3;
- const int SDC_PIN = D4;
-
-
-
-
-
- String OPEN_WEATHER_MAP_APP_ID = "3213ac05f30cc2f7d8d8da6d2b03f2e8";
-
- String OPEN_WEATHER_MAP_LOCATION_ID = "1795565";
-
-
-
-
-
-
-
-
-
- String OPEN_WEATHER_MAP_LANGUAGE = "zh_cn";
- const uint8_t MAX_FORECASTS = 4;
-
- const boolean IS_METRIC = true;
-
-
- 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"};
-
-
-
-
- SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
- OLEDDisplayUi ui( &display );
-
- OpenWeatherMapCurrentData currentWeather;
- OpenWeatherMapCurrent currentWeatherClient;
-
- OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
- OpenWeatherMapForecast forecastClient;
-
- #define TZ_MN ((TZ)*60)
- #define TZ_SEC ((TZ)*3600)
- #define DST_SEC ((DST_MN)*60)
- time_t now;
-
-
- bool readyForWeatherUpdate = false;
-
- String lastUpdate = "--";
-
- long timeSinceLastWUpdate = 0;
-
-
- void drawProgress(OLEDDisplay *display, int percentage, String label);
- void updateData(OLEDDisplay *display);
- void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
- void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
- void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
- void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex);
- void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
- void setReadyForWeatherUpdate();
-
-
-
-
- FrameCallback frames[] = { drawDateTime, drawCurrentWeather, drawForecast };
- int numberOfFrames = 3;
-
- OverlayCallback overlays[] = { drawHeaderOverlay };
- int numberOfOverlays = 1;
-
- 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 ? activeSymbole : inactiveSymbole);
- display.drawXbm(60, 30, 8, 8, counter % 3 == 1 ? activeSymbole : inactiveSymbole);
- display.drawXbm(74, 30, 8, 8, counter % 3 == 2 ? activeSymbole : inactiveSymbole);
- display.display();
-
- counter++;
- }
-
- configTime(TZ_SEC, DST_SEC, "pool.ntp.org");
-
- ui.setTargetFPS(30);
-
- ui.setActiveSymbol(activeSymbole);
- ui.setInactiveSymbol(inactiveSymbole);
-
-
-
- ui.setIndicatorPosition(BOTTOM);
-
-
- ui.setIndicatorDirection(LEFT_RIGHT);
-
-
-
- ui.setFrameAnimation(SLIDE_LEFT);
-
- ui.setFrames(frames, numberOfFrames);
-
- ui.setOverlays(overlays, numberOfOverlays);
-
-
- ui.init();
-
- Serial.println("");
-
- updateData(&display);
-
- }
-
- void loop() {
-
- if (millis() - timeSinceLastWUpdate > (1000L*UPDATE_INTERVAL_SECS)) {
- setReadyForWeatherUpdate();
- timeSinceLastWUpdate = millis();
- }
-
- if (readyForWeatherUpdate && ui.getUiState()->frameState == FIXED) {
- updateData(&display);
- }
-
- int remainingTimeBudget = ui.update();
-
- if (remainingTimeBudget > 0) {
-
-
- delay(remainingTimeBudget);
- }
-
-
- }
-
- 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(2, 28, 124, 10, percentage);
- display->display();
- }
-
- void updateData(OLEDDisplay *display)
- {
- drawProgress(display, 10, "Updating time...");
- drawProgress(display, 30, "Updating weather...");
- currentWeatherClient.setMetric(IS_METRIC);
- currentWeatherClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
- currentWeatherClient.updateCurrentById(¤tWeather, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
- drawProgress(display, 50, "Updating forecasts...");
- forecastClient.setMetric(IS_METRIC);
- forecastClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
- uint8_t allowedHours[] = {12};
- forecastClient.setAllowedHours(allowedHours, sizeof(allowedHours));
- forecastClient.updateForecastsById(forecasts, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);
-
- readyForWeatherUpdate = false;
- drawProgress(display, 100, "Done...");
- delay(1000);
- }
-
-
-
- void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y)
-
- {
- now = time(nullptr);
- struct tm* timeInfo;
- timeInfo = localtime(&now);
- char buff[16];
-
-
- display->setTextAlignment(TEXT_ALIGN_CENTER);
- display->setFont(ArialMT_Plain_10);
- String date = WDAY_NAMES[timeInfo->tm_wday];
-
- sprintf_P(buff, PSTR("%s, %02d/%02d/%04d"), WDAY_NAMES[timeInfo->tm_wday].c_str(), timeInfo->tm_mday, timeInfo->tm_mon+1, timeInfo->tm_year + 1900);
- display->drawString(64 + x, 5 + y, String(buff));
- display->setFont(ArialMT_Plain_24);
-
- sprintf_P(buff, PSTR("%02d:%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
- display->drawString(64 + x, 15 + y, String(buff));
- display->setTextAlignment(TEXT_ALIGN_LEFT);
- }
-
- void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y)
- {
- display->setFont(ArialMT_Plain_10);
- display->setTextAlignment(TEXT_ALIGN_CENTER);
- display->drawString(64 + x, 38 + y, currentWeather.description);
-
- display->setFont(ArialMT_Plain_24);
- display->setTextAlignment(TEXT_ALIGN_LEFT);
- String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "°C" : "°F");
- display->drawString(60 + x, 5 + y, temp);
-
- display->setFont(Meteocons_Plain_36);
- display->setTextAlignment(TEXT_ALIGN_CENTER);
- display->drawString(32 + x, 0 + y, currentWeather.iconMeteoCon);
- }
-
-
- void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y)
- {
- drawForecastDetails(display, x, y, 0);
- drawForecastDetails(display, x + 44, y, 1);
- drawForecastDetails(display, x + 88, y, 2);
- }
-
- void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex)
- {
- time_t observationTimestamp = forecasts[dayIndex].observationTime;
- struct tm* timeInfo;
- timeInfo = localtime(&observationTimestamp);
- display->setTextAlignment(TEXT_ALIGN_CENTER);
- display->setFont(ArialMT_Plain_10);
- display->drawString(x + 20, y, WDAY_NAMES[timeInfo->tm_wday]);
-
- display->setFont(Meteocons_Plain_21);
- display->drawString(x + 20, y + 12, forecasts[dayIndex].iconMeteoCon);
- String temp = String(forecasts[dayIndex].temp, 0) + (IS_METRIC ? "°C" : "°F");
- display->setFont(ArialMT_Plain_10);
- display->drawString(x + 20, y + 34, temp);
- display->setTextAlignment(TEXT_ALIGN_LEFT);
- }
-
- void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state)
-
- {
- now = time(nullptr);
- struct tm* timeInfo;
- timeInfo = localtime(&now);
- char buff[14];
- sprintf_P(buff, PSTR("%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min);
- display->setColor(WHITE);
- display->setFont(ArialMT_Plain_10);
- display->setTextAlignment(TEXT_ALIGN_LEFT);
- display->drawString(0, 54, String(buff));
- display->setTextAlignment(TEXT_ALIGN_RIGHT);
- String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "°C" : "°F");
- display->drawString(128, 54, temp);
- display->drawHorizontalLine(0, 52, 128);
- }
-
- void setReadyForWeatherUpdate()
- {
- Serial.println("Setting readyForUpdate to true");
- readyForWeatherUpdate = true;
- }
-
验证完毕,然后再点击“上传”即可。
代码中获取数据的方法:点击这里
完整工程:weather_station1.7z(若有任何问题,欢迎评论留言)
五、结果
获取返回结果代码:200表示成功了
如果错误码,可以把地址粘贴到浏览器里看看是什么原因。
效果视频:点我拂尘
|