HTTP + GET + HEROKU

Hi! I am trying the following code to make my Heroku app listen to new messages, but I'm obviously missing something. Can someone help me find it?

import processing.net.*;

Client c;
String data;

void setup() {
  size(200, 200);
  background(50);
  fill(200);
  c = new Client(this, "newapp.herokuapp.com", 80); // Connect to server on port 80
  c.write("GET / HTTP/1.0\r\n"); // Use the HTTP "GET" command to ask for a Web page
  c.write("\r\n");

  int newValue = 10;

  String tempLink = "GET /api/device/mydevice/sensor/temp/report?value=";
  String postDataTemp = tempLink+ newValue +" "+"HTTP/1.1";

  if (c.available()>0) {  
    c.write(postDataTemp);
    c.write("Host: newapp.herokuapp.com");
    c.write("Connection: close");
    String message = c.readString();
    println(message);
    c.stop();
  }
}

void draw() {
}
Tagged:

Answers

  • Changed a couple of things, still nothing as a response:

    import processing.net.*;
    Client c;
    
    //Variables
    String tempLink;
    String serverName;
    String postDataTemp;
    int newValue;
    
    void setup() {
      size(200, 200);
      background(50);
      fill(200);
    
      serverName = "newapp.herokuapp.com";
    
      //String to GET the new value
      tempLink = "GET /api/device/mydevice/sensor/temp/report?value=";
      c = new Client(this, serverName, 80); // Connect to server on port 80
      newValue = 10;
    
      //String to include the HTTP command for the server
      postDataTemp = tempLink+ newValue +" "+"HTTP/1.1";
    
      if (c.available()>0) {  
        //Client writes the HTTP command...
        c.write(postDataTemp+"\r\n"); // Use the HTTP "GET" command to ask for a Web page
        c.write("Host: newapp.herokuapp.com");
        c.write("Connection: close");
        String message = c.readString();
        println(message);
        c.stop();
      }
    }
    
    void draw() {
    }
    
  • edited November 2014

    Now I get the following:

    HTTP/1.1 404 Not Found Connection: keep-alive Server: Cowboy Date: Thu, 06 Nov 2014 12:53:03 GMT Content-Length: 2960 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache, no-store

    import processing.net.*;
    Client c;
    String data;
    String myserver = "newapp.herokuapp.com";
    
    void setup() {
      c = new Client(this, myserver, 80); 
      c.write("GET /api/device/mydevice/sensor/temp/report?value=1 HTTP/1.1"); 
      c.write("\r\n"); 
      c.write("Host: "+myserver+" \r\n"); 
      c.write("Connection: close");
      c.write("Content-Type: text/html; charset=utf-8\r\n"); 
      c.write("Content-Length: 2960\r\n"); 
      c.write("\r\n"); 
    }
    
    void draw() {
      if (c.available() > 0) { // If there's incoming data from the client...
        data = c.readString(); // ...then grab it and print it
        println(data);
      }
    }
    
  • There is no issue with your Processing Code whatsoever. My guess would be that your issue lies with Hirokuapp (or how you understood their api) and not with Processing. Maybe you should take a closer look at the Server response:

    There is no app configured at that hostname. Perhaps the app owner has renamed it, or you mistyped the URL.

    By the way, since it's only a GET command, you can try this on your Browser too: newapp.herokuapp.com/api/device/mydevice/sensor/temp/report?value= newapp.herokuapp.com/api/device/mydevice/sensor/temp/report?value=1

  • edited November 2014

    The link points to nowhere - this isn't the name of the app.

    However, when I post the correct link in a browser, data are received just fine in the app (values are updated). I still haven't been able to use Processing for this job...

  • edited November 2014

    aaaaaaahhhhhh.....!!!!!!! Found the bug!

    Instead of:   c.write("Host: "+myserver+" \r\n");

    It had to be:   c.write("Host: "+myserver+"\r\n");

    Get it?

    :-)

  • ah, there you go. :)

Sign In or Register to comment.