1

I am not new here but this is my first question. I have searched a lot and quite frankly can't understand how this is supposed to work. I get data periodically (temperature) to my ESP32 and while having it set as a WiFi client, connect to my router and somehow store this data on my Laptop(or somewhere else, like a local/web site, don't know if that's possible/better). How is the connection supposed to work? I have installed XAMPP and run the Apache and MySQL servers and I tried to connect to my Laptop with some sketches from Arduino using the ESP32 libraries

    // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
const char* host = "192.168.1.109";     //The local IP of my Laptop
if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
}

but it doesn't connect. Can someone please explain to me how this connection is supposed to take form or is this question too vague? I really just wanna know the "how-things-should-work-together" in this situation. Thank you in advance.

2
  • is the port open on firewall on PC? can you access the http server from a browser on the PC and from some other device? it is better to use the IP address with IPAddress class and not as hostname string Commented Nov 13, 2019 at 20:27
  • I can access the XAMPP Welcome page from my laptop using either 127.0.0.1 or 192.168.1.109 but can't for example access it from my phone(which is connected in the same WiFi network). I also set an inbound rule for the ports to open but nothing changed. It also seems that XAMPP had already taken care of that but I'm not sure. Commented Nov 14, 2019 at 8:46

1 Answer 1

8

OK, so after a lot of research and trying, I managed to work it out. I can now send an HTTP request (like GET or POST) from my ESP32 to a local server that is running on my laptop using XAMP and get a response. I can also connect to my local IP from my mobile phone (which is also in the same WiFi network).

Just for anyone else who wants to connect to a location in a server hosted on a PC in a local network, the steps are:

  • Create a local server on your PC, laptop whatever using an application like XAMPP (I have Windows 10 so WAMP would also work), download, install, open and start Apache.
  • Make sure that the Firewall lets your requests pass through (for me it was open by default, but I had seen elsewhere Firewall being an issue)
  • Go to your network settings, select the network that your devices(ESP32, phone, etc.)are connected and change its profile to Private, meaning that you trust this network, making your PC discoverable and able to accept requests. (That is really simple but took me hours to find)

Now, in order to connect from your phone to your PC, open a browser and enter the local IP (that is the IP that is given to your PC from the router as a local network name) of your PC to a browser and that's it, you are connected.
If you installed and ran XAMP, when connecting to your local IP(from same PC or other local device), it will forward you to 192.168.x.x/dashboard. If you want to create new workspaces and files, browse the XAMP folder in the installed location and inside the '/htdocs' subfolder do your testing.

For the ESP32 communication in Arduino(basic steps, not full code):

#include <WiFi.h>
#include <HTTPClient.h>

String host = "http://192.168.x.x/testfolder/";
String file_to_access = "test_post.php";
String URL = host + file_to_access;

void setup(){
 WiFi.begin(ssid, password); //Connect to WiFi

 HTTPClient http;

 bool http_begin = http.begin(URL);
 String message_name = "message_sent";
 String message_value = "This is the value of a message sent by the ESP32 to local server 
 via HTTP POST request";
 String payload_request = message_name + "=" + message_value;  //Combine the name and value
 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
 int httpResponseCode = http.sendRequest("POST", payload_request);
 String payload_response = http.getString();
}

In the test_post.php (located in "C:\xampp\htdocs\testfolder\") file I used a simple script to echo a message received using a POST request, so it's only 'readable' from POST requests. Connecting to it from your browser will give you the "Sorry, accepting..." message.

<?php
$message_received = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
        $message_received = $_POST["message_sent"];
        echo "Welcome ESP32, the message you sent me is: " . $message_received;
    }
    else {
        echo "Sorry, accepting only POST requests...";
    }
?>

Finally, using Serial prints, the output is:

Response Code: 200
Payload: Welcome ESP32, the message you sent me is: This is the value of a message sent by the ESP32 to local server via HTTP POST request

There it is, hope that this helps someone.

Sign up to request clarification or add additional context in comments.

1 Comment

It is a solution however not a very safe solution (easy hackable). I don't know about your exact configuration and if it accesable from the outside world, I will suggest you to take a look at sockets (or even Websockets) and 'post' data on a 'hidden' port to the server. It is more safe, faster (low costs) and you can update it more frequently if you want. Store the data in a file or database on the server and create a hidden entry (url) on the XAMP server to be able to view the data (user name and password protected). I think that is a more elegant and more safe solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.