#!/usr/bin/env bash # # This script is used to indicate whether a device is connected to the GoPiGo access point using the Wifi LED on GoPiGo3. # This script checks the connection at an interval of 2 secs. # # # Requirement: # GoPiGo3 # nmap # # Output # Broadcasting SSID Connected to a device Connected to Wifi Dex Antenna(LED) # Yes No No Green # Yes Yes No Blue # No -- Yes Yellow # No No No Turned off # Function to switch on WiFi status LED # Called by install/wifi_led.service start() { while true do # SSID present in hostapd.conf if [ -f /etc/hostapd/hostapd.conf ] then ssid=$(sed -n 's/^ *ssid*= *//p' /etc/hostapd/hostapd.conf) else ssid="no AP" fi echo "WiFi_led_status.sh, ssid: $ssid" # Desired ssid stored in /Dexter/ssid file if [ -f /home/pi/Dexter/ssid ] then ssid_check=$(head -1 /home/pi/Dexter/ssid) else ssid_check="no ssid" fi echo "wifi_led_statu.sh: ssid_check is $ssid_check" # checking if hostapd is active but not running if [ -f /etc/init.d/hostapd ] then exited_hostapd=$(/etc/init.d/hostapd status | grep -c "active (exited)") if [[ $exited_hostapd == 1 ]]; then sudo /etc/init.d/hostapd restart fi # Status of the hostapd service: expected to be active hostapd_status=$((/etc/init.d/hostapd status & /etc/init.d/dnsmasq status) | grep -c "active (running)") echo "WiFi_led_status.sh: Detected Robot: $detected_robot" # Check if the detected robot is GPG3 fi echo "WiFi_led_status.sh: connection_status = $connection_status" # Condition to check if SSID is broadcasted # Check if SSID in hostapd.conf matches the desired SSID, also if hostpad is active if [[ "$ssid" == "$ssid_check" || "$ssid" == "$(echo $ssid_check)_5Ghz" ]] && [[ $hostapd_status == 2 ]] && ! iwgetid --scheme then echo "WiFi_led_status.sh: WiFi Broadcasting SSID: $ssid" # Scanning to find any connections, retrying 3 times to make sure connection is not missed on scanning. # python3 /opt/Sam_Dev/install/python_scripts/detect_devices.py || [ $connected_devices = "no" ] python3 /opt/Sam_Dev/install/python_scripts/detect_devices.py exit_status=$? if [ "${exit_status}" == 0 ]; then connected_devices="yes" else connected_devices="no" fi # Checking if devices are connected if [[ $connected_devices == "yes" ]] then echo "WiFi_led_status.sh: WiFi Connected to a device" # skip checking connection_status as we are facing an issue # with firmware resetting for yet unknown reason # *always* set the LED to either green or blue on a GPG3 # but keep the check on connection_status on the GPG2 # as it can only be done once on that platform. # (we return the blinker use to the end user) if [[ "$detected_robot" == "GoPiGo3" ]] then # To make the LED_WIFI glow BLUE echo "WiFi_led_status.sh: turn LED BLUE" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,0,0,20)" connection_status=1 fi if [[ "$detected_robot" == "GoPiGo" ]] then if [ $connection_status != 1 ] then # turn the left blinker off echo "turn left blinker off" python -c "import gopigo;gopigo.led_off(gopigo.LED_L)" connection_status=1 fi fi else if [[ "$detected_robot" == "GoPiGo3" ]] then # To make the LED_WIFI glow GREEN echo "WiFi_led_status.sh: turn LED GREEN" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,0,20,0)" connection_status=0 fi if [[ "$detected_robot" == "GoPiGo" ]] then if [ $connection_status != 0 ] then # turn the left blinker on and right blinker off echo "turn left blinker on" python -c "import gopigo;gopigo.led_off(gopigo.LED_R);gopigo.led_on(gopigo.LED_L)" connection_status=0 fi fi echo "WiFi_led_status.sh: WiFi Not connected to a device" fi # the else is when the SSID is not being broadcasted else echo "WiFi_led_status.sh: No Access Point" # if we are on a GPG3 if [ -f "/home/pi/Dexter/detected_robot.txt" ] && grep -q GoPiGo3 /home/pi/Dexter/detected_robot.txt || [ ! -f "/home/pi/Dexter/detected_robot.txt" ] then # and the GPG3 is connected via Wifi to a network if iwgetid --scheme then # To make the LED_WIFI glow YELLOW echo "WiFi_led_status.sh: Yellow" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,5,5,0)" connection_status=-1 # the GPG3 is not connected to a network, nor is it broadcasting an access point else echo "no Wifi Connection detected: OFF" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,0,0,0)" connection_status=-1 # source /opt/Sam_Dev/access_point_on_off.sh on fi else # To turn the LED_WIFI off echo "WiFi_led_status.sh: WiFi Not Broadcasting SSID" echo "WiFi_led_status.sh: turn LED off" if [[ "$detected_robot" == "GoPiGo3" ]] then echo "no Wifi Connection detected: OFF" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,1,0,0)" connection_status=-1 fi if [[ "$detected_robot" == "GoPiGo" ]] then # turn the left blinker on and right blinker off echo "WiFi_led_status.sh: turn left blinker on" python -c "import gopigo;gopigo.led_off(gopigo.LED_R);gopigo.led_off(gopigo.LED_L)" connection_status=-1 fi fi fi # Time to wait till next ping, so that dnsmasq can function in allocating addresses to devices during connection. sleep 2 done } # Function used to turn off the wifi LED on stopping the wifi_led.service, reboot and shutdown stop() { echo "STOPPING WIFI LED STATUS" if [[ "$detected_robot" == "GoPiGo3" ]] then echo "WiFi_led_status.sh: Stopping WiFi Led Status" echo "WiFi_led_status.sh: turn LED off" python -c "import gopigo3;GPG=gopigo3.GoPiGo3();GPG.set_led(GPG.LED_WIFI,1,0,0)" fi if [[ "$detected_robot" == "GoPiGo" ]] then # turn the left blinker on and right blinker off echo "WiFi_led_status.sh: turn left blinker on" python -c "import gopigo;gopigo.led_off(gopigo.LED_R);gopigo.led_off(gopigo.LED_L)" fi } # Detected Robot detected_robot=$(head -1 /home/pi/Dexter/detected_robot.txt) # Connection Status is -1 initially when program starts or when /home/pi/Dexter/ssid has been changed without updating the hostapd # Connection Status is 0 when not connected to a device and 1 when connected. connection_status=-1 # Argument passed through the command line $1