首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想使用arduino中的spiffs存储来自我的esp32中传感器的数据

我想使用arduino中的spiffs存储来自我的esp32中传感器的数据
EN

Stack Overflow用户
提问于 2022-05-27 20:12:32
回答 1查看 475关注 0票数 -1

我想知道如何在我的esp32中存储传感器24小时的数据?我遇到的问题是,在我的串行监视器上,文本是通过尖峰显示的,但是当打开文本文档时,什么都没有。我还试图通过使用首选项来保存数据,但没有表示空间,我发现最好使用space来处理长数据。

守则:

代码语言:javascript
复制
//#include "SPIFFS.h"
#include <heltec.h>
#include <DHT12.h>
SSD1306Wire  aff(0x3c, SDA_OLED, SCL_OLED, RST_OLED, GEOMETRY_64_32);
DHT12 dht12;

void setup() {

  Serial.begin(115200);
  dht12.begin();
  aff.init();
  aff.flipScreenVertically();
  aff.setFont(ArialMT_Plain_10);
}

void loop() {
  float temp = dht12.readTemperature();
  float hum = dht12.readHumidity();
  Serial.println(temp);
  
  aff.clear();
  aff.drawString(0, 0, "Temp:" + (String)temp + "�C");
  aff.drawString(0, 10, "Hum:" + (String)hum + "%");
  aff.display();

   if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  //--------- Write to file
  File fileToWrite = SPIFFS.open("/test.txt", FILE_WRITE);

  if (!fileToWrite) {
    Serial.println("There was an error opening the file for writing");
    return;
  }

  if (fileToWrite.print("ORIGINAL LINE")) {
    Serial.println("File was written");;
  } else {
    Serial.println("File write failed");
  }

  fileToWrite.close();

  //--------- Apend content to file
  File fileToAppend = SPIFFS.open("/test.txt", FILE_APPEND);

  if (!fileToAppend) {
    Serial.println("There was an error opening the file for appending");
    return;
  }

  if (fileToAppend.println(temp)) {
    Serial.println("File content was appended");
  } else {
    Serial.println("File append failed");
  }

  fileToAppend.close();

  //---------- Read file
  File fileToRead = SPIFFS.open("/test.txt");

  if (!fileToRead) {
    Serial.println("Failed to open file for reading");
    return;
  }

  Serial.println("File Content:");

  while (fileToRead.available()) {

    Serial.write(fileToRead.read());
  }

  fileToRead.close();
  delay(3000);
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-05 23:23:15

首先,您需要配置一个SPIFFS分区。看起来您使用的是Arduino IDE,因为我当时没有看到包含的#。建议您尝试使用PlatformIO -因为它将与许多董事会工作。

https://community.platformio.org/t/solved-choose-1m-spiffs-partition-on-esp32/20935

代码中似乎有一些逻辑错误--如果打开FILE_WRITE失败,或者FILE_APPEND文件失败,您的逻辑就退出。尝试始终使用FILE_APPEND打开--如果文件不存在,并且您有一个分区,则应该编写数据.

即使文件不存在,IIRC打开附件也会成功,因此值得尝试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72410615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档