本帖最后由 ljs硬件 于 2024-11-11 19:39 编辑  
 
       前几天想做一个骑行助手,但是我们在骑车时不能掏出手表或手机查看时间和天气,所以骑行多久根本不知道,且可能被突如其来的雨淋成“落汤鸡”。为了更低成本地解决问题,我准备做一个天气时钟,装到自行车上。 
 
1.材料准备(以下图片资源来自百度图片https://image.baidu.com): 
   1.1 硬件材料 
   1.1.1 esp826601s模块(esp01s): 
   ![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图1](https://mc.dfrobot.com.cn/data/attachment/forum/202410/03/181809zr181rdry63lo0id.jpeg)  
   1.1.2 esp01s烧写器(带引脚引出): 
   ![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图2](https://mc.dfrobot.com.cn/data/attachment/forum/202410/03/181819mp6t33j6teefr002.jpeg)  
   1.1.3 oled显示屏ssd1306驱动i2c接口128x64 
   ![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图3](https://mc.dfrobot.com.cn/data/attachment/forum/202410/03/181824qw3rfuztssxysxvs.jpeg)  
   1.1.4 杜邦线母对母x4(无图) 
   1.2 软件材料 
   1.2.1 ArduinoIDE_v1.8.19->点击下载压缩包 
   1.2.2 库文件 
   1.2.2.1 U8g2lib显示屏库:点击下载arduino安装包(github源) 
   1.2.2.2 NTPClient网络时间库:点击下载arduino安装包(github源,下载可能较慢) 
   1.2.2.3 ArduinoJson数据处理库:点击下载arduino安装包(github源) 
   1.2.2.4 ESP8266_Seniverse心知天气信息读取库:点击下载arduino安装包(github源,下载可能缓慢) 
 
2.功能: 
   2.1 从天气信息中的最后更新时间中读取日期显示在oled(NTPClient无法读取日期) 
   2.2 从NTPClient中读取格式化时间显示在oled(时:分:秒)(24小时制) 
   2.3 从NTPClient中读取星期显示在oled(0-6,0代表星期日) 
   2.4 从天气和天气预报信息中读取当前温湿度显示在oled(摄氏度℃)3.接线图 
| OLED | ESP01S |  | GND | GND |  | VCC(VDD) | 3V3 
 |  | SCL(SCK) | IO0 |  | SDA | IO2 
 |  
 ![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图6](https://mc.dfrobot.com.cn/data/attachment/forum/202411/11/193643vhepcqqeqqzlm84c.png)  
4.代码 
			
			
			- #include<ESP8266_Seniverse.h>
 - #include<ESP8266WiFi.h>
 - #include<NTPClient.h>
 - #include<U8g2lib.h>
 - #include<WiFiUdp.h>
 - 
 - String weather;
 - String date;
 - int tmp,hum;
 - 
 - U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled(U8G2_R0,U8X8_PIN_NONE,0,2);//初始化oled
 - WiFiUDP udp;
 - 
 - NTPClient ntp(udp,"pool.ntp.org",28800,60000);
 - WeatherNow wther;
 - Forecast fore;//初始化时间、天气信息读取对象
 - 
 - String myGetday(){//把0-7的数字星期信息转换成文本
 -   int wk=ntp.getDay();
 -   String week[]={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};//星期缩写文本列表
 -   return week[wk];
 - }
 - 
 - void setup(){
 -   Serial.begin(115200);
 -   WiFi.begin("Your ssid","Your pswd");
 -   while(WiFi.status()!=WL_CONNECTED)delay(1);//连接WiFi
 -   oled.begin();
 -   oled.enableUTF8Print();
 -   oled.setFont(u8g2_font_unifont_t_chinese2);//初始化oled字体
 -   oled.setFontDirection(0);//初始化oled文字方向
 -   ntp.begin();
 -   wther.config("Your Seniverse key","hangzhou","c");
 -   fore.config("Your Seniverse key","hangzhou","c");//初始化时间、天气信息读取类
 - }
 - void loop(){
 -   Serial.println("Sending");//串口输出,可用于确保工作正常
 -   ntp.update();
 -   wther.update();
 -   fore.update();//更新信息
 -   weather=wther.getWeatherText();
 -   tmp=wther.getDegree();
 -   hum=fore.getHumidity(0);
 -   date=fore.getLastUpdate().substring(0,10);//读取信息
 -   oled.clearBuffer();
 -   oled.drawFrame(0,1,127,15);
 -   oled.drawFrame(0,16,127,48);
 -   oled.drawLine(85,0,85,15);//画出边框
 -   oled.setCursor(87,14);
 -   oled.print(myGetday());//打印星期
 -   oled.setCursor(30,30);
 -   oled.print(ntp.getFormattedTime());//打印时间
 -   oled.setCursor(30,43);
 -   oled.print(weather);//打印天气
 -   oled.setCursor(30,56);
 -   oled.print(String(tmp)+"DG"+String(hum)+"%RH");//打印当前城市室外温湿度(℃)
 -   oled.setCursor(3,13);
 -   oled.print(date);//打印日期
 -   oled.sendBuffer();//输送缓存给oled
 -   delay(900);//延时<1秒,防止刷新频率过快
 - }
 
  复制代码
  
5.成品展示(拍摄时间:2024.10.4) 
![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图4](https://mc.dfrobot.com.cn/data/attachment/forum/202410/04/144532rqm1ms51r10aa14b.jpg)  
 
 
 
 
 
 
 
 
 
 
![[数智出行/智能家居]基于arduino框架的esp8266天气时钟图5](https://mc.dfrobot.com.cn/data/attachment/forum/202410/04/144532wuxl2ccsyivf0his.jpg)  
 
 
 
 
 
 
 |