kopia lustrzana https://github.com/alanesq/esp32cam-demo
Add files via upload
rodzic
be048390e0
commit
ae9037b4ce
|
@ -14,8 +14,9 @@
|
||||||
|
|
||||||
// Wifi settings
|
// Wifi settings
|
||||||
|
|
||||||
const char *SSID = "your_wifi_ssid";
|
const char *SSID = "your_wifi_ssid";
|
||||||
const char *PWD = "your_wifi_pwd";
|
|
||||||
|
const char *PWD = "your_wifi_pwd";
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
@ -209,13 +210,11 @@ void handleButton(){
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// -ajax web page requested i.e. http://x.x.x.x/ajax
|
// -ajax web page requested i.e. http://x.x.x.x/ajax
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// demonstrate use ofAJAX to refresh info. on web page
|
// demonstrate use of AJAX to refresh info. on web page
|
||||||
// see: https://circuits4you.com/2018/02/04/esp8266-ajax-update-part-of-web-page-without-refreshing/
|
// see: https://circuits4you.com/2018/02/04/esp8266-ajax-update-part-of-web-page-without-refreshing/
|
||||||
|
|
||||||
void handleAJAX() {
|
void handleAJAX() {
|
||||||
|
|
||||||
#define LED 2 //On board LED
|
|
||||||
|
|
||||||
WiFiClient client = server.client(); // open link with client
|
WiFiClient client = server.client(); // open link with client
|
||||||
|
|
||||||
// HTML header
|
// HTML header
|
||||||
|
@ -235,45 +234,53 @@ void handleAJAX() {
|
||||||
<div>\
|
<div>\
|
||||||
Current Millis : <span id='MillisValue'>0</span><br>\
|
Current Millis : <span id='MillisValue'>0</span><br>\
|
||||||
LED State is : <span id='LEDState'>NA</span>\
|
LED State is : <span id='LEDState'>NA</span>\
|
||||||
</div>\
|
</div>");
|
||||||
");
|
|
||||||
|
|
||||||
// Javascript
|
|
||||||
client.print("\
|
// Javascript
|
||||||
<script>\
|
|
||||||
function sendData(led) {\
|
client.print("<script>");
|
||||||
var xhttp = new XMLHttpRequest();\
|
|
||||||
xhttp.onreadystatechange = function() {\
|
// triggered when a button is pressed - calls the 'handleLED()' procedure below via '/setled'
|
||||||
if (this.readyState == 4 && this.status == 200) {\
|
client.print("\
|
||||||
document.getElementById('LEDState').innerHTML =\
|
function sendData(led) {\
|
||||||
this.responseText;\
|
var xhttp = new XMLHttpRequest();\
|
||||||
}\
|
xhttp.onreadystatechange = function() {\
|
||||||
};\
|
if (this.readyState == 4 && this.status == 200) {\
|
||||||
xhttp.open('GET', 'setLED?LEDstate='+led, true);\
|
document.getElementById('LEDState').innerHTML = this.responseText;\
|
||||||
xhttp.send();\
|
}\
|
||||||
}\
|
};\
|
||||||
setInterval(function() {\
|
xhttp.open('GET', 'setLED?LEDstate='+led, true);\
|
||||||
getData();\
|
xhttp.send();\
|
||||||
}, 2000);\
|
}");
|
||||||
function getData() {\
|
|
||||||
var xhttp = new XMLHttpRequest();\
|
// auto triggers 'getData' every 2 seconds
|
||||||
xhttp.onreadystatechange = function() {\
|
client.print("\
|
||||||
if (this.readyState == 4 && this.status == 200) {\
|
setInterval(function() {\
|
||||||
document.getElementById('MillisValue').innerHTML =\
|
getData();\
|
||||||
this.responseText;\
|
}, 2000);");
|
||||||
}\
|
|
||||||
};\
|
// 'getdata' = requests the current millis() value - calls the 'handleSendtime()' procedure below via '/sendtime'
|
||||||
xhttp.open('GET', 'sendtime', true);\
|
client.print("\
|
||||||
xhttp.send();\
|
function getData() {\
|
||||||
}\
|
var xhttp = new XMLHttpRequest();\
|
||||||
</script>\
|
xhttp.onreadystatechange = function() {\
|
||||||
");
|
if (this.readyState == 4 && this.status == 200) {\
|
||||||
|
document.getElementById('MillisValue').innerHTML = this.responseText;\
|
||||||
|
}\
|
||||||
|
};\
|
||||||
|
xhttp.open('GET', 'sendtime', true);\
|
||||||
|
xhttp.send();}");
|
||||||
|
|
||||||
|
client.print("</script>");
|
||||||
|
|
||||||
|
|
||||||
// close HTML
|
// close HTML
|
||||||
client.print("\
|
client.print("\
|
||||||
</body>\
|
</body>\
|
||||||
</html>\
|
</html>\
|
||||||
");
|
");
|
||||||
|
|
||||||
delay(3);
|
delay(3);
|
||||||
client.stop();
|
client.stop();
|
||||||
|
|
||||||
|
@ -281,6 +288,7 @@ void handleAJAX() {
|
||||||
|
|
||||||
|
|
||||||
// send millis (ajax request)
|
// send millis (ajax request)
|
||||||
|
// it just replies with the current Millis value as plain text
|
||||||
void handleSendtime() {
|
void handleSendtime() {
|
||||||
String cTime = String(millis());
|
String cTime = String(millis());
|
||||||
server.send(200, "text/plane", cTime); //Send millis value only to client ajax request
|
server.send(200, "text/plane", cTime); //Send millis value only to client ajax request
|
||||||
|
@ -288,6 +296,7 @@ void handleSendtime() {
|
||||||
|
|
||||||
|
|
||||||
// handle button clicks on AJAX web page
|
// handle button clicks on AJAX web page
|
||||||
|
// it sets the onboard LED status and replies with it's status as plain text
|
||||||
void handleLED() {
|
void handleLED() {
|
||||||
String ledState = "OFF";
|
String ledState = "OFF";
|
||||||
String t_state = server.arg("LEDstate"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
|
String t_state = server.arg("LEDstate"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
|
||||||
|
|
Ładowanie…
Reference in New Issue