I'm trying to print a page I made in html via Arduino ethernet.
I splitted this page in 15 files.
If I print every single one without changing data, the result is exactly what I wanted.
But I need to change values inside an input putting some values I have in Arduino's memory, so I tried doing this using a
String.replace("value=\"\"/>", "value=\"DataIWantToPutInHere\"/>")
every time a line is printed, but the result in the browser is totally different...
This is the code:
#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
const char* DATASTRING = "DATASTARTSHERE";
const char* HOURSETUP = "HOUR";
bool hour_selected = false;
byte mac[] = { 0x**, 0x**, 0x**, 0x**, 0x**, 0x** };
IPAddress ip(***, ***, *, **);
EthernetServer server(**);
char* vector[14];
int hours[28] = {12,0,12,2, 12,0,12,2, 12,0,12,2, 12,0,12,2,
12,0,12,2, 12,0,12,2, 12,0,12,2};
void setup() {
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
if(SD.begin(4)){
Serial.println("SD initialized succesfully.");
}else{
Serial.println("Error while initializing SD.");
while(1);
}
}
void loop() {
EthernetClient client = server.available();
if(client){
char c;
String http_message;
String message = "";
Serial.println("New client");
while(client.connected()){
if(client.available()){
c = client.read();
message += c;
if(c == '\n'){
int cmd = message.indexOf(DATASTRING);
Serial.println(message);
if(cmd > 0){
char* msg = message.c_str();
char actChar = msg[cmd + 14];
char* vals;
int count = 0;
String values = "";
char* temp;
while(actChar != 'D'){
values += actChar;
count++;
actChar = msg[cmd + 14 + count];
}
vals = values.c_str();
Serial.println(values);
Serial.println("Now I print random strtoks:");
count = 0;
for(temp = strtok(vals, ";"); temp != NULL; temp = strtok(NULL, ";")){
Serial.println(temp);
vector[count] = temp;
count++;
}
for(int i = 0; i < 14; i++){
temp = strtok(vector[i], ":");
hours[i*2] = atoi(temp);
Serial.println(hours[i*2]);
temp = strtok(NULL, ":");
hours[i*2 + 1] = atoi(temp);
Serial.println(hours[i*2 + 1]);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("I received data.");
client.println("</html>");
break;
}else{
char c;
String tab;
String str = "";
File file;
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
for(int i = 0; i < 15; i++){
if(i < 10){
tab = "tab";
tab += i;
}else{
tab = "tac";
tab += i -10;
}
tab += ".txt";
Serial.print("Trying to open ");
Serial.println(tab);
file = SD.open(tab, FILE_READ);
if(file){
Serial.println("Opened.");
}else{
Serial.println("Error opening file.");
}
str = "";
while(file.available()){
c = file.read();
if(c == '\n'){
// if(i < 14){
// String toRep = "value=\"";
// toRep += hours[i*2];
// toRep += ":";
// toRep += hours[i*2+1];
// toRep += "\"/>";
// str.replace("value=\"\"/>", "value=\"12:32\"/>");
// }
client.print(str);
str = "";
}else{
str += c;
}
}
file.close();
}
break;
}
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
}
}
The commented lines are the ones where I use String.replace()
Anyway, even putting String.replace("Value", "defaultValue") the page doesn't print all the , printing something like <input type="time" value=" which destroys all the page.