Hi everyone,
I'm having trouble connecting my Raspberry Pi 3B+ to a mobile hotspot over Wi-Fi. I’m using Raspberry Pi OS (Linux), and here’s the issue:
When I configure the hotspot to 2.4 GHz, the Raspberry Pi fails to connect.
When I switch the hotspot to 5 GHz, the Pi connects immediately without issues.
The Pi connects just fine to 2.4 GHz networks from regular routers, so the problem seems to happen only when using a mobile hotspot in 2.4 GHz mode.
I'm using the following custom Python code to manage the Wi-Fi connection programmatically:
# Check Internet connection
def verificar_internet(host="8.8.8.8", port=53, timeout=5):
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception:
return False
# Connect to Wi-Fi
def conectar_wifi(ssid, password):
wpa_supplicant_path = "/etc/wpa_supplicant/wpa_supplicant.conf"
config = f"""ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=AR
network={{
ssid="{ssid}"
psk="{password}"
key_mgmt=WPA-PSK
scan_ssid=1
}}
"""
# Write temporary file
with open("/tmp/wpa_supplicant.conf", "w") as f:
f.write(config)
# Copy config file and set permissions
try:
subprocess.run(["sudo", "cp", "/tmp/wpa_supplicant.conf", wpa_supplicant_path], check=True)
subprocess.run(["sudo", "chown", "root:root", wpa_supplicant_path], check=True)
subprocess.run(["sudo", "chmod", "600", wpa_supplicant_path], check=True)
except subprocess.CalledProcessError as e:
print(f"
Error copying or setting permissions: {e}")
return False
# Restart wpa_supplicant
try:
subprocess.run(["sudo", "systemctl", "restart", "wpa_supplicant"], check=True)
time.sleep(2)
except subprocess.CalledProcessError as e:
print(f"
Error restarting wpa_supplicant: {e}")
return False
# Restart wlan0
try:
subprocess.run(["sudo", "ip", "link", "set", "wlan0", "down"], check=True)
time.sleep(3)
subprocess.run(["sudo", "ip", "link", "set", "wlan0", "up"], check=True)
time.sleep(5)
except subprocess.CalledProcessError as e:
print(f"
Error restarting wlan0: {e}")
return False
# Force DHCP
try:
subprocess.run(["sudo", "dhclient", "-r", "wlan0"])
subprocess.run(["sudo", "dhclient", "wlan0"], check=True)
except subprocess.CalledProcessError as e:
print(f"
Error requesting DHCP: {e}")
return False
print("
Attempting to connect to Wi-Fi...")
# Check Wi-Fi connection (timeout ~24s)
for i in range(12):
result = subprocess.run("iwgetid -r", shell=True, text=True, capture_output=True)
connected_ssid = result.stdout.strip()
if connected_ssid == ssid:
print(f"
Connected to Wi-Fi '{ssid}'")
if verificar_internet():
print("
Internet connection verified.")
return True
else:
print("
Connected to Wi-Fi but no Internet access.")
return False
print(f"
Waiting for connection... ({i+1}/12)")
time.sleep(2)
print("
Failed to connect to the Wi-Fi network.")
return False
This script works perfectly with router-based networks and 5 GHz hotspots, but always fails with 2.4 GHz mobile hotspots.
Any ideas on why this might be happening or how to fix or debug it? Could this be related to country settings, power saving mode, hotspot compatibility, or something else?
Thanks in advance for any help or suggestions!
I'm having trouble connecting my Raspberry Pi 3B+ to a mobile hotspot over Wi-Fi. I’m using Raspberry Pi OS (Linux), and here’s the issue:
When I configure the hotspot to 2.4 GHz, the Raspberry Pi fails to connect.
When I switch the hotspot to 5 GHz, the Pi connects immediately without issues.
The Pi connects just fine to 2.4 GHz networks from regular routers, so the problem seems to happen only when using a mobile hotspot in 2.4 GHz mode.
I'm using the following custom Python code to manage the Wi-Fi connection programmatically:
# Check Internet connection
def verificar_internet(host="8.8.8.8", port=53, timeout=5):
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception:
return False
# Connect to Wi-Fi
def conectar_wifi(ssid, password):
wpa_supplicant_path = "/etc/wpa_supplicant/wpa_supplicant.conf"
config = f"""ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=AR
network={{
ssid="{ssid}"
psk="{password}"
key_mgmt=WPA-PSK
scan_ssid=1
}}
"""
# Write temporary file
with open("/tmp/wpa_supplicant.conf", "w") as f:
f.write(config)
# Copy config file and set permissions
try:
subprocess.run(["sudo", "cp", "/tmp/wpa_supplicant.conf", wpa_supplicant_path], check=True)
subprocess.run(["sudo", "chown", "root:root", wpa_supplicant_path], check=True)
subprocess.run(["sudo", "chmod", "600", wpa_supplicant_path], check=True)
except subprocess.CalledProcessError as e:
print(f"
return False
# Restart wpa_supplicant
try:
subprocess.run(["sudo", "systemctl", "restart", "wpa_supplicant"], check=True)
time.sleep(2)
except subprocess.CalledProcessError as e:
print(f"
return False
# Restart wlan0
try:
subprocess.run(["sudo", "ip", "link", "set", "wlan0", "down"], check=True)
time.sleep(3)
subprocess.run(["sudo", "ip", "link", "set", "wlan0", "up"], check=True)
time.sleep(5)
except subprocess.CalledProcessError as e:
print(f"
return False
# Force DHCP
try:
subprocess.run(["sudo", "dhclient", "-r", "wlan0"])
subprocess.run(["sudo", "dhclient", "wlan0"], check=True)
except subprocess.CalledProcessError as e:
print(f"
return False
print("
# Check Wi-Fi connection (timeout ~24s)
for i in range(12):
result = subprocess.run("iwgetid -r", shell=True, text=True, capture_output=True)
connected_ssid = result.stdout.strip()
if connected_ssid == ssid:
print(f"
if verificar_internet():
print("
return True
else:
print("
return False
print(f"
time.sleep(2)
print("
return False
This script works perfectly with router-based networks and 5 GHz hotspots, but always fails with 2.4 GHz mobile hotspots.
Any ideas on why this might be happening or how to fix or debug it? Could this be related to country settings, power saving mode, hotspot compatibility, or something else?
Thanks in advance for any help or suggestions!
Statistics: Posted by HectorB25rasp — Wed Aug 06, 2025 11:47 am