WiFi does not want to connect ESP32 on IDE ARDUINO

manbomb
Posts: 1
Joined: Thu Dec 26, 2019 1:31 pm

WiFi does not want to connect ESP32 on IDE ARDUINO

Postby manbomb » Thu Dec 26, 2019 1:38 pm

Below is the code I created to configure, store and connect to wifi networks via serial bluetooth and WiFi. But it does not connect, try 5 times and can not connect.

Code: Select all

#include <EEPROM.h>
#include <WiFi.h>
#include <BluetoothSerial.h>

#define EEPROM_SIZE 512
#define led_verde 5
#define led_verme 4
#define jumper 15

TaskHandle_t pisca_verde;
//TaskHandle_t inter_verde;
TaskHandle_t pisca_verme;
//TaskHandle_t inter_verme;

void pisca_verme_fun( void * parameter ) {
  while(true) {
  digitalWrite(led_verme,HIGH);
  delay(500);
  digitalWrite(led_verme,LOW);
  delay(500);
  }
}

void pisca_verde_fun( void * parameter ) {
  while(true) {
  digitalWrite(led_verde,HIGH);
  delay(500);
  digitalWrite(led_verde,LOW);
  delay(500);
  }
}

/*void inter_verme_fun( void * parameter ) {
  while(true) {
  digitalWrite(led_verme,HIGH);  
  }
}

void inter_verde_fun( void * parameter ) {
  while(true) {
  digitalWrite(led_verde,HIGH);  
  }
}*/

bool write_eeprom(int ini, String text) {
  bool res = false;
  int tam = text.length();

  EEPROM.write(ini,tam);
  
  if (ini >= 0 && tam < EEPROM_SIZE) {
      for (int i = ini+1 ; i <= ini+tam ; i++) {
        char c = text.charAt(i-1-ini);
        EEPROM.write(i,c);
        res = true;
      }
    }
    return res;
}

String read_eeprom(int ini) {
  String search;
  if(ini >= 0 && ini < EEPROM_SIZE) {
    int tam = EEPROM.read(ini);
    for (int i = ini+1; i <= ini+tam; i++) {
      int c = EEPROM.read(i);
      search += char(c);
    }
  } 
  return search;
}

bool conecta_wifi_eeprom() {
  digitalWrite(led_verde,LOW);
  
  Serial.println("USER: "+read_eeprom(0));
  Serial.println("PASS: "+read_eeprom(34));

  int attem = 0;

  String ssid_buff = read_eeprom(0);
  String pass_buff = read_eeprom(34);

  xTaskCreate (
    pisca_verme_fun,
    "pisca_led_verme",
    10000,
    NULL,
    1,
    &pisca_verme
  );

  while(attem < 5 && digitalRead(jumper) != 0) {

    WiFi.disconnect();

    WiFi.begin(ssid_buff.c_str(),pass_buff.c_str());
  
    Serial.print("Conectando...");
  
    int t = 0;
    
    while(WiFi.status() != WL_CONNECTED && t < 75 && digitalRead(jumper) != 0) {
      Serial.print(".");
      delay(500);
      t++;
    }
  
    if(WiFi.status() == WL_CONNECTED) {
      Serial.println("Conectado!");
      return true;
    } else {
      Serial.println("Erro na conexão! - "+String(WiFi.status())+" - "+String(attem));
      attem = attem+1;
    }
  
  }
  vTaskDelete(pisca_verme);
  digitalWrite(led_verme,HIGH);
  Serial.println("esperando");
  while(digitalRead(jumper) != 0) {}
  digitalWrite(led_verme,LOW);
  ESP.restart();
}

bool config_bluet_eeprom() {
  digitalWrite(led_verme,LOW);
  
  BluetoothSerial ESP_BT;
  ESP_BT.begin("AutoHouse - conf.");
  Serial.println("BLE ligado");

  xTaskCreate (
    pisca_verde_fun,
    "pisca_led_verde",
    10000,
    NULL,
    1,
    &pisca_verde
  );

  int buff;
  
  for(int t = 0; t < 3001; t++) {
    String text = "";
    while(ESP_BT.available()) {
      buff = ESP_BT.read();
      text += char(buff);
      delay(20);
    }
    if (text != "") { 
      Serial.println(text); // format: ssid;pass
      ESP_BT.println(text); 

      int pos_corte = text.indexOf(";");
      String ssid = text.substring(0,pos_corte);
      String pass = text.substring(pos_corte+1,text.length()-1);

      Serial.println(ssid);
      Serial.println(pass);

      write_eeprom(0,ssid);
      write_eeprom(34,pass);

      EEPROM.commit();
      delay(100);
      break;
      
    }
    delay(20);
  }
  Serial.println("esperando");
  vTaskDelete(pisca_verde);
  digitalWrite(led_verde,HIGH);
  while(digitalRead(jumper) == 0) { delay(100); }
  /*digitalWrite(led_verde,LOW);*/
  ESP.restart();
}

void setup() {
  pinMode(jumper,INPUT_PULLUP);
  pinMode(led_verme,OUTPUT);
  pinMode(led_verde,OUTPUT);

  EEPROM.begin(EEPROM_SIZE);
  Serial.begin(115200);

  if(digitalRead(jumper) == 0) {
    config_bluet_eeprom();
  } else {
    conecta_wifi_eeprom();
  }

}

void loop() {
  delay(1000);

}
Serial monitor:

Code: Select all

USER: ***********
PASS: *******

Conectando..............................................................................Erro na conexão! - 6 - 0
Conectando..............................................................................Erro na conexão! - 6 - 1
Conectando..............................................................................Erro na conexão! - 6 - 2
Conectando..............................................................................Erro na conexão! - 6 - 3
Conectando..............................................................................Erro na conexão! - 6 - 4
esperando


peterT13
Posts: 1
Joined: Fri Jan 31, 2020 6:38 am

Re: WiFi does not want to connect ESP32 on IDE ARDUINO

Postby peterT13 » Sat Feb 01, 2020 10:59 pm

I had a similar problem and found it to be a bug in the latest release of ESP32. That is, with version 1.0.4 it wouldn't work but if I rolled back to version 1.0.3 it worked perfectly. Curiously with v1.0.4 if I ran WiFi as an access point it worked but it wouldn't work as a client.

If you are using ESP32 release 1.0.4, can I suggest you go to board manager, select ESP32 and remove the current version then reinstall version 1.0.3. After you have reinstalled V1.0.3 you'll need to reselect your board type.

Maybe your circumstances are different but doing this worked for me.

Who is online

Users browsing this forum: Bing [Bot] and 83 guests