Advertisement
RuiViana

Muda_Senha_ESP

Jun 12th, 2017
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. /*
  2.    www.carloskwiek.com.br
  3.    kwiekcinema@gmail.com
  4. */
  5. #include <ESP8266WiFi.h>
  6. #include <ESP8266WebServer.h>
  7. #include <EEPROM.h>
  8.  
  9.  
  10. String pral = "<html>"
  11.               "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>"
  12.               "<title>WIFI CONFIG</title> <style type='text/css'> body,td,th { color: #036; } body { background-color: #999; } </style> </head>"
  13.               "<body> "
  14.               "<h1>WIFI CONF</h1><br>"
  15.               "<form action='config' method='get' target='pantalla'>"
  16.               "<fieldset align='left' style='border-style:solid; border-color:#336666; width:200px; height:180px; padding:10px; margin: 5px;'>"
  17.               "<legend><strong>Configurar WI-FI</strong></legend>"
  18.               "SSID: <br> <input name='ssid' type='text' size='15'/> <br><br>"
  19.               "PASSWORD: <br> <input name='pass' type='password' size='15'/> <br><br>"
  20.               "<input type='submit' value='Comprovar conexao' />"
  21.               "</fieldset>"
  22.               "</form>"
  23.               "<iframe id='pantalla' name='pantalla' src='' width=900px height=400px frameborder='0' scrolling='no'></iframe>"
  24.               "</body>"
  25.               "</html>";
  26.  
  27. ESP8266WebServer server(80);
  28.  
  29. char ssid[20];
  30. char pass[20];
  31. String ssid_leido;
  32. String pass_leido;
  33. int ssid_tamano = 0;
  34. int pass_tamano = 0;
  35.  
  36. String arregla_simbolos(String a) {
  37.   a.replace("%C3%A1", "á");
  38.   a.replace("%C3%A9", "é");
  39.   a.replace("%C3%A", "i");
  40.   a.replace("%C3%B3", "ó");
  41.   a.replace("%C3%BA", "ú");
  42.   a.replace("%21", "!");
  43.   a.replace("%23", "#");
  44.   a.replace("%24", "$");
  45.   a.replace("%25", "%");
  46.   a.replace("%26", "&");
  47.   a.replace("%27", "/");
  48.   a.replace("%28", "(");
  49.   a.replace("%29", ")");
  50.   a.replace("%3D", "=");
  51.   a.replace("%3F", "?");
  52.   a.replace("%27", "'");
  53.   a.replace("%C2%BF", "¿");
  54.   a.replace("%C2%A1", "¡");
  55.   a.replace("%C3%B1", "ñ");
  56.   a.replace("%C3%91", "Ñ");
  57.   a.replace("+", " ");
  58.   a.replace("%2B", "+");
  59.   a.replace("%22", "\"");
  60.   return a;
  61. }
  62. //**** CONFIGURACION WIFI  *******
  63. void wifi_conf()
  64. {
  65.   WiFi.disconnect();
  66.   int cuenta = 0;
  67.  
  68.   String getssid = server.arg("ssid"); //Recibimos los valores que envia por GET el formulario web
  69.   String getpass = server.arg("pass");
  70.   getssid = arregla_simbolos(getssid); //Reemplazamos los simbolos que aparecen cun UTF8 por el simbolo correcto
  71.   getpass = arregla_simbolos(getpass);
  72.  
  73.   ssid_tamano = getssid.length() + 1;  //Calculamos la cantidad de caracteres que tiene el ssid y la clave
  74.   pass_tamano = getpass.length() + 1;
  75.  
  76.   getssid.toCharArray(ssid, ssid_tamano); //Transformamos el string en un char array ya que es lo que nos pide WIFI.begin()
  77.   getpass.toCharArray(pass, pass_tamano);
  78.  
  79.   Serial.println(ssid);     //para depuracion
  80.   Serial.println(pass);
  81.  
  82.   WiFi.begin(ssid, pass);     //Intentamos conectar
  83.   while (WiFi.status() != WL_CONNECTED)
  84.   {
  85.     delay(500);
  86.     Serial.print(";");
  87.     cuenta++;
  88.     if (cuenta > 20)
  89.     {
  90.       graba(70, "noaconfigurado");
  91.       server.send(200, "text/html", String("<h2>Nao realizou a conexion<br>dados não salvos.</h2>"));
  92.       return;
  93.     }
  94.   }
  95.   Serial.print(WiFi.localIP());
  96.   graba(70, "nconfigurado");
  97.   graba(1, getssid);
  98.   graba(30, getpass);
  99.   server.send(200, "text/html", String("<h2>Conexao realizada em: "
  100.                                        + getssid + "<br> Pass ingresado: " + getpass + "<br>Datos correctamente salvos."));
  101. }
  102. //*******  G R A B A R  EN LA  E E P R O M  ***********
  103. void graba(int addr, String a)
  104. {
  105.   int tamano = (a.length() + 1);
  106.   Serial.print(tamano);
  107.   char inchar[30];    //'30' Tamaño maximo del string
  108.   a.toCharArray(inchar, tamano);
  109.   EEPROM.write(addr, tamano);
  110.   for (int i = 0; i < tamano; i++)
  111.   {
  112.     addr++;
  113.     EEPROM.write(addr, inchar[i]);
  114.   }
  115.   EEPROM.commit();
  116. }
  117.  
  118. //*******  L E E R   EN LA  E E P R O M    **************
  119. String lee(int addr)
  120. {
  121.   String nuevoString;
  122.   int valor;
  123.   int tamano = EEPROM.read(addr);
  124.   for (int i = 0; i < tamano; i++)
  125.   {
  126.     addr++;
  127.     valor = EEPROM.read(addr);
  128.     nuevoString += (char)valor;
  129.   }
  130.   return nuevoString;
  131. }
  132.  
  133. //*********  INTENTO DE CONEXION   *********************
  134. void intento_conexion()
  135. {
  136.   if (lee(70).equals("configurado"))
  137.   {
  138.     ssid_leido = lee(1);      //leemos ssid y password
  139.     pass_leido = lee(30);
  140.  
  141.     Serial.println(ssid_leido);  //Para depuracion
  142.     Serial.println(pass_leido);
  143.  
  144.     ssid_tamano = ssid_leido.length() + 1;  //Calculamos la cantidad de caracteres que tiene el ssid y la clave
  145.     pass_tamano = pass_leido.length() + 1;
  146.  
  147.     ssid_leido.toCharArray(ssid, ssid_tamano); //Transf. el String en un char array ya que es lo que nos pide WiFi.begin()
  148.     pass_leido.toCharArray(pass, pass_tamano);
  149.  
  150.     int cuenta = 0;
  151.     WiFi.begin(ssid, pass);      //Intentamos conectar
  152. //    IPAddress subnet(255, 255, 255, 0);                                             // Acrescentei para fixar o IP  12/5/2017
  153. //    WiFi.config(IPAddress(192, 168, 0, 28), IPAddress(192, 168, 0, 1), subnet);     // Idem
  154.     while (WiFi.status() != WL_CONNECTED)
  155.     {
  156.       delay(500);
  157.       cuenta++;
  158.       if (cuenta > 20) {
  159.         Serial.println("Fallo al conectar");
  160.         return;
  161.       }
  162.     }
  163.   }
  164.   if (WiFi.status() == WL_CONNECTED)
  165.   {
  166.     Serial.print("Conexion bem sucedida a: ");
  167.     Serial.println(ssid);
  168.     Serial.println(WiFi.localIP());
  169.   }
  170. }
  171. //*****  S E T U P  **************
  172. void setup()
  173. {
  174.   Serial.begin(115200);
  175.   EEPROM.begin(4096);
  176.   WiFi.softAP("Virus");      //Nombre que se mostrara en las redes wifi
  177.   //WiFi.mode(WIFI_OFF);
  178.   server.on("/", []()
  179.   {
  180.     server.send(200, "text/html", pral);
  181.   });
  182.   server.on("/config", wifi_conf);
  183.   server.begin();
  184.   Serial.println();
  185.   Serial.println("Webserver iniciado...");
  186.  
  187.   Serial.println(lee(70));
  188.   Serial.println(lee(1));
  189.   Serial.println(lee(30));
  190.   intento_conexion();
  191. }
  192. //*****   L O O P   **************
  193. void loop()
  194. {
  195.   server.handleClient();
  196.   delay(2000);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement