Hi, after several days trying different websocket programs, never finding
one that wpould compile due to lib updates, misidentified libs, I found one
that comes close.
Idea is ~ 21 lines, simple text data, to be sent to page.
-
I can get numeric converted to string to print 1 and only 1 line,
but never feeding a standard string var in the send. Only one
when it converts a numeric using String() does it display -
Note there are two sends here, as a test, only 1 gets displayed
on webpage.
const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<style>
.card{
max-width: 400px;
min-height: 250px;
background: #02b875;
padding: 30px;
box-sizing: border-box;
color: #FFF;
margin:20px;
box-shadow: 0px 2px 18px -4px rgba(0,0,0,0.75);
}
</style>
<body>
<div class="card">
<h4>The ESP32 Update web page without refresh</h4><br>
<h1>Sensor Value:<span id="adcValue">0</span></h1><br>
<br><a href="https://circuits4you.com">Circuits4you.com</a>
</div>
<script>
setInterval(function() {
// Call a function repetatively with 2 Second interval
getData();
}, 2000); //2000mSeconds update rate
function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("adcValue").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "readADC", true);
xhttp.send();
}
</script>
</body>
</html>
)=====";
index.h page above in project for formatting
below code segment sending data to page
void handleADC() {
delay(5000);
testval++;
String adcValue = String(testval);
//int a = analogRead(A0);
//String adcValue = String(a);
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
testval++;
adcValue = String(testval);
delay(1000);
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
}
main -
/*
* ESP32 AJAX Demo
* Updates and Gets data from webpage without page refresh
* https://circuits4you.com
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "index.h" //Web page header file
WebServer server(80);
//Enter your SSID and PASSWORD
const char* ssid = "xxx";
const char* password = "xxxx";
int testval = 0;
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void handleADC() {
delay(5000);
testval++;
String adcValue = String(testval);
//int a = analogRead(A0);
//String adcValue = String(a);
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
testval++;
adcValue = String(testval);
delay(1000);
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
}
//===============================================================
// Setup
//===============================================================
void setup(void){
Serial.begin(115200);
Serial.println();
Serial.println("Booting Sketch...");
/*
//ESP32 As access point
WiFi.mode(WIFI_AP); //Access Point mode
WiFi.softAP(ssid, password);
*/
//ESP32 connects to your wifi -----------------------------------
WiFi.mode(WIFI_STA); //Connectto your wifi
WiFi.begin(ssid, password);
Serial.println("Connecting to ");
Serial.print(ssid);
//Wait for WiFi to connect
while(WiFi.waitForConnectResult() != WL_CONNECTED){
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
//----------------------------------------------------------------
server.on("/", handleRoot); //This is display page
server.on("/readADC", handleADC);//To get update of ADC Value only
server.begin(); //Start server
Serial.println("HTTP server started");
}
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void loop(void){
server.handleClient();
delay(1000);
}
Any and all help greatly appreciated.
Regards, Dana.
