|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
#include <Wire.h> // I²C的標準libriry
#include <Adafruit_SHT31.h> //SHT31的librirry
#include <ArduinoJson.h>
#include <BH1750.h>
// // [Mega2560]
// -USB (UART0) → 筆電 (地面站)
// -I²C (20/21) → SHT31, BH1750
// -SPI (50/51/52/CS53) → micro-SD
// 預計執行結果 包成json檔
// {"time_ms":4257,"temp_c":25.13,"hum_pct":56.90}
// {"time_ms":6257,"temp_c":25.12,"hum_pct":56.91}
const uint32_t SERIAL_BAUD = 9600;
const uint16_t PERIOD_MS = 2000;
const size_t JSON_CAP = 128;
const int SD_CS_PIN = 53;
// SD 卡選項
#include <SPI.h>
#include <SD.h>
File logFile; // CSV 紀錄檔
/*
#include <mcp_can.h>
MCP_CAN CAN0(10); // MCP2515 模組 CS=10
void canSend(float tmp, float hum) {
byte buf[8];
memcpy(buf, &tmp, 4);
memcpy(buf + 4, &hum, 4);
CAN0.sendMsgBuf(0x301, 0, 8, buf);
}
*/
Adafruit_SHT31 sht31 = Adafruit_SHT31();
BH1750 lightMeter;
BH1750 LightSensor;
String sensorMode="BH";
bool enableSD = 1;
void setup() {
Serial.begin(SERIAL_BAUD);
while (!Serial) { ; }
Wire.begin();
// 初始化 SHT31 or BH1750
if (sensorMode == "SHT"){
Serial.println(F("=== SHT31 JSON Demo Boot ==="));
if (!sht31.begin(0x44)) {
Serial.println(F("[ERR] 找不到 SHT31,請檢查 SDA/SCL!"));
while (1)
; // 卡住,等人來救
}
} else if(sensorMode == "BH"){
Serial.println(F("=== BH1750 JSON Demo Boot ==="));
lightMeter.begin();
LightSensor.begin();
Serial.println(F("BH1750 Test begin"));
} else {
Serial.println(F("Sensor Function Disabled"));
}
Serial.println(F("Sensor OK,開始輸出 JSON"));
//SD 卡初始化
if (enableSD){
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
while (1)
;
}
Serial.println("initialization done.");
logFile = SD.open("telemetry.txt", FILE_WRITE);
if (logFile) {
Serial.print("Writing header to telemetry.txt...");
logFile.println(F("epoch_ms,tempC,humPct"));
logFile.flush();
logFile.close();
Serial.println(F("[SD] ready"));
} else {
// if the file didn't open, print an error:
Serial.println("error opening telemetry.txt when setup");
}
} else {
Serial.println("SD card function unenabled");
}
//CAN-Bus 初始化
/*
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println(F("[CAN] init OK"));
} else {
Serial.println(F("[ERR] CAN init fail"));
}
*/
}
void loop() {
static unsigned long last = 0;
const unsigned long now = millis();
if (now - last >= PERIOD_MS) {
last = now;
//讀取溫溼度 for SHT
float tempC;
float hum;
// Read Light level and sensor for BH
uint16_t lux;
uint16_t lux2;
StaticJsonDocument<JSON_CAP> doc;
if( sensorMode == "SHT"){
//送 溫溼度 JSON
tempC = sht31.readTemperature();
hum = sht31.readHumidity();
doc["time_ms"] = now; // 一旦開機就開始計算秒數
doc["temp_c"] = tempC;
doc["hum_pct"] = hum;
} else if (sensorMode == "BH") {
// BH Json output (光照)
lux = lightMeter.readLightLevel();
lux2 = LightSensor.readLightLevel();
doc["time_ms"] = now; // 一旦開機就開始計算秒數 doc["lightlvl"] = lux;
doc["lightsnsr"] = lux2;
} else {
Serial.println("Sensor Function Disabled, empty output:");
}
serializeJson(doc, Serial);
Serial.println(); // 用換行作為封包邊界
if(enableSD){
//SD 卡記錄 (CSV)
logFile = SD.open("telemetry.txt", FILE_WRITE);
if (logFile) {
Serial.print("Writing new data to telemetry.txt...");
logFile.print(now);
logFile.print(',');
logFile.print(tempC, 2);
logFile.print(',');
logFile.println(hum, 2);
logFile.flush(); // 立即寫盤避免斷電遺失
logFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening telemetry.txt when writing");
}
// SD card read
logFile = SD.open("telemetry.txt");
if (logFile) {
Serial.println("telemetry.txt:");
while (logFile.available()) {
Serial.write(logFile.read());
}
} else {
// if the file didn't open, print an error:
Serial.println("error opening telemetry.txt when read");
}
}
/* ③ CAN-Bus 廣播 ── */
/*
canSend(tempC, hum); // 0x301:TCS 遙測
*/
}
}
各位前輩好,我在使用SD卡紀錄BH1750的資訊時遇到了無法創建檔案(程式碼中寫的telemetry.txt)的問題,其中輸出
Initializing SD card...initialization done.
error opening telemetry.txt when setup
想請問怎麼解決,並且也想知道問題原因
|
|