SD card access (SD/sdFat)

Pi support is really exciting and I look forward to trying it out. I’m curious how SD access is handled. If I want to use a library like SD or sdFat, am I required to connect another SD reader using SPI, or is there some way to store and retrieve files on the native Pi SD, perhaps using an aliased folder?

Particle firmware can call bash commands and run bash scripts, so you could do some file manipulation with that. I bet there will be a more C++ native way of manipulating files soon.

The short answer is that all Linux functions are available within your firmware. You can open files, write files, etc.

There’s no easy interface yet but you can look at http://www.cplusplus.com/doc/tutorial/files/

#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;
void setup() {
  myfile.open ("/home/pi/example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
}
2 Likes

Thats pretty cool actually. Will this be documented with examples in the future?

I find it interesting that Particle Firmware on Raspberry Pi has access to full C++, and this will surely give me a chance to work on my non-(Photon / Electron / 'duino) C++ skills, and I would love to make some great projects with Particle on Raspberry Pi.

1 Like

How do I include mySQL access on the Pi? #include “mysql.h”

I’m trying to test this out by using a cloud function which logs the input to a file, but extra " keep getting added to the file:

Here is my code:

#include "Particle.h"
#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;

#define FILENAME "/home/pi/log.txt"

int writeToFile(String message)
{
  message.concat("\n");
  myfile.open(FILENAME, ios::app);
  myfile << message;
  myfile.close();
  return 0;
}


void setup()
{
  Particle.function("write", writeToFile);
}

void loop()
{

}

The output always has " around it
"

"HELLO WORLD
"

If I call the function multiple times, the output comes out like:

"HELLO WORLD
""HELLO WORLD
""HELLO WORLD
""HELLO WORLD
"

Why are the extra quotes added?

I would try changing this line to use the C-style string instead of the String object. You can use the toCharArray() method or the c_str() method.

2 Likes

I was able to use the c_str() method from here http://www.cplusplus.com/reference/string/string/c_str/

#include "Particle.h"
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;

#define FILENAME "/home/pi/log.txt"

int writeToFile(String message) {
  message.concat("\n");
  myfile.open(FILENAME, ios::app);

  char * cstr = new char [message.length()+1];
  std::strcpy (cstr, message.c_str());

  myfile << cstr;
  myfile.close();
  return 0;
}

void setup() {
  Particle.function("write", writeToFile);
}

void loop() {
}
1 Like

Good, it sounds like that worked for you then.

There is probably a way to do it without copying the String data.

The problem is that Arduino String is not a type that std files understand (but char array is).

2 Likes

Thanks for the help @bko

1 Like

How about using Process::run to call the mySQL command line app to add the data to your database?

Process mysql = Process::run("mysql");
// TODO: replace by real command to insert your data
mysql.in().printf("INSERT %d\n", sensorValue);
mysql.in().close();
mysql.wait();
2 Likes

I have never used Process::run, but how can you access that process without a user name and password? I will try this out. For now, I have been using a ‘system(program data);’ call to a small python script to insert the data into my database.

Can’t (yet) get Process::run to work, but I can now use the system() call to directly insert my data into the database:

system("\path\mysql -u user - p password -D database -e \"INSERT INTO table VALUES (sensor data)\"");