pptsk
August 7, 2015, 6:24am
1
Hi,
I am trying some example with ESP8266.Everything works well but I want save more SRAM and use PROGMEM function instead.
Can someone help how to do it with this piece of the code?
void serve_homepage(char ClientId) {
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n";
String content = "";
content += "<p>WLAN Setup</p>";
content += "<form method=GET>";
content += "<label>SSID</label>
";
content += "<input type='text'name='ssid'maxlength='30'size='15'>
";
content += "<label>Password</label>
";
content += "<input type='password'name='pass'maxlength='30'size='15'>
";
content += "<input type='submit'value='CONNECT'>";
content += "</form>";
header += "Content-Length:";
header += (int)(content.length());
header += "\r\n\r\n";
Serial.print(F("AT+CIPSEND="));
Serial.print(ClientId);
Serial.print(",");
Serial.println(header.length()+content.length());
if(get_response("> ",2000)) {
Serial.print(header);
Serial.print(content);
}
else {
Serial.print(F("AT+CIPCLOSE="));
Serial.println(ClientId);
}
}
I tried this but it does not works - page is not loaded.
const char *const content PROGMEM =
"<form method=GET>"
"<label>SSID</label>
"
"<input type='text'name='ssid'maxlength='30'size='15'>
"
"<label>Password</label>
"
"<input type='password'name='pass'maxlength='30'size='15'>
"
"<input type='submit'value='CONNECT'>"
"</form>";
//--------------------
void serve_homepage(char ClientId) {
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n";
header += "Content-Length:";
header += (int)strlen(content);
header += "\r\n\r\n";
Serial.print(F("AT+CIPSEND="));
Serial.print(ClientId);
Serial.print(F(","));
Serial.println(header.length()+strlen(content));
if(get_response("> ",2000)) {
Serial.print(header);
Serial.print(*content);
}
else {
Serial.print(F("AT+CIPCLOSE="));
Serial.println(ClientId);
}
}
Can somebody help?
ppt
guix
August 7, 2015, 6:35am
2
Hello and welcome,
Take a look at the progmem examples
Best advice is to rewrite your code without using the String Class and use char array's instead.
These can be easily stored in PROGMEM. - PROGMEM - Arduino Reference -
pptsk
August 7, 2015, 5:20pm
4
Hi Rob,
Can you please show on my code how to use char arrays?
I am beginner so I don't understand this technique so much...
It would be useful also for another users of this forum.
ppt
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
See also https://www.baldengineer.com/arduino-f-macro.html
pptsk
August 7, 2015, 7:49pm
6
Hi,
finally I got it working by this modification:
const char string_0[] PROGMEM = "<form method=GET>";
const char string_1[] PROGMEM = "<label>SSID</label>
";
const char string_2[] PROGMEM = "<input type='text'name='ssid'maxlength='30'size='15'>
";
const char string_3[] PROGMEM = "<label>Password</label>
";
const char string_4[] PROGMEM = "<input type='password'name='pass'maxlength='30'size='15'>
";
const char string_5[] PROGMEM = "<input type='submit'value='CONNECT'>";
const char string_6[] PROGMEM = "</form>";
const char* const content[] PROGMEM= {string_0 ,string_1 ,string_2 ,string_3 ,string_4 ,string_5 ,string_6 };
//*************************************************
void serve_homepage(char ClientId) {
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n";
header += "Content-Length:";
header += 232; // (sizeof(content)/sizeof(char *));
header += "\r\n\r\n";
Serial.print(F("AT+CIPSEND="));
Serial.print(ClientId);
Serial.print(F(","));
Serial.println(header.length()+232); //Serial.println(header.length()+strlen(content));
if(get_response("> ",2000)) {
Serial.print(header);
unsigned int count = 0;
for (int i = 0; i < 7; i++){
char * ptr = (char *) pgm_read_word (&content [i]);
char buffer [500]; // must be large enough!
strcpy_P (buffer, ptr);
Serial.print (buffer);
count += strlen (buffer) + 1;
}
}
else {
Serial.print(F("AT+CIPCLOSE="));
Serial.println(ClientId);
}
}
But another problem is how to calculate size of array 'content'.
for me works only this (gives 232):
Serial.println(strlen(content[0])+ strlen(content[1])+ strlen(content[2])+ strlen(content[3])+ strlen(content[4])+ strlen(content[5])+ strlen(content[6]));
but this one not works:(gives unrealistic numbers)
for (int i = 0;i < 7;i++){
cc = cc + strlen(content[i]);
}
Serial.println(cc);
can someone help why???my Arduino is latest 1.65.
thanks!
pptsk
August 7, 2015, 7:55pm
8
Hi,
can you more explain?
ppt
The variable content[] is an array of pointers to your string arrays. Because each pointer requires 2 bytes and you have 7 of them, the size of content[] is 14, which I don't think is what you want. Also, you're mixing Strings (i.e., in header ) with strings (content[] ). (Note that Strings with a capital 'S' is not the same as strings with a lower case 's'.)
pptsk
August 7, 2015, 8:24pm
11
can you please write some example for me?
I didn't found anything what I can understand on the web.
ppt