Created
January 28, 2021 01:06
-
-
Save dj1711572002/889f99fda307ea9cc0b466b04539bd81 to your computer and use it in GitHub Desktop.
M5ATOMIC TF RTK-MovingBase LOG SYSTEM rev02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//M5Atom-F9P_MovingBaseSend_Serial2_ESP-NOW_Add_Serial1_rev01.ino | |
//#include <M5StickC.h> | |
#include<M5Atom.h> | |
#include <BluetoothSerial.h> | |
#include <esp_now.h> | |
#include <WiFi.h> | |
#include <SPI.h> | |
#include "FS.h" | |
#include "SD.h" | |
#include "esp_http_client.h" | |
//---WiFi--------------- | |
const char* ssid = "Your WiFi SSID";// | |
const char* password = "WiFI pass";// | |
//f9pdata------------------- | |
uint8_t mdata[12];//monitor para | |
uint8_t f9pHead[4]; | |
uint8_t fdata[172];//Binary Array | |
char f9pchr[600];//ASCII Char Array | |
//File--------------------------- | |
#define FORMAT_SD_IF_FAILED true | |
char file_name[20] = "/test1.txt"; | |
int var_size = 2000; | |
String Listname[1000] ; | |
char fname[30];//File name char Array | |
//-Logging-------------------- | |
int Logon; | |
int sdFlag=0; | |
int monFlag=0; | |
int headerFlag=0; | |
int startPin = 19; | |
int t, t_1; | |
int x = 1; | |
int i = 0; | |
int j, k; | |
int stime; | |
int waitFlag; | |
int rt1; | |
int rcvFlag = 0; | |
int firstFlag; | |
int sendFlag = 0; | |
uint8_t macTarget[6]; | |
int count, ci ; | |
String sdata; | |
String pdata;//NAV-PVT data | |
//==================UBX_DEC parameters========================== | |
int FIX; | |
String sFix; | |
int JST; | |
int SEC; | |
long relN; | |
long relE; | |
long relD; | |
long Length; | |
long Head; | |
long Long; | |
long Lati; | |
long Seah; | |
long Acc2d; | |
char numSV;//satellite number | |
String timstmp;//PVT MMDDHHMM | |
long iTow; | |
int iTows; | |
long su; | |
int sNo;//pvt start | |
int rNo;//relposned start | |
//=================================================================== | |
//============================================================================================ | |
//=========================ESP-NOW Functions================================================== | |
//==============slave:PC master:SCP,AD7194================================================================= | |
// マルチキャスト用 MAC登録 | |
// PC M5ATOM COM7=D8:A0:1D:50:54:E8 | |
// SCP 1ch M5Stick COM13=D8:A0:1D:58:56:CC | |
// AD7194 M5ATOM COM9=24:0A:C4:A5:30:DC | |
esp_now_peer_info_t slave; //slave peer定義 | |
uint8_t data[100];//12byte 4*value0,value1,value2,value3 //送信データ配列 | |
char cdata[100]; | |
//char *cdata;//シリアル受信したstring | |
//uint8_t *data;//シリアル受信stiringを文字列配列にしてそれをuint8_t配列に変換する、ポインタで定義しておく 配列数が決まってからnewで再定義 | |
int dnum;//data配列の要素数 | |
volatile int comst = 0; | |
volatile int comtr = 0; | |
// 送信コールバック************************************************************************************* | |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
char macStr[18]; | |
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", | |
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
//Serial.print("Last Packet Sent to: "); | |
//Serial.println(macStr); | |
//Serial.print("Last Packet Send Status: "); | |
//Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); | |
} | |
//*********************************************************************************************** | |
// 受信コールバック************************************************************************** | |
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { | |
char macStr[18]; | |
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", | |
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
macTarget[0] = mac_addr[0]; | |
macTarget[1] = mac_addr[1]; | |
macTarget[2] = mac_addr[2]; | |
macTarget[3] = mac_addr[3]; | |
macTarget[4] = mac_addr[4]; | |
macTarget[5] = mac_addr[5]; | |
rcvFlag = 1; | |
Serial.print( macStr); | |
Serial.print(","); | |
Serial.print(data_len); | |
for ( int i = 0 ; i < data_len ; i++ ) { | |
Serial.print(data[i]); | |
Serial.print(" "); | |
} | |
Serial.println(""); | |
//====Go Stop | |
if(data[0]==0x73)//'s' Log StartFlag | |
{ | |
if(sdFlag==1) | |
{ | |
sdFlag=0; | |
} | |
else if(sdFlag==0) | |
{ | |
sdFlag=1; | |
firstFlag=1;// start Fname timestamp | |
} | |
} | |
if(data[0]==0x6d)//'m' monitorFlag | |
{ | |
if(monFlag==1) | |
{ | |
monFlag=0; | |
} | |
else if(monFlag==0) | |
{ | |
monFlag=1; | |
} | |
} | |
}//ESP-NOW Rcv********************************************************************** | |
//f9pMonitor Parameter cal---------------------------------- | |
// iTow,numSV,hAcc,relN,relE,relLength,long,lat,Head | |
void f9pMon() | |
{ | |
if (monFlag==1) | |
{ | |
//==================MB Monitor ============================== | |
sNo=0;//PVT start No | |
// Serial.printf("sec 16=%d\n\r",fdata[sNo+16]); | |
// SEC=(int)fdata[sNo+16]; | |
FIX=fdata[sNo+26]; | |
if (FIX==3){sFix="FIXED";} | |
if (FIX==0){sFix="NO FIX";} | |
Serial.printf("FIX=%d,sFix=%s\n\r",FIX,sFix); | |
JST=(fdata[sNo+14]+9)%24; | |
timstmp=String(fdata[sNo+12],DEC)+"-"+String(fdata[sNo+13],DEC)+"-"+String(JST,DEC)+":"+String(fdata[sNo+15],DEC);//MMDDHHMM | |
Long=B2L(fdata[sNo+33],fdata[sNo+32],fdata[sNo+31],fdata[sNo+30]);//Longtitude上位ー>下位 | |
Lati=B2L(fdata[sNo+37],fdata[sNo+36],fdata[sNo+35],fdata[sNo+34]);//latitude 上位ー>下位 | |
Seah=B2L(fdata[sNo+45],fdata[sNo+44],fdata[sNo+43],fdata[sNo+42]);//SeaHeight 上位ー>下位 | |
Acc2d=B2L(fdata[sNo+49],fdata[sNo+48],fdata[sNo+47],fdata[sNo+46]);//Accuracy 2D 上位ー>下位 | |
rNo=100;//RELPOSNED Start No | |
relN=B2L(fdata[rNo+17],fdata[rNo+16],fdata[rNo+15],fdata[rNo+14]); | |
relE=B2L(fdata[rNo+21],fdata[rNo+20],fdata[rNo+19],fdata[rNo+18]); | |
relD=B2L(fdata[rNo+25],fdata[rNo+24],fdata[rNo+23],fdata[rNo+22]); | |
Length=B2L(fdata[rNo+29],fdata[rNo+28],fdata[rNo+27],fdata[rNo+26]); | |
Head=B2L(fdata[rNo+33],fdata[rNo+32],fdata[rNo+31],fdata[rNo+30]); | |
numSV=fdata[sNo+29]; | |
//Serial.printf("timestmp=%s,Long=%d,Lati=%d,Seaheight=%d\n\r",timstmp,Long,Lati,Seah); | |
//Serial.printf("Acc2d=%d,relN=%d,relE=%d,relD=%d,Length=%d,Head=%d,NumSV=%d\n\r",Acc2d,relN,relE,relD,Length,Head,numSV); | |
Serial.printf("Acc2d=%d,numSV=%d,Length=%d,Long=%d,Lati=%d,relN=%d,relE=%d,relD=%d\n\r",Acc2d,numSV,Length,Long,Lati,relN,relE,relD); | |
//M5StickC ESP-NOW Send Monitor-mondata[]------------------------------------------- | |
//Acc2d:4byte,numSV:1byte,Length:4byte,Long:4byte,Lati:4byte,relN:4byte,relE:4byte,relD:4byte | |
mdata[0]=fdata[sNo+46] ;//Acc2d_1 | |
mdata[1]=fdata[sNo+47] ;//Acc2d_2 | |
mdata[2]=fdata[sNo+48];//Acc2d_3 | |
mdata[3]=fdata[sNo+49];//Acc2d_4 | |
mdata[4]=fdata[sNo+29];//numSV | |
mdata[5]=fdata[rNo+26];//Length_1 | |
mdata[6]=fdata[rNo+27];//Length_2 | |
mdata[7]=fdata[rNo+28];//Length_3 | |
mdata[8]=fdata[rNo+29];//Length_4 | |
mdata[9]=fdata[sNo+30];//Long_1 | |
mdata[10]=fdata[sNo+31];//Long_2 | |
mdata[11]=fdata[sNo+32];//Long_3 | |
mdata[12]=fdata[sNo+33];//Long_4 | |
mdata[13]=fdata[sNo+34];//Lati_1 | |
mdata[14]=fdata[sNo+35];//Lati_2 | |
mdata[15]=fdata[sNo+36];//Lati_3 | |
mdata[16]=fdata[sNo+37];//Lati_4 | |
mdata[17]=fdata[rNo+14];//relN_1 | |
mdata[18]=fdata[rNo+15];//relN_2 | |
mdata[19]=fdata[rNo+16];//relN_3 | |
mdata[20]=fdata[rNo+17];//relN_4 | |
mdata[21]=fdata[rNo+18];//relE_1 | |
mdata[22]=fdata[rNo+19];//relE_2 | |
mdata[23]=fdata[rNo+20];//relE_3 | |
mdata[24]=fdata[rNo+21];//relE_4 | |
mdata[25]=fdata[rNo+22];//relD_1 | |
mdata[26]=fdata[rNo+23];//relD_2 | |
mdata[27]=fdata[rNo+24];//relD_3 | |
mdata[28]=fdata[rNo+25];//relD_4 | |
esp_err_t result = esp_now_send(slave.peer_addr, mdata, 29);//sizeof(mdata)); | |
}//monFlag | |
}//f9pMon END | |
//+++++++++++++4byte Binary to Long ++++++++++++++++++++++++++++++++++++++++++++++ | |
long B2L(uint8_t b4 , uint8_t b3 ,uint8_t b2 , uint8_t b1 ) { | |
//pc.printf("B2L IN=%s,%x,%x,%x,%x,b4&0x80=%d\n\r",sen,b4,b3,b2,b1,b4 &0x80); | |
//pc.printf("B2L IN=b4&0x80=%d\n\r",b4 & 0x80); | |
if ((b4 & 0x80) && 0x80){//最上位ビットたっていればマイナス | |
su = -(256-(int)b1)+(255-(int)b2)*256+(255-(int)b3)*65536+(255-(int)b4)*256*256*256; | |
//pc.printf("B2L-:sen=%s,%d,%d,%d,%d,%d\n\r",sen,b4,b3,b2,b1,su); | |
} | |
else { | |
su=(int)b1+(int)b2*256+(int)b3* 65536+(int)b4*256*256*256; | |
//pc.printf("B2L+:sen=%s,%d,%d,%d,%d,%d,%d\n\r",sen,b4,b3,b2,b1,su); | |
} | |
return su; | |
} | |
//================================================================================= | |
//+++++++++++++++i_to_char++++++++++++++++++++++++++++++++++++ | |
// i=IntegerValueData,*d=Array pointer, n=Array start No | |
void i_to_char(int i, uint8_t *d, int n) | |
{ | |
d[n] = i & 0x000000ff; | |
d[n + 1] = (i & 0x0000ff00) >> 8; | |
d[n + 2] = (i & 0x00ff0000) >> 16; | |
d[n + 3] = (i & 0xff000000) >> 24; | |
} | |
//++++++++++++++++String to CharArray Trans++++++++++++++++++++ | |
void str2char(char c[],String dataS) | |
{ | |
//String dataS; | |
//dataS="HELLO dataS"; | |
int dataS_len=dataS.length()+1; | |
char char_array[dataS_len]; | |
dataS.toCharArray(c,dataS_len); | |
} | |
//============================================================================================== | |
//*************************************************************************************************************************************** | |
//SET UP | |
//*************************************************************************************************************************************** | |
void setup() { | |
M5.begin(); | |
pinMode(startPin, OUTPUT); //startPin Start DataSending | |
//Serial1.begin(115200,SERIAL_8N1,25,21);//*******M5 Atom1-Atom2 EXT(baudrate,config,RX25,TX21)******* | |
Serial2.begin(115200, SERIAL_8N1, 32, 26); //******AtomicTF -F9P Grove(baudrate,config,RX32,TX26)******* | |
t_1 = millis(); | |
//------------------------------------------------ | |
//--------- setup:ESP-NOW初期化-------------------------- | |
//------------------------------------------------ | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
if (esp_now_init() == ESP_OK) { | |
//Serial.println("ESPNow Init Success"); | |
} else { | |
//Serial.println("ESPNow Init Failed"); | |
ESP.restart(); | |
} | |
// マルチキャスト用Slave<pnのM5ATOM> address 50:02:91:86:9B:CC | |
memset(&slave, 0, sizeof(slave)); | |
//============MPC Slave AP Mac====================== | |
slave.peer_addr[0] = (uint8_t)0xD8;//0x50; | |
slave.peer_addr[1] = (uint8_t)0xA0;//0x02; | |
slave.peer_addr[2] = (uint8_t)0x1D;//0x91; | |
slave.peer_addr[3] = (uint8_t)0x58;//0x87; | |
slave.peer_addr[4] = (uint8_t)0x47;//0x21; | |
slave.peer_addr[5] = (uint8_t)0x30;//0x84; | |
esp_err_t addStatus = esp_now_add_peer(&slave); | |
// ESP-NOWコールバック登録 | |
esp_now_register_send_cb(OnDataSent); | |
esp_now_register_recv_cb(OnDataRecv); | |
//-------------------------------------------------- | |
//-------------------------------------------------- | |
//=============SD setup=============================== | |
SPI.begin(23,33,19,-1); | |
if(!SD.begin(-1, SPI, 10000000)){ | |
Serial.println("Card Mount Failed"); | |
return; | |
} | |
uint8_t cardType = SD.cardType(); | |
if(cardType == CARD_NONE){ | |
Serial.println("No SD card attached"); | |
return; | |
} | |
Serial.print("SD Card Type: "); | |
if(cardType == CARD_MMC){ | |
Serial.println("MMC"); | |
} else if(cardType == CARD_SD){ | |
Serial.println("SDSC"); | |
} else if(cardType == CARD_SDHC){ | |
Serial.println("SDHC"); | |
} else { | |
Serial.println("UNKNOWN"); | |
} | |
uint64_t cardSize = SD.cardSize() / (1024 * 1024); | |
Serial.printf("SD Card Size: %lluMB\n", cardSize); | |
//listDir(SD, "/", 0); | |
} | |
//**************************************************************************************************************************************************** | |
//**************************************************************************************************************************************************** | |
void loop() | |
{ | |
M5.update(); | |
t = millis(); | |
keyin(); | |
//Serial.println(Serial2.available()); | |
//===============Serial2 REceiving================================== | |
if (Serial2.available() > 0 ) //Serial2 OK | |
{ | |
//Serial.println(Serial2.read(),HEX); | |
for (j = 0; j < 4; j++) | |
{ | |
fdata[j] = Serial2.read(); | |
} | |
if (fdata[0] == 0xB5 && fdata[1] == 0x62 && fdata[2] == 0x01 && fdata[3] == 0x07) | |
{ | |
k = 4; | |
while (k < 172) | |
{ | |
if (Serial2.available() > 0 ) | |
{ | |
fdata[k] = Serial2.read(); | |
//Serial.print(fdata[k],HEX); | |
k++; | |
} | |
} | |
if (k > 171)//172Byte Receiving Finished | |
{ | |
//ESP-NOW send to Monitor--------- | |
f9pMon();// monFlag==1 dispaly | |
//fdata[26] = 0x14; // | |
//esp_err_t result = esp_now_send(slave.peer_addr, f9pdata, 172); //ESP-NOW Binary Send | |
//SD logging----------------------------------------------------------------------------- | |
String str=""; | |
String strTotal=""; | |
if (sdFlag==1) | |
{ | |
//Filename make timestamp | |
if(firstFlag==1) | |
{ | |
sNo=0; | |
JST=(fdata[sNo+14]+9)%24; | |
String stime=String(fdata[sNo+12],DEC)+"-"+String(fdata[sNo+13],DEC)+"-"+String(JST,DEC)+"-"+String(fdata[sNo+15],DEC);//MMDDHHMM | |
stime="/"+stime+".txt"; | |
int slen=stime.length()+1; | |
stime.toCharArray(fname,slen);//stime to fname[] chara Array | |
firstFlag=0; | |
} | |
// fdata => str => charArray | |
for(ci=0;ci<172;ci++) | |
{ | |
str=String(fdata[ci],HEX); | |
strTotal=strTotal+","+str; | |
} | |
strTotal=strTotal+"\n\r";// Last add CRLF | |
//-----SD input data making------------- | |
int slen=strTotal.length()+1; | |
//Serial.print("strTotal="); | |
//Serial.println(strTotal); | |
//Serial.print("slen="); | |
//Serial.println(slen); | |
char buf[slen]; | |
strTotal.toCharArray(buf,slen); | |
//--File Header is fname ---- | |
if (headerFlag==1) | |
{ | |
writeFile(SD, fname, fname); //file Header is file name | |
headerFlag=0; | |
} | |
//--------SD writing -------------- | |
//writeFile(SD, "/f9p001.txt", buf); //duplicate write | |
appendFile(SD,fname, buf);//add write | |
}//SD flag==1 | |
}// A set SDwriting finished | |
}// b5620107 | |
}//Serial2.available() | |
// ボタンを押したら送信 | |
//if ( M5.BtnB.wasPressed() ) {// buttonA=>M5StickC buttonB=>M5Atom lite | |
if (M5.Btn.pressedFor(500))//for M5Atom Button | |
{ | |
uint8_t data[2] = {123, 234}; | |
esp_err_t result = esp_now_send(slave.peer_addr, data, sizeof(data)); | |
Serial.print("Send Status: "); | |
if (result == ESP_OK) { | |
Serial.println("Success"); | |
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) { | |
Serial.println("ESPNOW not Init."); | |
} else if (result == ESP_ERR_ESPNOW_ARG) { | |
Serial.println("Invalid Argument"); | |
} else if (result == ESP_ERR_ESPNOW_INTERNAL) { | |
Serial.println("Internal Error"); | |
} else if (result == ESP_ERR_ESPNOW_NO_MEM) { | |
Serial.println("ESP_ERR_ESPNOW_NO_MEM"); | |
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { | |
Serial.println("Peer not found."); | |
} else { | |
Serial.println("Not sure what happened"); | |
} | |
} | |
} | |
//======================================================================== | |
//----------------------------------------- | |
//***KEY IN******************************************** | |
void keyin() | |
{ | |
if( Serial.available()){ | |
char key =Serial.read(); //KEY In from PC | |
//SD Log Start======================== | |
if(key=='S') | |
{ | |
Serial.println("Key S:Logging Start/Stop"); | |
if(sdFlag==0) | |
{ | |
sdFlag=1; | |
firstFlag=1; | |
} | |
else if(sdFlag==1) | |
{ | |
sdFlag=0; | |
} | |
} | |
//Monitor Flag=========================== | |
if(key=='M') | |
{ | |
Serial.println("KeyIn=M"); | |
if (monFlag==1) | |
{ | |
monFlag=0; | |
} | |
else if(monFlag==0) | |
{ | |
monFlag=1; | |
} | |
}// key 'M' | |
//File Name create 'N'======================== | |
if(key=='N') | |
{ | |
//key in FileName----------------------------- | |
Serial.print("Please Input File Name[last'.']="); | |
fname[0]='/';// ROOT Mark | |
char inputC; | |
while( inputC!= '.'){ | |
if(Serial.available()) | |
{ | |
j++; | |
inputC=Serial.read(); | |
fname[j%30] =inputC; | |
Serial.print(fname[j%30]); | |
} | |
}//while end | |
fname[j+1]='t'; | |
fname[j+2]='x'; | |
fname[j+3]='t'; | |
fname[j+4]=0x00; | |
Serial.println(); | |
Serial.print("Input Filename="); | |
String sfname=fname; | |
Serial.println(sfname); | |
} | |
//Logon========================================= | |
if (key=='G') | |
{ | |
if (Logon==0) | |
{ | |
Logon=1; | |
headerFlag=1;//header add | |
}//Go Logging | |
else if(Logon==1){Logon=0;}//Stop Logging | |
} | |
//DeleteFile================================================= | |
if(key=='D')//D Key Pushed | |
{ | |
int dk; //File No 0-9999 | |
int dflag=0; | |
listDir(SD, "/", 0); | |
Serial.print("Input Delete File No="); | |
String dsN="";//d selectNo | |
char dC;//Input key char | |
char dN[4]={};//4keta No 0-9999 Char Array | |
dk=0; | |
while(dC!= '\r'){ | |
if(Serial.available()) | |
{ | |
dC=Serial.read(); | |
Serial.print(dC); | |
dN[dk]=dC; | |
dk++; | |
} | |
} | |
dsN=String(dN);//FileNo CharArray to String dsN | |
Serial.print(dsN);//FileNo Diplay | |
int deN=dsN.toInt();//FileNo String dsN to Integer | |
Serial.printf("deN="); | |
Serial.println(deN); | |
String dstr=Listname[deN];//FileName [FileNo deN] to dstr | |
int dslen=dstr.length()+1;//Filename Length | |
char buf[dslen];//Filename CharArry buf | |
dstr.toCharArray(buf,dslen);//buf charArry Filename | |
Serial.print("DeleteFilename="); | |
Serial.print(dstr); | |
Serial.print("="); | |
Serial.print(Listname[deN]); | |
Serial.print("dslen="); | |
Serial.println(dslen); | |
deleteFile(SD, buf); | |
} | |
//List File=========================================================== | |
if (key=='L'){ | |
listDir(SD, "/", 0); | |
} | |
//Append FIle============================================================= | |
if(key=='A'){ | |
int j=0; | |
String dataStr; | |
char char_array[]={}; | |
for (j=0;j<10;j++) | |
{ | |
dataStr=String(millis())+","; | |
str2char(char_array,dataStr); | |
appendFile(SD, "/hello.txt",char_array); | |
Serial.print("Appending:"); | |
Serial.print(j); | |
//appendFile(SD, "/hello.txt",String(millis())+"\r\n"); | |
} | |
} | |
//Read File==================================================================== | |
if(key=='R'){ | |
int rflag=0; | |
listDir(SD, "/", 0); | |
Serial.print("Input Read File No="); | |
String sN=""; | |
char bC; | |
bC='a'; | |
char cN[4]; | |
k=0; | |
while(bC!= '\r'){ | |
if(Serial.available()) | |
{ | |
bC=Serial.read(); | |
Serial.print(bC); | |
cN[k%4]=bC; | |
k++; | |
} | |
cN[k]=0x00; | |
} | |
sN=String(cN); | |
Serial.print(sN); | |
int aN=sN.toInt(); | |
String str=Listname[aN]; | |
//Serial.print("str="); | |
//Serial.print(str); | |
int slen=str.length()+1; | |
Serial.println("ReadFilename:"); | |
Serial.print(str); | |
Serial.print("/slen="); | |
Serial.print(slen); | |
char buf[slen]; | |
str.toCharArray(buf,slen); | |
readFile(SD, buf); | |
//readFile(SD, "/hello.txt"); | |
} | |
//Write FileName messages===================================================== | |
if(key=='W'){ | |
//key in FileName | |
Serial.print("Please Input File Name[last'.']="); | |
fname[0]='/';// ROOT Mark | |
char inputC; | |
while( inputC!= '.'){ | |
if(Serial.available()) | |
{ | |
j++; | |
inputC=Serial.read(); | |
fname[j%30] =inputC; | |
Serial.print(fname[j%30]); | |
} | |
}//while end | |
fname[j+1]='t'; | |
fname[j+2]='x'; | |
fname[j+3]='t'; | |
fname[j+4]=0x00; | |
Serial.println(); | |
Serial.print("Input Filename="); | |
String sfname=fname; | |
Serial.println(sfname); | |
//key in Message--------------------------------- | |
char mes[100]; | |
Serial.print("Please Input message="); | |
j=0; | |
while( inputC!= '\r'){ | |
if(Serial.available()) | |
{ | |
inputC=Serial.read(); | |
mes[j%100] =inputC; | |
Serial.print(mes[j%100]); | |
j++; | |
} | |
mes[j]=0x00; | |
}//while end | |
//writeFile(SD, "/hello.txt", "Hello "); | |
Serial.println(fname); | |
Serial.println(mes); | |
//--SEt up writing chars--------- | |
String stotal,smes; | |
smes=mes;// charArray to String | |
stotal=sfname+"\n\r"+smes; | |
int slen=stotal.length()+1; | |
Serial.print("Write data="); | |
Serial.print(stotal); | |
Serial.print("/slen="); | |
Serial.print(slen); | |
char buf[slen]; | |
stotal.toCharArray(buf,slen); | |
//------------------------------- | |
writeFile(SD, fname, buf); | |
for (i=0;i<100;i++) | |
{ | |
mes[i]=0x00; | |
} | |
}//key 'W' end | |
//Upload WebServer ======================================================================= | |
if(key=='U'){ | |
//==WiFi Begin======== | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(" CONNECTED"); | |
//=============================== | |
int rflag=0; | |
listDir(SD, "/", 0); | |
Serial.print("Input Upload File No="); | |
String sN=""; | |
char bC; | |
char cN[4]=""; | |
k=0; | |
bC='a'; | |
while(bC!= '\r'){ | |
if(Serial.available()) | |
{ | |
bC=Serial.read(); | |
Serial.print(bC); | |
cN[k]=bC; | |
k++; | |
} | |
cN[k]=0x00; | |
} | |
sN=String(cN); | |
Serial.print(sN); | |
int aN=sN.toInt(); | |
String str=Listname[aN];//Selected FileName | |
int slen=str.length()+1; | |
Serial.print("UploadFilename="); | |
Serial.print(str); | |
Serial.print("/slen="); | |
Serial.print(slen); | |
char buf[slen]; | |
str.toCharArray(buf,slen); | |
Webserver_writeSPfile(SD,str); | |
//--Reset parameters---- | |
str=""; | |
WiFi.disconnect();// WiFi end to ESP-NOW | |
} | |
Serial.println(); | |
Serial.println("===Please key IN===="); | |
Serial.println("S:Log/M:Mon/W:write/R:Read/L:List/D:Del/U:Up/N:FName");//Append Not working | |
} | |
} | |
//========WEB Server write======================================= | |
void Webserver_writeSPfile( fs::FS &fs,String fname) | |
{ | |
//file = SD.open(file_name, FILE_READ); | |
//file = SD.open(fname, FILE_READ); | |
//SD readfile | |
File file = fs.open(fname); | |
if(!file || file.isDirectory()){ | |
Serial.println("- failed to open file for reading"); | |
return; | |
} | |
// Serial.println("- read from file:"); | |
// while(file.available()){ | |
// Serial.write(file.read()); | |
// } | |
// file.close(); | |
//M5.Lcd.fillScreen(BLACK); | |
//M5.Lcd.setCursor(0, 0, 2); | |
//M5.Lcd.print("FILE SIZE : "); | |
//M5.Lcd.println(file.size()); | |
//char tmp_var[file.size()] = ""; | |
char tmp_var[var_size+1] = ""; | |
esp_http_client_config_t config = {0}; | |
config.url = "http://xxxxxxxxxxxxx/writefile.php";//Your HTTP Server URL | |
config.method = HTTP_METHOD_POST; | |
esp_http_client_handle_t client = esp_http_client_init(&config); | |
esp_http_client_open(client, file.size()); | |
//----------------------------------------------- | |
Serial.print("Start Upload time="); | |
Serial.println(millis()); | |
while(file.available()) { | |
char c=file.read();//SD file read | |
//Serial.print(c);//List PC | |
sprintf(tmp_var, "%s%c", tmp_var, c); | |
if(strlen(tmp_var)>=var_size) { | |
esp_http_client_write(client, (const char *)tmp_var, strlen(tmp_var)); | |
sprintf(tmp_var, ""); | |
Serial.print("*"); | |
} | |
} | |
esp_http_client_write(client, (const char *)tmp_var, strlen(tmp_var)); | |
// M5.Lcd.fillScreen(BLACK); | |
// M5.Lcd.setCursor(0, 0, 2); | |
// M5.Lcd.println("FINISH"); | |
esp_http_client_close(client); | |
esp_http_client_cleanup(client); | |
file.close(); | |
Serial.print("Upload Finished time="); | |
Serial.println(millis()); | |
} | |
//=========================================================================== | |
//----------SD Utility ------------------------------------------ | |
//============================================================================== | |
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ | |
Serial.printf("Listing directory: %s\r\n", dirname); | |
File root = fs.open(dirname); | |
if(!root){ | |
Serial.println("- failed to open directory"); | |
return; | |
} | |
if(!root.isDirectory()){ | |
Serial.println(" - not a directory=>Fomatting"); | |
// bool formatted = SD.format(); | |
return; | |
} | |
File file = root.openNextFile(); | |
int n=0; | |
while(file){ | |
if(file.isDirectory()){ | |
Serial.print(" DIR : "); | |
Serial.println(file.name()); | |
if(levels){ | |
listDir(fs, file.name(), levels -1); | |
} | |
} else { | |
n++; | |
Listname[n]=file.name(); | |
Serial.print("FILE No "); | |
Serial.print(n); | |
Serial.print("="); | |
Serial.print(Listname[n]); | |
Serial.print("\tSIZE: "); | |
Serial.println(file.size()); | |
} | |
file = root.openNextFile(); | |
} | |
} | |
void createDir(fs::FS &fs, const char * path){ | |
Serial.printf("Creating Dir:"); | |
Serial.println(path); | |
if(fs.mkdir(path)){ | |
Serial.println("Dir created"); | |
} else { | |
Serial.println("mkdir failed"); | |
} | |
} | |
void removeDir(fs::FS &fs, const char * path) | |
{ | |
Serial.print("Removing Dir:"); | |
Serial.println(path); | |
if(fs.rmdir(path)){ | |
Serial.println("Dir removed"); | |
} else { | |
Serial.println("rmdir failed"); | |
} | |
} | |
void readFile(fs::FS &fs, const char * path){ | |
Serial.print("Reading file:"); | |
Serial.println(path); | |
File file = fs.open(path); | |
if(!file){ | |
Serial.println("Failed to open file for reading"); | |
return; | |
} | |
Serial.println("Read from file: "); | |
while(file.available()){ | |
Serial.write(file.read()); | |
} | |
file.close(); | |
} | |
void writeFile(fs::FS &fs, const char * path, const char * message){ | |
Serial.printf("Writing file: %s\n", path); | |
File file = fs.open(path, FILE_WRITE); | |
if(!file){ | |
Serial.println("Failed to open file for writing"); | |
return; | |
} | |
if(file.print(message)){ | |
Serial.println("File written"); | |
} else { | |
Serial.println("Write failed"); | |
} | |
file.close(); | |
} | |
void appendFile(fs::FS &fs, const char * path, const char * message){ | |
// Serial.printf("Appending to file: %s\n", path); | |
File file = fs.open(path, FILE_APPEND); | |
if(!file){ | |
Serial.println("Failed to open file for appending"); | |
return; | |
} | |
if(file.print(message)){ | |
Serial.print("*");//"Message appended"); | |
} else { | |
Serial.println("Append failed"); | |
} | |
file.close(); | |
} | |
void renameFile(fs::FS &fs, const char * path1, const char * path2){ | |
Serial.printf("Renaming file %s to %s\n", path1, path2); | |
if (fs.rename(path1, path2)) { | |
Serial.println("File renamed"); | |
} else { | |
Serial.println("Rename failed"); | |
} | |
} | |
void deleteFile(fs::FS &fs, const char * path){ | |
Serial.printf("Deleting file: %s\n", path); | |
if(fs.remove(path)){ | |
Serial.println("File deleted"); | |
} else { | |
Serial.println("Delete failed"); | |
} | |
} | |
void testFileIO(fs::FS &fs, const char * path){ | |
File file = fs.open(path); | |
static uint8_t buf[512]; | |
size_t len = 0; | |
uint32_t start = millis(); | |
uint32_t end = start; | |
if(file){ | |
len = file.size(); | |
size_t flen = len; | |
start = millis(); | |
while(len){ | |
size_t toRead = len; | |
if(toRead > 512){ | |
toRead = 512; | |
} | |
file.read(buf, toRead); | |
len -= toRead; | |
} | |
end = millis() - start; | |
Serial.printf("%u bytes read for %u ms\n", flen, end); | |
file.close(); | |
} else { | |
Serial.println("Failed to open file for reading"); | |
} | |
file = fs.open(path, FILE_WRITE); | |
if(!file){ | |
Serial.println("Failed to open file for writing"); | |
return; | |
} | |
size_t i; | |
start = millis(); | |
for(i=0; i<2048; i++){ | |
file.write(buf, 512); | |
} | |
end = millis() - start; | |
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end); | |
file.close(); | |
} | |
//===============================SD UTILITY END ======================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment