ESP8266 Project Assistance: Seeking Guidance for Two-Way Communication with Web Server

Hey fellow Arduino enthusiasts,

I'm excited to be a part of this community! I've recently started working on a project involving an ESP8266 board and ran into a bit of a roadblock. I managed to find a code that successfully sends data to a web server hosted on 000webhost.com. However, I'm now trying to figure out how to receive data from the same web server back to my ESP8266.

Here's the code I'm using to send data to the server:

ESP Code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "testing";
const char* password = "testing123*";
const char* serverUrl = "http://esp8266.000webhostapp.com/submit.php"; // Change to your server URL
const char* uniqueID = "ESPdlf%**"; // Unique ID for the device

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  if (Serial.available()) {
    String data = Serial.readStringUntil('\n'); // Read data from Serial monitor
    String dataWithID = uniqueID + data; // Append the unique ID to the data
    sendDataToServer(dataWithID);
  }
}

void sendDataToServer(String data) {
  WiFiClient client;
  HTTPClient http;

  http.begin(client, serverUrl);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  String postData = "data=" + data + "&device_id=" + uniqueID; // Send unique ID with data
  int httpResponseCode = http.POST(postData);

  if (httpResponseCode > 0) {
    String response = http.getString();
    Serial.println("HTTP Response Code: " + String(httpResponseCode));
    Serial.println("Server response: " + response);
  } else {
    Serial.println("HTTP POST request failed");
  }

  http.end();
}

PHP:

<?php
// Get the data and device ID from the POST request
$data = $_POST['data'];
$device_id = $_POST['device_id'];

// Sanitize and validate the input data
$data = htmlspecialchars($data); // Sanitize data to prevent HTML/JS injection
$device_id = htmlspecialchars($device_id); // Sanitize device ID

// Create HTML content to display the data and response
$response = "Data received";
$write = "<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h1> ESP Data </h1><br>
    <div class='container'>
        <p>Data: <span id='data'>$data</span></p>
        <p>Device ID: <span id='device_id'>$device_id</span></p>
        <p>Server Response: <span id='response'>$response</span></p>
    </div>
</body>
</html>";

// Write the HTML content to the esp1.php file
file_put_contents('esp1.php', $write);

// Send a response to the client
echo $response;
?>

Now, I'd greatly appreciate your help in modifying this code or providing additional code snippets to enable the ESP8266 to receive data from the web server. Ideally, I'd like to receive the data in a format that I can then process and utilize within my project.

Thanks a lot in advance for your guidance and support! Looking forward to learning from this awesome community.

I'd highly recommend looking at Rui Santos' website: https://randomnerdtutorials.com/
He and his wife have put together everything you need to accomplish what you are asking.

hi,
Watch this video,

i have a repo related to a project like this, Have a look, (for ESP32 but the concept is the same)

You can't.
An HTTP web request follow always the path from client to server and not vice-versa.

The server can only reply to a client request.

In order to achieve what you need, you should run on your server other services (for example run a websocket server), but with this kind of hosting I don't think it's possible.

Does anyone know if I use Firebase, Is it free for my whole house (18 devices) or stick with a web server (000webhost)?

I think 000webhost will do the job

can you please guide?

Check the link that @ktauchathuranga provided to you.
It's more or less what you need I think, but the key is that is always the client which do the request to the server, so in your sketch you need to implement a polling mechanism.

1 Like

you can try,

or

and if you have any problems feel free ask! :innocent:

I came across this, but what I need is a solution that fetches data from a web server and prints it to the serial port, my other controller will handle from serial read

as i said, if you look closely you will notice in both video and the GitHub repo, that you have to make a request to the server to get data from the web. you have to implement an API like a feature, so you can send a request to that API like URL with some parameters, and according to that parameter you set, related data will return.

in my project you can see my "API" folder,

and you can see that in my ESP32 I am sending a POST request to my API URL to get data from the web using a parameter called readstate=1,

    // Specify the target URL
    http.begin("https://YOUR-DOMAIN.com/api/command");

    // Set the data to be sent in the POST request
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    String payload = "readstate=1";  // Add "readstate=1&value=1" for More

    // Send the POST request
    int statusCode = http.POST(payload);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.