cnsat1
1
Hi,
I have to connect Arduino to a specific TCP/IP port and parse the streaming data received.
Basically I have a server that outputs continuously strings like the following:
Dec 4 2020 11:32:20.304 THD: 42.3 42.2 42.2 42.3 42.3
Dec 4 2020 11:32:23.008 +PLN: 283.6 42.4 2.6
Dec 4 2020 11:32:05.159 +ACTIVE: 00000000 00000000
Every string of course finish with CR and it contains a data that I have to parse (for example the value after +PLN).
I know that I should first create a Ethernet.client connection than collect the strings and Parse it.
But the tests done are unsuccessful.
Any Suggestion?
Thank you
Have you got as far as being able to receive the data and store each character in a line in an array as it is received ?
Juraj
3
I think I can help you, but show us some code
cnsat1
4
This is the code I'm using.
#include <SPI.h>
#include <Ethernet.h>
String id = "id";
String state = "State/";
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
char server[] = "192.168.86.54";
EthernetClient client;
String readString = "";
String newString ="";
void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println("Connecting...");
if (client.connect(server, 3000))
{
Serial.println("Arduino connected with TCP server");
char deviceConnected[25];
id.toCharArray(deviceConnected, 25);
client.write(deviceConnected);
delay(1000);
}
else
{
Serial.println("Connection failed.");
delay(1000);
setup();
}
}
void loop()
{
while (client.connected())
{
while (client.available())
{
char c = client.read();
readString += c;
if(readString.indexOf("+PLN") >= 0) {
//Serial.println(readString); //prints string to serial port out
int pos1 = readString.indexOf(':');
int pos2 = readString.indexOf('.');
newString = readString.substring(pos1+1, pos2+2);
Serial.print("PLN is: ");
Serial.println(newString);
readString = "";
}
}
}
Serial.println();
Serial.println("Disconnecting.");
client.stop();
delay(1000);
Serial.println("Disconnected.");
setup();
}
Juraj
5
the readString will contain +PLN before the rest of the line is read
cnsat1
6
sorry, I don't understand what you mean. Basically It works but after few seconds it stops
system
Closed
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.