// 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 RPI Raspbian // and processing IDE 3.5.2 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 = "/run/shm/arduinocatch.json"; //"data/arduinocatch.json"; 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 = 57600; //115200; int lf = 10; // Linefeed in ASCII boolean diagp = true, goodline=false; //_________________________________________________________ void setup() { size(200, 200); get_line(); // init printArray(Serial.list()); String portName = Serial.list()[1]; // here it was "/dev/ttyACM0" myPort = new Serial(this, portName, bdrate); } //_________________________________________________________ void get_line() { try { arduinoLine = parseJSONObject(inString); goodline = true; } catch (Exception e) { goodline = false; println("badline!"); //e.printStackTrace(); } if (diagp) println(" new inString: "+arduinoLine); if ( goodline) allArduinoLines.append(arduinoLine); } //_________________________________________________________ void to_file() { saveJSONArray(allArduinoLines, outfilename); } //_________________________________________________________ void draw() { background(200, 200, 0); check_serial(); if ( goodline) show_line(); } void check_serial() { while (myPort.available() > 0) { //inString = myPort.readString(); // did work on win 7 laptop but not RPI ?timing? inString = myPort.readStringUntil(lf); //println("serial event: "+inString); if (inString != null) { get_line(); if ( goodline) 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); } */