สร้างเครื่องบันทึกข้อมูล SD Card
พื้นที่จัดเก็บข้อมูลเซ็นเซอร์IoT
เครื่องบันทึกข้อมูลจริงแบบออฟไลน์ ไม่ต้องใช้ Wi-Fi ไม่ต้องมีบัญชีคลาวด์ แค่บันทึกข้อมูลที่ประทับเวลาได้อย่างแม่นยำลงในการ์ดโดยตรง
รวมชิ้นส่วนจริง 7 รายการ
ราคารวมชุดนี้: 1,679.00 ฿
ยังไม่รวมส่วนลดคำสั่งซื้อที่คุณอาจได้รับตอนชำระเงิน
สิ่งที่คุณต้องใช้
บอร์ดพัฒนา ESP32-C3 SuperMini — Wi-Fi และ Bluetooth, USB-C
670.00 ฿
โมดูลเซ็นเซอร์อุณหภูมิและความชื้น AHT20 — I2C
260.00 ฿
โมดูล Micro SD Card — SPI, รองรับ 3.3V/5V
75.00 ฿
โมดูลนาฬิกาเรียลไทม์ DS3231 — สำรองแบตเตอรี่
149.00 ฿
เบรดบอร์ดไร้บัดกรี 830 จุด
125.00 ฿
ชุดสายจัมเปอร์ — ผู้-ผู้, ผู้-เมีย, เมีย-เมีย
220.00 ฿
สายชาร์จและซิงค์ข้อมูล USB-C — 1 ม.
180.00 ฿
แผนภาพการต่อสาย
โค้ดเริ่มต้น
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <Adafruit_AHTX0.h>
#define SDA_PIN 8 // ESP32-C3 default I2C SDA - adjust if your board differs
#define SCL_PIN 9 // ESP32-C3 default I2C SCL - adjust if your board differs
#define SPI_SCK_PIN 4 // ESP32-C3 default hardware SPI pins - adjust to match your board's silkscreen if different
#define SPI_MISO_PIN 5
#define SPI_MOSI_PIN 6
#define SD_CS_PIN 7 // SD module chip select - any free digital GPIO
const char* LOG_FILENAME = "/datalog.csv";
const unsigned long LOG_INTERVAL_MS = 5000; // log every 5 seconds
Adafruit_AHTX0 aht;
RTC_DS3231 rtc;
unsigned long lastLog = 0;
void setup() {
Serial.begin(115200);
delay(500);
Wire.begin(SDA_PIN, SCL_PIN);
if (!aht.begin()) {
Serial.println("AHT20 not found - check I2C wiring");
while (true) delay(1000);
}
if (!rtc.begin()) {
Serial.println("DS3231 not found - check I2C wiring");
while (true) delay(1000);
}
if (rtc.lostPower()) {
// Sets the RTC to the sketch's compile time. Only needed once after the
// coin cell has been out / on first power-up - remove after the RTC is set.
Serial.println("RTC lost power, setting time to compile time");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
SPI.begin(SPI_SCK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN, SD_CS_PIN);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card init failed - check wiring/card");
while (true) delay(1000);
}
if (!SD.exists(LOG_FILENAME)) {
File f = SD.open(LOG_FILENAME, FILE_WRITE);
if (f) {
f.println("timestamp,temperature_C,humidity_percent");
f.close();
}
}
Serial.println("Logger ready");
}
void loop() {
if (millis() - lastLog < LOG_INTERVAL_MS) return;
lastLog = millis();
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
DateTime now = rtc.now();
char timestamp[20];
snprintf(timestamp, sizeof(timestamp), "%04d-%02d-%02d %02d:%02d:%02d",
now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
File f = SD.open(LOG_FILENAME, FILE_WRITE);
if (f) {
f.printf("%s,%.2f,%.2f\n", timestamp, temp.temperature, humidity.relative_humidity);
f.close();
Serial.printf("Logged: %s, %.2f C, %.2f %%\n", timestamp, temp.temperature, humidity.relative_humidity);
} else {
Serial.println("Failed to open log file for append");
}
}ไม่แน่ใจขั้นตอนไหน? ถามเรา