ESP32: WiFi: Periodic, Unrequested update of Client

Neophyte Question

Could someone please provide an example of ESP32 Wifi sketch that has the Server performing periodic updates to the client interface without any user client input.

My project involves retreiving the temperature from a sensor at periodic intervals. I would like the client to display the new value of the temperature as it is sensed.

The code below indicates the the outline of my current implementation.

I have tried numerous variation on this code, but I have yet been able to produce an update of the client display without making a explicit client request (eg clicking on a button).

An example of such a situation, or a strightforward update to the code would be appreciated.

ardquiz

#include <WiFi.h>

const char* ssid = "ESP32-WiFi";   /* Add your router's SSID */
const char* password = "12345678"; /*Add the password */

WiFiServer espServer(80); /* Instance of WiFiServer with port number 80 */
/* 80 is the Port Number for HTTP Web Server */

/* A String to capture the incoming HTTP GET Request */
String request;

void setup() {

  WiFi.mode(WIFI_STA);        /* Configure ESP32 in STA Mode */
  WiFi.begin(ssid, password); /* Connect to Wi-Fi based on the above SSID and Password */
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("*");
    delay(100);
  }
  Serial.print("\n");
  Serial.print("Connected to Wi-Fi: ");
  Serial.println(WiFi.SSID());
  delay(100);

  delay(2000);

  Serial.println("Starting ESP32 Web Server...");
  espServer.begin(); /* Start the HTTP web Server */
}

void loop() {
  WiFiClient client = espServer.available(); /* Check if a client is available */
  if (!client) {
    return;
  }

  Serial.println("New Client!!!");
  boolean currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      request += c;
      Serial.write(c);
      /* check if the the http request has ended to send a reply */

      if (c == '\n' && currentLineIsBlank) {
        /* Extract the URL of the request */
        .
        /* Based on the content of the URL from the request, Performm some local Action .... */
        .
        /* Send HTTP Response in the form of HTML Web Page .....*/
        /* N.B. Contains client Input Button */

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

        break;
      }
      if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
      //client.print("\n");
    }
  }

  delay(1);
  request = "";
  //client.flush();
  client.stop();
  Serial.println("Client disconnected");
  Serial.print("\n");
}

Just making sure I got it right, you want the server to push data to the client which then the client will detect and respond?

Might be over kill for your situation but MQTT can do the thing.

hi @ardquiz welcome to the forum..

the client can be set to refresh itself..
here's your code changed to this idea..

#include <WiFi.h>
#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;


const char* ssid = "ESP32-WiFi";   /* Add your router's SSID */
const char* password = "12345678"; /*Add the password */

WiFiServer espServer(80); /* Instance of WiFiServer with port number 80 */
/* 80 is the Port Number for HTTP Web Server */

/* A String to capture the incoming HTTP GET Request */
String request;

void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  WiFi.mode(WIFI_STA);        /* Configure ESP32 in STA Mode */
  WiFi.begin(ssid, password); /* Connect to Wi-Fi based on the above SSID and Password */
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("*");
    delay(100);
  }
  Serial.print("\n");
  Serial.print("Connected to Wi-Fi: ");
  Serial.println(WiFi.SSID());
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  delay(2000);

  Serial.println("Starting ESP32 Web Server...");
  espServer.begin(); /* Start the HTTP web Server */
}

void loop() {
  WiFiClient client = espServer.available(); /* Check if a client is available */
  if (!client) {
    return;
  }

  Serial.println("New Client!!!");
  boolean currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      request += c;
      Serial.write(c);
      /* check if the the http request has ended to send a reply */

      if (c == '\n' && currentLineIsBlank) {
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");  // the connection will be closed after completion of the response
        client.println("Refresh: 10");  // refresh the page automatically every 10 sec
        client.println();
        client.println("<!DOCTYPE HTML>");
        client.println("<html>");
        client.println("DHT22 Sensor <br>");
        client.print("<br><br>");
        //read sensor
        TempAndHumidity  data = dhtSensor.getTempAndHumidity();
        client.println("Temp: " + String(data.temperature, 2) + "°C <br>");
        client.println("Humidity: " + String(data.humidity, 1) + "% <br>");
        client.println("</html>");

        break;
      }
      if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
      //client.print("\n");
    }
  }

  delay(1);
  request = "";
  //client.flush();
  client.stop();
  Serial.println("Client disconnected");
  Serial.print("\n");
}

server only sends the one page which should auto refresh every 10 seconds..
added a DHT22 as an example of temp reading..

good luck.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.