本文介绍了如何基于 ESP32-C3 开发一个墨水屏天气站的小项目。本项目将结合 ESP32-C3 的功能、墨水屏显示器(e-Ink)、天气 API 和日历功能,创建一个既能显示天气信息,又能展示当前日期和时间的智能设备。
4.2 寸墨水屏显示效果如下图(屏幕稍有老化):
项目代码:百度网盘链接 https://pan.baidu.com/s/1GuYrQ42u0oNo0DQ47HD-bQ?pwd=k5rx
本项目旨在实现以下功能:
序号 | ESP32C3 开发板引脚 | 墨水屏驱动板引脚 |
---|---|---|
1 | MOSI | GPIO6 |
2 | SCK | GPIO4 |
3 | CS | GPIO7 |
4 | DC | GPIO5 |
5 | RST | GPIO10 |
6 | BUSY | GPIO11 |
注意:合宙 ESP32-C3 开发板的 GPIO11 是个特殊引脚,默认为 SPI flash 的 VDD 引脚,需要解锁后才能使用。
注册流程不作详细介绍,注册登录后前往后台页面获取 Key https://home.openweathermap.org/api_keys ,本项目用的 2.5 版本的接口。
需在 Arduino IDE 的 库管理 中安装以下库:
配置 Wi-Fi 网络和天气 API(例如 OpenWeatherMap API):
1// Wi-Fi 配置
2const char* ssid = "your_SSID";
3const char* password = "your_PASSWORD";
4
5// 天气 API 配置
6const String apiKey = "your_API_KEY"; // 从 OpenWeatherMap 或其他服务获取
7const String city = "your_city"; // 设置城市名称
8const String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey + "&units=metric";
通过 API 获取天气信息:
1String getWeatherData() {
2 if (client.connect("api.openweathermap.org", 80)) {
3 client.print("GET " + apiUrl + " HTTP/1.1\r\n");
4 client.print("Host: api.openweathermap.org\r\n");
5 client.print("Connection: close\r\n\r\n");
6
7 unsigned long timeout = millis();
8 while (client.available() == 0) {
9 if (millis() - timeout > 5000) {
10 Serial.println("Timeout!");
11 client.stop();
12 return "";
13 }
14 }
15
16 String response = "";
17 while (client.available()) {
18 response += char(client.read());
19 }
20 // Serial.println(response);
21 client.stop();
22
23 // 处理返回信息
24 int jsonStart = response.indexOf('{'); // 找到第一个 '{'
25 if (jsonStart != -1) {
26 response = response.substring(jsonStart); // 从 '{' 开始截取
27 }
28 return parseWeatherJson(response);
29 }
30 return "";
31}
配置 NTP 客户端,注意东八区时间要使用 UTC+8:
1// 创建 NTP 客户端
2WiFiUDP udp;
3// NTPClient timeClient(udp, "pool.ntp.org", 0, 60000); // 默认时区为 UTC
4NTPClient timeClient(udp, "pool.ntp.org", 28800, 60000); // UTC+8 (中国标准时间)
获取 NTP 时间:
1String getDateTimeData() {
2 // 获取当前时间戳
3 unsigned long epochTime = timeClient.getEpochTime();
4 // 转换为时间结构
5 struct tm* timeInfo = localtime((time_t*)&epochTime);
6 // 格式化年月日和时分秒
7 char buffer[40];
8 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); // 格式化为 "2024-12-22 12:34:56"
9
10 return String(buffer);
11}
OpenWeatherMapDemo.ino 完整文件内容关注文末公众号后,发送消息“241220”获取链接或直接点击本站资源下载链接。
天气 API 返回结果信息样例如下
1{
2 "coord":{
3 "lon":113.25,
4 "lat":23.1167
5 },
6 "weather":[
7 {
8 "id":804,
9 "main":"Clouds",
10 "description":"overcast clouds",
11 "icon":"04n"
12 }
13 ],
14 "base":"stations",
15 "main":{
16 "temp":19.29,
17 "feels_like":17.88,
18 "temp_min":19.29,
19 "temp_max":19.29,
20 "pressure":1020,
21 "humidity":23,
22 "sea_level":1020,
23 "grnd_level":1019
24 },
25 "visibility":10000,
26 "wind":{
27 "speed":2.85,
28 "deg":13,
29 "gust":4.63
30 },
31 "clouds":{
32 "all":100
33 },
34 "dt":1734862385,
35 "sys":{
36 "country":"CN",
37 "sunrise":1734822253,
38 "sunset":1734860813
39 },
40 "timezone":28800,
41 "id":1809858,
42 "name":"Guangzhou",
43 "cod":200
44}
这个 ESP32-C3 开发的墨水屏天气站项目,通过结合墨水屏显示、Wi-Fi、天气 API 和 NTP 时间同步功能,展示了如何在物联网设备上构建一个实用的智能日历与天气站。你可以根据需求扩展更多功能,如显示更多的天气信息、定时更新数据等。