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:
@@ -43,12 +43,19 @@ echo "getting RPI serial number"
|
|||||||
# Get the last 8 characters of the serial number and write to text file
|
# 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)}')
|
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"
|
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"
|
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
|
# 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'")
|
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
|
# Perform a wifi scan and save its output to a csv file
|
||||||
# nmcli device wifi list
|
# 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"
|
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
|
# Start the hotspot with SSID based on deviceName
|
||||||
echo "Starting hotspot..."
|
echo "Starting hotspot with SSID: $DEVICE_NAME"
|
||||||
sudo nmcli device wifi hotspot ifname wlan0 ssid nebuleair_pro password nebuleaircfg
|
sudo nmcli device wifi hotspot ifname wlan0 ssid "$DEVICE_NAME" password nebuleaircfg
|
||||||
|
|
||||||
# Update SQLite to reflect hotspot mode
|
# 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'"
|
sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='hotspot' WHERE key='WIFI_status'"
|
||||||
|
|||||||
41
connexion.sh
41
connexion.sh
@@ -2,26 +2,49 @@
|
|||||||
echo "-------"
|
echo "-------"
|
||||||
echo "Start connexion shell script at $(date)"
|
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
|
# Find and disable any active hotspot connection
|
||||||
echo "Disable Hotspot:"
|
echo "Disable Hotspot..."
|
||||||
sudo nmcli connection down Hotspot
|
# Get all wireless connections that are currently active (excludes the target WiFi)
|
||||||
sleep 10
|
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 "Start connection with:"
|
||||||
echo "SSID: $1"
|
echo "SSID: $1"
|
||||||
echo "Password: $2"
|
echo "Password: [HIDDEN]"
|
||||||
sudo nmcli device wifi connect "$1" password "$2"
|
sudo nmcli device wifi connect "$1" password "$2"
|
||||||
|
|
||||||
#check if connection is successfull
|
# Check if connection is successful
|
||||||
if [ $? -eq 0 ]; then
|
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
|
else
|
||||||
echo "Connection to $1 failed"
|
echo "Connection to $1 failed"
|
||||||
echo "Restarting hotspot..."
|
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
|
fi
|
||||||
|
|
||||||
echo "End connexion shell script"
|
echo "End connexion shell script"
|
||||||
echo "-------"
|
echo "-------"
|
||||||
|
|
||||||
|
|||||||
@@ -792,33 +792,39 @@ if ($type == "wifi_connect") {
|
|||||||
|
|
||||||
if ($type == "wifi_scan") {
|
if ($type == "wifi_scan") {
|
||||||
|
|
||||||
// Set the path to your CSV file
|
// Perform live WiFi scan instead of reading stale CSV file
|
||||||
$csvFile = '/var/www/nebuleair_pro_4g/wifi_list.csv';
|
$output = shell_exec('nmcli -f SSID,SIGNAL,SECURITY device wifi list ifname wlan0 2>/dev/null');
|
||||||
|
|
||||||
// Initialize an array to hold the JSON data
|
// Initialize an array to hold the JSON data
|
||||||
$jsonData = [];
|
$jsonData = [];
|
||||||
|
|
||||||
// Open the CSV file for reading
|
if ($output) {
|
||||||
if (($handle = fopen($csvFile, 'r')) !== false) {
|
// Split the output into lines
|
||||||
// Get the headers from the first row
|
$lines = explode("\n", trim($output));
|
||||||
$headers = fgetcsv($handle);
|
|
||||||
|
// Skip the header line and process each network
|
||||||
// Loop through the rest of the rows
|
for ($i = 1; $i < count($lines); $i++) {
|
||||||
while (($row = fgetcsv($handle)) !== false) {
|
$line = trim($lines[$i]);
|
||||||
// Combine headers with row data to create an associative array
|
if (empty($line)) continue;
|
||||||
$jsonData[] = array_combine($headers, $row);
|
|
||||||
|
// 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
|
// Set the content type to JSON
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
// Convert the array to JSON format and output it
|
// Convert the array to JSON format and output it
|
||||||
echo json_encode($jsonData, JSON_PRETTY_PRINT);
|
echo json_encode($jsonData, JSON_PRETTY_PRINT);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user