当前位置:网站首页>FLASH read / write operation and flash upload file of esp8266
FLASH read / write operation and flash upload file of esp8266
2022-07-25 09:51:00 【F l e】
1、Flash Read and write operations
Esp8266 Of Flash by 4M, among 1M For storing programs , Some of the other spaces are used for the system ,3M Most of the space left in the can be used to store files .
#include <FS.h>
String file_name = "/Fle/note.txt"; // The location and name of the file being read , Put it in /Fle Under the folder
void setup() {
Serial.begin(9600);
Serial.println("");
Serial.println("SPIFFS format start");
SPIFFS.format(); // format SPIFFS
Serial.println("SPIFFS format finish");
if (SPIFFS.begin()) {
// start-up SPIFFS
Serial.println("SPIFFS Started.");
} else {
Serial.println("SPIFFS Failed to Start.");
}
file_write(file_name, "1111");
file_read(file_name);
file_add(file_name, "2222");
file_read(file_name);
}
void loop() {
}
// Write operations
void file_write(String file_name, String content)
{
/***************************************** Write the contents of the flash file *************************************************/
File dataFile = SPIFFS.open(file_name, "w");// establish File Object is used to SPIFFS Medium file object ( namely /notes.txt) Write information
dataFile.println(content); // towards dataFile Write string information
dataFile.close(); // Close the file after writing
Serial.print(" The contents of the flash file have been written :");
Serial.println(content);
}
// Read operations
void file_read(String file_name)
{
// Confirm whether there is file_name file
File dataFile;
if (SPIFFS.exists(file_name))
{
// establish File Object to be used from SPIFFS Read files from
dataFile = SPIFFS.open(file_name, "r");
// Read the contents of the file and output the file information through the serial port monitor
Serial.println(" Read the contents of the file :");
for (int i = 0; i < dataFile.size(); i++)
{
Serial.print((char)dataFile.read());
}
}
else
{
Serial.print(file_name);
Serial.print(" NOT FOUND.");
}
// Close the file after reading it
dataFile.close();
}
// Additional operations
void file_add(String file_name, String content)
{
if (SPIFFS.exists(file_name))
{
File dataFile = SPIFFS.open(file_name, "a");// establish File Object is used to SPIFFS Medium file object ( namely /notes.txt) Write information
dataFile.println(content); // towards dataFile Add string information
dataFile.close(); // Close the file after completing the file operation
Serial.println(" Content has been added :");
Serial.println(content);
}
else
{
Serial.print(file_name);
Serial.print(" NOT FOUND.");
}
}

2、Flash Upload the file and read the display , To verify whether it is really uploaded
Here is to upload to Flash The task of , You need to download one based on esp8266 and Arduino Of Flash File upload plugin
Here is the installation Flash Specific steps of file upload plug-in :
tools Folders should be created by yourself , Put it in tools Under folder ESP8266FS The folder is the unzipped folder . Remember to restart after setting Arduino IDE Software can take effect .
Now use this plug-in Flash Upload and verify the file :
First, create a new one under the project folder data Folder , Put the file to be uploaded in data Under the folder . open Arduino IDE Then click Tools -----ESP8266 Sketch Data Upload, Click to see Esp8266 The blue light is flashing . After burning, the file has been saved in Esp8266 Of Flash In the .
Now use the following procedure to verify :
#include <FS.h>
String file_name = "/note.txt"; // The location and name of the file being read , Put it in the root directory
void setup() {
Serial.begin(9600);
Serial.println("");
if (SPIFFS.begin()) {
// start-up SPIFFS
Serial.println("SPIFFS Started.");
} else {
Serial.println("SPIFFS Failed to Start.");
}
file_read(file_name);
file_add(file_name, "\n2222");
file_read(file_name);
}
void loop() {
}
void file_write(String file_name, String content)
{
/***************************************** Write the contents of the flash file *************************************************/
File dataFile = SPIFFS.open(file_name, "w");// establish File Object is used to SPIFFS Medium file object ( namely /notes.txt) Write information
dataFile.println(content); // towards dataFile Write string information
dataFile.close(); // Close the file after writing
Serial.print(" The contents of the flash file have been written :");
Serial.println(content);
}
void file_read(String file_name)
{
// Confirm whether there is file_name file
File dataFile;
if (SPIFFS.exists(file_name))
{
// establish File Object to be used from SPIFFS Read files from
dataFile = SPIFFS.open(file_name, "r");
// Read the contents of the file and output the file information through the serial port monitor
Serial.println(" Read the contents of the file :");
for (int i = 0; i < dataFile.size(); i++)
{
Serial.print((char)dataFile.read());
}
}
else
{
Serial.print(file_name);
Serial.print(" NOT FOUND.");
}
// Close the file after reading it
dataFile.close();
}
void file_add(String file_name, String content)
{
if (SPIFFS.exists(file_name))
{
File dataFile = SPIFFS.open(file_name, "a");// establish File Object is used to SPIFFS Medium file object ( namely /notes.txt) Write information
dataFile.println(content); // towards dataFile Add string information
dataFile.close(); // Close the file after completing the file operation
Serial.println(" Content has been added :");
Serial.println(content);
}
else
{
Serial.print(file_name);
Serial.print(" NOT FOUND.");
}
}
The above procedure cannot be carried out SPIFFS.format() Format operation , Otherwise, the newly saved files will be cleared , And notice the place data The files under the folder are placed in esp8266 Of Flash Under the root directory of , So the file directory in the program should also use /note.txt. At the same time, we should also pay attention not to /note.txt Write files , Otherwise, it will also be covered 123456789 This information .
Running results :
As can be seen from the above , except 123456789, There are two more in the back 2222, This is because the program will run again after the download , In order to make the serial port display data, I press the reset button once , So the program ran twice , Added twice 2222.
边栏推荐
- 无向连通图邻接矩阵的创建输出广度深度遍历
- 关于MLOps中的数据工程,你一定要知道的.......
- Expect+sh realize automatic interaction
- Principle analysis of self supervised depth estimation of fish eye image and interpretation of omnidet core code
- @4-1 CCF 2020-06-1 linear classifier
- Server CUDA toolkit multi version switching
- CCF 201604-2 俄罗斯方块
- 【数据挖掘】最近邻和贝叶斯分类器
- 单目深度估计模型Featdepth实战中的问题和拓展
- 解决QTCreator使用VS编译中文乱码错误
猜你喜欢

First knowledge of opencv4.x --- box filtering

AMD EPYC 9664旗舰规格曝光:96核192线程 480MB缓存 3.8GHz频率

无向连通图邻接矩阵的创建输出广度深度遍历

CDA Level1知识点总结之业务分析报告与数据可视化报表

相机姿态估计

【深度学习】卷积神经网络

CUDA explanation - why GPU is used in deep learning

Matlab drawing | some common settings of axis
![Customize the view to realize the background of redeeming lottery tickets [elementary]](/img/97/53e28673dcd52b31ac7eb7b00d42b3.png)
Customize the view to realize the background of redeeming lottery tickets [elementary]

【数据挖掘】第四章 分类任务(决策树)
随机推荐
Esp8266的Flash读写操作以及Flash上传文件
T5论文总结
【深度学习】卷积神经网络
First knowledge of opencv4.x --- image convolution
Binary Cross Entropy真的适合多标签分类吗?
Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)
Learning new technology language process
Get to know opencv4.x for the first time --- add salt and pepper noise to the image
Expect+sh realize automatic interaction
[dimension reduction strike] Hilbert curve
[data mining] nearest neighbor and Bayesian classifier
CDA Level1复盘总结
@4-1 CCF 2020-06-1 linear classifier
解决QTCreator使用VS编译中文乱码错误
基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(上)
[data mining] Chapter 3 basis of data analysis
Hyperautomation for the enhancement of automation in industries 论文翻译
@2-1 safety index predicted by CCF at the end of December 1, 2020
CDA Level1知识点总结之业务数据分析
Raspberry sect door ban system based on face recognition