我只是找到一些好的代码来定期使用带时间日期的温度和湿度值来编写
文件系统。
我无处可寻,所以我做了我的。
这是一个已完成的项目:
DS3231 模块
Esp8266 12E
DHT22
Rtclib
Dht lib(支持 DHT11 和 DHT22 等)
代码从读取文件记录和强制离线模式开始。定期将值从 DHT22 保存到
。
代码在首次使用时对 Spiff 进行格式化以制作初始化文件(以检查其格式是否正确)
您可以使用简单的 made 函数来读/写/格式化 spiffs。
看见:
代码:
全选
/*
Github:
http://github.com/ajaybnl
More Source Code on Github
*/
#include
#include
#include
#include
#include
#include
#include "DHT.h"
#include "RTClib.h"
RTC_DS3231 rtc;
#define DHTPIN D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
long lastReading
time = 0;
char results[4];
int fail = 0;
int x1 = 0;
unsigned long timer1 = 0;
int offline = 0;
String sdata;
unsigned long timer2 = 0;
int pdate = 0;
//#####################################################
void setup() {
Serial.begin(115200);
Serial.println("init...");
Serial.println("Spiffs Read: ")
//Read all data of file
sdata="";
spiff("data.txt", 1, "");
Serial.println(sdata);
Serial.println("---------");
//DHT init
dht.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
}
// if (rtc.lostPower()) {
// Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// }
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.dayOfTheWeek());
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
//#####################################################
void loop() {
//5 sec timer
if (((millis() - timer2) / 1000) > 5) {
DateTime now = rtc.now();
String e = "\r\n";
String date1;
date1 = now.day();
date1 += ":";
date1 += now.month();
date1 += ":";
date1 += now.year();
date1 += e;
String time1;
time1 = now.hour();
time1 += ":";
time1 += now.minute();
//next start write date
if (now.day() != pdate) {
Serial.print("Writing Log: Date ");
Serial.println(date1);
pdate = now.day();
spiff("data.txt", 0, date1);
}
int h = dht.readHumidity();
int t = dht.readTemperature();
if (isnan(h) || isnan(t) ) {
// Serial.println("Sensor Err");
}
if (t > 100 || h > 100){t=50;h=50; }// just for test
String d;
d = time1;
d += " T:" ;
d += t;
d += " H:" ;
d += h;
d += "\r\n";
Serial.print("Saving = ");
Serial.print(d);
// Write Data
spiff("data.txt", 0, d);
timer2 = millis();
}
}
//#####################################################
// Usage: spiff(FILE_NAME,0=WRITE/1=READ,STRING DATA To Write)
//Write String Data: spiff("data.txt", 0, d);
//Read String Data in "sdata" Variable spiff("data.txt", 1);
//#####################################################
long spiff(String file, int read, String value = "") {
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return (0);
}
FSInfo fs_info;
SPIFFS.info(fs_info);
//Serial.print("Total Bytes: ");
// Serial.println(fs_info.totalBytes);
// Serial.print("Used Bytes: ");
// Serial.println(fs_info.usedBytes);
if ((fs_info.totalBytes - fs_info.usedBytes) < 1000) {
Serial.println("Memory Full");
return (0);
}
//Init the Spiff FS (check if our INIT file exists or not
File f = SPIFFS.open("init", "r+");
if (!f) {
Serial.println("Formatting Spiff...");
delay(200);
//Format
SPIFFS.format();
//Make a Init File
File f1 = SPIFFS.open("init", "w+");
f1.println("init");
f1.close();
Serial.println("Done");
delay(200);
}
//######################################################
//If Reading Check file
if (read == 1) {
if (!SPIFFS.exists(file)) {
Serial.println("File not exists");
return (0);
}
}
//If Reading Open file R or A
if (read == 1) {
f = SPIFFS.open(file, "r+");
} else {
f = SPIFFS.open(file, "a");
}
int s = 0;
//if File Exists
if (f) {
s = f.size();
// Serial.printf("File Opened , Size=%d\r\n", s);
//Read
if (read == 1) {
sdata = f.readString();
} else {
//Write data to file
f.println(value);
}
f.close();
//Return the size of file
return (s);
} else {
Serial.print("Unable to open file ");
Serial.println(file);
}
}