kopia lustrzana https://github.com/alanesq/esp32cam-demo
Add files via upload
rodzic
7b70c15f83
commit
499691d1d9
|
@ -13,9 +13,11 @@
|
||||||
|
|
||||||
// Wifi Settings
|
// Wifi Settings
|
||||||
|
|
||||||
const char *SSID = "your_wifi_ssid";
|
#include <wifiSettings.h> // delete this line, un-comment the below two lines and enter your wifi details
|
||||||
|
|
||||||
const char *PWD = "your_wifi_pwd";
|
//const char *SSID = "your_wifi_ssid";
|
||||||
|
|
||||||
|
//const char *PWD = "your_wifi_pwd";
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
@ -59,6 +61,7 @@ bool serialDebug = 1; // enable debugging info on serial port
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200); // start serial comms at speed 115200
|
Serial.begin(115200); // start serial comms at speed 115200
|
||||||
|
delay(200);
|
||||||
Serial.println("\n\nWebserver demo sketch");
|
Serial.println("\n\nWebserver demo sketch");
|
||||||
Serial.println("- Reset reason: " + ESP.getResetReason());
|
Serial.println("- Reset reason: " + ESP.getResetReason());
|
||||||
|
|
||||||
|
@ -211,46 +214,45 @@ void handleButton(){
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// demonstrate use of AJAX 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() {
|
||||||
|
|
||||||
|
WiFiClient client = server.client(); // open link with client
|
||||||
|
|
||||||
// html used in handleAJAX()
|
// log page request including clients IP address
|
||||||
|
IPAddress cip = client.remoteIP();
|
||||||
// HTML header
|
Serial.println("Ajax page requested from: " + String(cip[0]) +"." + String(cip[1]) + "." + String(cip[2]) + "." + String(cip[3]) );
|
||||||
const char HTML_Header[] = R"=====(
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
// ------------------- send the html -------------------
|
||||||
<html lang='en'>
|
|
||||||
<head>
|
client.print (R"=====(
|
||||||
<title>AJAX Demo</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
)=====";
|
<!DOCTYPE html>
|
||||||
|
<html lang='en'>
|
||||||
|
<head>
|
||||||
|
<title>AJAX Demo</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
// HTML Body
|
<div id='demo'>
|
||||||
const char HTML_Body[] = R"=====(
|
<h1>Update web page using AJAX</h1>
|
||||||
|
<button type='button' onclick='sendData(1)'>LED ON</button>
|
||||||
|
<button type='button' onclick='sendData(0)'>LED OFF</button><BR>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Current Millis : <span id='MillisValue'>0</span><br>
|
||||||
|
Received text : <span id='ReceivedText'>NA</span><br>
|
||||||
|
LED State is : <span id='LEDState'>NA</span><br>
|
||||||
|
</div>
|
||||||
|
|
||||||
<body>
|
)=====");
|
||||||
<div id='demo'>
|
|
||||||
<h1>Update web page using AJAX</h1>
|
|
||||||
<button type='button' onclick='sendData(1)'>LED ON</button>
|
|
||||||
<button type='button' onclick='sendData(0)'>LED OFF</button><BR>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
Current Millis : <span id='MillisValue'>0</span><br>
|
|
||||||
Received text : <span id='ReceivedText'>NA</span><br>
|
|
||||||
LED State is : <span id='LEDState'>NA</span><br>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
)=====";
|
|
||||||
|
|
||||||
|
// ------------------- JavaScript -------------------
|
||||||
|
|
||||||
|
client.print (R"=====(
|
||||||
|
|
||||||
// Javascript
|
<script>
|
||||||
// functions: sendData - triggered when a html button is pressed
|
|
||||||
// getData - refreshes data on page - received as a comma deliminated list - triggers every 2 seconds
|
|
||||||
const char HTML_JavaScript[] = R"=====(
|
|
||||||
|
|
||||||
<script>
|
|
||||||
|
|
||||||
function sendData(led) {
|
function sendData(led) {
|
||||||
var xhttp = new XMLHttpRequest();
|
var xhttp = new XMLHttpRequest();
|
||||||
|
@ -277,23 +279,17 @@ void handleButton(){
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
getData();
|
getData();
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
)=====";
|
|
||||||
|
|
||||||
|
)=====");
|
||||||
void handleAJAX() {
|
|
||||||
|
|
||||||
WiFiClient client = server.client(); // open link with client
|
// --------------------------------------------------
|
||||||
|
|
||||||
client.print(HTML_Header); // send html
|
// close html page
|
||||||
client.print(HTML_Body);
|
client.print("</body></html>\n"); // close HTML
|
||||||
client.print(HTML_JavaScript);
|
delay(3);
|
||||||
|
client.stop();
|
||||||
client.print("</body></html>\n"); // close HTML
|
|
||||||
delay(3);
|
|
||||||
client.stop();
|
|
||||||
|
|
||||||
} // handleAJAX
|
} // handleAJAX
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue