Refresh a HTTP page on Arduino+Wifi Shield

Hi, I’m really new on arduino, and i need some help. I’m reading the value of 3 sensors with an Arduino UNO + Wi-Fi shield, and putting the values on an HTTP server. The MATLAB pick these values and does a PID control. But in my code, the page changes only when i press refresh /F5 (the MATLAB is responsible for choosing the values and updating the server via read an write function). To improve my system for a faster response, I want to refresh the page every 1 second (and see the values as live as possible), reducing the code and “stress” in MATLAB. So what do I need to do / change so to the HTTP page automatically refresh the page? I have no knowledge on meta refresh, and really new on C programming. Here is my code:

/*
                                    Coupled Tanks Control

This code reads the data of a pressure sensor, converts to centimeters e sends via Wi-Fi. Also, receives a voltage valor between 0-255, responsible to feed the water pump.

 */

#include <SPI.h>
#include <WiFi.h>
#include <TextFinder.h> //Search some character
int yr= 10; // Setpoint of the tank
int val=0;
int bomba =9; //pin of water pump voltage
int EntradaAnalogica1 =0;
int EntradaAnalogica2 =0;
int EntradaAnalogica3 =0;
float pow0;
float pow1;
float pow2;
float pow3;
float pow4;
float pow5;
float NivelTanque1=0.0;
float NivelTanque2=0.0;
double NivelTanque3=0.0;
char ssid[] = "LabAuto";      
char pass[] = "LabAuto123";   

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  Serial.begin(9600); 
  while (!Serial) {
    ; 
  }
  
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("Modulo Wifi OFF"); 
    while(true);
  } 
  
  // Starts Connection
  while ( status != WL_CONNECTED) { 
    Serial.print("Iniciando conexao com a rede SSID: ");
    Serial.println(ssid);    
    status = WiFi.begin(ssid, pass);

    // Wait 10 secs:
    delay(10000);
  } 
  server.begin();
  printWifiStatus();

}


void loop() {

  while (Serial.available() > 0)  {


    int referencia = Serial.parseInt();

    if ((referencia >= 0 ) && (referencia <30 )) {
     yr = referencia;
     Serial.println(yr); // Recebe a nova referêcia (setpoint)
    }

  }

  // Sensors Reading
  EntradaAnalogica1= analogRead(A1); //sensor 1
  EntradaAnalogica2= analogRead(A4); //sensor 2
  EntradaAnalogica3= analogRead(A5); //sensor 3
  NivelTanque1 = EntradaAnalogica1;
  NivelTanque1 = ((NivelTanque1*5)/1023);
  NivelTanque1 = NivelTanque1*6;
  NivelTanque2 = EntradaAnalogica2;
  NivelTanque2 = ((NivelTanque2*5)/1023);
  NivelTanque2 = NivelTanque2*6;
  //Polinominal Linearization of Sensor 3
    pow5=pow(EntradaAnalogica3,5);
    pow4=pow(EntradaAnalogica3,4);
    pow3=pow(EntradaAnalogica3,3);
    pow2=pow(EntradaAnalogica3,2);
    pow1=pow(EntradaAnalogica3,1);
    pow0=-8.640623008953932;
  NivelTanque3 = ((0.000000000094108)*pow5)+((-0.000000070886966)*pow4)+((0.000020462289992)*pow3)+((-0.002783032703534)*pow2)+((0.289110819198763)*pow1)+pow0;

  Serial.print(EntradaAnalogica1);
  // Client List
  WiFiClient client = server.available();
  if (client) {
    TextFinder  finder(client ); 
    while (client.connected()) {
      if (client.available()) {
        if(finder.find("GET /")) {
// Will search de word voltage after ?=:
           while(finder.findUntil("tensao", "\n\r")) { 
       
             val = finder.getValue();

           } 
        }

        //Give the Response to HTTP
        printWebResponse(client);
        break;
      }
      
    }
    // Time to receive data
    delay(1);
      // Finish Connection
     client.stop();
     //Serial.println("Cliente desconectado");

  }
// Voltage Limits
 if(val<0){
  val=0;
 }
 if(val>255){
  val=255;
 }

 if(val>0){
              
              Serial.print("Bomba ligada! Valor: ");
             Serial.println(val);
             analogWrite(bomba, val);             
  
 }else{
  Serial.println("Bomba desligada!");
      analogWrite(bomba, 0);
       
 }

  
}


void printWebResponse(WiFiClient client) {
  Serial.println();
  // The HTTP finish the response
  // Send the response with this head

  client.println("HTTP/1.1 200 OK");
  client.println("Access-Control-Allow-Origin: *");
           client.println("Content-Type: text/html");
 

           client.println();
            client.print("<form action=\"/?\" method=get>");
            //client.print("<div align=\"center\">");
            client.print("<b>Nivel_1: </b> "); // Tank level (cm) on sensor 1
            client.print(NivelTanque1);
            client.print("<b>Nivel_2: </b> "); //Tank level (cm) on sensor 2
            client.print(NivelTanque2);
            client.print("<b>Nivel_3: </b> "); // Tank level (cm) on sensor 3
            client.print(NivelTanque3);
            client.print("<b>yr: </b> "); // Setpoint
            client.print(yr);
            client.println();

}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Thanks!

try with

client.println(F("Refresh: [color=red]5[/color]"));  // refresh the page automatically every [color=red]5[/color] sec

refreshing every second might be somewhat aggressive… if your browser is up to the latest standards, that instructs the browser to trigger the URL again every 5 seconds

Test with something like this for your printWebResponse function (note that I added a & for the parameter, to pass the client as reference). I just typed that in and did not test so fine tune as you see fit

void printWebResponse(WiFiClient &client) {
{
    // send a standard http response header
  client.println(F("HTTP/1.1 200 OK"));
  client.println("Access-Control-Allow-Origin: *");
  client.println(F("Content-Type: text/html"));
  client.println(F("Connection: close"));  // the connection will be closed after completion of the response
  client.println(F("Refresh: 5"));  // refresh the page automatically every 5 sec
  client.println();
  client.println(F("<!DOCTYPE HTML>"));

  // ****************************************************************
  // THIS IS WHERE YOU BUILD YOUR HTML PAGE
  // ****************************************************************

  client.println(F("<html><head><style>body {text-align:center;}</style></head>")); // body will be centered
  client.println(F("<body bgcolor='#00979C'>")); // page backgroud color if you want Arduino color style
  client.println();
  client.print("<form action=\"/?\" method=get>");
  //client.print("<div align=\"center\">");
  client.print("<b>Nivel_1: </b> "); // Tank level (cm) on sensor 1
  client.print(NivelTanque1);
  client.print("<b>Nivel_2: </b> "); //Tank level (cm) on sensor 2
  client.print(NivelTanque2);
  client.print("<b>Nivel_3: </b> "); // Tank level (cm) on sensor 3
  client.print(NivelTanque3);
  client.print("<b>yr: </b> "); // Setpoint
  client.print(yr);
  client.println();

  client.println(F("</center></body></html>"));
}

Thank you sir! I will test on monday, and give you a reply.

Why is MATLAB doing PID control based on old data? The Arduino is perfectly capable of doing PID based on current data.