Problem in recieving data in serial port ESP8266(Node Mcu)

Hi

I have a problem

I have sent data to a http server(I have Created the server with esp8266) and the server gives the data completley.

but the problem is when I referesh the web browser the data removed.

I dont know how can I have a backup of my data and every time I refresh the browser I can see the older data.

Here Is my code:

  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="";

amiroruji72:
I dont know how can I have a backup of my data and every time I refresh the browser I can see the older data.

I can come up with various options for storing data:

  • store in a global variable
  • store it in EEPROM
  • store it on an sd card
  • re-read it from where-ever you got it from in the first place

No I want store the data in a string and every time the server runs the data loads.

Well, it's the first option then.

But I dont Know How To do It

Could You help me please?

#include <ESP8266WiFi.h>
 
const char* ssid = "*";
const char* password = "*";
 
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");
}

Make it a global variable.

If you don't know what a global variable is or how it works, look it up. That's how you learn.

Ok

That worked but another problem created

How can I make the str empty??

because every 1 second it prints the str

str="";

I write It as below But it does not work:

  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>";


  client.print(s);
  delay(1);
  str="";
str=Serial.readStringUntil('\n');

Risky. What if the '\n' doesn't arrive? What if you get more characters than expected? Both give trouble. Read this.

And what "does not work"? No obvious errors in that snippet, other than that I thought you wanted to preserve the info in str for future requests yet you clear it right after sending the page. And it's odd that the page is sent upon receipt of the Serial string, not upon an incoming http request.