Go Down

Topic: Problem writing a string to a client (Read 196 times) previous topic - next topic

TimEllis

I am using a Wifi Rev2 with the wifinina.h library.  I have the code working for a server with web page info going both directions.  When I wanted to make my web page larger I found using strings I would run out of RAM.  So I declared my web page to be

const char PROGMEM StyleHTML[] ="<!DOCTYPE html> <html> <head>... long string";

If I use client.println(StyleHTML);  It compiles with out error but does not print the char array to the client.  Do I have to print one character at a time from the array?  Or is there another way to declare a array that will work with client.println.

If StyleHTML is declared to be a String the code works.

Code snippet below.

const char PROGMEM StyleHTML[] ="<!DOCTYPE html> <html> <head>... long string";

 WiFiClient client = server.available();
  // wait for a client (web browser) to connect
  if (client)
  {
    Serial.println("\n[Client connected]");
    while (client.connected())
    {
      // read line by line what the client (web browser) is requesting
      if (client.available())
      {
        line = client.readStringUntil('\r');
// wait for end of client's request, that is marked with an empty line
        if (line.length() == 1 && line[0] == '\n')
        {
              client.println(StyleHTML);

Juraj

#1
Apr 24, 2019, 06:46 am Last Edit: Apr 24, 2019, 06:49 am by Juraj
print(const char[]) doesn't know that your const char[] is in PROGMEM

use the PROGMEM version of print() by casting the parameter to __FlashStringHelper used by F() macro

Code: [Select]
client.println((__FlashStringHelper*) StyleHTML);
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

TimEllis

Thanks for the reply.

Where is __FlashStringHelper used by F() macro documented?
-tim

Juraj

#3
Apr 24, 2019, 05:38 pm Last Edit: Apr 24, 2019, 05:39 pm by Juraj
Thanks for the reply.

Where is __FlashStringHelper used by F() macro documented?
-tim
nowhere. it was meant to be a hidden helper. but in case where you don't want to use F() macro but a global PROGMEM string with Print class progmem functions, it is useful to know.

F() macro doc
https://www.arduino.cc/reference/en/language/variables/utilities/progmem/#_the_code_f_code_macro
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up