Connect esp32 to Wifi

I am trying to connect my esp32 to my wifi connection but it keeps giving that it is trying to connect and it didnt connect,

The internet connection that I have very strong and the router is beside me.

I tried so many solution from google but it didnt work
any suggestion please?

image

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>


// WiFi
const char *ssid = "*****"; // Enter your WiFi name
const char *password = "******";  // Enter WiFi password



// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "esp32/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
 // Set software serial baud to 115200;
 Serial.begin(115200);
 // connecting to a WiFi network
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.println("Connecting to WiFi..");
 }
 Serial.println("Connected to the WiFi network");
 //connecting to a mqtt broker
 client.setServer(mqtt_broker, mqtt_port);
 client.setCallback(callback);
 while (!client.connected()) {
     String client_id = "esp32-client-";
     client_id += String(WiFi.macAddress());
     Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
     if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
         Serial.println("Public emqx mqtt broker connected");
     } else {
         Serial.print("failed with state ");
         Serial.print(client.state());
         delay(2000);
     }
 }
 // publish and subscribe
 client.publish(topic, "Hi EMQX I'm ESP32 ^^");
 client.subscribe(topic);
}

void callback(char *topic, byte *payload, unsigned int length) {
 Serial.print("Message arrived in topic: ");
 Serial.println(topic);
 Serial.print("Message:");
 for (int i = 0; i < length; i++) {
     Serial.print((char) payload[i]);
 }
 Serial.println();
 Serial.println("-----------------------");
}

void loop() {
 client.loop();
}

Presumably your actual sketch has the correct SSID and password

Did you try wifiscan ?

Sure I put them correctly and I tried to connect it to different connections but same problem

Yes and I got my wifi connection name in the scanning

I am trying since two days and I waste much time but unfortunately same problem, I will get insane!!!

OK now try this

#include "WiFi.h"
 
const char* ssid = "yourNetworkName"; //choose your wireless ssid
const char* password =  "yourNetworkPass"; //put your wireless password here.
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); //show ip address when connected on serial monitor.
}
 
void loop() {}

I do the thing this way.

void connectToWiFi()
{
  int TryCount = 0;
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    vTaskDelay( 4000 );
    if ( TryCount == 10 )
    {
      ESP.restart();
    }
  }
  WiFi.onEvent( WiFiEvent );
}

@ZX80 I tried, I got the same
Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

Connecting to WiFi..

@Idahowalker I got the following error:

Compilation error: 'WiFiEvent' was not declared in this scope

Do you know how to solve this error please?

Put a / in front of the offending words.

like this

void connectToWiFi()
{
  int TryCount = 0;
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    vTaskDelay( 4000 );
    if ( TryCount == 10 )
    {
      ESP.restart();
    }
  }
  ////////////////WiFi.onEvent( WiFiEvent );
}

I got nothing in the serial monitor

#include "WiFi.h"
 

const char *SSID = "****"; // Enter your WiFi name
const char *PASSWORD = "****";  // Enter WiFi passwordvoid connectToWiFi()

void connectToWiFi()
{
  int TryCount = 0;
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    vTaskDelay( 4000 );
    if ( TryCount == 10 )
    {
      ESP.restart();
    }
  }
  ////////////////WiFi.onEvent( WiFiEvent );
}

void setup() {
 
  Serial.begin(115200);
 connectToWiFi();
  
}
 
void loop() {}

The serial prints you put in the code did not work? Oh, wait you did not put serial prints to try to troubleshoot your issue. I guess I'll write some debugging code for you. I'll post it as soon as I've written the code. Wait for it.

I found this code @Idahowalker

and I got output:

connect to wifi

1

What does that mean? it didnt count more?





#include <Arduino.h>
#include <WiFi.h>

#define WIFI_NETWORK "***"
#define WIFI_PASSWORD "***"
#define WIFI_TIMEOUT_MS 10000

void WiFiEvent(WiFiEvent_t event)
{
   Serial.println( "[WiFi-event] event: " + event );
  switch (event) {
        case SYSTEM_EVENT_WIFI_READY:
          Serial.println("WiFi interface ready");
          break;
        case SYSTEM_EVENT_SCAN_DONE:
          Serial.println("Completed scan for access points");
          break;
        case SYSTEM_EVENT_STA_START:
          Serial.println("WiFi client started");
          break;
        case SYSTEM_EVENT_STA_STOP:
          Serial.println("WiFi clients stopped");
          break;
    case SYSTEM_EVENT_STA_CONNECTED:
      Serial.println("Connected to access point");
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("Disconnected from WiFi access point");
      break;
        case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
          Serial.println("Authentication mode of access point has changed");
          break;
        case SYSTEM_EVENT_STA_GOT_IP:
          Serial.println("Obtained IP address: " + WiFi.localIP() );
          break;
        case SYSTEM_EVENT_STA_LOST_IP:
          Serial.println("Lost IP address and IP address is reset to 0");
          //      vTaskDelay( 5000 );
          //      ESP.restart();
          break;
        case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
          Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
          break;
        case SYSTEM_EVENT_STA_WPS_ER_FAILED:
          Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
          //      ESP.restart();
          break;
        case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
          Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
          break;
        case SYSTEM_EVENT_STA_WPS_ER_PIN:
          Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
          break;
        case SYSTEM_EVENT_AP_START:
          Serial.println("WiFi access point started");
          break;
        case SYSTEM_EVENT_AP_STOP:
          Serial.println("WiFi access point  stopped");
          //      WiFi.mode( WIFI_OFF);
          //      esp_sleep_enable_timer_wakeup( 1000000 * 2 ); // 1 second times how many seconds wanted
          //      esp_deep_sleep_start();
          break;
        case SYSTEM_EVENT_AP_STACONNECTED:
          Serial.println("Client connected");
          break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:
      Serial.println("WiFi client disconnected");
          break;
        case SYSTEM_EVENT_AP_STAIPASSIGNED:
          Serial.println("Assigned IP address to client");
          break;
        case SYSTEM_EVENT_AP_PROBEREQRECVED:
          Serial.println("Received probe request");
          break;
        case SYSTEM_EVENT_GOT_IP6:
          Serial.println("IPv6 is preferred");
          break;
        case SYSTEM_EVENT_ETH_GOT_IP:
          Serial.println("Obtained IP address");
          break;
    default: break;
  }
}

void connectToWiFi()
{
  int TryCount = 0;
  Serial.println( "connect to wifi" );
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
Serial.println( TryCount);
    delay(4000);
    if ( TryCount == 10 )
    {
      ESP.restart();
    }
  }
  WiFi.onEvent(WiFiEvent);
}

void setup()
{
    Serial.begin(115200);
    connectToWiFi();
}

void loop()
{
}

I tried another esp32 board it counts to 10 and then restart, but the first one only counts to 1
What does that mean?
@Idahowalker

why would you want it to count to more than 1?

What's the count for?

Notice how the count is incremented each time the loop tries to make a WiFI connection? Notice how if it gets to 10 it causes a reset? It only counts to 10 when it cannot connect.

It only counts to 1 because?

A, a connection is made.
B, no connection is made
C, clueless

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.