数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 942|回复: 65

[综合] 旧活新整,再做桌面天气时钟

[复制链接]
发表于 2026-3-4 11:01:23 | 显示全部楼层 |阅读模式
本帖最后由 wuyaozi 于 2026-3-4 11:10 编辑

  由于最近逛论坛的时候坛友说有一款温湿度计很小巧便宜(如下图),才1块多钱,第一次买的话才几毛钱。于是乎撸了三个


原机拆机,简单得令人发指,难怪可以做到1块多钱,内脏先不管,我们要的是外壳。外壳像一个小电视,内部空间也足够大,放板子和电池问题不大,就想到用来做一个桌面天气时钟。

说干就干,首先是产品功能规划:
11.54寸TFT LCD显示屏240x240驱动ST7789 TFT液晶屏IPS
[color=rgba(9, 9, 10, 0.92)]

大小和壳子差不多,用镜片当一下就差不多了,手上也有现成的。
2ESP32C3,功能不用多介绍,手头上也有。
3.扬声器,tyoe-c,电池充电电路,不能破坏外壳的完整性,所以不用设计按键 只需要一个开关机按键,可以使用触摸按键(vdk233dr单按键触摸芯片),开关机电路
以上是硬件选型,功能规划如下:
1,长按触摸按键开关机,开关机扬声器播放开关机音乐。
2, 开机后连接WIFI,如果是首次使用,会进入智能配网界面,手机连接ESP32C3的AP后会自动弹出配网网页,填写WIFI_SSID和WIFI密码,显示天气的城市全拼。点击保存后ESP32C3自动重启
3.连接WIFI成功后,等待80S界面就会更新时间和显示天气

产品功能规划完成后,就开始画原理图了。




手工贴片,铁板烧真蛋疼

硬件完成了,测试充电正常,开始码程序,esp32开发还是喜欢用vscode+platform,该项目程序使用的是x-track的程序架构(大神的代码常用常新)。

这是获取时间
  1. #include "HAL.h"
  2. #include <WiFi.h>
  3. #include <ESP8266_Seniverse.h>
  4. #include <Preferences.h>
  5. #include "SetWiFi.h"

  6. Preferences prefs;
  7. String PrefSSID, PrefPassword;
  8. String cityCode = "";

  9. using namespace HAL;

  10. /*********************************NTP Service******************************************/
  11. //时间服务器地址
  12. #define NTP1  "ntp1.aliyun.com"
  13. #define NTP2  "ntp2.aliyun.com"
  14. #define NTP3  "ntp3.aliyun.com"

  15. //初始化时钟服务器参数
  16. static void Clock_Init()
  17. {
  18.   configTime(8 * 3600, 0, NTP1, NTP2,NTP3);
  19. }

  20. //获取网络时间
  21. void HAL::Ntp_GetTime(Clock_Info_t *clock)
  22. {
  23.     static tm timeinfo;
  24.     if(getLocalTime(&timeinfo))
  25.     {
  26.       clock->minute = timeinfo.tm_min;
  27.       clock->hour = timeinfo.tm_hour;
  28.       clock->day = timeinfo.tm_mday;
  29.       clock->month = timeinfo.tm_mon + 1;
  30.       clock->week = timeinfo.tm_wday;
  31.     }
  32. }
  33. /*********************************NTP Service End******************************************/

  34. String reqUserKey = "Sw6cfCC8Mud98gQpD";   // 私钥
  35. String reqUnit = "c";                      // 摄氏/华氏

  36. WeatherNow weatherNow; // 建立Forecast对象用于获取心知天气信息

  37. //获取天气
  38. void HAL::Weather_GetInfo(Weather_Info_t* info)
  39. {
  40.       if(weatherNow.update()){  // 更新天气信息
  41.           info->WeatherStr = weatherNow.getWeatherText();
  42.           info->WeatherIco = weatherNow.getWeatherCode();
  43.           info->Temperature = weatherNow.getDegree();
  44.       }         
  45. }


  46. //强制门户Web配网
  47. bool setWiFi_Flag = false;
  48. void WiFi_Set() {
  49.   initSoftAP();
  50.   initWebServer();
  51.   initDNS();
  52.   while(setWiFi_Flag == false) {
  53.     server.handleClient();
  54.     dnsServer.processNextRequest();
  55.     if(WiFi.status() == WL_CONNECTED) {
  56.       server.stop();
  57.       setWiFi_Flag = true;


  58.     }
  59.   }
  60. }

  61. bool AutoConfig()
  62. {
  63. //首次使用自动进入配网模式,读取NVS存储空间内的ssid、password和citycode
  64.   prefs.begin("wifi", false);
  65.   PrefSSID =  prefs.getString("ssid", "none");
  66.   PrefPassword =  prefs.getString("password", "none");
  67.   cityCode =  prefs.getString("citycode", "dongguan");
  68.   prefs.end();

  69.   WiFi.begin(PrefSSID.c_str(), PrefPassword.c_str());
  70.   WiFi.setTxPower(WIFI_POWER_15dBm);
  71.   //如果觉得时间太长可改
  72.   for (int i = 0; i < 10; i++)
  73.   {
  74.       int wstatus = WiFi.status();
  75.       if (wstatus == WL_CONNECTED)
  76.       {
  77.         return true;
  78.       }
  79.       else
  80.       {
  81.         delay(1000);
  82.       }
  83.   }
  84.     return false;
  85. }


  86. //连接wifi
  87. void HAL::Wifi_Init()
  88. {
  89.   if (!AutoConfig())
  90.     {
  91.       WiFi_Set();
  92.     }

  93.   //初始化系统时间  
  94.   Clock_Init();
  95.   weatherNow.config(reqUserKey, cityCode, reqUnit);
  96.   
  97.   //初始化完成后关闭WiFi以节省功耗
  98.   WiFi.disconnect(true);
  99.   WiFi.mode(WIFI_OFF);
  100. }

  101. //启用WiFi
  102. void HAL::Wifi_Enable()
  103. {
  104.   if (WiFi.status() != WL_CONNECTED)
  105.   {
  106.     WiFi.mode(WIFI_STA);
  107.     WiFi.begin(PrefSSID.c_str(), PrefPassword.c_str());
  108.     for (int i = 0; i < 10; i++)
  109.     {
  110.       if (WiFi.status() == WL_CONNECTED)
  111.       {
  112.         break;
  113.       }
  114.       delay(500);
  115.     }
  116.   }
  117. }

  118. //禁用WiFi
  119. void HAL::Wifi_Disable()
  120. {
  121.   WiFi.disconnect(true);
  122.   WiFi.mode(WIFI_OFF);
  123. }

  124. //获取天气
  125. void HAL::Weather_GetInfo(Weather_Info_t* info)
  126. {
  127.   //启用WiFi
  128.   Wifi_Enable();
  129.   
  130.   if(weatherNow.update()){  // 更新天气信息
  131.       info->WeatherStr = weatherNow.getWeatherText();
  132.       info->WeatherIco = weatherNow.getWeatherCode();
  133.       info->Temperature = weatherNow.getDegree();
  134.   }         
  135.   
  136.   //获取完成后关闭WiFi
  137.   Wifi_Disable();
  138. }





复制代码
下面是MQTT的代码,为了省电 注释掉了。可以自己添加一些命令来使用通过MQTT控制天气时钟
  1. #include "HAL.h"
  2. #include <WiFi.h>



  3. /*********************************      MQTT     ******************************************/
  4. // #include <PubSubClient.h>
  5. // const char *mqtt_broker = "broker-cn.emqx.io";  //公共服务器
  6. // const char *topic = "qinxl/weather";
  7. // const char *mqtt_username = "";
  8. // const char *mqtt_password = "";
  9. // const int mqtt_port = 1883;

  10. // WiFiClient espClient;
  11. // PubSubClient client(espClient);

  12. // void SetDateTime(uint8_t *indata)
  13. // {        

  14. // }

  15. // void QueryVersion(uint8_t *indata)
  16. // {        
  17. // }


  18. // void SetBacklight(uint8_t *indata)
  19. // {
  20. //           HAL::Backlight_SetGradual(indata[1]*100,1000);
  21. // }

  22. // void GetBacklight(uint8_t *indata)
  23. // {
  24. //     char sendtext[30] = {0};
  25. //     uint32_t value = HAL::Backlight_GetValue();
  26. //     sprintf(sendtext,"Backlight value :%d",value);

  27. //     HAL::Mqtt_SendTest(sendtext);
  28. // }

  29. // void SetPowerOff(uint8_t *indata)
  30. // {               
  31. //    ESP.restart(); //重启ESP32
  32. // }

  33. // void GetVoltage(uint8_t *indata)
  34. // {
  35. //   HAL::Power_Info_t info;
  36. //   HAL::Power_GetInfo(&info);
  37. //   char sendtext[30] = {0};
  38. //   sprintf(sendtext,"usage:%d\nvoltage:%d",info.usage,info.voltage);

  39. //   HAL::Mqtt_SendTest(sendtext);

  40. // }
  41. // void CommandErr(uint8_t *indatan)
  42. // {        
  43. // }


  44. // //收到下发的消息(订阅主题的消息)后的回调函数
  45. // void callback(char *topic, byte *payload, unsigned int length)
  46. // {
  47. //   uint8_t dataLength = length/2;
  48. //   uint8_t data[dataLength] = {0};

  49. //    for (unsigned int i = 0; i < dataLength; i++) {
  50. //     char hexByte[3] = {(char)payload[i*2], (char)payload[i*2+1], '\0'};
  51. //     data[i] = strtoul(hexByte, NULL, 16);
  52. //   }
  53. //       switch(data[0])
  54. //       {
  55. //         case 1:
  56. //           SetPowerOff(data);
  57. //           break;
  58. //         case 2:
  59. //           SetBacklight(data);
  60. //           break;
  61. //         case 3:
  62. //           GetVoltage(data);
  63. //           break;
  64. //         case 4:
  65. //           GetBacklight(data);
  66. //           break;
  67. //       }
  68. // }

  69. // //执行一次的初始化参数函数
  70. // void HAL::Mqtt_Init()
  71. // {
  72. //     client.setServer(mqtt_broker, mqtt_port);
  73. //     client.setCallback(callback);

  74. //     if(!client.connected()) //如果没有连接上
  75. //     {
  76. //       client.connect("esp32c3_1987", mqtt_username, mqtt_password);
  77. //     }  
  78. //     client.subscribe("qinxl/weatherset"); //订阅"send" Topic


  79. // }
  80. // //MQTT消息发送
  81. // void HAL::Mqtt_SendTest(const char* str)
  82. // {   
  83. //     client.publish("qinxl/weatherup", str); //发布到"update"   
  84. // }

  85. // // 重连MQTT
  86. // void reconnectMQTT()
  87. // {
  88. //   for(int i=0;i<10;i++)
  89. //   {
  90. //       if (client.connect("esp32c3_1987", mqtt_username, mqtt_password))
  91. //       {
  92. //         HAL::Mqtt_SendTest("ESP32C3 MQTT connect success!");
  93. //         return;
  94. //       }
  95. //       else
  96. //       {delay(100);}     
  97. //   }
  98. // }

  99. // void HAL::MQtt_Update()
  100. // {
  101. //   if (!client.connected()) {
  102. //     reconnectMQTT();
  103. //   }
  104. //   client.loop(); // 必须调用,处理MQTT消息接收
  105. // }

  106. /*********************************      MQTT END ******************************************/
复制代码
最终效果如图:



固件源码和原理图PCB
















附网盘

通过百度网盘分享的文件:桌面天气.rar
链接:https://pan.baidu.com/s/1V1ayky0vu3Pjd9msdkq3ag?pwd=x8ea
提取码:x8ea


通过百度网盘分享的文件:WeatherDesktop1.rar
链接:https://pan.baidu.com/s/1ZIcymakeOjLz5No4QpHcBw?pwd=5fpr
提取码:5fpr


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x

打赏

参与人数 3家元 +75 收起 理由
mggimg + 15 厉害
jf201006 + 30 原創內容
8139 + 30 優秀文章

查看全部打赏

发表于 2026-3-4 11:13:27 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-3-4 11:14:53 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:15:34 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:18:36 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:18:52 | 显示全部楼层
游客请登录后查看回复内容

打赏

参与人数 1家元 +9 收起 理由
8139 + 9 哈哈哈哈

查看全部打赏

回复 支持 1 反对 0

使用道具 举报

发表于 2026-3-4 11:26:38 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:36:22 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:41:19 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:42:23 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-3-4 11:47:23 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 11:55:14 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-3-4 11:57:19 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 12:04:58 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 12:37:54 来自手机浏览器 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 12:54:54 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-3-4 13:07:07 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-3-4 13:09:10 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-4-20 02:13 , Processed in 0.218401 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

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