The following source code is from the official ESP8266 website(Explanation provided): https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-examples.html
This .ino code returns the html code of the server name mentioned in "const char* server" declaration, on to the Serial Monitor.
And Yes, this code perfectly returned the html codes of wwww.google.com, www.arduino.cc etc. But when I try it with my 'led blinking.html' I am unable to find any luck. I guess it is because the website should be hosted on a live server.
Other Details:
1. uC Board: Wemos D1 mini (An ESP8266 Variant)
2. I am using Xampp to run Apache server and MySQL
3. Wemos board is set up as a WiFiClient
#include <ESP8266WiFi.h>
const char* ssid = "xxxxx";
const char* password = "xxxxxxxx";
I have put the html,php code of the website in the directory : C:/xampp/htdocs/LED BLINKING/led blinking.html
IP Address of my Windows PC: 192.168.100.9
const char* server = "192.168.100.9/LED BLINKING/led blinking.html";
I tried with just "192.168.100.9"; too. I also unblocked the windows firewall for my ApacheHTTP Server. Now I can view the webpage on my mobile with the URL: 192.168.100.9/LED BLINKING/led blinking.html.
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop()
{
WiFiClient client;
Serial.printf("\n[Connecting ...");
if (client.connect(server, 80))
{
Serial.println("connected]");
Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"server: " + server + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
Serial.println("[Response:]");
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}
The output on Serial Monitor is: Connecting to ssid connected [Connecting connection failed!]
1 Answer 1
Variable server
is a hostname or IP address for TCP connection.
The /LED BLINKING/led blinking.html
goes after HTTP GET. Now there is /
and it worked for main page of servers.
You need
client.print(String("GET /LED%20BLINKING/led%20blinking.htm") + " HTTP/1.1\r\n" +
"server: " + server + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
and you must escape the space