setup-wifi.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. #
  3. # Dune Weaver - Autohotspot Setup
  4. #
  5. # Sets up the autohotspot system so the Pi creates a "Dune Weaver" WiFi
  6. # hotspot when no known network is available. Users connect to the hotspot
  7. # and configure WiFi through the Dune Weaver app.
  8. #
  9. # Run: bash wifi/setup-wifi.sh
  10. # Or: dw wifi setup
  11. #
  12. set -e
  13. # Colors
  14. RED='\033[0;31m'
  15. GREEN='\033[0;32m'
  16. YELLOW='\033[1;33m'
  17. BLUE='\033[0;34m'
  18. NC='\033[0m'
  19. # Determine script and project directories
  20. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  21. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  22. HOTSPOT_CON_NAME="DuneWeaver-Hotspot"
  23. HOTSPOT_SSID="Dune Weaver"
  24. HOTSPOT_IP="10.42.0.1/24"
  25. IFACE="wlan0"
  26. CONF_DIR="/etc/dune-weaver"
  27. print_step() {
  28. echo -e "\n${BLUE}==>${NC} ${GREEN}$1${NC}"
  29. }
  30. print_success() {
  31. echo -e "${GREEN}$1${NC}"
  32. }
  33. print_warning() {
  34. echo -e "${YELLOW}Warning:${NC} $1"
  35. }
  36. print_error() {
  37. echo -e "${RED}Error:${NC} $1"
  38. }
  39. # Check prerequisites
  40. check_prereqs() {
  41. print_step "Checking prerequisites..."
  42. # Check for NetworkManager
  43. if ! command -v nmcli &>/dev/null; then
  44. print_error "NetworkManager (nmcli) not found. This requires Pi OS Trixie or later."
  45. exit 1
  46. fi
  47. # Check for wlan0
  48. if ! nmcli dev show "$IFACE" &>/dev/null; then
  49. print_warning "WiFi interface '$IFACE' not found. Hotspot may not work."
  50. fi
  51. print_success "Prerequisites OK"
  52. }
  53. # Install dnsmasq for DNS redirect (captive portal)
  54. install_dnsmasq() {
  55. print_step "Installing dnsmasq..."
  56. if command -v dnsmasq &>/dev/null; then
  57. echo "dnsmasq already installed"
  58. else
  59. sudo apt update
  60. sudo DEBIAN_FRONTEND=noninteractive apt install -y dnsmasq
  61. fi
  62. # Disable the default dnsmasq service — we manage it manually in autohotspot
  63. sudo systemctl disable --now dnsmasq 2>/dev/null || true
  64. print_success "dnsmasq installed and default service disabled"
  65. }
  66. # Create the NetworkManager hotspot connection profile
  67. create_hotspot_profile() {
  68. print_step "Creating hotspot connection profile..."
  69. # Remove existing profile if present
  70. if nmcli con show "$HOTSPOT_CON_NAME" &>/dev/null; then
  71. echo "Removing existing hotspot profile..."
  72. sudo nmcli con delete "$HOTSPOT_CON_NAME" 2>/dev/null || true
  73. fi
  74. # Read app name from state.json if available
  75. local ssid="$HOTSPOT_SSID"
  76. local state_file="$PROJECT_DIR/state.json"
  77. if [ -f "$state_file" ]; then
  78. local app_name
  79. app_name=$(python3 -c "import json; print(json.load(open('$state_file')).get('app_name', ''))" 2>/dev/null || true)
  80. if [ -n "$app_name" ]; then
  81. ssid="$app_name"
  82. echo "Using app name from state.json: $ssid"
  83. fi
  84. fi
  85. # Create the hotspot profile (open network, no password)
  86. sudo nmcli con add type wifi ifname "$IFACE" mode ap con-name "$HOTSPOT_CON_NAME" \
  87. ssid "$ssid" autoconnect no \
  88. ipv4.method shared ipv4.addresses "$HOTSPOT_IP"
  89. print_success "Hotspot profile created: SSID='$ssid', IP=${HOTSPOT_IP%/*}"
  90. }
  91. # Copy configuration files
  92. install_configs() {
  93. print_step "Installing configuration files..."
  94. # Create config directory
  95. sudo mkdir -p "$CONF_DIR"
  96. # Copy dnsmasq config
  97. sudo cp "$SCRIPT_DIR/dnsmasq-hotspot.conf" "$CONF_DIR/dnsmasq-hotspot.conf"
  98. echo "Installed $CONF_DIR/dnsmasq-hotspot.conf"
  99. # Copy autohotspot script
  100. sudo cp "$SCRIPT_DIR/autohotspot" /usr/local/bin/autohotspot
  101. sudo chmod +x /usr/local/bin/autohotspot
  102. echo "Installed /usr/local/bin/autohotspot"
  103. print_success "Configuration files installed"
  104. }
  105. # Install and enable the systemd service
  106. install_service() {
  107. print_step "Installing autohotspot service..."
  108. sudo cp "$SCRIPT_DIR/autohotspot.service" /etc/systemd/system/autohotspot.service
  109. sudo systemctl daemon-reload
  110. sudo systemctl enable autohotspot.service
  111. print_success "autohotspot.service installed and enabled"
  112. }
  113. # Main
  114. main() {
  115. echo -e "${GREEN}Dune Weaver Autohotspot Setup${NC}"
  116. echo ""
  117. check_prereqs
  118. install_dnsmasq
  119. create_hotspot_profile
  120. install_configs
  121. install_service
  122. echo ""
  123. echo -e "${GREEN}============================================${NC}"
  124. echo -e "${GREEN} Autohotspot Setup Complete!${NC}"
  125. echo -e "${GREEN}============================================${NC}"
  126. echo ""
  127. echo "On next boot, the Pi will:"
  128. echo " 1. Scan for known WiFi networks (30s timeout)"
  129. echo " 2. If found → connect normally"
  130. echo " 3. If not found → create a '$(nmcli -t -f 802-11-wireless.ssid con show "$HOTSPOT_CON_NAME" 2>/dev/null | cut -d: -f2 || echo "$HOTSPOT_SSID")' hotspot"
  131. echo ""
  132. echo "Users can connect to the hotspot and open the Dune Weaver app"
  133. echo "to configure WiFi credentials."
  134. echo ""
  135. echo "Commands:"
  136. echo " dw wifi status — Show current WiFi mode"
  137. echo " dw wifi hotspot — Manually switch to hotspot mode"
  138. echo " dw wifi scan — Scan for available networks"
  139. }
  140. main "$@"