Hi,
So I have a particle Boron and I am programming it locally in Workbench. And I have a project where I get data from an accelerometer and I want to save it to the on board flash (only 2 MB are available to users), even after it's powered off.
Can someone please point me to the correct library I am supposed to install, and how I am supposed to use this?
For a place to start, there is a library called: StorageHelperRK with a number of examples. I have utilized code in the form of examples: 04-persistent.cpp & 07-validate.cpp on the Argon. I have not used it with the Boron yet, but it should work.
However, you might find it easier to eliminate the .ino file from your Workbench project and only use the .cpp file. To do that, try adding the following #include line at the top of your .cpp file, - above your other library #include(s):
#include "Particle.h"
// add the above line to the top of your example code
// - above ALL your library #includes - and run it in the .cpp file ONLY
#include "StorageHelperRK.h"
SerialLogHandler logHandler(LOG_LEVEL_TRACE);
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
... // the rest of your code here
Thank you so much; you were of great help. Do you know of a library other than the one you said where reading and writing files are much simpler; like you write data with one simple function, etc.
It would help to create a "struct" for persistent data you want to store/retrieve and assign an address to it. This thread nicely covers this as well as assigning a data version number which comes in very handy: https://community.particle.io/t/eeprom-technique/29263
On the Boron (also Argon, B Series SoM, P2, and Photon 2), the EEPROM is actually just a 4096 byte file on the file system. It's part of the baseline usage of the 2 MB flash file system along with a couple K of configuration data. The total system usage is under 10K, except on the Tracker.
What development environment are you using? Workbench or Web IDE?
If Workbench, make sure you've use Particle: Configure Project for Device and selected your Boron and a recent version of Device OS.
If Web IDE, make sure you've selected a Boron and a recent version of Device OS from the Devices tab. Make sure there is a gold star before the name. The default is a device that does not support the file system if you do not select a device.
So this time it actually didn't get an error, but it's not running my code correctly. Here is my code:
#include <fcntl.h>
SYSTEM_THREAD(ENABLED)
void setup() {
pinMode(7, OUTPUT);
int fd = open("/FileSystemTest/test1.txt", O_RDWR | O_CREAT | O_TRUNC);
if (fd != -1) {
for(int ii = 0; ii < 100; ii++) {
String msg = String::format("testing %d\n", ii);
digitalWrite(7,1);
write(fd, msg.c_str(), msg.length());
}
close(fd);
}
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
// The core of your code will likely live here.
}
The LED on D7 is not on, thus I concluded that it didn't write to the flash. Am I doing something wrong?
So, I am trying to write code to read data from the file that was created in setup; here is my code:
#include <fcntl.h>
SYSTEM_THREAD(ENABLED)
int fd;
String* buf = new String[100];
void setup() {
pinMode(7, OUTPUT);
Serial.begin(9600);
fd = open("/usr/test1.txt", O_RDWR | O_CREAT | O_TRUNC);
if (fd != -1) {
for(int ii = 0; ii < 100; ii++) {
String msg = String::format("testing %d\n", ii);
digitalWrite(7,1);
Serial.println(msg);
write(fd, msg.c_str(), msg.length());
}
close(fd);
}
}
int gd;
// loop() runs over and over again, as quickly as it can execute.
void loop() {
// The core of your code will likely live here.
gd = open("/test1.txt", O_RDWR);
if (gd != -1) {
digitalWrite(7,0);
int a = read(gd, buf, 100);
Serial.println(a);
if (a != 0) {
Serial.println(*buf);
close(gd);
}
}
It's not working and I don't know how to fix it since I am very new to this environment. I also tried:
gd = open("/usr/test1.txt", O_RDWR);
but that resulted in a blinking red LED. Please Help.