Data from current sensor reset in HTML page, using Ethernet Shield

I'm using an Ethernet Shield, a current sensor ACS712 and a relay module linked to a lamp to make some tests.

With the lamp on, the ACS712 module read a current of 0.32A and calculates the power consumption, send it to Arduino which communicates with the Ethernet Shield. This data is sent to a HTML page.

My problem is to make the program refresh the data in the web page in real time. To that, now, I'm using in the HTML code, a "Refresh: 0.1" command. However when some value of power is reached, the data reset. That also happens when I turn the lamp on or off.

If someone have some clue of what is happening, please, let me your comment. Anything will be welcome.

Thanks!

My problem is to make the program refresh the data in the web page in real time.

You can't.

If the Arduino is a client, it can update a server, where the browser can access the data, at whatever interval it is supposed to.

If the Arduino is a server, it can provide new data whenever the client asks for it.

In either case, you can't push new data to a client, since you have no idea what clients have ever asked for data.

Thank you for your reply PaulS. That problem I think I can solve with something like a refresh button in the browser for the user, for example.
Anyway, I yet have the problem of the reset of my data. I thought that this could be some memory problem but, even when my load (the lamp) is turned on or off, the reset occurs.
Below, there is the code (there are some comments in portuguese because that is my language):

#include <SPI.h>
// Biblioteca utilizada para comunicação com o Arduino
#include <Ethernet.h>

// A linha abaixo permite definir o endereço físico (MAC ADDRESS) da...
//placa de rede.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip[] = { 192, 168, 1, 178 }; //Define o endereço IP.

// Porta onde estará aberta para comunicação Internet e Arduino.
EthernetServer server(80);

String readString;
const int sensorPin = A0;
float sensorValue_aux = 0;
float sensorValue = 0;
float currentValue = 0;
float voltsporUnidade = 0.0048828125;
double potencia = 0;
double consumo;
double preco;
unsigned long tempo, tempo1, tempoT = millis();
int Pin = 9; // Pino digital onde será ligado e desligado o LED.

void setup(){

pinMode(Pin, OUTPUT); // Define o Pino 9 como saída.
Ethernet.begin(mac, ip); // Chama o MAC e o endereço IP da placa Ethernet.
server.begin(); // Inicia o servidor que esta inserido junto a placa Ethernet.
}

void loop(){

EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

if (readString.length() < 100) {
readString += c;
}

if (c == '\n') {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Refresh: 0.1");
client.println();

// A partir daqui começa os códigos html.

client.println("");
client.println("");
client.println("

Acende Lampada

");
client.println("

Acendendo lampada com Shield Ethernet e rele

");
client.println("
");
client.println("
");

client.println
("<a href="/facacomarduino/LampadaOn"">Acender lampada");
client.println
("<a href="/facacomarduino/LampadaOff"">Apagar lampada
");

client.println("");
client.println("");

if(readString.indexOf("facacomarduino/LampadaOn") > 0)
{
digitalWrite(Pin, HIGH); // Liga Lampada.

}
else {
if(readString.indexOf("facacomarduino/LampadaOff") > 0)
{
digitalWrite(Pin, LOW); // Desliga Lampada.

}
}

for(int i=500; i>0; i--)
{
sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é vcc/2 para corrente =0
sensorValue += pow(sensorValue_aux,2); // soma os quadardos das leituras no laco
}

sensorValue = (sqrt(sensorValue/ 500)) * voltsporUnidade; // finaliza o calculo da médiaa quadratica e ajusta o valor lido para volts
currentValue = (sensorValue/66)*1000; // calcula a corrente considerando a sensibilidade do sensor (66 mV por amper)

tempo1 = tempo;
tempo = millis();
tempoT = tempo - tempo1;

consumo = consumo + (potencia/1000);

potencia = currentValue * 110;

preco = consumo * 0.35;

client.println("
");
client.println("corrente : ");
client.println(currentValue);
client.println(" A");
client.println("");
client.println("
");
client.println("
");

client.println("potencia : ");
client.println(potencia);
client.println(" W");
client.println("");
client.println("
");
client.println("
");
client.println("Consumo : ");
client.println(consumo);
client.println(" KWh");
client.println("");
client.println("
");
client.println("
");
client.println("Valor a pagar : ");
client.println(preco);
client.println(" (R$)");
client.println("");

readString="";
delay(1);

client.stop();
}
}
}
}
}

if (readString.length() < 100) {
          readString += c;

If you KNOW the maximum length that the String should be, you don't really need a String, do you? Quit pissing away resources using the String class.

client.println("HTTP/1.1 200 OK");

All those string literals end up in SRAM. Stop that from happening:
client.println(F("HTTP/1.1 200 OK"));

 sensorValue += pow(sensorValue_aux,2); // soma os quadardos das leituras no laco

pow() is a VERY expensive way to get sensorValue_aux * sensorValue_aux.

Squaring all the values when you then take the square root hardly seems useful.

I would think that you would want to divide by 500 AFTER taking the square root, not before.

 consumo = consumo + (potencia/1000);
 
 potencia = currentValue * 110;

Use the value of potencia and then assign it a value. Hmmm...

I thought that this could be some memory problem but, even when my load (the lamp) is turned on or off, the reset occurs.

That the lamp is turned off or on has nothing to do with how much memory you are using (or wasting).

Thank you PaulS for the tips about memory consumption. I'll make some of the changes you suggested.

I dicovered how to solve the problem of the reset.

Regards