/*
* DeepSleep()でSPIFFS領域がクリアされるかどうかのテスト
*/
#include <ESP8266WiFi.h>
#include "FS.h"
#define SLEEP 10
//RTC memory(512Byte)の定義
struct {
uint8_t data[512]; // User Data
} rtcData;
void listDir() {
char cwdName[2];
strcpy(cwdName,"/");
Dir dir=SPIFFS.openDir(cwdName);
while( dir.next()) {
String fn, fs;
fn = dir.fileName();
fn.remove(0, 1);
fs = String(dir.fileSize());
Serial.println("<" + fn + ">
size=" + fs);
} // end while
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("ESP.getResetReason()=");
Serial.println(ESP.getResetReason());
String resetReason = ESP.getResetReason();
/*
enum rst_reason {
REANSON_DEFAULT_RST = 0, // ノーマルスタート。電源オンなど。
REANSON_WDT_RST = 1, // ハードウェアウォッチドッグによるリセット
REANSON_EXCEPTION_RST = 2, //
例外によるリセット。GPIO状態は変化しない
REANSON_SOFT_WDT_RST = 3, //
ソフトウェアウォッチドッグによるリセット。GPIO状態は変化しない
REANSON_SOFT_RESTART = 4, //
ソフトウェアによるリセット。GPIO状態は変化しない
REANSON_DEEP_SLEEP_AWAKE= 5, // ディープスリープ復帰
REANSON_EXT_SYS_RST = 6, // 外部要因(RSTピン)によるリセット。
};
*/
rst_info *prst = ESP.getResetInfoPtr();
/*
struct rst_info{
uint32 reason;
uint32 exccause;
uint32 epc1;
uint32 epc2;
uint32 epc3;
uint32 excvaddr;
uint32 depc;
};
*/
Serial.print("reset reason=");
Serial.println(prst->reason);
// RTC memoryからデータを読み込む
if (ESP.rtcUserMemoryRead(0, (uint32_t*)
&rtcData, sizeof(rtcData))) {
Serial.println("rtcUserMemoryRead
Success");
if (prst->reason != 5) { // Not
REANSON_DEEP_SLEEP_AWAKE
for (int i = 0; i <
sizeof(rtcData.data); i++) {
rtcData.data[i]
= 0;
}
} else {
rtcData.data[0]++;
}
} else {
Serial.println("rtcUserMemoryRead
Fail");
}
// RTC memoryにデータを書き込む
if (ESP.rtcUserMemoryWrite(0, (uint32_t*)
&rtcData, sizeof(rtcData))) {
Serial.println("rtcUserMemoryWrite
Success");
} else {
Serial.println("rtcUserMemoryWrite
Fail");
}
Serial.println("SPIFFS Started");
SPIFFS.begin();
// 初回起動時のみファイルを作成
if (rtcData.data[0] == 0) {
//Serial.println("SPIFFS Started");
//SPIFFS.begin();
// Next lines have to be done ONLY
ONCE!!!!!When SPIFFS is formatted ONCE you can comment
these lines out!!
Serial.println("Please wait 30 secs for
SPIFFS to be formatted");
SPIFFS.format();
Serial.println("Spiffs formatted");
// open file for writing
File f = SPIFFS.open("/f.txt", "w");
if (!f) {
Serial.println("file open
failed");
} else {
Serial.println("======
Writing to SPIFFS file =========");
// write 10 strings to file
for (int i=1; i<=10;
i++){
f.print("Line :
");
f.println(i);
} // end for
f.close();
} // end if
// DeepSleepからの復帰
} else {
//Serial.println("SPIFFS Started");
//SPIFFS.begin();
// list directory
listDir();
// open file for reading
File f = SPIFFS.open("/f.txt", "r");
if (!f) {
Serial.println("file open
failed");
} else {
Serial.println("======
Reading from SPIFFS file =======");
// write 10 strings to file
for (int i=1; i<=10;
i++){
String
s=f.readStringUntil('\n');
Serial.print(i);
Serial.print(":");
Serial.println(s);
} // end for
f.close();
} // end if
}
Serial.println("DEEP SLEEP START!!");
for(int i=0;i<20;i++) delay(1000);
ESP.deepSleep(SLEEP * 1000 * 1000 ,
WAKE_RF_DEFAULT);
delay(1000);
}
void loop() {
}
|