Processing and Photon - how to diagnose server errors [Resolved]

I am trying to write a Processing application that will communicate with a Photon. I am using the httprequests processing library which simplifies the apache http java interface.

My problem is that if I do a terminal-based curl POST to access a function on the photon I have no problem and get the expected response, but the same POST request through the program returns a server error and I have no clue as to how to diagnose this further. GET requests work fine programmatically.

Anyone have any experience working with processing and a photon?

Hey, @AndrewS.

So, I’ve not really used Processing much. I’ve done a little bit of Java, so I can’t be of much help there either.

Are you trying to hit the Photon directly (i.e. you have an HTTP server running on the Photon), or are you trying to hit the Particle API?

If you want to see exactly what your Processing client is sending vs what curl is sending, you can use a service like http://requestb.in to inspect what you’re sending and see how the two clients compare. So instead of sending POSTs to https://api.particle.io, you’d create a RequestBin and use that URL in your curl request and Processing request.

RequestBin even shows you examples of how to make requests with several clients including Java and curl.

Regardless of the client, that’d be where I would start.

I hope that helps!

Sharing the code you’re trying to use also never hurts, and gives us an idea what we’re dealing with :wink:

Thanks for the advice @jtzemp and @Moors7. I will try the requestbin approach and see what happens.

The processing code is below and is calling the Particle API. The GET call to read the SSID variable works fine. Both the getDevice and postData POST calls return server errors in the response, as follows:

Response Content: {
  "error": "server_error",
  "error_description": "server_error"
}

Processing code:


 import http.requests.*;

String device_id = "nnnnnnnnnnn";
String access_token = "xxxxxxxxxxxx";

int pdreturn = 0;

void setup() 
{
  size(400, 400);  
  println("SSID : "+getData("SSID"));
  println("EEP update: "+postData("updateEEP", "004.1.0.15.0"));
  getDevice();
}

String getData(String var) {
  String reqprefix = "https://api.particle.io/v1/devices/"+device_id+"/"+var+"?access_token="+access_token;
  GetRequest get = new GetRequest(reqprefix);
  get.send();
  JSONObject response = parseJSONObject(get.getContent());
  JSONObject info = response.getJSONObject("coreInfo");
  return(response.getString("result"));
}

int getDevice() {
  String reqprefix = "https://api.particle.io/v1/devices/"+device_id+"?";
  PostRequest post = new PostRequest(reqprefix);
  post.addData("access_token", access_token);
  post.send();
  println("Response Content: " + post.getContent());
  JSONObject response = parseJSONObject(post.getContent());
  return(response.getInt("return_value"));
}

int postData(String var, String args) {
  String reqprefix = "https://api.particle.io/v1/devices/"+device_id+"/"+var;
  PostRequest post = new PostRequest(reqprefix);
  post.addData("access_token", access_token);
  post.addData("args", args);
  post.send();
  println("Response Content: " + post.getContent());
  JSONObject response = parseJSONObject(post.getContent());
  return(response.getInt("return_value"));
}

I have ‘exposed’ variables and functions as follows. The ‘updateEEPROM’ function simply serial prints the supplied argument (for testing purposes).

  Particle.function("updateEEP", updateEEPROM);  // get data and add to EEPROM
  Particle.function("useEEPROM", useEEPROM);     // forces the use of the EEPROM data after getting new data
  Particle.function("reportStats", rptStats);     // returns requested network/timing stats
  
  Particle.variable("SSID", SSID);
  Particle.variable("bugtime", bugtime);
  Particle.variable("bugsig", bugsig);

The goal is to make the following artwork operate over wi-fi. I have previously tried with a Core but problems with the cc3000 made it too unreliable. https://vimeo.com/99318586

Hey @Moors7, are you able to help interpret the requestb.in results please?

I have tried the requestb.in approach and both the cURL and Processing code versions give identical results, ie they both appear to be properly formed. A screen shot of the two results is below. Obviously there are some limitations with this because the url is totally different to that used in the actual cloud api request.

I have also spent 2 days trying other approaches within Processing with no luck, but at least I have found some code which reports the http error code from particle.io. This appears to be error 503 - service unavailable. If that means anything.

Andrew

@AndrewS, were you able to work though this issue and send POST requests from Processing?

I’ve found myself in the same situation, receiving the same server errors that you shared in your first post. Could it be that the Particle server is secure (https)?

I’ve posted on the Processing forums as well. There seems to be little documentation on how to make this happen.

Thanks++ for any insights!

Here’s my solution: https://forum.processing.org/two/discussion/comment/76458/

I never did Jeremy, In desperation I ended up using the temboo subscription service as their interface was quite simple and they offered user support to get me up and running. I use this method occasionally in my artworks so will definitely take a look at your solution (cheaper when it’s free!).
Andrew