我已将数据发送到HTTP服务器(我已使用ESP8266创建了服务器),服务器完全提供数据。
但问题是,当我刷新网页浏览器时,数据已被删除。
我不知道如何备份我的数据,每次刷新浏览器时都可以看到较旧的数据。
这是我的代码:
#include <ESP8266WiFi.h>
const char* ssid = "Ashnay-E-Aval";
const char* password = "8841525252";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
String s;
String str=Serial.readStringUntil('\n');
s+= "<!DOCTYPE html>";
s+= "<html>";
s+= "<body>";
s+= "<h1>My First Heading</h1>";
s+= "<p>My "+STR+".</p>";
s+= "</body>";
s+= "</html>";
str="";
client.print(s);
delay(1);
Serial.println("Client disconnected");
}
答案 0 :(得分:1)
如果我理解正确,您的ESP8266会从STM32通过Serial获取一些数据(字符串)。每次执行主循环时,ESP8266可能会或可能不会获得新数据。您希望在STM32中有新数据(新字符串)时更新您的网站。如果没有发送任何内容,则需要显示旧数据(旧字符串)。
为了实现这一目标,您需要:
readStringUntil()
的新数据进行比较。代码:
#include <ESP8266WiFi.h>
const char* ssid = "Ashnay-E-Aval";
const char* password = "8841525252";
WiFiServer server(80);
String str = "";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
String s;
String newStr = Serial.readStringUntil('\n');
if (!newStr.empty()) {
str = newStr;
}
s+= "<!DOCTYPE html>";
s+= "<html>";
s+= "<body>";
s+= "<h1>My First Heading</h1>";
s+= "<p>My "+STR+".</p>";
s+= "</body>";
s+= "</html>";
str="";
client.print(s);
delay(1);
Serial.println("Client disconnected");
}