I'm trying to make a web server using an Arduino R3 and Sparkfun ESP8266 Wifi Shield. I've built a webpage that has a form I would like to control the Arduino with.
Currently my sketch can successfully send the webpage, but when I use the form, 9 / 10 times I see no output on the serial monitor, and the 1/10 times I see the frame from the client the sketch seems to hang and the client keeps waiting for more data.
I'm using the Sparkfun ESP8266 libraries for the product I bought.
I don't think this is a hardware problem, I've visually inspected the Arduino and ESP pins and they seem ok. I don't think it's a memory problem.
I've put the salient part of the sketch below:
void serverDemo()
{
// available() is an ESP8266Server function which will
// return an ESP8266Client object for printing and reading.
// available() has one parameter -- a timeout value. This
// is the number of milliseconds the function waits,
// checking for a connection.
ESP8266Client client = server.available(1000);
char clientline[BUFSIZ];
int index = 0;
if (client)
{
Serial.println(F("Client Connected!"));
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
if( index < BUFSIZ ) {
clientline[index++] = c;
} // end of if
if (c == '\n' && currentLineIsBlank)
{
clientPrintProgmemArray(htmlHeaderTable, htmlHeaderTableSize, client);
clientPrintProgmemArray(webpageTable, webpageTableSize, client);
Serial.println(F("Page sent"));
break;
} // end of if
if (c == '\n')
{
currentLineIsBlank = true;
} // end of if
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
} // end of if
} // end of if( client.available() )
} // end of while ( client.connected() )
delay(10);
client.stop();
Serial.println(F("Client disconnected"));
Serial.print(F("Index: "));
Serial.println(index);
Serial.print(F("Clientline: \""));
for( int i = 0; i <= index; i++)
{
Serial.print(clientline[i]);
}
Serial.print(F("\"\r\n"));
if( strstr(clientline, "e=1") != 0) {
Serial.println("hooray!");
}
}
}
The header and webpage the Arduino sends to the client is:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
<!DOCTYPE HTML>
<html>
<body>
<form action="/?" method="get">
<input name="e" type="submit" value="0">
<input name="e" type="submit" value="1">
</form>
</body>
</html>
The output I get on the serial monitor is:
ESP8266 Shield Present
Mode set to station
Connected to: XXXXX
My IP: 192.168.0.13
Server started! Go to 192.168.0.13
Client Connected!
Page sent
Client disconnected
Index: 2
Clientline: "
"
Client Connected!
Page sent
Client disconnected
Index: 2
Clientline: "
"
I'd be very grateful if anybody has any advice on where the problem lies.
Thanks in advance
1 Answer 1
So I figured out the answer to my own question.
It turns out there must be a mistake in the example sketches that come with the ESP8266 shield I bought, as the function to begin Arduino/ESP8266 communications didn't have any arguments when the function expected parameters. So it appeared to be trying to set their baud rate to 0!!
I edited the library to have a hard value of 57600 and now it seems to work far more reliably.
for( int i = 0; i <= index; i++)
, I don't believe you've initializedclientline[index]
anywhere, so the final iteration will print an undefined character. I don't know if this is the error, but I think it is an error. You could instead setclientline[index] = 0円;
(i.e. NUL-terminate the line), and replace the for-loop withSerial.print(clientline);
, letting Serial.print() print the whole string for you.