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");
}