ESP8266+0.96寸OLED+心知天气做的简约版天气时钟

简述:
学习ESP8266就想做一个小项目,就在网上看别人的天气时钟跟着做了一遍,大致上差不多,就进行了一些小的改动。

一:硬件准备
Nodemcu开发板
0.96寸OLED(SPI七线制)

GND - GND
VCC - 3.3V
D0 - D2
D1 - D1
RES - D7
DC - D5
CS - D6

面包板
杜邦线

二:软件准备
进行库安装,用到的库有:
<ESP8266WiFi.h>
<ESP8266_Seniverse.h>
<U8g2lib.h>
<NTPClient.h>
<WiFiUdp.h>
<WiFiManager.h>

三:代码

#include <ESP8266WiFi.h>
#include <ESP8266_Seniverse.h>
#include <U8g2lib.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <WiFiManager.h>


U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 4, /* data=*/ 5, /* cs=*/ 12, /* dc=*/ 14, /* reset=*/ 13);/*DI   ------ D1 D0  ------- D2 DC  ------- D5 CS  ------- D6 RES -------D7*/

// weather
const String weathers[] = {"晴", "晴", "晴", "晴", "多云", "晴间多云", "晴间多云", "大部多云", "大部多云", "阴",
                           "阵雨", "雷阵雨", "雷雨冰雹", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨",
                           "冻雨", "雨夹雪", "阵雪", "小雪", "中雪", "大雪", "暴雪", "浮尘", "扬沙", "沙尘暴",
                           "强沙暴", "雾", "霾", "风", "大风", "飓风", "热带风暴", "龙卷风", "冷", "热", "未知"
                          };

const char* ssid     = "***";       // 连接WiFi名
const char* password = "***";          // 连接WiFi密码
String reqUserKey = "***";// 心知天气私钥
String reqLocation = "***";// 城市
String reqUnit = "c";// 摄氏/华氏

WeatherNow weatherNow;
Forecast forecast;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);

int times = 0;

void setup() {
  Serial.begin(115200);
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.sendBuffer();
  delay(850);

  connectWiFi();// 连接wifi
  // 配置心知天气请求信息
  weatherNow.config(reqUserKey, reqLocation, reqUnit);
  forecast.config(reqUserKey, reqLocation, reqUnit);
  timeClient.begin();
}

void loop() {
  u8g2.clearBuffer();// 清空显示设备内部缓冲区
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);

  //天气
  if (forecast.update()) { // 更新天气信息

    //今天的天气、温度
    u8g2.setCursor(43, 12);
    String str1 = weathers[forecast.getDayCode(0)] + " " + forecast.getLow(0) + "-" + forecast.getHigh(0) + "℃" + " " + "今";
    u8g2.print(str1);

    //明天的天气、温度范围
    u8g2.setCursor(43, 27);
    str1 = weathers[forecast.getDayCode(1)] + " " + forecast.getLow(1) + "-" + forecast.getHigh(1) + "℃" + " " + "明";
    u8g2.print(str1);

    //后天的天气、温度范围
    u8g2.setCursor(43, 42);
    str1 = weathers[forecast.getDayCode(2)] + " " + forecast.getLow(2) + "-" + forecast.getHigh(2) + "℃" + " " + "后";
    u8g2.print(str1);

  } else {// 更新失败
    //u8g2.setCursor(53, 61);
    //u8g2.print(">_<...网络慢");
  }

  //时间
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  if (timeClient.update()) {
    u8g2.setCursor(53, 61);
    String time = timeClient.getFormattedTime().substring(0, 5);
    u8g2.print(time);
  } else {
    //u8g2.setCursor(53, 61);
    //u8g2.print(">_<...网络慢");
  }

  u8g2.setFont(u8g2_font_unifont_t_symbols);   //先设置字体字集
  if (times == 0) {
    u8g2.drawGlyph(13, 18, 0x2603);
    u8g2.drawGlyph(13, 38, 0x2615);
    u8g2.drawGlyph(15, 58, 0x2600);
    times = 1;
  } else if (times == 1) {
    u8g2.drawGlyph(13, 18, 0x23f0);
    u8g2.drawGlyph(13, 38, 0x23f3);
    u8g2.drawGlyph(15, 58, 0x2614);
    times = 2;
  } else {
    u8g2.drawGlyph(13, 18, 0x2618);
    u8g2.drawGlyph(13, 38, 0x2619);
    u8g2.drawGlyph(15, 58, 0x2606);
    times = 0;
  }

  u8g2.sendBuffer();// 显示缓冲区内容
  delay(10000);
}

// 连接WiFi
void connectWiFi() {
  // 建立WiFiManager对象
  WiFiManager wifiManager;
  // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
  wifiManager.autoConnect("AutoConnectAP");
}

四:效果展示
在这里插入图片描述

  • 16
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是基于esp82660.96OLED显示屏的天气时钟代码,采用了BME280传感器获取温度、湿度和气压数据,并通过WiFi连接获取天气信息并实时更新显示: ```c++ #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266HTTPClient.h> #include <SSD1306Wire.h> #include <OLEDDisplayUi.h> // WiFi设置 const char* ssid = "YourSSID"; // 你的WiFi名称 const char* password = "YourPassword"; // 你的WiFi密码 // 服务器设置 const String serverName = "http://api.openweathermap.org/data/2.5/weather?q=Shenzhen&appid=YourAPIKey"; // 你的OpenWeatherMap API Key和城市名称 // OLED显示屏设置 SSD1306Wire display(0x3c, D2, D1); OLEDDisplayUi ui(&display); // BME280传感器设置 Adafruit_BME280 bme; // 时间设置 unsigned long currentTime = millis(); unsigned long previousTime = 0; const long interval = 60000; // 更新时间间隔,单位为毫秒 void setup() { Serial.begin(115200); // 初始化OLED显示屏 display.init(); display.setContrast(255); display.setFont(ArialMT_Plain_10); // 初始化BME280传感器 if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } // 连接WiFi WiFi.begin(ssid, password); Serial.print("Connecting to "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void loop() { currentTime = millis(); if (currentTime - previousTime >= interval) { // 获取天气信息 if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(serverName); int httpResponseCode = http.GET(); if (httpResponseCode > 0) { String payload = http.getString(); Serial.println(payload); // 解析JSON数据 const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 60; DynamicJsonDocument doc(capacity); deserializeJson(doc, payload); const char* description = doc["weather"][0]["description"]; float temp = doc["main"]["temp"]; float humidity = doc["main"]["humidity"]; float pressure = doc["main"]["pressure"]; // 显示天气信息 display.clear(); display.drawString(0, 0, "Weather in Shenzhen"); display.drawString(0, 12, description); display.drawString(0, 24, "Temp: " + String(temp) + " C"); display.drawString(0, 36, "Humidity: " + String(humidity) + " %"); display.drawString(0, 48, "Pressure: " + String(pressure) + " hPa"); display.display(); } else { Serial.println("Error on HTTP request"); } http.end(); } else { Serial.println("WiFi Disconnected"); } previousTime = currentTime; } } ``` 需要注意的是,需要在OpenWeatherMap网站上注册账号并申请API Key,替换代码中的YourAPIKey和城市名称。 此外,如果你的OLED显示屏型号不同,可能需要修改相关的设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值