Hi,
The example 2.4 “Web Server with Slider” in the book “Build Web Servers with the ESP32 and ESP8266” doesn’t update the connected clients after moving the slider.
To fix this, after having given a look at the working example 2.3, I made following changes to some of the files of example 2.4.
1) In the main.cpp file I added this method:
void notifyClients(String state) {
ws.textAll(state);
}
and called it inside handleWebSocketMessage after getting the sliderValue and before calculating the dutyCycle:
sliderValue = (*char)data;
notifyClients(sliderValue);
dutyCycle = map(sliderValue.toInt(), 0, 100, 0, 255);
2) In the script.js file I added getCurrentValue inside the onMessage function:
function onMessage(event) {
console.log(event.data);
getCurrentValue();
}
3) In the index.html I changed “oninput” to “onchange” inside the <input> element to avoid sending a lot of wrong values during the moving of the slider. This sends only the last value leaving the slider:
<input type=”range” onchange=”updateSliderPWM(this)” id=”pwmSlider” …>
Please let me know if this is a good fix for the existing problem.
Many thanks!
Best regards,
Enrico