Esp not connecting to wifi

#include <ESP32Servo.h>
#include <WiFi.h>
#include <WebSocketsServer.h>
 
#define TRIG_PIN 32
#define ECHO_PIN 33
#define BUZZER_PIN 15
#define SERVO_PIN 18
 
const char* ssid = "Yotam’s iPhone";
const char* password = "12345678";
 
Servo myServo;
WebSocketsServer webSocket(81);
float distanceArray[181] = {0};
 
void setup() {
    Serial.begin(115200);
    pinMode(TRIG_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);
    pinMode(BUZZER_PIN, OUTPUT);
 
    connectWiFi();
 
    webSocket.begin();
    Serial.println("WebSocket server started on port 81");
 
    webSocket.onEvent(webSocketEvent);
 
    myServo.setPeriodHertz(50);
    myServo.attach(SERVO_PIN, 1000, 2000);
    myServo.write(90);
 
    testSensor();
    testServo();
    Serial.print("ESP32 IP Address: ");
    Serial.println(WiFi.localIP());
}
 
void connectWiFi() {
    // Static IP configuration - tailored for phone hotspot
    IPAddress local_IP(172, 20, 10, 6);  // This will be the ESP32's IP on the hotspot
    IPAddress gateway(172, 20, 10, 1);     // Most phones use this gateway for hotspots
    IPAddress subnet(255, 255, 255, 0);     // Standard subnet mask
 
    Serial.print("Connecting to WiFi: ");
    Serial.println(ssid);
 
    // Apply static IP configuration
    if (!WiFi.config(local_IP, gateway, subnet)) {
        Serial.println("⚠️ Failed to configure Static IP!");
    }
 
    WiFi.begin(ssid, password);
 
    int retry = 0;
    while (WiFi.status() != WL_CONNECTED && retry < 30) {  
        delay(500);
        Serial.print(".");
        retry++;
    }
 
    if (WiFi.status() == WL_CONNECTED) {
        Serial.println("\n✅ WiFi Connected!");
        Serial.print("📡 ESP32 Static IP: ");
        Serial.println(WiFi.localIP());
    } else {
        Serial.println("\n❌ WiFi Connection Failed. Restarting...");
        delay(2000);
        ESP.restart();
    }
}
 
void testSensor() {
    Serial.println("Testing ultrasonic sensor...");
    for (int i = 0; i < 5; i++) {
        float dist = getDistance();
        Serial.print("Distance: ");
        Serial.println(dist);
        delay(1000);
    }
}
 
void testServo() {
    Serial.println("Testing servo...");
    myServo.write(90);
    delay(1000);
    myServo.write(0);
    delay(1000);
    myServo.write(180);
    delay(1000);
    myServo.write(90);
}
 
float getDistance() {
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
 
    long duration = pulseIn(ECHO_PIN, HIGH, 60000);
    if (duration == 0) {
        Serial.println("Sensor timeout");
        return 999;
    }
    float distance = duration * 0.0343 / 2;
    return distance;
}
 
void loop() {
    float newDistanceArray[181] = {0};
    webSocket.loop();
 
    for (int pos = 0; pos <= 180; pos += 5) { 
        myServo.write(pos);
        delay(300);
 
        float dist = getDistance();
        if (dist != 999) {
            newDistanceArray[pos] = dist;
            sendWebData(pos, dist);
        }
    }
 
    for (int pos = 180; pos >= 0; pos -= 5) {
        myServo.write(pos);
        delay(300);
 
        float dist = getDistance();
        if (dist != 999) {
            newDistanceArray[pos] = dist;
            sendWebData(pos, dist);
        }
    }
 
    for (int i = 0; i <= 180; i += 5) {
        if (abs(newDistanceArray[i] - distanceArray[i]) > 20 && newDistanceArray[i] < 999) {
            Serial.println("INTRUDER DETECTED!");
            digitalWrite(BUZZER_PIN, HIGH);
            delay(1000);
            digitalWrite(BUZZER_PIN, LOW);
            break;
        }
    }
 
    memcpy(distanceArray, newDistanceArray, sizeof(distanceArray));
    delay(2000);
}
 
void sendWebData(int angle, float distance) {
    String message = "{\"angle\": " + String(angle) + ", \"distance\": " + String(distance) + "}";
    Serial.println("Sending data: " + message);
    webSocket.broadcastTXT(message);
}
 
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
    if (type == WStype_CONNECTED) {
        Serial.println("Client Connected!");
    } else if (type == WStype_TEXT) {
        Serial.print("Received: ");
        Serial.println((char*)payload);
    }
}

the output is always ❌ WiFi Connection Failed. Restarting..., does anyone know why?

According to this reference the options are:

Hi @ariealzeev.

Is the phone configured to produce a 2.4 GHz hotspot, or a 5 GHz hotspot?:

https://it-training.apple.com/tutorials/support/sup040/#:~:text=On%20iPhone%2012%20or%20later%2C%20you%20can%20turn%20on%20Maximize%20Compatibility%20for%20Personal%20Hotspot%20to%20use%20a%202.4GHz%20connection

The ESP32 can only connect to 2.4 GHz access points, so if the iPhone is producing a 5 GHz hotspot then this would explain why the ESP32 can't connect.

I think it is correct for the ESP32 WiFi library:

1 Like

Thank you for the correction.

1 Like