Zápisník experimentátora
Hierarchy: ESP8266
When using ESP8266 as a webserver, you will often encounter examples of strings that are extremely laborious. Any minor change in the HTML code of the site is terribly difficult. C++11 allows us to write text strings more conveniently. How to do it, we will explain in this article.
This it example of a simple HTML code of the page, we want to view in the browser, using the ESP8266.
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Hello ESP8266</title>
<style>
p { text-indent: 30px; margin: 0px; }
</style>
</head>
<body>
<h1>HELLO</h1>
<p>Hello from ESP8266.</p>
</body>
</html>
Typically, you will encounter such entries in the examples. The htmlPage
variable is a string that has each line closed in double quotes. In addition, you must customize each occurrence of the character "
and edit it to \"
so that the compiler does not shows an error. It's laborious and unobtrusive. Any change requires a great deal of effort so you do not make a mistake.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "arduino_secret.h"
ESP8266WebServer server(80);
const char htmlPage[] PROGMEM = "<html>"
"<head>"
"<meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\">"
"<title>Hello ESP8266</title>"
"<style>"
"p { text-indent: 30px; margin: 0px; }"
"</style>"
"</head>"
"<body>"
" <h1>HELLO</h1>"
" <p>Hello from ESP8266.</p>"
"</body>"
"</html>";
void handleRoot() {
server.send(200, "text/html", htmlPage);
}
void setup(void){
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
C++11 provides a more convenient way to write text strings containing HTML, XML or JSON. See Wikipedia for details. You can type a text string in a R"delimiter(text)delimiter"
format. In such a form, the content of the variable is easily readable and you can make changes easily. Or write them directly into the text, or copy and paste the original text into a string.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "arduino_secret.h"
ESP8266WebServer server(80);
const char htmlPage[] PROGMEM = R"=====(
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Hello ESP8266</title>
<style>
p { text-indent: 30px; margin: 0px; }
</style>
</head>
<body>
<h1>HELLO</h1>
<p>Hello from ESP8266.</p>
</body>
</html>
)=====";
void handleRoot() {
server.send(200, "text/html", htmlPage);
}
void setup(void){
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
The source code is located on the GitHub server.
01.08.2018