-->
Page 1 of 4

client.print() / HTML problems

PostPosted: Sun Jan 17, 2016 12:33 pm
by xtal
My current sketch is working , but occasionally client stops communicating...
I am using similar Header,page,footer pages..that my NodeMCU [working fine] is using...
I noticed the header did not contain then following "HTTP/1.1 200 OK\r\n";
If I add this to the header I get no display.....
code is below... I was moving code to new sketch for diagnosing and encounter a new wrinkle...
and I cannot get multiple client.print(X); to work. with or without "HTTP/1.1 200 OK\r\n";
The 1st client.print shows nothing, the 2nd client.print - displays the code on the page ..
I have no clue what is happening.... This symptom is very similar to the problem in nodemcu
dealing with call backs .......I'm apparently doing something wrong but don't know what....

Code: Select allString Header()  { 
  String h1 = ""; //"HTTP/1.1 200 OK\r\n";
  h1 += "<!DOCTYPE HTML>\n";
  h1 += "<html lang='en-US'>\n";
  h1 += "<head><meta http-equiv='refresh' content=";  h1 += RFH; h1 += "></head>\n";       // Page refresh Time     
  h1 += "<body>\n";   
  return h1;     }   
/*--------------------------- This works if I leave out   "HTTP/1.1 200 OK\r\n";---------------*/

  rfh = RFH;                 //  init refresh ctr   
  String r = Header();
  r += HDR1(); 
  r += JavaScript();
  r += Page();              // 14xx
  LH = r.length();   
  LB = 0; 
Serial.println(r); 
client.print(r); 
  r = Buttons();
  LB = r.length();
client.print(r);
  r = Txt();
  r += "Baud: "; r += baud; r += " | "; r +=  LH; r += " | ";
  r += LP; r += " | "; r += LB; r += " | ";         
  LT = r.length()+22;
  r += LT;
  r += "</body>\n"; r +=  "</html>\n";   
client.print(r);                                   //  send the response to the client
client.stop();       

Re: client.print() / HTML problems

PostPosted: Sun Jan 17, 2016 2:08 pm
by martinayotte
Your header function Header() has incomplete header, you need Content-Type, and at the end of header an empty line \r\n :

Code: Select all  String h1 = "HTTP/1.1 200 OK\r\n";
  h1 += "Content-Type: text/html\r\n";
  h1 += "\r\n";
  h1 += "<!DOCTYPE HTML>\n";
  h1 += "<html lang='en-US'>\n";
  h1 += "<head><meta http-equiv='refresh' content=";  h1 += RFH; h1 += "></head>\n";       // Page refresh Time     
  h1 += "<body>\n";   


But why are you not using ESP8266WebServer ? You won't end up reinventing the wheel ... ;)

Re: client.print() / HTML problems

PostPosted: Sun Jan 17, 2016 3:46 pm
by xtal
But why are you not using ESP8266WebServer ? You won't end up reinventing the wheel ... ;)


Thanks Martin, looks like that solved both problems, but created another
I'm no longer able to control refresh ???? any ideas

I didn't know ESP8266WebServer existed,, is it in core , or do I have to include a library?
Why is it better than ESP8266WiFi.h ?
Is RSSI stuff better?
Is there any way to send my serial data to the client other than wait refresh....

Re: client.print() / HTML problems

PostPosted: Sun Jan 17, 2016 4:40 pm
by martinayotte
Yes, the ESP8266WebServer is in the core, but maybe it will be renamed to something like ESPLiteWebServer since the is a better one that will come soon from @me-no-dev.
It is not better, it is a HTTP Server implemented on top of ESP8266Wifi, which is a plain TCP Server, not HTTP.
So, when I've said to you can avoid reinventing the wheel, it is simply that WebServer as much more functionalities that you don't need to write yourself, like those header that you tried, would be simply done with webserver.send(200, "text/html", thebody); and sending an image done with webserver.send(200, "image/jpeg", led_icon);
About RSSI, it still be provided by ESP8266Wifi, both can co-exist.
About Serial/Client, I'm not understanding your question : do you mean a web page that refresh itself to display character coming in from serial ?
Yes, it is possible, using Javascript doing some AJAX call to the WebServer and display new incoming characters and add them to text field, for example.