diff --git a/boot_hotspot.sh b/boot_hotspot.sh index 6852891..2adbc8f 100755 --- a/boot_hotspot.sh +++ b/boot_hotspot.sh @@ -64,9 +64,18 @@ SSH_TUNNEL_PORT=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT va sleep 20 # IMPORTANT: Always enable WiFi radio at boot (in case it was disabled by power save) -echo "Ensuring WiFi radio is enabled..." -nmcli radio wifi on -sleep 2 +WIFI_RADIO_STATE=$(nmcli radio wifi) +echo "WiFi radio state: $WIFI_RADIO_STATE" + +if [ "$WIFI_RADIO_STATE" == "disabled" ]; then + echo "WiFi radio is disabled, enabling it..." + nmcli radio wifi on + # Wait longer for NetworkManager to scan and reconnect to known networks + echo "Waiting 15 seconds for WiFi to reconnect to known networks..." + sleep 15 +else + echo "WiFi radio is already enabled" +fi # Get the connection state of wlan0 STATE=$(nmcli -g GENERAL.STATE device show wlan0) diff --git a/html/launcher.php b/html/launcher.php index 161305a..42d3036 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -817,22 +817,50 @@ if ($type == "wifi_connect") { $SSID=$_GET['SSID']; $PASS=$_GET['pass']; - echo "will try to connect to "; - echo "SSID: " . $SSID; - echo ""; - echo "Password: " . $PASS; - echo ""; - echo ""; + // Get device name from database for instructions + try { + $db = new PDO("sqlite:$database_path"); + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $stmt = $db->prepare("SELECT value FROM config_table WHERE key = 'deviceName'"); + $stmt->execute(); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + $deviceName = $result ? $result['value'] : 'NebuleAir'; + $db = null; + } catch (PDOException $e) { + $deviceName = 'NebuleAir'; + } - echo "You will be disconnected. If connection is successfull you can find the device on your local network."; - + // Launch connection script in background $script_path = '/var/www/nebuleair_pro_4g/connexion.sh'; $log_file = '/var/www/nebuleair_pro_4g/logs/app.log'; shell_exec("$script_path $SSID $PASS >> $log_file 2>&1 &"); - #$output = shell_exec('sudo nmcli connection down Hotspot'); - #$output2 = shell_exec('sudo nmcli device wifi connect "AirLab" password "123plouf"'); - + // Return JSON response with instructions + header('Content-Type: application/json'); + echo json_encode([ + 'success' => true, + 'ssid' => $SSID, + 'deviceName' => $deviceName, + 'message' => 'Connection attempt started', + 'instructions' => [ + 'fr' => [ + 'title' => 'Connexion en cours...', + 'step1' => "Le capteur tente de se connecter au réseau « $SSID »", + 'step2' => "Vous allez être déconnecté du hotspot dans quelques secondes", + 'step3' => "Reconnectez-vous au WiFi « $SSID » sur votre appareil", + 'step4' => "Accédez au capteur via http://$deviceName.local ou cherchez son IP dans votre routeur", + 'warning' => "Si la connexion échoue, le capteur recréera automatiquement le hotspot" + ], + 'en' => [ + 'title' => 'Connection in progress...', + 'step1' => "The sensor is attempting to connect to network « $SSID »", + 'step2' => "You will be disconnected from the hotspot in a few seconds", + 'step3' => "Reconnect your device to WiFi « $SSID »", + 'step4' => "Access the sensor via http://$deviceName.local or find its IP in your router", + 'warning' => "If connection fails, the sensor will automatically recreate the hotspot" + ] + ] + ]); } diff --git a/html/wifi.html b/html/wifi.html index 32ace1a..dc16978 100755 --- a/html/wifi.html +++ b/html/wifi.html @@ -194,23 +194,104 @@ function get_internet(){ } else { console.log("Will try to connect to "+SSID+" with password "+PASS); console.log("Start PHP script:"); - + + // Close modal + var myModal = bootstrap.Modal.getInstance(document.getElementById('myModal')); + if (myModal) { + myModal.hide(); + } + $.ajax({ url: 'launcher.php?type=wifi_connect&SSID='+SSID+'&pass='+PASS, - dataType: 'text', // Specify that you expect a JSON response - method: 'GET', // Use GET or POST depending on your needs + dataType: 'json', // Changed to JSON + method: 'GET', success: function(response) { console.log(response); - + + // Show connection status message + showConnectionStatus(response); }, error: function(xhr, status, error) { console.error('AJAX request failed:', status, error); + alert('Error: Could not start connection process'); } }); - + } } + function showConnectionStatus(response) { + // Get user language (default to French) + const lang = localStorage.getItem('language') || 'fr'; + const instructions = response.instructions[lang] || response.instructions['fr']; + + // Create overlay with instructions + const overlay = document.createElement('div'); + overlay.id = 'connection-status-overlay'; + overlay.style.cssText = ` + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: rgba(0, 0, 0, 0.9); + z-index: 9999; + display: flex; + align-items: center; + justify-content: center; + color: white; + `; + + overlay.innerHTML = ` +
+ ${lang === 'fr' ? 'Reconnexion à' : 'Reconnecting to'}: + ${response.ssid} +
++ ${lang === 'fr' ? 'Nom du capteur' : 'Sensor name'}: + ${response.deviceName} +
+