Willard & Mik, thanks for your replies.
Mik, I'm still having some other problems working with GoBetWino, and I hope you can help me figure out whats wrong - perhaps there's something that I'm just not seeing.
Sometimes, when I use GoBet to run the code below, it does not execute the last print statement (in the getData function). And if I were to restart the arduino, it would then show the result of that statement and re-run the code. But when it re-runs the (after the restart), some errors occur: it doesn't print some lines and even if goBetWino reads the lines of the file correctly (it shows up in the output box), the readData lines in the code return an empty string or the value of a previous string . What could be wrong?
int serInLen=25;
char serInString[25], moreThanLatString[9];
char code[25], lonString[8], latString[8];
char t1String[7], t2String[5];
long timeT, timeT1, timeT2;
int pId =0;
int result;
void setup() // run once, when the sketch starts
{
Serial.begin(19200);
getData();
}
void getData(){
delay(3000);
char buffer[15];
//wait till I start GobetWino
delay(3000);
Serial.flush();
Serial.println("#S|RDCODE|[1]#");
readData(code, 5000, 7);
Serial.println("#S|INTERNET|[]#"); // start IE
readSerialString(serInString, 5000); // wait 5 seconds (max) for answer from Gobetwino (=porcess ID)
pId= atoi(serInString); // convert result to integer
//Wait for IE to start then enter a url, and go there
delay(5000);
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("&");
Serial.print("http://url.com/sunfollower/products.php?code=");
Serial.print(code);
Serial.print(" {ENTER} ");
Serial.println("]#");
//wait till file is created then tell GoBetWino to download the file
delay(15000);
Serial.println("#S|DLDATA|[]#");
//Close the IE window
Serial.flush();
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("&");
Serial.print("^w");
Serial.println("]#");
Serial.flush();
delay(5000);
//GoBetWino reads lines of data from the file and attempts to store them in strings
Serial.flush();
Serial.print("#S|RDDATA|[");
Serial.print(itoa((1), buffer, 10));
Serial.println("]#");
delay(100);
readData(moreThanLatString, 10000, 9);
//to remove '00' which always comes before
//the actual latString for some reason I don't know
for (int i = 0; i < 7; i++) {
latString[i] = moreThanLatString[i];
}
//readData(latString, 10000, 7);
Serial.println(latString);
double lat = atof(latString);
Serial.print("Lat = ");
Serial.println(lat);
Serial.flush();
Serial.print("#S|RDDATA|[");
Serial.print(itoa((2), buffer, 10));
Serial.println("]#");
delay(100);
readData(lonString, 10000, 7);
delay(100);
readData(lonString, 10000, 7);
double lon = atof(lonString);
Serial.print("Lon = ");
Serial.println(lon);
Serial.flush();
Serial.print("#S|RDDATA|[");
Serial.print(itoa((3), buffer, 10));
Serial.println("]#");
delay(100);
readData(t1String, 10000, 6);
Serial.flush();
Serial.print("#S|RDDATA|[");
Serial.print(itoa((4), buffer, 10));
Serial.println("]#");
delay(100);
readData(t2String, 10000, 4);
Serial.print("Time_T = ");
Serial.println(t1String);
Serial.println(t2String);
timeT1 = (atol(t1String))*10000;
timeT2 = atol(t2String);
timeT = timeT1 + timeT2;
Serial.print("Time_T = ");
Serial.println(timeT);
}
void loop()
{
}
void readSerialString (char *strArray,long timeOut) {
// Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
long startTime=millis();
int i = 0;
while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray[i] = Serial.read();
i++;
}
}
void readData (char *strArray,long timeOut, int length) {
// Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
long startTime=millis();
int i;
while (!Serial.available() || Serial.available() < length) {
if (millis()-startTime >= timeOut) {
return;
}
}
if (Serial.available() > (length-1)) {
for (i = 0; i < length; i++) {
/* tried this so that non-zero values are read but it seems to cause an infinite loop
while (i = 0 && Serial.read() == '0')
;
*/
strArray[i] = Serial.read();
}
}
}
By the way, the many flushes and some of the delays were just so that the serial.read would work right.
Thanks.