macca-nz

ESPFlash Example

Jun 29th, 2021 (edited)
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.60 KB | None | 0 0
  1. /* Example sketch using ESPFlash SPIFFS helper library to save
  2.  * two variables and one 10 value averaging array
  3.  * Author: R McCleery <macca448@gmail.com>
  4.  *
  5.  * NOTE1: To avoid needing to include SPIFFS.h in your config change
  6.  *        #include <FS.h> to #include <SPIFFS.h> in the "ESPFlash.h" library
  7.  *
  8.  * NOTE2: This sketch was tested on an ESP32 but should work on other
  9.  *        ESP devices, just check your PIN functions
  10.  *
  11.  * NOTE3: The idea of this sketch is to execuite all functions in setup()
  12.  *        and use "Deep Sleep" mode to ultimately build BLYNK sketches from it.
  13.  *
  14.  * This Arduino ESP sketch is free software: you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation, either version 3 of the License, or
  17.  * (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details. https://www.gnu.org/licenses/gpl-3.0.en.html
  23.  */
  24.  
  25. #include "ESPFlash.h"                                // https://github.com/DaleGia/ESPFlash
  26. #include <limits>
  27. #define LED 2                                        // Provides a Visual idication of State (Solid = Running)
  28. #define RESET 4                                      // This is to erase SPIFFS and ZERO all Values
  29. #define SEED 36                                      // This Seed is based on an ESP32 Analog Pin
  30. #define HOLD 15                                      // Optional, I use it just to "PAUSE" the loop
  31. #define uS_TO_S_FACTOR 1000000                       // Conversion factor for micro seconds to seconds
  32. #define TIME_TO_SLEEP  15                            // Time ESP32 will go to sleep (in seconds)
  33. #define SIZE 10
  34. #define SAVE 500
  35. int avgArray[SIZE], averageIndex, lastVal, total,
  36.     ranVal, average;
  37. long ct, rate;
  38. bool jump = false;
  39.  
  40. void setup() {
  41.     Serial.begin(115200);
  42.     while(!Serial);
  43.     randomSeed(SEED);
  44.     pinMode(LED, OUTPUT);                                        // LED ON to give visual that loop is running
  45.     digitalWrite(LED, HIGH);
  46.     pinMode(RESET, INPUT_PULLUP);
  47.     pinMode(HOLD, INPUT_PULLUP);    
  48.     ranVal = (random(200, 401));
  49.     Serial.println("");
  50.     Serial.println("ESP Booting Up............");
  51.     Serial.println("");
  52.  
  53.         // RESET Functions
  54.         if(digitalRead(RESET) == LOW){
  55.             Serial.print("\n");
  56.             Serial.println("Resetting ESP Device ...........");Serial.println("");
  57.             Serial.println("Formatting SPIFFS. Will take a while...");
  58.             Serial.println("");Serial.println("LED will start a Slow Blink when RESET complete");
  59.             Serial.println("");                                 // Make sure to configure SPIFFS correctly
  60.             SPIFFS.format();                                    // This erases the SPIFFS partition
  61.             Serial.println("Done!");                            // but it does not ZERO data
  62.             delay(SAVE);                                        // Small delays() are used to give SPIFFS time to save
  63.            
  64.             //Zero data for Index Val
  65.             Serial.println("Setting values to ZERO............");
  66.             ESPFlash<int> espIndexVal("/averageIndexVal");
  67.                 if(espIndexVal.get() > 0){
  68.                     averageIndex = 0;
  69.                 }
  70.             bool sucess1 = espIndexVal.set(averageIndex);       // This saves the averageIndex INT to SPIFFS
  71.             delay(SAVE);                                        // You can use the bool to confirm sucess
  72.             Serial.print("Reading RESET Stored 'averageIndex': ");
  73.             Serial.println(espIndexVal.get());                  // Retrieve the stored file to confirm
  74.            
  75.             //Zero data for Last Val
  76.             ESPFlash<int> espLastVal("/lastValStore");
  77.                 if(espLastVal.get() > 0){
  78.                     lastVal = 0;
  79.                 }
  80.             bool sucess2 = espLastVal.set(lastVal);             // This saves the lastVal INT to SPIFFS
  81.             delay(SAVE);                                        // You can use the bool to confirm sucess
  82.             Serial.print("Reading RESET Stored 'lastVal': ");
  83.             Serial.println(espLastVal.get());Serial.println("");// Retrieve the stored file to confirm
  84.  
  85.             //Zero data for Array Values
  86.             ESPFlash<int> espArray("/arrayStore");
  87.                 for(int i = 0; i < SIZE; i++){
  88.                     avgArray[i] = 0;
  89.                     total = total + avgArray[i];
  90.                 }
  91.             bool sucess3 = espArray.setElements(avgArray, sizeof(avgArray)); // This save the avgArray to SPIFFS
  92.             delay(SAVE);
  93.             espArray.getFrontElements(avgArray, sizeof(avgArray));           // Retrieve the stored file to confirm
  94.             Serial.print("Reading RESET Stored 'avgArray' values: ");
  95.                 for(int i = 0; i < SIZE; i++){
  96.                     Serial.print(avgArray[i]);Serial.print("  ");
  97.                     total = total + avgArray[i];
  98.                 }
  99.             Serial.print("\t TOTAL: ");Serial.println(total);
  100.             Serial.println("");
  101.             rate = SAVE;                                                      // Sets LED blink rate
  102.             jump = true;                                                      // Bypass update functions
  103.         }                                                                     // to loop() and blink LED
  104.      
  105.         // PAUSE Loop
  106.         if(digitalRead(HOLD) == LOW){                                         // Optional PAUSE statement
  107.             Serial.println("LOOP is on PAUSED and LED will FLASH Fast");
  108.             rate = 150;                                                       // Sets LED blink rate
  109.             jump = true;                                                      // Bypass update functions
  110.         }
  111.  
  112.     if(jump == false){  
  113.    
  114.     esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);            // Setup Deep Sleep function            
  115.    
  116.     //ESP Flash Index
  117.     ESPFlash<int> espIndexVal("/averageIndexVal");
  118.         if(espIndexVal.getFileName()){
  119.             averageIndex = espIndexVal.get();                                // Reead saved averageIndex value
  120.                 if(averageIndex >= SIZE){                                    // Rest index to zero when index max is reached
  121.                     averageIndex = 0;
  122.                 }    
  123.         }
  124.  
  125.     //ESP Flash Last Val
  126.     ESPFlash<int> espLastVal("/lastValStore");    
  127.         if(espLastVal.getFileName()){
  128.             lastVal = espLastVal.get();                                     // Retrieve saved lastVal
  129.                 if(espLastVal.get() == 0){                                  // For testing we pass an initial value
  130.                     lastVal = 23500;                                        // to last Value after reset.
  131.                     bool sucess2 = espLastVal.set(lastVal);                 // Save new value to SPIFFS
  132.                     delay(SAVE);
  133.                 }else{
  134.                     lastVal = lastVal - ranVal;
  135.                     bool sucess2 = espLastVal.set(lastVal);                 // Save updated value to SPIFFS
  136.                     delay(SAVE);
  137.                 }    
  138.         }
  139.  
  140.     //ESP Flash avgARRAY
  141.     ESPFlash<int> espArray("/arrayStore");
  142.     espArray.getFrontElements(avgArray, sizeof(avgArray));                 // Retrieve avgArry Values
  143.         for(int i = 0; i < SIZE; i++){
  144.             total = total + avgArray[i];
  145.         }  
  146.         if(total == 0){
  147.             for(int i = 0; i < SIZE; i++){
  148.                 avgArray[i] = ranVal;                                     // After a RESET populate the avgArray with
  149.             }                                                             // a single value
  150.             averageIndex = 1;                                             // Set Index to One for next update
  151.             bool sucess1 = espIndexVal.set(averageIndex);                 // Save new array values to SPIFFS
  152.             delay(SAVE);
  153.         }else{
  154.             total = total - avgArray[averageIndex];                       // Once the avgArray is > 0 we step along the        
  155.             avgArray[averageIndex] = ranVal;                              // array adding a new random value
  156.             total = total + avgArray[averageIndex];
  157.             averageIndex = averageIndex + 1;                              // Increment Index value for next update
  158.                 if(averageIndex >= SIZE){
  159.                     averageIndex = 0;                                
  160.                 }
  161.                 bool sucess1 = espIndexVal.set(averageIndex);             // Save new Index value
  162.                 delay(SAVE);
  163.         }
  164.         bool sucess3 = espArray.setElements(avgArray, sizeof(avgArray));  // Save updated avgArray values
  165.         delay(SAVE);                                                      // We now read all the saved files back
  166.         Serial.print("Reading after save Index Value: ");                 // for Print debugging
  167.         Serial.println(espIndexVal.get());
  168.         Serial.print("Reading after save Last Value: ");
  169.         Serial.println(espLastVal.get());
  170.         Serial.print("Reading after save Array Values: ");
  171.         espArray.getFrontElements(avgArray, sizeof(avgArray));
  172.             for(int i = 0; i < SIZE; i++){
  173.                 Serial.print(avgArray[i]);Serial.print("  ");
  174.                 total = total + avgArray[i];
  175.             }
  176.         delay(SAVE);    
  177.         Serial.print("\n");
  178.         average = total / SIZE;                                           // Create an Average value from updated avgArray Total
  179.         Serial.print("TOTAL: ");Serial.print(total);
  180.         Serial.print("\t Average: ");Serial.println(average);
  181.         Serial.println("");
  182.         Serial.println("ESP Going to Sleep.............");
  183.         Serial.println("");
  184.         digitalWrite(LED, LOW);
  185.         delay(SAVE);
  186.         esp_deep_sleep_start();                                          // Put's the ESP32 into Deep Sleep mode.
  187.     }
  188.     ct = millis();
  189. }
  190.  
  191. void loop() {
  192.     if(millis() - ct > rate){                                           // We only hit the main loop() in RESET or PAUSE modes
  193.       int state = digitalRead(LED);
  194.       ct = millis();
  195.       digitalWrite(LED, state = !state);
  196.         if(digitalRead(RESET) == HIGH && digitalRead(HOLD) == HIGH){   // Device will do a restart when condition met
  197.           ESP.restart();
  198.         }
  199.     }
  200. }
Add Comment
Please, Sign In to add comment