# \[Processing net POST request\] Error : Client got end of stream

**URL:** https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339
**Category:** Electronics (Arduino, etc.)
**Created:** [June 27, 2019, 1:08pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339 "2019-06-27T13:08:03Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [June 27, 2019, 1:08pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/1 "2019-06-27T13:08:03Z")

</div>

Hello !

I want to send data from arduino to a local server via a serial connection. I have found this [example](https://eacpds.com/thingworx-using-an-arduino-uno-and-serial-connection/) online but I have an End of stream error when I execute the code. This is follows by many java.lang.NullPointerException.  
I don’t know how to debug the code because i don’t understand what can end of stream and for NullPointerException (it is write errors at line 95 but i have only 94 lines.

I think the issue is not on the serial connection to the arduino board because I receive the data from my arduino\_sensor.

Moreover when I try with HTTPIE to send the POST request by hand, it is working.

Thanks in advance for your help !

Here is the following code, i divided it in three part but it is the same code page.

> **Code\_arduino\_to\_local\_server\_via\_serial**
>
> > **Import\_declaration**
> >
> > import http.requests._;  
> > import processing. net._;  
> > import processing.serial.\*;
> > 
> > Client myClient; // Client object
> > 
> > Serial myPort; // The serial port
> > 
> > final int SENSORCOUNT = 12; // This value must match SENSORCOUNT in your Arduino Code
> > 
> > String sensorValues[] = new String[SENSORCOUNT];  
> > String junk;  
> > String beginString = “begin”;  
> > String myServer = “127.0.0.1”;//retirer port  
> > String appKey = “keyd461e-90cf-4c7c-9258-25da3838c36a”;  
> > String thingName = “myArduinoThing”;  
> > String serviceName = “myArduinoService”;  
> > String myURI = “http -v -j POST :8080/Thingworx/Things/” + thingName + “/Services/” + serviceName + " appKey:" + appKey;  
> > //meme erreur String myURI = “POST :8080/Thingworx/Things/” + thingName + “/Services/” + serviceName + " appKey:" + appKey;  
> > String myHost = "Host: " + myServer;  
> > String myContent = “Content-type: text/html\n”;  
> > String sensorNames[] = {  
> > “value1”, “value2”, “value3”, “value4”, “value5”, “value6”, “value7”, “value8”, “value9”, “value10”, “value11”, “value12”  
> > }; //Enter your variable names (these must match the inputs in your Service)
> 
> > **Setup()**
> >
> > void setup() {
> > 
> > // Print a list of the serial ports, for debugging purposes:  
> > println(Serial.list());
> > 
> > String portName = Serial.list()[0];  
> > myPort = new Serial(this, portName, 9600);  
> > //myPort = new Serial(this, “COM14”, 9600);
> > 
> > myClient = new Client(this, myServer, 8080);  
> > }
> > 
> > int idx = SENSORCOUNT + 2;
> 
> > **Draw()**
> >
> > void draw() {
> > 
> > if (myPort.available() \> 0)  
> > {  
> > junk = null;  
> > junk = myPort.readStringUntil(’\n’);
> > 
> > ```
> > // look for the initial “begin” string that Arduino sends
> > if (junk != null)
> > { 
> > if (beginString.equals(trim(junk)))
> > {
> > junk = null;
> > idx=0;
> > }
> > }
> > 
> > //Read each sensor value
> > if ((junk != null) && (idx < SENSORCOUNT))
> > {
> > sensorValues[idx] = junk;
> > junk = null;
> > idx++;
> > }
> > 
> > //When all sensor values have been read, send the info to ThingWorx
> > if (idx == SENSORCOUNT)
> > {
> > myURI=trim(myURI);// ajout
> > myClient.write(myURI);
> > 
> > for (int index = 0; index < SENSORCOUNT; index++)
> > {
> > myClient.write(" " + sensorNames[index] + "=" + trim(sensorValues[index] + " "));
> > }
> > myClient.write(" HTTP/1.1\n");
> > myClient.write(myHost + '\n');
> > myClient.write(myContent + '\n');
> > 
> > println("Sending this REST call:");
> > print(myURI);
> > for (int index = 0; index < SENSORCOUNT; index++)
> > {
> > print(" " + sensorNames[index] + "=" + trim(sensorValues[index] + " "));
> > }
> > print(" HTTP/1.1\n");
> > print(myHost + '\n');
> > print(myContent + '\n');
> > print('\n');
> > 
> > idx = SENSORCOUNT + 2;
> > }
> > 
> > ```
> > 
> > }  
> > }

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [June 27, 2019, 1:33pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/2 "2019-06-27T13:33:54Z")

</div>

I forget to say that I am using the lastest version of processing 3.5.3 and I use net and serial libraries.

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 1, 2019, 7:51am UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/3 "2019-07-01T07:51:27Z")

</div>

I am still stuck, to be more specific my first error is **Client got end-of-stream** follow by null pointer exception.  
I am new in processing and server client interaction but doesn’t it mean that my error is between my client (so my computer) and my local server in the part of my computer ?

Is there a way to correct end of stream by telling my client that the stream will end after 12  
values ?

If someone use processing with a server your help will be appreciated 🙂

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 1, 2019, 12:04pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/4 "2019-07-01T12:04:30Z")

</div>

Hello,

I was very interested but…

When I did a cut and past of your code the first thing is the quotes are an issue.  
I will have to do a song and dance to convert the formatting and I won’t.

I you post clean formatted code that can be run in Processing or viewed in an editor of my choice I may look at it.

I did some exploration last year with Arduino to server with hardware device:

[![](https://img.youtube.com/vi/_lIU0OwWNs4/maxresdefault.jpg "WebSockets with the USR-TCP232-E2") ](https://www.youtube.com/watch?v=_lIU0OwWNs4)

And also with Processing using the hardware.

Hardware devices:  
[https://www.usriot.com/products/serial-ttl-to-ethernet-module.html](https://www.usriot.com/products/serial-ttl-to-ethernet-module.html)  
[https://www.usriot.com/products/dual-ttl-uart-to-ethernet-module.html](https://www.usriot.com/products/dual-ttl-uart-to-ethernet-module.html)

It would be interesting to do Arduino \<\> Processing \<\> Server (\> is “to”, \< is “from”) some more.

🙂  
glv

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 1, 2019, 12:23pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/5 "2019-07-01T12:23:55Z")

</div>

Here is my code without quotes. I used quote because i didn’t want to afraid people with my code. I looked your links thank you @glv . It was interresting to see your setup.

```auto
import http.requests.*;
import processing.net.*;
import processing.serial.*;
import java.net.SocketException; //add for client end of stream (socket)

Client myClient; // Client object

Serial myPort; // The serial port

final int SENSORCOUNT = 12; // This value must match SENSORCOUNT in your Arduino Code

String sensorValues[] = new String[SENSORCOUNT];
String junk;
String beginString = "begin";
String myServer = "127.0.0.1";
String appKey = "140d461e-90cf-4c7c-9258-25da3838c36a";
String thingName = "myArduinoThing";
String serviceName = "myArduinoService";
String myURI = "http -v -j POST :8080/Thingworx/Things/" + thingName + "/Services/" + serviceName + " appKey:" + appKey;
//meme erreur String myURI = "POST :8080/Thingworx/Things/" + thingName + "/Services/" + serviceName + " appKey:" + appKey;
String myHost = "Host: " + myServer;
String myContent = "Content-type: text/html\n";
String sensorNames[] = {
  "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12"
}; //Enter your variable names (these must match the inputs in your Service)

void setup() {

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  //myPort = new Serial(this, "COM14", 9600);

  myClient = new Client(this, myServer, 8080);
}

int idx = SENSORCOUNT + 2;

void draw() {

  if (myPort.available() > 0)
  {
    junk = null;
    junk = myPort.readStringUntil('\n');

    // look for the initial “begin” string that Arduino sends
    if (junk != null)
    { 
      if (beginString.equals(trim(junk)))
      {
        junk = null;
        idx=0;
      }
    }
    
    //Read each sensor value
    if ((junk != null) && (idx < SENSORCOUNT))
    {
      sensorValues[idx] = junk;
      junk = null;
      idx++;
    }

    //When all sensor values have been read, send the info to ThingWorx
    if (idx == SENSORCOUNT)
    {
      // myURI=trim(myURI);// ajout
      myClient.write(myURI);

      for (int index = 0; index < SENSORCOUNT; index++)
      {
       
        myClient.write(sensorNames[index] + "=" + trim(sensorValues[index])+"");
      }
      myClient.write(" HTTP/1.1\n");
      myClient.write(myHost + '\n');
      myClient.write(myContent + '\n');

      println("Sending this REST call:");
      print(myURI);
      for (int index = 0; index < SENSORCOUNT; index++)
      {
        print(" " + sensorNames[index] + "=" + trim(sensorValues[index] + " "));
      }
      print(" HTTP/1.1\n");
      print(myHost + '\n');
      print(myContent + '\n');
      print('\n');

      idx = SENSORCOUNT + 2;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 2, 2019, 8:23am UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/6 "2019-07-02T08:23:58Z")

</div>

My goal is to do  
Arduino\>Processing\>Server

> (\> is “to”, \< is “from”)

Here the server is thingworx. I am able to do, arduino\>processing and by using httpie, I can simulate my programm to do processing\>server.  
However this last part is what cause me some issues.

You are using a websocket in your video. I was wondering if I need to create a websocket.

I am also using arduino mega like in your video.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 2, 2019, 1:08pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/7 "2019-07-02T13:08:14Z")

</div>

> [@Dupond](#):
>
> You are using a websocket in your video.

It was the device that was using websockets.

On my end…  
Working:  
Arduino \<\> Serial to Ethernet device \<\> Server (local on PC)  
Replace:  
“Arduino \<\> Serial to Ethernet device” with “Processing”  
Project:  
Processing \<\> Server (local on PC)

I may try that last project if I have time based on what I did in my last project (see videos).

You may want to consider setting up a local server (I beleive you have already) and work on communicating with that and leave ThingWorx for later.

> [@Dupond](#):
>
> **Client got end-of-stream** follow by null pointer exception.

I am unable to assist with this until I explore further…

On vacation so just popping in to keep my mind engaged.

That was not a simple project for me at the time but I did progress by taking it one step at a time with simple working examples. They are all buried away somewhere at the moment.

🙂

glv

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 2, 2019, 1:37pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/8 "2019-07-02T13:37:53Z")

</div>

Thanks for your particiption and have nice vacation @glv !

Indeed, my Thingworx server is on local.  
Does someone has example of processing program using post request with local server ?  
It would be helpful.

I will try to do other processing example with interaction between local server and processing.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 2, 2019, 6:20pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/9 "2019-07-02T18:20:31Z")

</div>

I **ported** my Arduino web page “server” to Processing as a warm up exercise.  
There are better ways to deal with strings but just left it as is for now.

> <https://github.com/processing/processing/issues/3937>

```auto
// Web page is at http://127.0.0.1:20000/1 
// Count on page increases with each refresh of page
// The Processing console shows request from web page
  
import processing.net.*;

Server s;
Client c;

String input;
int data[];
int count = 0;

int val = 0;

void setup() 
  {
  size(200, 200);
  // Starts a myServer on port 5204
  s = new Server(this, 20000); 
  }

void draw() 
  {
  background(0);

  c = s.available();
  if (c != null) 
    {
    input = c.readString();
    println("Received:\r\n"+ input);
    input = input.substring(0, input.indexOf("\r\n")); // Only up to the newline
    data = int(split(input, ' ')); // Split values into an array
    println(input);
    //printArray(data);
    }  
  
  if (input != null)
    {
    //if (input.equals("GET /1 HTTP/1.1") == true)
    // println("/1");  
  
    //if (input.equals("GET /favicon.ico HTTP/1.1") == true)
    // println("/favicon.ico");

    if (input.equals("GET /1 HTTP/1.1") == true)
      {
      webPage1();
      count++;
      println("Count sent: " + count);
      println();
      println(" *************************************************************");
      println();
      }
    }
  input = null;
  }

void webPage1()
  {                                                                                                                               
  s.write("HTTP/1.1 200 OK\r\n");
  s.write("Content-Length: ");
  s.write("131");
  s.write("\r\n");
  s.write("Content-Type: text/html\r\n");
  s.write("Connection: close\r\n\r\n");                        
 
  s.write("<!DOCTYPE html>\r\n");
  s.write("<head>\r\n");
  s.write("<link rel=\"icon\" href=\"data:,\">"); // Does not request fav.icon now!
  s.write("</head>\r\n");
  s.write("<html>\r\n");                                       
  s.write("<body>\r\n");
  s.write("<h3>Hello Folks!</h3>\r\n");
  s.write("<h3>" + count + "\r\n");
  s.write("</body>\r\n");
  s.write("</html>\r\n\r\n");
  s.write(" *******************************************************************");
  }

```

Next step is to install WAMP and have Processing communicate with it:

> **[WampServer](https://sourceforge.net/projects/wampserver/)**
>
> Download WampServer for free. A Windows Web development environment for Apache, MySQL, PHP databases. WampServer is a Web development platform on Windows that allows you to create dynamic Web applications with Apache2, PHP, MySQL and MariaDB....

  
Lot’s of options there to explore.

🙂

glv

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 3, 2019, 12:18am UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/10 "2019-07-03T00:18:48Z")

</div>

I posted an example in the gallery:

> [@Networking: Processing Client to PHP Server](https://discourse.processing.org/t/networking-processing-client-to-php-server/12466):
>
> A very simple example of a Processing network client communicating with a PHP server. I used the WAMP server: An “update” increments a counter (Processing) and is sent to a server( with c.write()) and stored in a file on server. You can run the web page (reads “counter” from file) to watch the updates or just open the file to see current status. Restarting the Processing sketch resets the counter on Processing and the saved counter on server. “Updates” are sent with a mouse left button …

🙂

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 3, 2019, 7:43am UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/11 "2019-07-03T07:43:59Z")

</div>

Thanks for sharing your experience !  
I will make some tests with your code 🙂

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 17, 2019, 4:49pm UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/12 "2019-07-17T16:49:43Z")

</div>

How is the project coming along?

---

<div class="post-metadata">

### Author: ![Dupond](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dupond](https://discourse.processing.org/u/Dupond)
#### Post date: [July 19, 2019, 7:03am UTC](https://discourse.processing.org/t/processing-net-post-request-error-client-got-end-of-stream/12339/13 "2019-07-19T07:03:15Z")

</div>

I am still stuck so I am trying to do other stuff and come later on that.  
The only thing that I have found is that my error seem to come from the functions write() that I use with my client but when I look other exemple they have done the same write() as me and I don’t see any change if at the end I put \r\n or \n I have the same resuts.
