connected but no internet im a newbie in coding please help
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <lwip/napt.h>
#include <DNSServer.h>
ESP8266WebServer server(80);
DNSServer dnsServer;
const char* ap_SSID = "ESP8266_Extender";
const char* ap_PASS = "12345678";
void autoConfigWiFi() {
WiFi.begin();
Serial.println("Attempting to connect to the last known network...");
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nReconnected to known Wi-Fi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nNo known networks available. Staying in AP mode.");
}
}
bool checkInternet() {
WiFiClient client;
return client.connect("8.8.8.8", 53);
}
void setup() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(ap_SSID, ap_PASS);
delay(1000);
dnsServer.start(53, "*", WiFi.softAPIP());
autoConfigWiFi();
Serial.print("AP Started. Connect to: ");
Serial.println(ap_SSID);
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP());
if (WiFi.status() == WL_CONNECTED) {
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
ip_napt_enable(WiFi.localIP(), 1);
Serial.println("NAT Enabled.");
}
server.on("/", handleRoot);
server.on("/connect", HTTP_POST, handleConnect);
server.begin();
Serial.println("Web Server started.");
}
void handleRoot() {
String html = "<html><head><title>ESP8266 Wi-Fi Repeater</title></head><body>";
html += "<h2>Available Networks</h2>";
int numNetworks = WiFi.scanNetworks();
if (numNetworks == 0) {
html += "<p>No networks found.</p>";
} else {
html += "<form action='/connect' method='POST'>";
for (int i = 0; i < numNetworks; i++) {
html += "<input type='radio' name='ssid' value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "<br>";
}
html += "<br>Password: <input type='password' name='password'><br>";
html += "<input type='submit' value='Connect'>";
html += "</form>";
}
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleConnect() {
String ssid = server.arg("ssid");
String password = server.arg("password");
if (ssid.length() == 0) {
server.send(400, "text/html", "<h2>Error! Empty SSID.</h2>");
return;
}
Serial.println("Switching to STA mode...");
WiFi.disconnect();
WiFi.begin(ssid.c_str(), password.length() ? password.c_str() : NULL);
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to Wi-Fi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Subnet: ");
Serial.println(WiFi.subnetMask());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
ip_napt_enable(WiFi.localIP(), 1);
Serial.println("NAT Enabled for internet sharing.");
if (checkInternet()) {
Serial.println("Internet access confirmed.");
server.send(200, "text/html", "<h2>Connected successfully!</h2><p>IP Address: " + WiFi.localIP().toString() + "</p>");
} else {
Serial.println("No internet access.");
server.send(400, "text/html", "<h2>Connected, but no internet access.</h2>");
}
} else {
Serial.println("\nFailed to connect.");
server.send(400, "text/html", "<h2>Failed to connect. Please try again.</h2>");
}
}
void loop() {
server.handleClient();
dnsServer.processNextRequest();
}
What’s the question ?
It’s connected to your WLAN, not directly exposed to the internet as a web server. You can only access it from a browser on any computer in the same LAN unless you poke a hole in your network to make the ESP reachable from the Internet.
1 Like
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.