You have some very curious code constructs. And please use code tags when posting code: click the </> button, to insert the tags, then paste the code between them. It makes it much easier to read.
TobiasCrackz:
HttpClient client;
client.get("http://xxxxx/xxx/led.txt");
if (client.available() > 0) {
char c = client.peek();
}
client.flush();
What are you trying to accomplish here? You make an HTTP request. Then, you look to see if there is any data returned: if so, you create a local variable, read a character, then throw away that character at the end of the IF block. Then you throw away the rest of the clients data when you flush it. Why bother peeking at a character if you don't do anything with it?
Do you actually need to look at the result of the request? If so, what does the result normally look like, and what do you want to do if the response is not as expected? We can give you some further hints with more information.
TobiasCrackz:
void gettimefromlinux () {
Process date;
if (!date.running()) {
date.begin("date");
date.addParameter("+%D-%T");
date.run();
}
char zeitdaten[18];
while (date.available() > 0) {
for (int i = 0; i < 18; i++) {
zeitdaten[i] = date.read();
}
}
hours = (zeitdaten[9] * 10 + zeitdaten[10] - 528);
minutes = (zeitdaten[12] * 10 + zeitdaten[13] - 528);
seconds = (zeitdaten[15] * 10 + zeitdaten[16] - 528);
dzien = (zeitdaten[3] * 10 + zeitdaten[4] - 528);
setTime(hours, minutes, seconds, dzien, 00, 00);
}
I see multiple issues here.
First, you create a new Process object upon entry to the function (because it's a local variable.) So why bother to check if it's running as the first step - you can be sure it's not.
Then, you start reading the result. You have a while characters available loop, and inside of that you have a loop that always reads 18 characters. If the Process returns exactly 18 characters, you are good. If it returns less than 18, the inner loop will block on the read() function once you run out of characters. If it returns more than 18 characters, the inner loop will complete, then the outer while loop will cycle again and the inner loop will try to read another 18 characters. This construct will tend to lock up if the return value is not an exact multiple of 18 characters.
Then, you go on to process the data. The code you have will work as long as the response is always exactly as you expect. What will happen if the above loop doesn't give you the data you are expecting? You will try to convert the wrong characters.
Everything seems to be predicated on the command returning exactly 18 characters. The command you are using will give 17 characters, and I assume a newline at the end. So it should normally work, but the code is currently not tolerant of anything being different than expected. Good code design should be tolerant of invalid input. It's entirely possible that sometimes the date command is giving you something different than expected, and that could cause problems with the current code. The hours/minutes/seconds/dzien values may be invalid -- what will setTime do with values that are out of range?
A safer way to handle this is to make your character array long enough to hold the entire response, plus a trailing NULL character. You are using the Process.run() function to run the command, which is a blocking function - it will not return until the process is complete. So at that time, you know how many characters have been returned by looking a Process.available(). I suggest that you read that value, shorten it if necessary to prevent running over your character buffer, and then call Process.readBytes() to read in all of the data. Then perform some sanity checks to make sure you have the data you expect.
An example sketch:
#include <Bridge.h>
#include <Process.h>
int counter;
void setup()
{
// Initialize the Bridge
Bridge.begin();
// Initialize the Serial port
Serial.begin(9600);
// Wait until a Serial Monitor is connected.
while (!Serial);
counter = 0;
}
void loop()
{
Process date;
Serial.print(++counter);
Serial.print(" ");
// Get the date from Linux
date.begin("date");
date.addParameter("+%D-%T");
date.run();
// Response should be of the form "MM/DD/YY-HH:MM:SS"
// Room for 17 character response plus a NULL terminator
char zeitdaten[18];
// Get the number of characters available,
byte len = date.available();
if (len == 18)
{
// Got the expected number of characters, 17 plus a newline.
// Read in the 17 characters, then NULL terminate the string.
date.readBytes(zeitdaten, 17);
zeitdaten[17] = '\0';
// Print the received string.
Serial.print(zeitdaten);
// Sanity checks to look for a well formed response
if ( (zeitdaten[2] == '/') &&
(zeitdaten[5] == '/') &&
(zeitdaten[8] == '-') &&
(zeitdaten[11] == ':') &&
(zeitdaten[14] == ':') )
{
// Extract the individual fields
byte hours = (zeitdaten[9] * 10 + zeitdaten[10] - 528);
byte minutes = (zeitdaten[12] * 10 + zeitdaten[13] - 528);
byte seconds = (zeitdaten[15] * 10 + zeitdaten[16] - 528);
byte dzien = (zeitdaten[3] * 10 + zeitdaten[4] - 528);
// Print the decoded values
Serial.print(" -- H:");
Serial.print(hours);
Serial.print(" M:");
Serial.print(minutes);
Serial.print(" S:");
Serial.print(seconds);
Serial.print(" D:");
Serial.println(dzien);
}
else
{
Serial.println(" Wrong format!");
// Error handling nere...
}
}
else
{
Serial.print("Process returned ");
Serial.print(len);
Serial.println(" characters!");
// Error handling here...
}
}
I let this run through a couple thousand loops, and got no errors. However, if the time function did return something strange, there are enough checks in there that it should be caught and not cause a problem.
The code you have works, if the input is exactly right. But good code should work properly and not crash even if the input is not what is expected. You need to qualify your input data and make sure it is really formatted the way you expect it to be.