Fix WiFi setup flow and improve hotspot management

- Fix deviceID overwrite bug: preserve manual configuration across reboots
- Use deviceName as hotspot SSID for better device identification
- Implement live WiFi scanning instead of reading stale CSV data
- Improve hotspot management with dynamic connection detection
- Add database status updates on WiFi connection success/failure
- Hide password in logs for better security

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
PaulVua
2026-01-05 17:27:37 +01:00
parent a38ce79555
commit 1f4d38257e
3 changed files with 69 additions and 33 deletions

View File

@@ -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'"

View File

@@ -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 "-------"

View File

@@ -792,25 +792,32 @@ 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);
if ($output) {
// Split the output into lines
$lines = explode("\n", trim($output));
// 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);
// 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
@@ -819,7 +826,6 @@ if ($type == "wifi_scan") {
// Convert the array to JSON format and output it
echo json_encode($jsonData, JSON_PRETTY_PRINT);
}
if ($type == "wifi_scan_old") {