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.
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
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:
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;
}
}
}
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.
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.
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.