Hello arduino world, i have a little problem here. I have a webserver running on arduino mega with SD module and esp8266. My webpage contains a lot of html css and JavaScript, so it is ~12kB in size. What am i doing is this:
I open a file, scan for special chars in file, replace them with values, write all bytes to String and then send the string to Web.
It all works good, but i have 6 files on SD and i am sending and opening them one by one, which is slow
and not ideal. And i am planing to add new features to web, so it Will have maybe 18kB total. Thats why i am asking here. I want to scan and write values to whole body of web in one time (6-8kB) and then send them thru esp8266 which accepts max 2kb. So it is possible to Split string every, say 2000 bytes?
Yes.
How do you keep your super long string in memory?
If you feel that you really must use Strings then what about the substring() function of the String class ?
Well if you have any other idea how to scan for FileSeparator in file and replace it, i'd appreciate if you share it i am aware of that function, but i Dont know how it works, or how do i Use it
i read the reference on this page, but it doesnt help much tho.
i read the reference on this page, but it doesnt help much tho.
Nor does your saying "this page" when we have no idea what page you are referring to.
Nor does you complete failure to post ANY code.
You can NOT have a 2000+ character string on most Arduinos, so you need to also tell us which Arduino you have.
Sounds like you've picked an optimally inefficient way to do your processing. Read from the file into a SMALL buffer, parsing as you go. If you find one of your special characters that tells you yo do a substitution, append it to what's in the buffer, then send the buffer. Or, if the buffer gets close to full, send it. That way you avoid needing any large buffers.
I do basically on-the-fly substitutions in my web server, using a lookup table, and function pointers, so variables are substituted entirely on-the-fly as the HTML (or whatever) is being sent to the client. That makes the parser dead simple, and it doesn't have to be changed at all when new variables are added or removed.
Regards,
Ray L.
Would programming the ESP in Arduino ide be easier?
the ESP8266 contains 4 Megs of flash. Can be programmed in C.
You can Forget the AT commands.
Check this out:
He even includes jquery as part of the esp8266 generated webpage!
#include <SD.h>
#include <SPI.h>
#include "RTClib\RTClib.h"
#include <Wire.h>
#include "Timer-master\Timer.h"
#define DEBUG true
bool led;
String ledvalue;
String webpage;
char buffer;
File body;
char FS = 0x1C;
char US = 0x1f;
String buf;
int href;
String gettime;
Timer t;
RTC_DS3231 rtc;
int YY;
byte MO, DD, HH, MM;
#define esp8266 Serial1
byte CWonH = 2;
byte CWonM = 15;
byte CWoffH = 21;
byte CWoffM = 20;
byte CWfade = 60;
byte cwmax = 255;
byte WWonH = 2;
byte WWonM = 15;
byte WWoffH = 21;
byte WWoffM = 20;
byte WWfade = 60;
byte wwmax = 255;
byte RonH = 2;
byte RonM = 15;
byte RoffH = 21;
byte RoffM = 20;
byte Rfade = 60;
byte rmax = 255;
byte GonH = 2;
byte GonM = 15;
byte GoffH = 21;
byte GoffM = 20;
byte Gfade = 60;
byte Gmax = 255;
byte BonH = 2;
byte BonM = 15;
byte BoffH = 21;
byte BoffM = 20;
byte Bfade = 60;
byte bmax = 255;
String bufstring;
int connectionId;
String closeCommand;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
if (SD.begin(53)) {
Serial.println(F("sd ok"));
}
sendData(F("AT+RST\r\n"), 2000, DEBUG); // reset module
sendData(F("AT+CWMODE=3\r\n"), 1000, DEBUG); // configure as access point
sendData(F("AT+CWJAP=\"ssid\",\"pass\""), 5000, DEBUG);
sendData(F("AT+CIPSTATUS\r\n"), 1000, DEBUG); // get ip address
sendData(F("AT+CIPMUX=1\r\n"), 1000, DEBUG); // configure for multiple connections
sendData(F("AT+CIPSERVER=1,80\r\n"), 1000, DEBUG); // turn on server on port 80
}
void loop()
{
t.update();
if (esp8266.available()) // check if the esp is sending a message
{
delay(500);
connectionId = 0;
if (esp8266.find("+IPD,"))
{
if (esp8266.find("?")) {
Serial.println("true");
if (esp8266.peek() == 'c') {
Serial.println("c");
CWonH = esp8266.parseInt();
CWonM = esp8266.parseInt();
CWoffH = esp8266.parseInt();
CWoffM = esp8266.parseInt();
CWfade = esp8266.parseInt();
esp8266.readStringUntil('=');
Serial.print(CWonH);
sendData("AT+CIPSEND=0,0 \r\n", 2000, DEBUG);
close();
} else if (esp8266.peek() == 'w') {
Serial.println("w");
WWonH = esp8266.parseInt();
WWonM = esp8266.parseInt();
WWoffH = esp8266.parseInt();
WWoffM = esp8266.parseInt();
WWfade = esp8266.parseInt();
esp8266.readStringUntil('=');
Serial.print(WWonH);
sendData("AT+CIPSEND=0,0 \r\n", 2000, DEBUG);
close();
}
else if (esp8266.peek() == 'r') {
Serial.println("r");
RonH = esp8266.parseInt();
RonM = esp8266.parseInt();
RoffH = esp8266.parseInt();
RoffM = esp8266.parseInt();
Rfade = esp8266.parseInt();
esp8266.readStringUntil('r');
Serial.print(RonH);
sendData("AT+CIPSEND=0,0 \r\n", 2000, DEBUG);
close();
}//rchannel//R
else if (esp8266.peek() == 'g') {
Serial.println("g");
GonH = esp8266.parseInt();
GonM = esp8266.parseInt();
GoffH = esp8266.parseInt();
GoffM = esp8266.parseInt();
Gfade = esp8266.parseInt();
esp8266.readStringUntil('g');
Serial.print(GonH);
sendData("AT+CIPSEND=0,0 \r\n", 2000, DEBUG);
close();
}//G
else if (esp8266.peek() == 'b') {
Serial.println("b");
BonH = esp8266.parseInt();
BonM = esp8266.parseInt();
BoffH = esp8266.parseInt();
BoffM = esp8266.parseInt();
Bfade = esp8266.parseInt();
esp8266.readStringUntil('=');
Serial.print(BonH);
sendData("AT+CIPSEND=0,0 \r\n", 2000, DEBUG);
close();
}//B
else if (esp8266.peek() == '=') {
Serial.println("=");
esp8266.readStringUntil('=');
href = esp8266.parseInt();
Serial.println(href);
switch (href) {
case 10:
cwmax = esp8266.parseInt();
digitalWrite(13, HIGH);
break;
case 11:
digitalWrite(13, LOW);
break;
}
close();
} //t.after(200, sendserver);
}
else {
sendserver();
}
}
}
}
void sendserver() {
File css1 = SD.open("css.htm");
if (css1) {
while (css1.available()) {
webpage = css1.readString();
css1.close();
}
}
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 100, DEBUG);
/**************************************/
File css2 = SD.open("css2.htm");
if (css2) {
while (css2.available()) {
webpage = css2.readString();
css2.close();
}
}
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 100, DEBUG);
/**************************************/
/**************************************/
File body = SD.open("body.htm");
if (body) {
while (body.available()) {
webpage = body.readStringUntil(FS);
webpage += cwmax;
webpage += body.readStringUntil(FS);
webpage += cwmax;
webpage += body.readStringUntil(FS);
webpage += CWonH;
webpage += body.readStringUntil(FS);
webpage += CWonM;
webpage += body.readStringUntil(FS);
webpage += CWoffH;
webpage += body.readStringUntil(FS);
webpage += CWoffM;
webpage += body.readString();
webpage += "\r\n";
body.close();
}
}
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 500, DEBUG);
/**********************************************/
File body2 = SD.open("body2.htm");
if (body2) {
while (body2.available()) {
/*webpage = body.readStringUntil(FS);
webpage += F("ON");
webpage += body.readStringUntil(FS);
webpage += F("OFF");
webpage += body.readStringUntil(FS);
webpage += F("ON");
webpage += body.readStringUntil(FS);
webpage += F("OFF");
webpage += body.readStringUntil(FS);
webpage += F("ON");
webpage += body.readStringUntil(FS);
webpage += F("OFF");
webpage += body.readStringUntil(FS);
webpage += F("ON");
webpage += body.readStringUntil(FS);
webpage += F("OFF");
webpage += body.readStringUntil(FS);
webpage += F("ON");
webpage += body.readStringUntil(FS);
webpage += F("OFF");*/
webpage = body2.readString();
webpage += "\r\n";
body2.close();
}
}
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 300, DEBUG);
/**************************************************/
File script = SD.open("script.htm");
if (script) {
while (script.available()) {
webpage = script.readString();
webpage += "\r\n";
body.close();
}
}
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 100, DEBUG);
close();
}
void close() {
closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId; // append connection id
closeCommand += "\r\n";
sendData(closeCommand, 2000, DEBUG);
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while ((time + timeout) > millis())
{
while (esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}
Thats the code. I am running it on arduino Mega, i stated that in first post. The substring function ref:https://www.arduino.cc/en/Tutorial/StringSubstring
I read that, but i Dont know how can it help me in my case. Ray: it is better to store the data in array than in String?
zhomeslice:
Would programming the ESP in Arduino ide be easier?
the ESP8266 contains 4 Megs of flash. Can be programmed in C.
You can Forget the AT commands.
Check this out:GitHub - jasoncoon/esp8266-fastled-webserver
He even includes jquery as part of the esp8266 generated webpage!
Well i need only webserver as a gui for arduino, so i think it wont work in that way
wiipro:
Well i need only webserver as a gui for arduino, so i think it wont work in that way
So I am doing something similar as you but I have the web interface part working on the ESP8266 and I have the arduino code working my goal is to transfer the resulting information between the arduino and the ESP8266 using ISP interface rather than serial. I am working on my resulting code over the next few days you can see my success here I will post it as it is completed. Transfer Structures of data Between UNO and ESP8266 using SPI.
If you decide to head in this way I will try to assist
Z
I'll keep that in mind, thank you. But i have already a pcb with sd module and esp8266 on it