Jump to content









Photo

Sent a stream using Processing

Processing create stream api

  • Please log in to reply
5 replies to this topic

#1 talumux

talumux

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 09 February 2015 - 08:27 AM

Hi,

 

I have a problem to sent a stream to carriots using Processing.

(Processing is an easy to use programming language and is very similar to the arduino environment.)

 

I receive no answer from the carriots-server  :mellow:

Here is my code:

import processing.net.*;

Client c;
String data;

void setup() {
  size(200, 200);
  background(50);
  fill(200);
  
  String json="{\"protocol\":\"v1\",\"checksum\": \"\",\"device\":\"defaultDevice@example.example\",\"at\":\"now\",\"data\":{\"level\":\"100\"}}";

  c = new Client(this, "api.carriots.com", 80);
  println(c.ip());

  c.write("POST /streams/ HTTP/1.1\r\n");
  c.write("Host: api.carriots.com\r\n");
  c.write("Accept: application/json\r\n");
  c.write("User-Agent: defaultDevice@example.example\r\n");
  c.write("Content-Type: application/json\r\n");
  c.write("Content-Length: 152\r\n");
  c.write("carriots.apikey: XXXXX5f5a980afaf95211a75819XXXXXX5573944790a2567794a9b259XXXXX\r\n");
  c.write(json);
  c.write("\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);
  }
}

You can also find an easy example at the processing website:

http://processing.or...ent_write_.html

 

thank you in advance for an help or suggestions!

 

 



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 178 posts

Posted 09 February 2015 - 11:16 AM

Hello talamux

 


We are checking our logs and in the last two days Carriots has not received any request from your account.

Looks like the problem is in the code. For some reason, you are not sending requests.

I'm sorry to tell you that we don't have any example using "Processing", but we will try to test in the future



#3 talumux

talumux

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 10 February 2015 - 07:18 PM

Hello, thanks for your reply!

 

I have played around and now I am getting response from the server. (Maybe I missed the empty line before the body?)

But nevertheless there is a little problem left.

 

I got the following response:

HTTP/1.1 401 Unauthorized
Date: Tue, 10 Feb 2015 19:06:37 GMT
Content-Type: application/vnd.carriots.api.v2+json
Content-Length: 40
Connection: keep-alive
Server: Carriots REST API
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control
 
{ "response": "Not supported payload." }
Client got end-of-stream.
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>

 

My "new" code:

import processing.net.*;

Client c;
String data;

void setup() {
  size(200, 200);  background(50);  fill(200);

  c = new Client(this, "api.carriots.com", 80);
  
   c.write("POST /streams/ HTTP/1.1\r\n");
   c.write("Host: api.carriots.com\r\n");
   c.write("Accept: application/json");
   c.write("User-Agent: defaultDevice@talumux.talumux\r\n");
   c.write("Content-Type: application/json\r\n");
   //c.write("Content-Length: 152");
   c.write("carriots.apikey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n");

  c.write("\r\n");  // emtpy line before body
  
  c.write("{");
  c.write("    \"protocol\": \"v1\",");
  c.write("    \"checksum\": \"\",");
  c.write("    \"device\": \"defaultDevice@talumux.talumux\",");
  c.write("    \"at\": \"now\",");
  c.write("    \"data\": {");
  c.write("        \"level\": \"200\"");
  c.write("    }");
  c.write("}");

  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);
  }
}

 

And when I include "Content-Length: 152"  in the header, I get this response  :mellow:

 

HTTP/1.1 411 Length Required
Date: Tue, 10 Feb 2015 19:16:23 GMT
Content-Type: text/html
Content-Length: 181
Connection: close
Server: Carriots REST API

 

Any suggestions?

kind regards!



#4 talumux

talumux

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 10 February 2015 - 08:37 PM

ah... sorry! I forgot the "\r\n" at the Content-Length.

After inserting this I get no response at all  :huh:

c.write("Content-Length: 152\r\n");


#5 jpastor

jpastor

    Development leader

  • Administrators
  • 178 posts

Posted 16 February 2015 - 04:52 PM

Hello talamux

 

We are checking the records again without finding a trace of its user. For some reason, you are not sending requests.

 

We recommend using a tool such as http://requestb.in/ to monitor the requests that you are sending

 

Hope it helps



#6 talumux

talumux

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 21 February 2015 - 06:27 PM

Hi,

 

thanks for the hint to request.in!

 

Since Processing is based on java and tried an other programming approach.

 

My working solution is the following:

 

import processing.net.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.*;

String line;
StringBuffer jsonString = new StringBuffer();

void setup() {

     try {

        URL url = new URL("http://api.carriots.com/streams");
        
        String value="321";
        String payload="{\"protocol\":\"v1\",\"checksum\": \"\",\"device\":\"defaultDevice@talumux.talumux\",\"at\":\"now\",\"data\":{\"humidity_test\":\""+value+"\"}}";     
         
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Host", "api.carriots.com");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("User-Agent", "defaultDevice@talumux.talumux");
        connection.setRequestProperty("Content-Type", "application/json");
        //connection.setRequestProperty("Content-Length", "152"); //connection fails if it is uncommented 
        connection.setRequestProperty("carriots.apikey", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((line = br.readLine()) != null) {
                jsonString.append(line);
                println(line);
        }
        br.close();
        connection.disconnect();
    } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
    }
} 

 

Now I am able to receive data sent by the arduino over uart and sent it to carriots using a processing script.  :)

regards!







Also tagged with one or more of these keywords: Processing, create stream, api

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users