Guest User

Untitled

a guest
Oct 19th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <EEPROM.h>
  6. #include <DNSServer.h>
  7. #include <ESP.h>
  8.  
  9. const byte DNS_PORT = 53;
  10. DNSServer dnsServer;
  11.  
  12. MDNSResponder mdns;
  13. ESP8266WebServer server(80);
  14.  
  15. String ssid = "";
  16. String password = "";
  17. String webPage = "";
  18.  
  19.  
  20. void setup(void){
  21.  
  22. IPAddress Ip(192, 168, 1, 1);
  23. IPAddress NMask(255, 255, 255, 0);
  24. WiFi.softAPConfig(Ip, Ip, NMask);
  25. WiFi.softAP(conf_ssid.c_str());
  26. WiFi.mode(WIFI_AP_STA);
  27. Serial.begin(115200);
  28. delay(1000);
  29. Serial.println("");
  30. Serial.println("Soft AP Started");
  31. Serial.println("");
  32.  
  33. /* Setup the DNS server redirecting all the domains to the Ip */
  34. dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  35. dnsServer.start(DNS_PORT, "www.example.com", Ip);
  36.  
  37. //connectToWifi();
  38. getCredentials(); //Reads Home WiFi credentials from the EEPROM
  39. if (ssid != "" and password != ""){
  40. WiFi.begin(ssid.c_str(), password.c_str());
  41. while (WiFi.status() != WL_CONNECTED) {
  42. delay(500);
  43. Serial.print(".");
  44. }
  45. WiFi.mode(WIFI_AP_STA);
  46. }
  47. server.begin();
  48.  
  49. // When no password is present
  50. if ((ssid == "" and password == "") or (ssid == "Error")){
  51. // ****** MAIN PAGE ******
  52. server.on("/", [](){ /* HANDLES CREDENTIAL INPUT */ });
  53. server.begin();
  54. }
  55. else // If we have ssid and password -- check if we have connection
  56. {
  57. webPage += "<h1>Main Webpage</h1>";
  58. Serial.println("");
  59. Serial.print("Connected to ");
  60. Serial.println(ssid);
  61. Serial.print("IP address: ");
  62. Serial.println(WiFi.localIP());
  63. server.begin();
  64. //if (mdns.begin("esp8266", WiFi.localIP())) {
  65. // Serial.println("MDNS responder started");
  66. //}
  67.  
  68. server.on("/", [](){
  69. server.send(200, "text/html", webPage);
  70. });
  71. Serial.println("HTTP server started");
  72. }
  73. }
  74.  
  75. void loop(void){
  76. dnsServer.processNextRequest();
  77. server.handleClient();
  78. }
Add Comment
Please, Sign In to add comment