Receiving data in serial port ESP8266

3.4k Views Asked by At

I have sent data to a HTTP server (I have created the server with ESP8266) and the server gives the data completely.

But the problem is, when I refresh the web browser the data removed.

I don't 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:

#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");
}
1

There are 1 best solutions below

0
On

If I understand you correctly, your ESP8266 gets some data (a string) over Serial from STM32. The ESP8266 may or may not get new data every execution of main loop. You want to update your website when there's new data (new string) from your STM32. If there's nothing sent, you want to display old data (old string).

In order to achieve that you need to:

  1. Declare global variable, so it's not recreated every loop.
  2. Compare that variable with new data from readStringUntil().
  3. If the new data is not empty (STM32 sent something), then assign to the global variable new data.
  4. Use the global variable to display data on the website.

Code:

#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");
}