demo1984s 的个人博客
本文介绍了如何基于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):
// Wi-Fi 配置
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// 天气 API 配置
const String apiKey = "your_API_KEY"; // 从 OpenWeatherMap 或其他服务获取
const String city = "your_city"; // 设置城市名称
const String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey + "&units=metric";
通过 API 获取天气信息:
String getWeatherData() {
if (client.connect("api.openweathermap.org", 80)) {
client.print("GET " + apiUrl + " HTTP/1.1\r\n");
client.print("Host: api.openweathermap.org\r\n");
client.print("Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println("Timeout!");
client.stop();
return "";
}
}
String response = "";
while (client.available()) {
response += char(client.read());
}
// Serial.println(response);
client.stop();
// 处理返回信息
int jsonStart = response.indexOf('{'); // 找到第一个 '{'
if (jsonStart != -1) {
response = response.substring(jsonStart); // 从 '{' 开始截取
}
return parseWeatherJson(response);
}
return "";
}
配置 NTP 客户端,注意东八区时间要使用 UTC+8:
// 创建 NTP 客户端
WiFiUDP udp;
// NTPClient timeClient(udp, "pool.ntp.org", 0, 60000); // 默认时区为 UTC
NTPClient timeClient(udp, "pool.ntp.org", 28800, 60000); // UTC+8 (中国标准时间)
获取 NTP 时间:
String getDateTimeData() {
// 获取当前时间戳
unsigned long epochTime = timeClient.getEpochTime();
// 转换为时间结构
struct tm* timeInfo = localtime((time_t*)&epochTime);
// 格式化年月日和时分秒
char buffer[40];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); // 格式化为 "2024-12-22 12:34:56"
return String(buffer);
}
OpenWeatherMapDemo.ino 完整文件内容关注文末公众号后,发送消息“241220”获取链接或直接点击本站资源下载链接。
天气 API 返回结果信息样例如下
{
"coord":{
"lon":113.25,
"lat":23.1167
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":19.29,
"feels_like":17.88,
"temp_min":19.29,
"temp_max":19.29,
"pressure":1020,
"humidity":23,
"sea_level":1020,
"grnd_level":1019
},
"visibility":10000,
"wind":{
"speed":2.85,
"deg":13,
"gust":4.63
},
"clouds":{
"all":100
},
"dt":1734862385,
"sys":{
"country":"CN",
"sunrise":1734822253,
"sunset":1734860813
},
"timezone":28800,
"id":1809858,
"name":"Guangzhou",
"cod":200
}
这个 ESP32-C3 开发的墨水屏天气站项目,通过结合墨水屏显示、Wi-Fi、天气 API 和 NTP 时间同步功能,展示了如何在物联网设备上构建一个实用的智能日历与天气站。你可以根据需求扩展更多功能,如显示更多的天气信息、定时更新数据等。