在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 133|回复: 0

[求助] SD卡無法錄入訊息

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

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


想請問怎麼解決,並且也想知道問題原因

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

小黑屋| 手机版| 关于我们| 联系我们| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2025-5-30 05:31 , Processed in 0.013546 second(s), 7 queries , Gzip On, MemCached On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表