Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By mconsidine
#58056 Hi,
I've got a feeling I'm overlooking something very simple, but can't put my finger on it. Specifically, I am trying to take the HelloServer example and modify it to use the sendContent_P function. The error I am getting during compilation is
Code: Select allno matching function for call to 'ESP8266WebServer::sendContent_P(String&, unsigned int)'

The actual routine I'm trying to get to work is this one:
Code: Select allvoid handleRoot() {
  digitalWrite(led, 1);
  //server.send(200, "text/plain", "hello from esp8266!");
  String webtext = "<h1>";
  webtext += "This would be the main homepage";
  webtext += "</h1>";
 
  server.setContentLength(strlen_P(webtext) );
  server.send(200, "text/html", "" );
  server.sendContent_P( webtext, sizeof(webtext));
  digitalWrite(led, 0);
}


I understand that this is an overly simple example, but I am trying to build up to send an image that is stored in base64. So I read the forum about someone trying to send 4K worth of data and thought this would be straightforward.

Can someone point me to what I am doing wrong? The headers included
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

In other words, the only change I am making to the example is the routine shown above.

Thanks in advance for any help - and for your patience.
mconsidine
User avatar
By martinayotte
#58072 server.send_P() and server.sendContent_P() are strickly for PROGMEM const variables in Flash, not for Strings.
For Strings, simply remove the _P and get it converted in char * using str.c_str() while getting the length with str.length().
Code: Select allserver.sendContent( webtext.c_str(), webtext.length() );
User avatar
By mconsidine
#58099 Ah! Ok, that probably would have taken me awhile to figure out. Thank you very much for the clarification and the example.

EDIT: Code needed a little tweak. Here's what actually worked:
Code: Select allvoid handleRoot() {
  digitalWrite(led, 1);
  //server.send(200, "text/plain", "hello from esp8266!");
  String webtext = "<h1>";
  webtext += "This would be the main homepage";
  webtext += "</h1>";
 
  server.setContentLength(webtext.length() + strlen_P(logo));
  server.send(200, "text/html", "" );
  server.sendContent( webtext.c_str() );
  server.sendContent_P( logo, sizeof(logo) ); 
  digitalWrite(led, 0);
}


"logo" is a JPEG image in Base64, converted at this website https://www.base64-image.de/

Regards,
mconsidine