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

Hello !

I want to send data from arduino to a local server via a serial connection. I have found this example 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;
}

}
}

1 Like

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

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 :slight_smile:

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:

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/dual-ttl-uart-to-ethernet-module.html

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

:slight_smile:
glv

1 Like

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.

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;
    }
  }
}
1 Like

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.

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.

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.

:slight_smile:

glv

1 Like

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.

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.

// 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:


Lot’s of options there to explore.

:slight_smile:

glv

I posted an example in the gallery:

:slight_smile:

1 Like

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

How is the project coming along?

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.