Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <EEPROM.h>
- #include <DNSServer.h>
- #include <ESP.h>
- const byte DNS_PORT = 53;
- DNSServer dnsServer;
- MDNSResponder mdns;
- ESP8266WebServer server(80);
- String ssid = "";
- String password = "";
- String webPage = "";
- void setup(void){
- IPAddress Ip(192, 168, 1, 1);
- IPAddress NMask(255, 255, 255, 0);
- WiFi.softAPConfig(Ip, Ip, NMask);
- WiFi.softAP(conf_ssid.c_str());
- WiFi.mode(WIFI_AP_STA);
- Serial.begin(115200);
- delay(1000);
- Serial.println("");
- Serial.println("Soft AP Started");
- Serial.println("");
- /* Setup the DNS server redirecting all the domains to the Ip */
- dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
- dnsServer.start(DNS_PORT, "www.example.com", Ip);
- //connectToWifi();
- getCredentials(); //Reads Home WiFi credentials from the EEPROM
- if (ssid != "" and password != ""){
- WiFi.begin(ssid.c_str(), password.c_str());
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- WiFi.mode(WIFI_AP_STA);
- }
- server.begin();
- // When no password is present
- if ((ssid == "" and password == "") or (ssid == "Error")){
- // ****** MAIN PAGE ******
- server.on("/", [](){ /* HANDLES CREDENTIAL INPUT */ });
- server.begin();
- }
- else // If we have ssid and password -- check if we have connection
- {
- webPage += "<h1>Main Webpage</h1>";
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- server.begin();
- //if (mdns.begin("esp8266", WiFi.localIP())) {
- // Serial.println("MDNS responder started");
- //}
- server.on("/", [](){
- server.send(200, "text/html", webPage);
- });
- Serial.println("HTTP server started");
- }
- }
- void loop(void){
- dnsServer.processNextRequest();
- server.handleClient();
- }
Add Comment
Please, Sign In to add comment