Advertisement
RuiViana

Muda_Senha_ESP

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