Advertisement
iqhtyar2k

Esp8266WebStAp

Jan 24th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.09 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266mDNS.h>
  4. #include <ESP8266WebServer.h>
  5.  
  6. const char* APssid = "iqhtyar";
  7. const char* APpassword = "12345678";
  8. const char* ssid = "iqhtyar";
  9. const char* password = "23456789";
  10.  
  11. ESP8266WebServer server(80);
  12.  
  13. void handleRoot();
  14. void handleNotFound();
  15.    
  16. void setup() {
  17.   Serial.begin(115200);
  18.   delay(100);
  19.   Serial.println();
  20.   Serial.print("Configuring access point...");
  21.   WiFi.mode(WIFI_AP_STA);
  22.   WiFi.softAP(APssid, APpassword);
  23.  
  24.   WiFi.disconnect();
  25.   delay(100);
  26.   Serial.println('\n');
  27.  
  28.   Serial.println("Scanning available WiFi networks...");
  29.   int n = WiFi.scanNetworks();
  30.   if (n == 0) {
  31.     Serial.println("No networks found");
  32.   } else {
  33.     Serial.println("Found networks:");
  34.     for (int i = 0; i < n; ++i) {
  35.       // Print SSID and RSSI for each network found
  36.       Serial.print(i + 1);
  37.       Serial.print(": ");
  38.       Serial.print(WiFi.SSID(i));
  39.       Serial.print(" (");
  40.       Serial.print(WiFi.RSSI(i));
  41.       Serial.print(")");
  42.       Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  43.       delay(10);
  44.     }
  45.   }
  46.  
  47.   // Connect to an open WiFi network
  48.   for (int i = 0; i < n; ++i) {
  49.     if (WiFi.encryptionType(i) == ENC_TYPE_NONE) {
  50.       Serial.println("Connecting to open network...");
  51.       WiFi.begin(WiFi.SSID(i), "");
  52.       break;
  53.     }
  54.   }
  55.  
  56.   while (WiFi.status() != WL_CONNECTED) {
  57.     delay(500);
  58.     Serial.print(".");
  59.   }
  60.  
  61.   Serial.println("");
  62.   Serial.println("WiFi connected");
  63.   Serial.println("IP address: ");
  64.   Serial.println(WiFi.localIP());
  65.  
  66.   if (MDNS.begin("esp8266")) {              
  67.     Serial.println("mDNS responder started");
  68.   } else {
  69.     Serial.println("Error setting up MDNS responder!");
  70.   }
  71.  
  72.   server.on("/", handleRoot);              
  73.   server.onNotFound(handleNotFound);        
  74.  
  75.   server.begin();                          
  76.   Serial.println("HTTP server started");
  77. }
  78.  
  79. void loop(void){
  80.   server.handleClient();                    
  81. }
  82.  
  83. void handleRoot() {
  84.   server.send(200, "text/plain", "Welcome, Node Mcu Esp8266");    
  85. }
  86.  
  87. void handleNotFound(){
  88.   server.send(404, "text/plain", "404: Not found");
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement