update
example v02
// https://discourse.processing.org/t/good-practice-for-sending-data-from-arduino-to-processing/6339/2
// on arduino make and send a JSON String,
// in processing catch the line and use
// https://processing.org/reference/parseJSONObject_.html
// https://processing.org/reference/JSONObject.html
// add to Array and save to file:
// https://processing.org/reference/saveJSONArray_.html
//_________________________________________________________
// test on WIN7 32bit
// and processing IDE 3.4
import processing.serial.*;
Serial myPort; // Create object from Serial class
String inString = "{ \"UNO_1\" : { \"A0\" : 1000 , \"A1\" : 1001, \"A2\" : 1002, \"A3\" : 1003, \"A4\" : 1004, \"A5\" : 1005 } }";
String outfilename = "data/arduinocatch.json";
// String outfilename = "/run/shm/arduinocatch.json"; // for RPI
//______
JSONArray allArduinoLines = new JSONArray();
JSONObject arduinoLine = new JSONObject();
JSONObject values = new JSONObject(); // to catch just the value list ( of the inner { } )
String remoteIOdevice = "UNO_1";
String[] channels = {"A0","A1","A2","A3","A4","A5"};
int bdrate = 115200;
//_________________________________________________________
void setup() {
size(200,200);
get_line(); // init
printArray(Serial.list());
String portName = Serial.list()[1]; // here it was "COM3"
myPort = new Serial(this, portName, bdrate);
}
//_________________________________________________________
void get_line() {
arduinoLine = parseJSONObject(inString);
println(" new inString: "+arduinoLine);
allArduinoLines.append(arduinoLine);
}
//_________________________________________________________
void to_file() {
saveJSONArray(allArduinoLines, outfilename);
}
//_________________________________________________________
void draw() {
background(200,200,0);
check_serial();
show_line();
}
void check_serial() {
while (myPort.available() > 0) {
inString = myPort.readString();
println("serial event: "+inString);
if (inString != null) {
get_line();
to_file();
}
}
}
//_________________________________________________________
void show_line() { // take JSON object "arduinoLine" appart and show as text
int dpos=20, ypos = dpos; // canvas text layout
values = arduinoLine.getJSONObject(remoteIOdevice);
//println(" data from: "+remoteIOdevice+" \n"+values);
fill(0);
text(remoteIOdevice , dpos, ypos); ypos += dpos;
for (int i = 0;i<6;i++) text(channels[i]+": "+values.getInt(channels[i]), 2*dpos, ypos +i*dpos);
}
//_________________________________________________________
/*
first time started: dir and file is created
/data/arduinocatch.json
content:
[{"UNO_1": {
"A1": 1001,
"A2": 1002,
"A3": 1003,
"A0": 1000
}}]
*/
/* Arduino code used here:
// test on Arduino Leonardo USB to win7 PC found "COM3"
// to send a JSON like full text record
String JSON;
int channels[6];
void setup() {
Serial.begin(115200);
while (!Serial) { ; } // wait for serial port to connect. Needed for LEONARDO..
}
void get_Ain() {
for (int i=0;i<6;i++) channels[i] = analogRead(i);
}
void makeJSON() {
JSON = "{ \"UNO_1\" : { ";
for (int i=0;i<6;i++) {
JSON += " \"A";
JSON += i;
JSON += "\" : ";
JSON += channels[i];
if ( i < 5 )JSON += " , ";
}
JSON += " } } ";
}
void loop() {
get_Ain();
makeJSON();
Serial.println(JSON); // to processing
delay(1000);
}
*/
@matheplica what hardware you have to test this?
update: i checked on Raspberry Pi 3B+
and there it was on
/dev/ttyACM0 (Arduino Leonardo )
and i recommend to change data storage from uSDcard to (temporary) RAM DISK
( because write every second to card wears it out in?? )
String outfilename = “/run/shm/arduinocatch.json”; // for RPI
//___________________
@rodrigogamboa i did notice in your arduino code
delay(1); // delay in between reads for stability
pls, that is one millisecond ( not one second )
so possibly you are up to high speed sampling,
then i would recommend a total different way.
- -a- you sample to array as fast as possible
( add there is special speedy code if you sample one channel only ) - -b- in a batch type communication you send that whole array to processing
- -c- you build a oscilloscope like view
i play this on processing, python or webserver
35kHz on one channel for Arduino and 520kHz for MAX32.
check out my BLOG and search for PoorManScope