Async IOT
#include <stdio.h>
#include <ESPAsync_WiFiManager.h>
AsyncWebServer server(80);
DNSServer dnsServer;
#define host "TMR01"
String station = "TMR01";
#define OK Serial.println("ok");
#define NOT_OK Serial.println("Not ok");
#define MEM_INFO MyMemInfo();
/*#include <FirebaseESP8266.h>
#define tmr_debug false
#define FIREBASE_HOST "wt-.firebaseio.com"
#define FIREBASE_AUTH "JpQKm0dK7vMYc86gaJKDQyquDMEPgPNQti8lAAA"
FirebaseData fbdo;
FirebaseJson json;*/
unsigned long stTime;
unsigned long useTime;
unsigned long total_mem;
unsigned long total_use;
FSInfo fs_info;
int Year=21,Month=2,Day=9,Hour=0,Minute =0;
float Rh=0.00, Rt=0.00, Tp1=0.00, Tp2=0.00;
String fname, sTm, sRh,sRt,sT1,sT2;
char buf[200];
void MyMemInfo(){
//-----------------------------
useTime=millis()-stTime;
Serial.print ("Format time = ");
Serial.print(useTime,DEC);
Serial.println(" mS");
//-----------------------------
Serial.print ("TotalBytes of Fs mem = ");
Serial.print(fs_info.totalBytes,DEC);
Serial.println(" Byte");
//-----------------------------
Serial.print ("UsedBytes of Fs mem = ");
Serial.print(fs_info.usedBytes,DEC);
Serial.println(" Byte");
//-----------------------------
Serial.print ("BlockSize of Fs = ");
Serial.print(fs_info.blockSize,DEC);
Serial.println(" Byte");
//-----------------------------
Serial.print ("pageSize of Fs = ");
Serial.print(fs_info.pageSize,DEC);
Serial.println(" Byte");
//-----------------------------
Serial.print ("MaxOpenFiles of Fs = ");
Serial.print(fs_info.maxOpenFiles,DEC);
Serial.println(" file");
//-----------------------------
Serial.print ("maxPathLength of Fs = ");
Serial.print(fs_info.maxPathLength,DEC);
Serial.println(" Byte");
}
void set_Fname(){
sprintf( buf,"Y%02dM%02dD%02d",Year,Month,Day);
fname = "/"+station+"/"+String(buf)+".CSV";
}
void save_to_csv( String fname, String sTm, String sRh, String sRt, String sT1, String sT2 )
{
if (!SPIFFS.exists(fname))
{
File f = SPIFFS.open(fname, "w");
if (f) {
f.print(fname+", * , * , * , * \r\n");
f.print("time ,Rh ,Rt ,T1 ,T2 \r\n");
}
f.close();
}
String fdata = sTm + "," ;
fdata += sRh + "," ;
fdata += sRt + "," ;
fdata += sT1 + "," ;
fdata += sT2 + "\r\n";
File f = SPIFFS.open(fname, "a");
if (f) { f.print(fdata); }
f.close();
}
//https://drive.google.com/drive/folders/19lAf8P9oaqXvTEwoXrM8Js-o1tKMPfBi?usp=sharing
void get_sval_val(){
sprintf(buf,"%02d:%02d",Hour,Minute);
sTm = String(buf);
sprintf(buf,"%2.02f",Rh);
sRh = String(buf);
sprintf(buf,"%2.02f",Rt);
sRt = String(buf);
sprintf(buf,"%2.02f",Tp1);
sT1 = String(buf);
sprintf(buf,"%2.02f",Tp2);
sT2 = String(buf);
}
bool get_csv_sval(String path, String sTm){
bool result =false;
File f = SPIFFS.open(fname, "r");
if (f) {
while (f.available()) {
String income=f.readStringUntil('\n');
if (sTm==String(income.substring(0,5))){
sRh=income.substring(6,11);
sRt=income.substring(12,17);
sT1=income.substring(18,23);
sT2=income.substring(24,29);
result=true;
}
}
return(result);
}
}
/*bool save_to_fb(String path, String sTm, String sRh,String sRt,String sT1,String sT2){
json.clear();
json.add("Tm",sTm);
json.add("Rh",sRh);
json.add("Rt",sRt);
json.add("T1",sT1);
json.add("T2",sT2);
return (Firebase.pushJSON(fbdo, path, json));
}*/
void sim_val(){
Rh = 1.1*random(20, 90);
Rt = 1.1*random(18, 35);
Tp1= -1.1*(random(15, 25)) ;
Tp2= 1.1*random(100);
}
void Fprint(){
Serial.println("data in file=================");
File f = SPIFFS.open(fname, "r");
if (f) {
while (f.available()) {
String income=f.readStringUntil('\n');
income+="\n";
Serial.print(income);
}
}
f.close();
}
void setup() {
Serial.begin(115200);
Serial.println();
//SPIFFS.format();
if (SPIFFS.begin()){OK}else{ NOT_OK}
SPIFFS.info(fs_info);
stTime=millis();
MEM_INFO
ESPAsync_WiFiManager ESPAsync_wifiManager(
&server,
&dnsServer,
host);
ESPAsync_wifiManager.resetSettings();
//ESPAsync_wifiManager.setAPStaticIPConfig(IPAddress(192,168,186,1),
// IPAddress(192,168,186,1),
// IPAddress(255,255,255,0));
ESPAsync_wifiManager.autoConnect(host);
if (WiFi.status() == WL_CONNECTED)
{ Serial.print("Connected. Local IP: ");
Serial.println(WiFi.localIP());
}
else
{ Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status())); }
server.on("/today", HTTP_GET, [](AsyncWebServerRequest *request){
sprintf(buf,"Y%dM%02dD%02d",Year,Month,Day);
String path = "/"+station+"/"+String(buf)+ ".CSV";
if (SPIFFS.exists(path)){request->send(SPIFFS, path,"text/csv");}
else{request->send(200, "text/plain", "data not found");}
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! I am ESP8266.");
});
server.begin();
Serial.println(WiFi.softAPIP());
// Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
// Firebase.reconnectWiFi(true);
// fbdo.setBSSLBufferSize(1024, 1024);
// fbdo.setResponseSize(1024);
// Firebase.setReadTimeout(fbdo, 1000 * 5);
// Firebase.setwriteSizeLimit(fbdo, "tiny");
// Firebase.setFloatDigits(2);
}
//====================================
void loop() {
if (Hour==24){Fprint(); Hour++;}
if (Hour>23){ return;}
set_Fname();
sim_val();
if (Hour<24){
get_sval_val();
save_to_csv(fname, sTm, sRh, sRt, sT1, sT2);
}
if (Minute ==0){Minute=30;}
else{
Minute =0;
Hour = Hour+1;
}
}
//----------------------------------------------------------------------------------------
seRver.on("/info", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/info.txt","text/html");
});
ความคิดเห็น
แสดงความคิดเห็น