diff --git a/boot_hotspot.sh b/boot_hotspot.sh index fd00f17..b30ae68 100755 --- a/boot_hotspot.sh +++ b/boot_hotspot.sh @@ -43,12 +43,19 @@ echo "getting RPI serial number" # Get the last 8 characters of the serial number and write to text file serial_number=$(cat /proc/cpuinfo | grep Serial | awk '{print substr($3, length($3) - 7)}') -# update Sqlite database +# update Sqlite database (only if not already set, i.e., still has default value 'XXXX') echo "Updating SQLite database with device ID: $serial_number" -sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='$serial_number' WHERE key='deviceID';" +sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='$serial_number' WHERE key='deviceID' AND value='XXXX';" echo "id: $serial_number" +# Get deviceID from SQLite config_table (may be different from serial_number if manually configured) +DEVICE_ID=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT value FROM config_table WHERE key='deviceID'") +echo "Device ID from database: $DEVICE_ID" + +# Get deviceName from SQLite config_table for use in hotspot SSID +DEVICE_NAME=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT value FROM config_table WHERE key='deviceName'") +echo "Device Name from database: $DEVICE_NAME" # Get SSH tunnel port from SQLite config_table SSH_TUNNEL_PORT=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT value FROM config_table WHERE key='sshTunnel_port'") @@ -65,9 +72,9 @@ if [ "$STATE" == "30 (disconnected)" ]; then # Perform a wifi scan and save its output to a csv file # nmcli device wifi list nmcli -f SSID,SIGNAL,SECURITY device wifi list | awk 'BEGIN { OFS=","; print "SSID,SIGNAL,SECURITY" } NR>1 { print $1,$2,$3 }' > "$OUTPUT_FILE" - # Start the hotspot - echo "Starting hotspot..." - sudo nmcli device wifi hotspot ifname wlan0 ssid nebuleair_pro password nebuleaircfg + # Start the hotspot with SSID based on deviceName + echo "Starting hotspot with SSID: $DEVICE_NAME" + sudo nmcli device wifi hotspot ifname wlan0 ssid "$DEVICE_NAME" password nebuleaircfg # Update SQLite to reflect hotspot mode sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='hotspot' WHERE key='WIFI_status'" diff --git a/connexion.sh b/connexion.sh index 3ec8f86..d72d945 100755 --- a/connexion.sh +++ b/connexion.sh @@ -2,26 +2,49 @@ echo "-------" echo "Start connexion shell script at $(date)" +# Get deviceName from database for hotspot SSID +DEVICE_NAME=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT value FROM config_table WHERE key='deviceName'") +echo "Device Name: $DEVICE_NAME" -#disable hotspot -echo "Disable Hotspot:" -sudo nmcli connection down Hotspot -sleep 10 +# Find and disable any active hotspot connection +echo "Disable Hotspot..." +# Get all wireless connections that are currently active (excludes the target WiFi) +ACTIVE_HOTSPOT=$(nmcli -t -f NAME,TYPE,DEVICE connection show --active | grep ':802-11-wireless:wlan0' | cut -d: -f1) + +if [ -n "$ACTIVE_HOTSPOT" ]; then + echo "Disabling hotspot connection: $ACTIVE_HOTSPOT" + sudo nmcli connection down "$ACTIVE_HOTSPOT" +else + echo "No active hotspot found" +fi + +sleep 5 echo "Start connection with:" echo "SSID: $1" -echo "Password: $2" +echo "Password: [HIDDEN]" sudo nmcli device wifi connect "$1" password "$2" -#check if connection is successfull +# Check if connection is successful if [ $? -eq 0 ]; then - echo "Connection to $1 is successfull" + echo "Connection to $1 is successful" + + # Update SQLite to reflect connected status + sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='connected' WHERE key='WIFI_status'" + echo "Updated database: WIFI_status = connected" else echo "Connection to $1 failed" echo "Restarting hotspot..." - #enable hotspot - sudo nmcli connection up Hotspot + + # Recreate hotspot with current deviceName as SSID + sudo nmcli device wifi hotspot ifname wlan0 ssid "$DEVICE_NAME" password nebuleaircfg + + # Update SQLite to reflect hotspot mode + sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='hotspot' WHERE key='WIFI_status'" + echo "Updated database: WIFI_status = hotspot" + echo "Hotspot restarted with SSID: $DEVICE_NAME" fi + echo "End connexion shell script" echo "-------" diff --git a/html/launcher.php b/html/launcher.php index b6de08b..7c0c0da 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -792,33 +792,39 @@ if ($type == "wifi_connect") { if ($type == "wifi_scan") { - // Set the path to your CSV file - $csvFile = '/var/www/nebuleair_pro_4g/wifi_list.csv'; - + // Perform live WiFi scan instead of reading stale CSV file + $output = shell_exec('nmcli -f SSID,SIGNAL,SECURITY device wifi list ifname wlan0 2>/dev/null'); + // Initialize an array to hold the JSON data $jsonData = []; - - // Open the CSV file for reading - if (($handle = fopen($csvFile, 'r')) !== false) { - // Get the headers from the first row - $headers = fgetcsv($handle); - - // Loop through the rest of the rows - while (($row = fgetcsv($handle)) !== false) { - // Combine headers with row data to create an associative array - $jsonData[] = array_combine($headers, $row); + + if ($output) { + // Split the output into lines + $lines = explode("\n", trim($output)); + + // Skip the header line and process each network + for ($i = 1; $i < count($lines); $i++) { + $line = trim($lines[$i]); + if (empty($line)) continue; + + // Split by multiple spaces (nmcli uses column formatting) + $parts = preg_split('/\s{2,}/', $line, 3); + + if (count($parts) >= 2) { + $jsonData[] = [ + 'SSID' => trim($parts[0]), + 'SIGNAL' => trim($parts[1]), + 'SECURITY' => isset($parts[2]) ? trim($parts[2]) : '--' + ]; + } } - - // Close the file handle - fclose($handle); } - + // Set the content type to JSON header('Content-Type: application/json'); - + // Convert the array to JSON format and output it echo json_encode($jsonData, JSON_PRETTY_PRINT); - }