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");
}

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

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值