POST requests to Particle Photon in Processing (HTTP Requests library)

So I'm trying to do a simple POST to my Particle Photon, in HTML the post looks like this:

    <!DOCTYPE>
    <html>
      <body>
      <center>
      <br>
      <br>
      <br>
      <form action="https://api.particle.io/v1/devices/27003b000647343232363230/led?access_token=8a0770b34e49d68e8a5e829f2a8e5f0ccb20b2a5" method="POST">
        Tell your device what to do!<br>
        <br>
        <input type="radio" name="args" value="on">Turn the LED on.
        <br>
        <input type="radio" name="args" value="off">Turn the LED off.
        <br>
        <br>
        <input type="submit" value="Do it!">
      </form>
      </center>
      </body>
</html>

I'm trying to use the [https://github.com/runemadsen/HTTP-Requests-for-Processing](HTTP Requests library) (version 0.1.2 because 0.1.3 is buggy). And my code looks like this:

import http.requests.*;

public void setup() {
  background(255);
  size(500, 500);
  PostRequest post = new PostRequest("https://api.particle.io/v1/devices/27003b000647343232363230/led?access_token=8a0770b34e49d68e8a5e829f2a8e5f0ccb20b2a5");
  post.addData("args", "on");
  post.send();
  System.out.println("Reponse Content: " + post.getContent());
  println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
}

It works fine in the HTTP version, but I can't get Processing talking. Any advice??

Answers

  • edited October 2016

    Hey Scott, did you get this to work in the end?

    I've been trying to do send POST requests to my Photon Particle, but haven't had much success.

    I'm using the HTTP Requests for Processing library and here's what my POST requests are looking like:

    PostRequest post = new PostRequest("https://api.particle.io/v1/devices/xxx/led");
    post.addData("access_token", "xxx");
    post.addData("args", "on");
    post.send();
    

    It crossed my mind that the library may not support HTTPS.

  • I'll answer my own question. I was able to communicate with the device with the following code:

    void postReq(String value) {
    
      String httpsURL = "https://api.particle.io/v1/devices/xxx/led";
    
      try {
    
        String query = "access_token=xxx"; 
        query += "&";
        query += "args="+value ;
    
        URL myurl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
        con.setRequestMethod("POST");
    
        con.setRequestProperty("Content-length", String.valueOf(query.length())); 
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"); 
        con.setDoOutput(true); 
        con.setDoInput(true); 
    
        DataOutputStream output = new DataOutputStream(con.getOutputStream());  
    
        output.writeBytes(query);
    
        output.close();
    
        DataInputStream input = new DataInputStream( con.getInputStream() ); 
    
        for ( int c = input.read (); c != -1; c = input.read() ) 
          System.out.print( (char)c ); 
        input.close(); 
        System.out.println();
        System.out.println("Resp Code: "+con.getResponseCode()); 
        System.out.println("Resp Message: "+ con.getResponseMessage());
      }
      catch(IOException e) {
    
        System.out.println("Failed");
      }
    }
    
Sign In or Register to comment.