Tai Chi maker Internet of things learning notes 1 --- 2022.2.25

Posted by bateman on Sat, 26 Feb 2022 12:51:41 +0100

1. Read the contents of the directory through the program

The procedure is as follows:

/**********************************************************************
Project name / Project: Internet of things for zero foundation introduction
 Program name: esp8266 flash folder read
 Team: Taiji maker team / Taichi maker (www.taichi-maker. Com)
Author: Cyno Shuo
 Date (YYYYMMDD): 20191109
 Purpose: This program is used to demonstrate how to read file information from the folder in the built-in SPIFs of nodemocu
                           The contents of the folder will be displayed through the serial port monitor.
***********************************************************************/                            
                             
 
#include <FS.h>
 
String file_name = "/taichi-maker/myFile.txt"; //Location and name of the file being read
String folder_name = "/taichi-maker";         //Read folder
 
void setup() {
  Serial.begin(9600);
  Serial.println("");
  
  if(SPIFFS.begin()){ // Boot flash file system
    Serial.println("SPIFFS Started.");
  } else {
    Serial.println("SPIFFS Failed to Start.");
  }
 
  File dataFile = SPIFFS.open(file_name, "w");// Create a file object to write information to the file object (i.e. myFile.txt) in SPIFs
  dataFile.println("Hello Taichi-Maker.");    // Write information to dataFile string
  dataFile.close();                           // Close the file after writing the file
  Serial.println(F("Finished Writing data to SPIFFS"));
  
  //Serial.println("Finished Writing data to SPIFFS");
 
  // Displays the contents and size of files in the directory
  Dir dir = SPIFFS.openDir(folder_name);  // Create directory object
  
  while (dir.next()) {  // dir.next() is used to check whether there is "next file" in the directory
    Serial.println(dir.fileName()); // Output file name
  }
}
 
void loop() {
}

1. Serial about Arduino Print (f ("")) problem

Serial.println(F("Finished Writing data to SPIFFS"));
Serial.println("Finished Writing data to SPIFFS");

Use serial println(“Finished Writing data to SPIFFS”);
The project uses 294644 bytes, occupying (28%) program storage space. The maximum is 1044464 bytes.
The global variable uses 27244 bytes (33%) of dynamic memory, leaving 54676 bytes of local variables. The maximum is 81920 bytes.

Use serial println(F(“Finished Writing data to SPIFFS”));
The program is compiled as follows:
The project uses 294820 bytes, occupying (28%) program storage space. The maximum is 1044464 bytes. FLASH occupation increased
Global variables use 27212 bytes (33%) of dynamic memory, leaving 54708 bytes of local variables. The maximum is 81920 bytes. SRAM occupancy reduction

It can be concluded that adding F() is equivalent to defining the PROGMEM attribute for the string constant. The constant string is still stored in FLASH, but when the program runs, it will no longer copy the constant string from FLASH to SRAM, but directly read the string in FLASH. In this way, SRAM is saved, but the running speed of the code is reduced.

When defining a global constant, use the PROGMEM keyword or the PROGMEM data type to tell the compiler to "save this information in the program storage space" instead of "dynamic memory".

SRAM (static random access memory) is a storage device that can store data as long as it is powered on, and it is a key part of most high-performance systems.
SRAM memory is a kind of random access memory. The so-called "static" means that the data stored in this memory can be maintained for a long time as it remains powered on. Compared with the data stored in DRAM memory, it needs to be updated periodically. However, when the power supply stops, the data stored in SRAM memory will still disappear. Therefore, SRAM memory is also called volatile memory, which is different from ROM memory or FLASH memory that can store data after power failure. SRAM memory has high performance and is widely used in high-end products.

Topics: IoT