Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Example showing timestamp support in LittleFS */
- /* Released into the public domain. */
- /* Earle F. Philhower, III <[email protected]> */
- /* Starting to shed superfluous parts, eg. old trial subroutines to test out new commands
- * Call this new ino file: littleFsForSensor ?
- */
- #include <FS.h>
- #include <LittleFS.h>
- #include <time.h>
- #include <ESP8266WiFi.h>
- //#include <stdio.h>
- #ifndef STASSID
- #define STASSID "SPARK-DTCUB2" //your-ssid"
- #define STAPSK "VHVHM2A9GD" //"your-password"
- #endif
- const char *ssid = STASSID;
- const char *pass = STAPSK;
- long timezone = 2;
- byte daysavetime = 1;
- bool getLocalTime(struct tm * info, uint32_t ms) {
- uint32_t count = ms / 10;
- time_t now;
- time(&now);
- localtime_r(&now, info);
- if (info->tm_year > (2016 - 1900)) {
- return true;
- }
- while (count--) {
- delay(10);
- time(&now);
- localtime_r(&now, info);
- if (info->tm_year > (2016 - 1900)) {
- return true;
- }
- }
- return false;
- }
- void listDir(const char * dirname) {
- Serial.printf("Listing directory: %s\n", dirname);
- Dir root = LittleFS.openDir(dirname);
- while (root.next()) {
- File file = root.openFile("r");
- Serial.print(" FILE: ");
- Serial.print(root.fileName());
- Serial.print(" SIZE: ");
- Serial.print(file.size());
- time_t cr = file.getCreationTime();
- time_t lw = file.getLastWrite();
- file.close();
- struct tm * tmstruct = localtime(&cr);
- Serial.printf(" CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
- tmstruct = localtime(&lw);
- Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
- }
- }
- void readFile(const char * path) {
- Serial.printf("Reading file: %s\n", path);
- File file = LittleFS.open(path, "r");
- if (!file) {
- Serial.println("Failed to open file for reading");
- return;
- }
- Serial.print("Read from file: ");
- while (file.available()) {
- Serial.write(file.read());
- }
- file.close();
- }
- void writeFile(const char * path, const char * message) {
- Serial.printf("Writing file: %s\n", path);
- File file = LittleFS.open(path, "w");
- if (!file) {
- Serial.println("Failed to open file for writing");
- return;
- }
- if (file.print(message)) {
- Serial.println("File written");
- } else {
- Serial.println("Write failed");
- }
- delay(2000); // Make sure the CREATE and LASTWRITE times are different
- file.close();
- }
- void appendFile(const char * path, const char * message) {
- Serial.printf("Appending to file: %s\n", path);
- File file = LittleFS.open(path, "a");
- if (!file) {
- Serial.println("Failed to open file for appending");
- return;
- }
- if (file.print(message)) {
- Serial.println("Message appended");
- } else {
- Serial.println("Append failed");
- }
- file.close();
- }
- void renameFile(const char * path1, const char * path2) {
- Serial.printf("Renaming file %s to %s\n", path1, path2);
- if (LittleFS.rename(path1, path2)) {
- Serial.println("File renamed");
- } else {
- Serial.println("Rename failed");
- }
- }
- void deleteFile(const char * path) {
- Serial.printf("Deleting file: %s\n", path);
- if (LittleFS.remove(path)) {
- Serial.println("File deleted");
- } else {
- Serial.println("Delete failed");
- }
- }
- void setup() {
- Serial.begin(115200);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("Contacting Time Server");
- configTime(3600 * timezone, daysavetime * 3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
- struct tm tmstruct ;
- delay(2000);
- tmstruct.tm_year = 0;
- getLocalTime(&tmstruct, 5000);
- Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
- Serial.println("");
- Serial.println("NOTFormatting LittleFS filesystem"); //**small change. No formatting
- //LittleFS.format();
- Serial.println("Mount LittleFS using *LittleFS.begin()*");
- if (!LittleFS.begin()) {
- Serial.println("LittleFS mount failed");
- return;
- }Serial.println("Now going to use *listDir* from root");
- listDir("/");
- //deleteFile("/hello.txt");
- appendFile("/hello.txt", "---- Wor\nld5!\n"); //small change
- appendFile("/hello.txt", "123456\n"); //small change
- const char *ssidx = "asdfg1234\n";
- appendFile("/hello.txt", ssidx); //try this. Worked!
- // appendFile("/hello.txt", STASSID); //small change
- //stringPlay();
- int a=54325;
- char buffer[20];
- itoa(a,buffer,10); // here 10 means decimal
- printf("Decimal value = %s\n", buffer);
- for(int i = 0;i<20;i++) {
- Serial.printf("Buffer[%u] is : %x\n",i,buffer[i]);
- }
- appendFile("/hello.txt", buffer); //small change
- listDir("/");
- Serial.println("The timestamp should be valid above");
- Serial.println("Now unmount and remount and perform the same operation.");
- Serial.println("Timestamp should be valid, data should be good.");
- LittleFS.end();
- Serial.println("Now mount it");
- if (!LittleFS.begin()) {
- Serial.println("LittleFS mount failed");
- return;
- }
- readFile("/hello.txt");
- listDir("/");
- //stringPlay();
- filePlay();
- }
- void loop() { }
- //---------------------------------------------------------------------------
- void filePlay() { //just tryint new seek() type words
- File f = LittleFS.open("/hello.txt", "r");
- Serial.printf("\nFile position is %d :",f.position());
- Serial.printf("\nFile size is %d :",f.size()); //worked!
- uint8_t charBuf[200]; //simulate big ESPNow buffer
- uint8_t a = f.read();
- Serial.print(a);
- a=f.read(); Serial.print(a);
- Serial.printf("\nFile position is %d :",f.position());
- for(int i=0;i<20;i++){
- charBuf[i]=f.read();
- }
- for(int i=0;i<20;i++){
- Serial.printf("\ncharBuf[%d]=%d\t",i,charBuf[i]);
- printChar(charBuf[i]);
- }
- Serial.printf("\nFile position is %d :",f.position());
- f.close();
- }
- void printChar(char r) {
- Serial.print(r);
- Serial.print(" (");
- Serial.print((byte)r);
- Serial.print(")");
- }
- void stringPlay(){
- int t1 = 42;
- int t2 = 56;
- int number=42;
- char text[20];
- const char *ssid = STASSID;
- Serial.print("Two ints are "); Serial.println(t1); Serial.println(t2);
- Serial.printf("Twof ints are %u and %u", t2,t2); // Serial.println(t1); Serial.println(t2);
- Serial.printf("SSid file: %s\n", ssid);
- //sprintf(str, "%d", 42);
- sprintf(text, "%d", number);
- Serial.printf("\nYou have entered: %s", text);
- Serial.printf("\ntext[0] is %x and text[1] is %X: \n", text[0],text[1] );
- for(int i = 0;i<20;i++) {
- Serial.printf("text[%u] is : %x\n",i,text[i]);
- }
- int a=54325;
- char buffer[20];
- itoa(a,buffer,10); // here 2 means binary
- printf("Binary value = %s\n", buffer);
- for(int i = 0;i<20;i++) {
- Serial.printf("buffer[%u] is : %x\n",i,buffer[i]);
- }
- strcat(text,buffer);
- for(int i = 0;i<20;i++) {
- Serial.printf("text[%u] is : %x\n",i,text[i]); //This works
- }
- }
- /*
- * for( i = 0 ; i < n ; i++ ) {
- printf("%d\n", rand() % 10);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment