Post topics, source code that relate to the Arduino Platform

User avatar
By aguzcaro
#50966 I have a code that is a mix of diferent codes that i found in the web. I add a function called "setupPASS();" that get a password entered by the client in AP mode. Then the program switch to STA mode to conect to the LAN with this password but the WiFi.begin(ssid, pass); didnt work and stays in the following loop:

while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(5000);
}

any idea of the problem??




here is the complete code:


#include <ESP8266WiFi.h>

const char* ssid = "SFR-b938"; // Aqui van vuestros datos
const char* password = "VH5TPG9P17CX";
const char WiFiAPPSK[] = "sparkfun";
char pass[30];
int lon;

WiFiServer server(80);



void setup() {

Serial.begin(9600);
delay(1500);
setupWiFi();

server.begin();




delay(1500);
WiFi.softAPdisconnect(true);
//WiFi.mode(WIFI_STA);
delay(1500);
WiFi.disconnect();
Serial.println("a ver cheeeee...");

// void(*Reset)(void) = 0;

pinMode(16, OUTPUT);
digitalWrite(16, HIGH);

Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);



while (WiFi.status() != WL_CONNECTED)
{
Serial.println("intentandoooo....");
WiFi.begin(ssid, pass);
delay(5000);
delay(5000);
}

Serial.println("WiFi connected");


//server.begin(); // Iniciamos el servidor
Serial.println("Server started");
Serial.println(WiFi.localIP()); // Imprimimos la IP

}



void loop() {

WiFiClient client = server.available();
if (!client) {
Serial.println("esperando cliente...");
delay(1000);
return;
}

String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();

int val;
if (req.indexOf("/gpio/0") != -1){

val = 0;}
else if (req.indexOf("/gpio/1") != -1){
val = 1;}
else {
Serial.println("req invalida");
return;
}

digitalWrite(16,val);
client.flush();

client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html><html lang='es'><head><title>luz cocina</title></head>");
client.print("<body><div id='pete'><b>LUZ COCINA</b></div></body></html>");

delay(1);

Serial.println("ready");

}




void setupWiFi()
{
WiFi.mode(WIFI_AP);

// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;

char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);

for (int i=0; i<AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);

WiFi.softAP(AP_NameChar, WiFiAPPSK);


}



void setupPASS()
{

inicio:
WiFiClient client = server.available();
if (!client) {
Serial.println("esperando cliente en setup...");
delay(1000);
goto inicio;
}

String pet = client.readStringUntil('\r');
Serial.println(pet);
client.flush();
Serial.println(pet.length());
int lon = pet.length()-15;
int fin = lon+5;

for(int n=5; n<fin+1; n++){
pass[n-5]=pet.charAt(n);
}


Serial.println(pass);

client.flush();
Serial.println("chauuu");

delay(1000);
delay(1000);
delay(1000);

}