prjbrook

LittleFS_Timestamp.ino

Aug 28th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. /* Example showing timestamp support in LittleFS */
  2. /* Released into the public domain. */
  3. /* Earle F. Philhower, III <[email protected]> */
  4. /* Starting to shed superfluous parts, eg. old trial subroutines to test out new commands
  5. * Call this new ino file: littleFsForSensor ?
  6. */
  7. #include <FS.h>
  8. #include <LittleFS.h>
  9. #include <time.h>
  10. #include <ESP8266WiFi.h>
  11. //#include <stdio.h>
  12.  
  13. #ifndef STASSID
  14. #define STASSID "SPARK-DTCUB2" //your-ssid"
  15. #define STAPSK "VHVHM2A9GD" //"your-password"
  16. #endif
  17.  
  18. const char *ssid = STASSID;
  19. const char *pass = STAPSK;
  20.  
  21. long timezone = 2;
  22. byte daysavetime = 1;
  23.  
  24.  
  25. bool getLocalTime(struct tm * info, uint32_t ms) {
  26. uint32_t count = ms / 10;
  27. time_t now;
  28.  
  29. time(&now);
  30. localtime_r(&now, info);
  31.  
  32. if (info->tm_year > (2016 - 1900)) {
  33. return true;
  34. }
  35.  
  36. while (count--) {
  37. delay(10);
  38. time(&now);
  39. localtime_r(&now, info);
  40. if (info->tm_year > (2016 - 1900)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46.  
  47.  
  48. void listDir(const char * dirname) {
  49. Serial.printf("Listing directory: %s\n", dirname);
  50.  
  51. Dir root = LittleFS.openDir(dirname);
  52.  
  53. while (root.next()) {
  54. File file = root.openFile("r");
  55. Serial.print(" FILE: ");
  56. Serial.print(root.fileName());
  57. Serial.print(" SIZE: ");
  58. Serial.print(file.size());
  59. time_t cr = file.getCreationTime();
  60. time_t lw = file.getLastWrite();
  61. file.close();
  62. struct tm * tmstruct = localtime(&cr);
  63. 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);
  64. tmstruct = localtime(&lw);
  65. 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);
  66. }
  67. }
  68.  
  69.  
  70. void readFile(const char * path) {
  71. Serial.printf("Reading file: %s\n", path);
  72.  
  73. File file = LittleFS.open(path, "r");
  74. if (!file) {
  75. Serial.println("Failed to open file for reading");
  76. return;
  77. }
  78.  
  79. Serial.print("Read from file: ");
  80. while (file.available()) {
  81. Serial.write(file.read());
  82. }
  83. file.close();
  84. }
  85.  
  86. void writeFile(const char * path, const char * message) {
  87. Serial.printf("Writing file: %s\n", path);
  88.  
  89. File file = LittleFS.open(path, "w");
  90. if (!file) {
  91. Serial.println("Failed to open file for writing");
  92. return;
  93. }
  94. if (file.print(message)) {
  95. Serial.println("File written");
  96. } else {
  97. Serial.println("Write failed");
  98. }
  99. delay(2000); // Make sure the CREATE and LASTWRITE times are different
  100. file.close();
  101. }
  102.  
  103. void appendFile(const char * path, const char * message) {
  104. Serial.printf("Appending to file: %s\n", path);
  105.  
  106. File file = LittleFS.open(path, "a");
  107. if (!file) {
  108. Serial.println("Failed to open file for appending");
  109. return;
  110. }
  111. if (file.print(message)) {
  112. Serial.println("Message appended");
  113. } else {
  114. Serial.println("Append failed");
  115. }
  116. file.close();
  117. }
  118.  
  119. void renameFile(const char * path1, const char * path2) {
  120. Serial.printf("Renaming file %s to %s\n", path1, path2);
  121. if (LittleFS.rename(path1, path2)) {
  122. Serial.println("File renamed");
  123. } else {
  124. Serial.println("Rename failed");
  125. }
  126. }
  127.  
  128. void deleteFile(const char * path) {
  129. Serial.printf("Deleting file: %s\n", path);
  130. if (LittleFS.remove(path)) {
  131. Serial.println("File deleted");
  132. } else {
  133. Serial.println("Delete failed");
  134. }
  135. }
  136.  
  137. void setup() {
  138. Serial.begin(115200);
  139. // We start by connecting to a WiFi network
  140. Serial.println();
  141. Serial.println();
  142. Serial.print("Connecting to ");
  143. Serial.println(ssid);
  144.  
  145. WiFi.begin(ssid, pass);
  146.  
  147. while (WiFi.status() != WL_CONNECTED) {
  148. delay(500);
  149. Serial.print(".");
  150. }
  151. Serial.println("WiFi connected");
  152. Serial.println("IP address: ");
  153. Serial.println(WiFi.localIP());
  154. Serial.println("Contacting Time Server");
  155. configTime(3600 * timezone, daysavetime * 3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
  156. struct tm tmstruct ;
  157. delay(2000);
  158. tmstruct.tm_year = 0;
  159. getLocalTime(&tmstruct, 5000);
  160. 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);
  161. Serial.println("");
  162. Serial.println("NOTFormatting LittleFS filesystem"); //**small change. No formatting
  163. //LittleFS.format();
  164. Serial.println("Mount LittleFS using *LittleFS.begin()*");
  165. if (!LittleFS.begin()) {
  166. Serial.println("LittleFS mount failed");
  167. return;
  168. }Serial.println("Now going to use *listDir* from root");
  169. listDir("/");
  170. //deleteFile("/hello.txt");
  171. appendFile("/hello.txt", "---- Wor\nld5!\n"); //small change
  172. appendFile("/hello.txt", "123456\n"); //small change
  173. const char *ssidx = "asdfg1234\n";
  174. appendFile("/hello.txt", ssidx); //try this. Worked!
  175. // appendFile("/hello.txt", STASSID); //small change
  176. //stringPlay();
  177.  
  178. int a=54325;
  179. char buffer[20];
  180. itoa(a,buffer,10); // here 10 means decimal
  181. printf("Decimal value = %s\n", buffer);
  182. for(int i = 0;i<20;i++) {
  183. Serial.printf("Buffer[%u] is : %x\n",i,buffer[i]);
  184. }
  185. appendFile("/hello.txt", buffer); //small change
  186. listDir("/");
  187.  
  188. Serial.println("The timestamp should be valid above");
  189.  
  190. Serial.println("Now unmount and remount and perform the same operation.");
  191. Serial.println("Timestamp should be valid, data should be good.");
  192. LittleFS.end();
  193. Serial.println("Now mount it");
  194. if (!LittleFS.begin()) {
  195. Serial.println("LittleFS mount failed");
  196. return;
  197. }
  198. readFile("/hello.txt");
  199. listDir("/");
  200. //stringPlay();
  201. filePlay();
  202.  
  203. }
  204.  
  205. void loop() { }
  206. //---------------------------------------------------------------------------
  207. void filePlay() { //just tryint new seek() type words
  208.  
  209. File f = LittleFS.open("/hello.txt", "r");
  210. Serial.printf("\nFile position is %d :",f.position());
  211. Serial.printf("\nFile size is %d :",f.size()); //worked!
  212. uint8_t charBuf[200]; //simulate big ESPNow buffer
  213. uint8_t a = f.read();
  214. Serial.print(a);
  215. a=f.read(); Serial.print(a);
  216. Serial.printf("\nFile position is %d :",f.position());
  217. for(int i=0;i<20;i++){
  218. charBuf[i]=f.read();
  219. }
  220.  
  221. for(int i=0;i<20;i++){
  222. Serial.printf("\ncharBuf[%d]=%d\t",i,charBuf[i]);
  223. printChar(charBuf[i]);
  224. }
  225. Serial.printf("\nFile position is %d :",f.position());
  226. f.close();
  227. }
  228. void printChar(char r) {
  229. Serial.print(r);
  230. Serial.print(" (");
  231. Serial.print((byte)r);
  232. Serial.print(")");
  233. }
  234.  
  235. void stringPlay(){
  236. int t1 = 42;
  237. int t2 = 56;
  238. int number=42;
  239. char text[20];
  240. const char *ssid = STASSID;
  241. Serial.print("Two ints are "); Serial.println(t1); Serial.println(t2);
  242. Serial.printf("Twof ints are %u and %u", t2,t2); // Serial.println(t1); Serial.println(t2);
  243. Serial.printf("SSid file: %s\n", ssid);
  244.  
  245. //sprintf(str, "%d", 42);
  246. sprintf(text, "%d", number);
  247.  
  248. Serial.printf("\nYou have entered: %s", text);
  249.  
  250. Serial.printf("\ntext[0] is %x and text[1] is %X: \n", text[0],text[1] );
  251. for(int i = 0;i<20;i++) {
  252. Serial.printf("text[%u] is : %x\n",i,text[i]);
  253. }
  254.  
  255. int a=54325;
  256. char buffer[20];
  257. itoa(a,buffer,10); // here 2 means binary
  258. printf("Binary value = %s\n", buffer);
  259. for(int i = 0;i<20;i++) {
  260. Serial.printf("buffer[%u] is : %x\n",i,buffer[i]);
  261. }
  262.  
  263. strcat(text,buffer);
  264. for(int i = 0;i<20;i++) {
  265. Serial.printf("text[%u] is : %x\n",i,text[i]); //This works
  266. }
  267.  
  268.  
  269. }
  270.  
  271. /*
  272. * for( i = 0 ; i < n ; i++ ) {
  273. printf("%d\n", rand() % 10);
  274. }
  275. */
  276.  
  277.  
Advertisement
Add Comment
Please, Sign In to add comment