Đo lường / Data logger
Đặt câu hỏi- 0 trả lời0 xem
ESP32 + DS3231 + thẻ SD = data logger ghi CSV có timestamp
Data logger đơn giản: **ESP32** đọc cảm biến, lấy giờ thật từ **DS3231 RTC** (không bị reset khi mất nguồn), ghi CSV lên **thẻ SD** — mở bằng Excel ngay. ## Pin map — 2 module chia 2 bus | Module | Chân | ESP32 | |---|---|---| | DS3231 (I2C) | SDA / SCL | GPIO21 / GPIO22 | | SD card (SPI) | CS / MOSI / MISO / SCK | GPIO5 / 23 / 19 / 18 | | Cả hai | VCC / GND | 5V (hoặc 3V3) / GND | ## Code ```cpp #include <Wire.h> #include <RTClib.h> #include <SPI.h> #include <SD.h> RTC_DS3231 rtc; const int SD_CS = 5; void setup() { Serial.begin(115200); Wire.begin(21, 22); if (!rtc.begin()) Serial.println("Khong thay DS3231"); // chi set 1 lan khi nap lan dau: // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); if (!SD.begin(SD_CS)) Serial.println("Khong thay the SD"); // ghi header neu file moi if (!SD.exists("/log.csv")) { File f = SD.open("/log.csv", FILE_WRITE); f.println("datetime,sensor"); f.close(); } } void loop() { DateTime now = rtc.now(); float v = analogRead(34) * 3.3f / 4095; File f = SD.open("/log.csv", FILE_APPEND); if (f) { f.printf("%04d-%02d-%02d %02d:%02d:%02d,%.3f\n", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second(), v); f.close(); } delay(10000); // ghi moi 10s } ``` ## Lưu ý - **DS3231 có pin CMOS** (CR2032/LIR2032) — giờ chạy tiếp khi mất nguồn; chỉ cần `rtc.adjust` một lần. - Module DS3231 rẻ dùng LIR2032 (sạc lại được); gắn CR2032 thường vào module có mạch sạc = pin phồng — tháo diode sạc hoặc dùng LIR2032. - Ghi SD thường xuyên → định kỳ `file.close()`/`flush()` tránh mất bảng FAT khi rút nguồn. - Thẻ format **FAT32**; lỗi init thử thẻ khác. - Cần giờ chính xác hơn: đồng bộ NTP khi có WiFi rồi `rtc.adjust` lại mỗi ngày.
BiLiChip
- 0 trả lời0 xem
ESP32 ghi dữ liệu cảm biến lên Google Sheets miễn phí — không cần server
Muốn xem log nhiệt độ/độ ẩm ở nhà từ xa mà không muốn dựng server → ghi thẳng lên **Google Sheets** qua Apps Script Web App. Miễn phí, đủ cho vài nghìn bản ghi. ## Bước 1 — Tạo Google Sheet + Apps Script 1. Tạo sheet mới, đặt header dòng 1: `thoigian | nhietdo | doam` 2. Extensions → Apps Script, dán: ```js function doGet(e) { const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sh.appendRow([new Date(), e.parameter.t, e.parameter.h]); return ContentService.createTextOutput("OK"); } ``` 3. Deploy → New deployment → type **Web app** → Execute as: **Me** → Who has access: **Anyone** → Deploy → copy URL `/exec`. ## Bước 2 — Code ESP32 ```cpp #include <WiFi.h> #include <HTTPClient.h> #include <DHT.h> DHT dht(4, DHT22); const char* URL = "https://script.google.com/macros/s/XXXX/exec"; void loop() { if (WiFi.status() == WL_CONNECTED) { float t = dht.readTemperature(), h = dht.readHumidity(); if (!isnan(t)) { HTTPClient http; http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); http.begin(URL + "?t=" + String(t) + "&h=" + String(h)); http.GET(); http.end(); } } delay(300000); // 5 phut/lan - free tier du } ``` ## Lưu ý - Apps Script redirect nên bắt buộc `setFollowRedirects`. - Free quota ~20.000 request/ngày — gửi 5–10 phút/lần là an toàn. - Xem chart trực tiếp trong Google Sheets: Insert → Chart trên cột dữ liệu. - Muốn nhanh hơn realtime thì chuyển sang MQTT/Blynk; Sheets hợp log dài hạn.
BiLiChip