1

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

asked Mar 5, 2016 at 11:13
6
  • At for( int i = 0; i <= index; i++), I don't believe you've initialized clientline[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 set clientline[index] = 0円; (i.e. NUL-terminate the line), and replace the for-loop with Serial.print(clientline);, letting Serial.print() print the whole string for you. Commented Mar 5, 2016 at 14:31
  • Hi @JRobert, thanks for the advice. I've made those changes, but the serial output is still the same. Commented Mar 5, 2016 at 15:53
  • You're welcome. I wasn't convinced that was it, but now it's one less possibility! So now you know that coming out of the while loop, clientline contains two characters, one of which is some kind of whitespace, likely a space, and a '\n' (because the terminal line-feeds). You may need to be debugging the client at this point. Are you sure it is sending what you think it should be? Commented Mar 5, 2016 at 16:54
  • @JRobert I've checked communication on Wireshark and it looks OK. Every time the client refreshes the webpage it should send a message to the ESP8266 that should be picked up by client.read() I think the problem lies in getting the frame from the ESP8266 to the Arduino, as the system is successfully sending the webpage as requested. Commented Mar 5, 2016 at 18:59
  • It looks like I have the same problem. I used the "ESP8266_Shield_Demo_Sparkfun" and often the webpage shows not a proper output ("AT.." commands instead). How did you adapt the baud rate in the "SparkFunESP8266WiFi.h"? Thanks! Commented Jun 6, 2016 at 19:23

1 Answer 1

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.

answered Mar 5, 2016 at 23:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.