Created
January 23, 2021 21:08
-
-
Save dj1711572002/7221088305b4134c12b292528fbd14de to your computer and use it in GitHub Desktop.
M5Stack-SD_FIle_WriteFile_to_WebServer
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
//#include <M5StickC.h> | |
#include <M5Stack.h> | |
#include <WiFi.h> | |
#include "SD.h" | |
#include "esp_http_client.h" | |
//SPIClass SPI2; | |
//File file; | |
//===DEF:SD FILE============ | |
File file; | |
char* fname;//保存するファイル名 | |
int i; | |
const char* ssid = "ssid";//Your Wifi ID | |
const char* password = "pass";//YOur Wifi Pass | |
char file_name[20] = "/test1.txt"; | |
int var_size = 2000; | |
//================================================ | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setRotation(3); | |
M5.Lcd.setCursor(0, 0, 2); | |
M5.Lcd.printf("Connecting to %s ", ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
M5.Lcd.print("."); | |
} | |
M5.Lcd.println(" CONNECTED"); | |
//*******************SD FILE OPEN ********************************************************* | |
//file = SD.open(file_name, FILE_APPEND); | |
file = SD.open(file_name, FILE_WRITE); | |
for (i=0;i<90000;i++) | |
{ | |
file.print(String(i)); | |
file.print(","); | |
if(i%1000==0) | |
{ | |
file.println(); | |
//Serial.print(i); | |
} | |
} | |
file.close(); | |
Serial.println("529KByte File Created "); | |
} | |
void loop() { | |
M5.update(); | |
if(M5.BtnA.wasPressed()) { | |
file = SD.open(file_name, FILE_READ); | |
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://******************/writefile.php";// Your WEb server Site 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.printf("Start Upload time=%d\n\r",millis()); | |
while(file.available()) { | |
char c=file.read(); | |
//Serial.print(c);//ComentOut for speed measurement | |
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, ""); | |
//M5.Lcd.print("*");//ComentOut for speed measurement | |
} | |
} | |
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.printf("Upload Finished time=%d\n\r",millis()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment