Màn hình
Bảng tin thời tiết e-ink
Thời tiết hôm nay trên màn hình như giấy, đọc rõ cả dưới nắng gắt. Tắt điện rồi nó vẫn hiện bản tin gần nhất.
- Khó
- khoảng 70 phút
- ESP32-S3
- 5 linh kiện
- 8 dây
- 111 dòng code
- Dùng Wi-Fi
Pack ESP32 được làm trên linh kiện thật: trình mô phỏng chỉ chạy mạch Arduino Uno.
Ý tưởng
E-ink dịch chuyển những hạt đen trắng li ti, sau đó không cần điện để giữ chúng. Nó đổi hình chậm, nên bo mạch vẽ cả trang một lượt rồi cho màn hình ngủ.
Học được gì
Điều khiển tấm e-paper, vẽ theo từng trang, và chọn chu kỳ làm mới phù hợp với màn hình.
Cách hoạt động
đầu vào
Thời tiết lấy từ internet
xử lý
ESP32-S3 · Wi-Fi
đầu ra
E-paper 4,2 in
Cấp nguồn 5 V qua cổng USB-C của bo.
Thiết bị kết nối vào Wi-Fi nhà bạn. Bạn gõ tên mạng và mật khẩu vào sketch trước khi nạp; chúng chỉ nằm trên bo.
Đây là thiết kế tham khảo. Sketch được biên dịch bằng bộ công cụ ESP32 thật, và các chân trong sketch được tự động đối chiếu với bảng nối dây. Chúng tôi chưa lắp và thử thiết kế này trên linh kiện thật, nên hãy nối mạch trên bàn và làm đủ các bước kiểm tra trước khi lắp vào vỏ. Module thật mỗi hãng một khác: hãy cắm theo ký hiệu in trên module của bạn, đừng theo vị trí trong hình.
Linh kiện
Đã có 0/5 món
Ở đây không ghi giá vì giá đổi theo từng shop và từng tuần. Hãy mua từng món theo đúng tên và lưu ý ghi bên cạnh.
Cần thêm
Máy tính đã cài Arduino IDE 2 (miễn phí)
Nối dây 3D
Mỗi bước một sợi dây, và nhớ rút cáp USB. Nhiều dây có thể dùng chung 3V3, 5V hoặc GND: board mở rộng hoặc bo cắm cho mỗi dây một chân riêng.
Bước 0/8
Bày linh kiện ra
Cắm bo lên board mở rộng hoặc bo cắm rồi bày các module ra xung quanh. Chưa nối dây nào.
Bảng nối dây
eink-weather.ino
// E-ink weather board
// Today's weather on paper-like e-ink. It refreshes every 30 minutes and
// keeps showing the last picture even if the power goes.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <GxEPD2_BW.h>
#include <Fonts/FreeSansBold24pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
// Your town. These are for Ho Chi Minh City.
const float LATITUDE = 10.82;
const float LONGITUDE = 106.63;
const char* TOWN = "Ho Chi Minh City";
const unsigned long REFRESH_MS = 30UL * 60UL * 1000UL;
const int PIN_PAPER_DIN = 11;
const int PIN_PAPER_CLK = 12;
const int PIN_PAPER_CS = 10;
const int PIN_PAPER_DC = 13;
const int PIN_PAPER_RST = 14;
const int PIN_PAPER_BUSY = 4;
// The driver class must match your panel. GDEY042T81 is the current 4.2 in
// black-and-white panel; older ones use GxEPD2_420 or GxEPD2_420_M01.
GxEPD2_BW<GxEPD2_420_GDEY042T81, GxEPD2_420_GDEY042T81::HEIGHT> paper(
GxEPD2_420_GDEY042T81(PIN_PAPER_CS, PIN_PAPER_DC, PIN_PAPER_RST, PIN_PAPER_BUSY));
unsigned long lastFetch = 0;
bool haveData = false;
const char* skyName(int code) {
if (code == 0) return "Clear sky";
if (code <= 3) return "Cloudy";
if (code <= 48) return "Fog";
if (code <= 67) return "Rain";
if (code <= 77) return "Snow";
if (code <= 82) return "Showers";
return "Thunderstorm";
}
void drawBoard(float now, float high, float low, int humidity, float rainMm, int code) {
paper.setFullWindow();
paper.firstPage();
do {
paper.fillScreen(GxEPD_WHITE);
paper.setTextColor(GxEPD_BLACK);
paper.setFont(&FreeSans12pt7b);
paper.setCursor(16, 34);
paper.print(TOWN);
paper.drawFastHLine(16, 46, 368, GxEPD_BLACK);
paper.setFont(&FreeSansBold24pt7b);
paper.setCursor(16, 120);
paper.print(now, 1);
paper.print(" C");
paper.setCursor(16, 180);
paper.print(skyName(code));
paper.setFont(&FreeSans12pt7b);
paper.setCursor(16, 226);
paper.printf("High %.0f C Low %.0f C", high, low);
paper.setCursor(16, 256);
paper.printf("Humidity %d%% Rain today %.1f mm", humidity, rainMm);
} while (paper.nextPage());
paper.hibernate();
}
bool fetchWeather() {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
String url = "https://api.open-meteo.com/v1/forecast?latitude=" + String(LATITUDE, 2) + "&longitude=" + String(LONGITUDE, 2) +
"¤t=temperature_2m,relative_humidity_2m,weather_code"
"&daily=temperature_2m_max,temperature_2m_min,precipitation_sum&forecast_days=1&timezone=auto";
if (!http.begin(client, url)) return false;
bool ok = false;
if (http.GET() == 200) {
JsonDocument doc;
if (!deserializeJson(doc, http.getString())) {
drawBoard(doc["current"]["temperature_2m"], doc["daily"]["temperature_2m_max"][0], doc["daily"]["temperature_2m_min"][0],
doc["current"]["relative_humidity_2m"], doc["daily"]["precipitation_sum"][0], doc["current"]["weather_code"]);
ok = true;
}
}
http.end();
return ok;
}
void setup() {
SPI.begin(PIN_PAPER_CLK, -1, PIN_PAPER_DIN, PIN_PAPER_CS);
paper.init(115200, true, 2, false);
paper.setRotation(0);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(250);
}
void loop() {
if (!haveData || millis() - lastFetch > REFRESH_MS) {
if (fetchWeather()) haveData = true;
lastFetch = millis();
if (!haveData) delay(10000);
}
delay(1000);
}
Thư viện
- ArduinoJson: cài từ Sketch > Include Library > Manage Libraries.
- GxEPD2: cài từ Sketch > Include Library > Manage Libraries.
Nạp sketch
Lần đầu dùng ESP32: trong Arduino IDE mở Boards Manager, tìm “esp32” và cài “esp32 by Espressif Systems”.
- Trước khi nạp, hãy gõ tên Wi-Fi và mật khẩu của bạn vào WIFI_SSID và WIFI_PASS ở đầu sketch.
- Mở sketch trong Arduino IDE.
- Cắm bo vào máy tính bằng cáp USB-C.
- Vào Tools > Board > ESP32S3 Dev Module, rồi Tools > Port và chọn cổng của bo.
- Bấm Upload (nút mũi tên). Chờ dòng “Done uploading”.