wifi hotspot improvements
This commit is contained in:
@@ -64,9 +64,18 @@ SSH_TUNNEL_PORT=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT va
|
|||||||
sleep 20
|
sleep 20
|
||||||
|
|
||||||
# IMPORTANT: Always enable WiFi radio at boot (in case it was disabled by power save)
|
# IMPORTANT: Always enable WiFi radio at boot (in case it was disabled by power save)
|
||||||
echo "Ensuring WiFi radio is enabled..."
|
WIFI_RADIO_STATE=$(nmcli radio wifi)
|
||||||
nmcli radio wifi on
|
echo "WiFi radio state: $WIFI_RADIO_STATE"
|
||||||
sleep 2
|
|
||||||
|
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
|
# Get the connection state of wlan0
|
||||||
STATE=$(nmcli -g GENERAL.STATE device show wlan0)
|
STATE=$(nmcli -g GENERAL.STATE device show wlan0)
|
||||||
|
|||||||
@@ -817,22 +817,50 @@ if ($type == "wifi_connect") {
|
|||||||
$SSID=$_GET['SSID'];
|
$SSID=$_GET['SSID'];
|
||||||
$PASS=$_GET['pass'];
|
$PASS=$_GET['pass'];
|
||||||
|
|
||||||
echo "will try to connect to </br>";
|
// Get device name from database for instructions
|
||||||
echo "SSID: " . $SSID;
|
try {
|
||||||
echo "</br>";
|
$db = new PDO("sqlite:$database_path");
|
||||||
echo "Password: " . $PASS;
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
echo "</br>";
|
$stmt = $db->prepare("SELECT value FROM config_table WHERE key = 'deviceName'");
|
||||||
echo "</br>";
|
$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';
|
$script_path = '/var/www/nebuleair_pro_4g/connexion.sh';
|
||||||
$log_file = '/var/www/nebuleair_pro_4g/logs/app.log';
|
$log_file = '/var/www/nebuleair_pro_4g/logs/app.log';
|
||||||
shell_exec("$script_path $SSID $PASS >> $log_file 2>&1 &");
|
shell_exec("$script_path $SSID $PASS >> $log_file 2>&1 &");
|
||||||
|
|
||||||
#$output = shell_exec('sudo nmcli connection down Hotspot');
|
// Return JSON response with instructions
|
||||||
#$output2 = shell_exec('sudo nmcli device wifi connect "AirLab" password "123plouf"');
|
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"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -194,23 +194,104 @@ function get_internet(){
|
|||||||
} else {
|
} else {
|
||||||
console.log("Will try to connect to "+SSID+" with password "+PASS);
|
console.log("Will try to connect to "+SSID+" with password "+PASS);
|
||||||
console.log("Start PHP script:");
|
console.log("Start PHP script:");
|
||||||
|
|
||||||
|
// Close modal
|
||||||
|
var myModal = bootstrap.Modal.getInstance(document.getElementById('myModal'));
|
||||||
|
if (myModal) {
|
||||||
|
myModal.hide();
|
||||||
|
}
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: 'launcher.php?type=wifi_connect&SSID='+SSID+'&pass='+PASS,
|
url: 'launcher.php?type=wifi_connect&SSID='+SSID+'&pass='+PASS,
|
||||||
dataType: 'text', // Specify that you expect a JSON response
|
dataType: 'json', // Changed to JSON
|
||||||
method: 'GET', // Use GET or POST depending on your needs
|
method: 'GET',
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
|
|
||||||
|
// Show connection status message
|
||||||
|
showConnectionStatus(response);
|
||||||
},
|
},
|
||||||
error: function(xhr, status, error) {
|
error: function(xhr, status, error) {
|
||||||
console.error('AJAX request failed:', 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 = `
|
||||||
|
<div style="max-width: 600px; padding: 40px; text-align: center;">
|
||||||
|
<div class="spinner-border text-primary mb-4" role="status" style="width: 4rem; height: 4rem;">
|
||||||
|
<span class="visually-hidden">Loading...</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2 class="mb-4">${instructions.title}</h2>
|
||||||
|
|
||||||
|
<div class="alert alert-info text-start" role="alert">
|
||||||
|
<ol class="mb-0" style="padding-left: 20px;">
|
||||||
|
<li class="mb-2"><strong>${instructions.step1}</strong></li>
|
||||||
|
<li class="mb-2">${instructions.step2}</li>
|
||||||
|
<li class="mb-2">${instructions.step3}</li>
|
||||||
|
<li class="mb-2">${instructions.step4}</li>
|
||||||
|
</ol>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="alert alert-warning text-start" role="alert">
|
||||||
|
<strong>⚠️ Important:</strong> ${instructions.warning}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<p class="text-muted">
|
||||||
|
${lang === 'fr' ? 'Reconnexion à' : 'Reconnecting to'}:
|
||||||
|
<strong class="text-white">${response.ssid}</strong>
|
||||||
|
</p>
|
||||||
|
<p class="text-muted">
|
||||||
|
${lang === 'fr' ? 'Nom du capteur' : 'Sensor name'}:
|
||||||
|
<strong class="text-white">${response.deviceName}</strong>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<small class="text-muted">
|
||||||
|
${lang === 'fr' ? 'Cette fenêtre va se fermer automatiquement...' : 'This window will close automatically...'}
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
document.body.appendChild(overlay);
|
||||||
|
|
||||||
|
// Auto-close after 30 seconds (gives time to read)
|
||||||
|
setTimeout(() => {
|
||||||
|
if (document.getElementById('connection-status-overlay')) {
|
||||||
|
document.getElementById('connection-status-overlay').remove();
|
||||||
|
}
|
||||||
|
}, 30000);
|
||||||
|
}
|
||||||
|
|
||||||
function wifi_scan(){
|
function wifi_scan(){
|
||||||
console.log("Scanning Wifi");
|
console.log("Scanning Wifi");
|
||||||
$.ajax({
|
$.ajax({
|
||||||
|
|||||||
Reference in New Issue
Block a user