Skip to main content
10-Marble
July 16, 2019
Question

[POST request] Client got end of stream

  • July 16, 2019
  • 5 replies
  • 2214 views

I want to send sensor value from a arduino mega to Thingworx.
I have a thing and a service created however I got an Client got end of stream when I execute my processing program.When I am using HTTPIE (that I have found on REST API tutorial) to make a single post request it is working however when i use my processing program. I am not able to make it work.
Is there conditions to make multiple post request each time I get my data ?


Is there a way to see what happen on thingworx when I make a POST request ? Thanks in advance

I have follow this tutorial.
https://eacpds.com/thingworx-using-an-arduino-uno-and-serial-connection/

My code :

import http.requests.*;
import processing.net.*; //librarie for post request to the server
import processing.serial.*; //serial connexion to arduino
//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 = "localhost";
String myServer = "127.0.0.1";//server IP, warning don't put port number here
String appKey = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // xxxx are remplace in my code by my application key for admin that you have created on thingworx
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/Thingworx/Things/" + thingName + "/Services/" + serviceName + " appKey:" + appKey;
String myHost = "Host: " + myServer;
//String myContent = "Content-type: text/html\n"; we need to send json type
String myContent = "Content-type: application/json\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); //Port number for serial to arduino

 myClient = new Client(this, myServer, 8080);
 print("my client: ",myClient);
}

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
 print("my URI : ",myURI);
 myClient.write(myURI);

 for (int index = 0; index < SENSORCOUNT; index++)
 {
 //myClient.write("" + sensorNames[index] + "=" + trim(sensorValues[index] + " "));
 myClient.write(sensorNames[index] + "=" + trim(sensorValues[index])+"");
 }
 
 //myClient.write(" HTTP/1.1\r\n");
 print("send http");
 myClient.write(myHost + "\r\n");
 myClient.write(myContent + "\r\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;
 }
 }
}

 

5 replies

22-Sapphire I
July 16, 2019

Have you tried looking at the application log and adding some logger statements to the services you are calling to help with the troubleshooting?

Clement_D10-MarbleAuthor
10-Marble
July 17, 2019

Yes, I am looking for some help to troubleshoot my POST request.

I am new to API and post request.
When I check my application log I have only log about my thingworx metrics that i have used for some test but I no longer need it.
Is there a way to see all the request POST that happen on my server ? I didn't find how I sould use logger. Is this something I had to put on my service in the javascript code ?
Thanks


log_application.JPG

 

Clement_D10-MarbleAuthor
10-Marble
July 17, 2019

When I add logger.info(); on my code I have this error :

Error executing service myArduinoService. Message :: La méthode 'ch.qos.logback.classic.Logger.info()' est introuvable - See Script Error Log for more details.

Is there a way to enable the logger ? Or is there an error on my code.