PiFi Controller - Pi Hotspot Control Using Cayenne App
by praju1 in Circuits > Raspberry Pi
3633 Views, 63 Favorites, 0 Comments
PiFi Controller - Pi Hotspot Control Using Cayenne App
![main.jpg](/proxy/?url=https://content.instructables.com/F23/TC08/IUSLVK49/F23TC08IUSLVK49.jpg&filename=main.jpg)
During a community event, most of the time attendees want to have internet access. However it is useful to have internet access but it should be limited under some circumstances. Usually the speech sessions are very short and speaker wants the audience to listen to him. Switching off the WiFi access through a mobile app can be very much useful during the speech period and it can be switched-on as soon as the session is over.
Few days ago, I was working with setting up hostapd based AP (Access Point) on raspberry pi. Getting the idea of script based AP server control, I thought to control WiFi with one button interface. The idea is that one master user can switch-on and switch-off WiFi anytime through Cayenne UI and SSID will disappear from available list.
Setup the WiFi Hotspot
![13 PiFi Wlan1 up.JPG](/proxy/?url=https://content.instructables.com/FZX/9HWO/IUSLVKNO/FZX9HWOIUSLVKNO.jpg&filename=13 PiFi Wlan1 up.JPG)
First thing need to do is to setup the wifi hotspot.
Clear steps for setting up RPi3 WiFi access point is shows at following link. The steps will make internal WiFi (wlan0) as access point physical layer. However the external dongle generally connects to wlan1 (use ifconfig or iw command to check all WiFi related options). So, I made all modifications according to wlan1. However, my wlan1 (Tenda W311M) dongle doesn't support the mentioned driver in hostapd configuration and thus the script automatically switch the AP access MAC address to wlan0 as shown in figure.
WiFi Switched-off in Default State
![first.png](/proxy/?url=https://content.instructables.com/FSO/3F49/IUSLVK6D/FSO3F49IUSLVK6D.png&filename=first.png)
![second.jpg](/proxy/?url=https://content.instructables.com/F1P/M9EC/IUSLVK24/F1PM9ECIUSLVK24.jpg&filename=second.jpg)
while True: # WiFi Hotstop turn-off condition if GPIO.input(inp)==0: subprocess.Popen('sudo service hostapd stop',shell=True) print "OFF" time.sleep(2)
Switch-on WiFi With Click of a Button
![third.png](/proxy/?url=https://content.instructables.com/FNI/XHBX/IUSLVK1P/FNIXHBXIUSLVK1P.png&filename=third.png)
![fourth.jpg](/proxy/?url=https://content.instructables.com/F9D/M5GB/IUSLVK5A/F9DM5GBIUSLVK5A.jpg&filename=fourth.jpg)
# WiFi Hotstop turn-on condition if GPIO.input(inp)==1: subprocess.Popen('sudo service hostapd start',shell=True) print "ON" time.sleep(2)
Source Code
![16 wificontrol program.JPG](/proxy/?url=https://content.instructables.com/FAN/11UY/IUSLVL66/FAN11UYIUSLVL66.jpg&filename=16 wificontrol program.JPG)
import RPi.GPIO as GPIO import time import subprocess
# Pin declaration for input and output but = 20 inp = 21
# Configuration setup for input and output pins GPIO.setmode(GPIO.BCM) GPIO.setup(but, GPIO.OUT) GPIO.setup(inp, GPIO.IN)
try: while True: # WiFi Hotstop turn-off condition if GPIO.input(inp)==0: subprocess.Popen('sudo service hostapd stop',shell=True) print "OFF" time.sleep(2) # WiFi Hotstop turn-on condition if GPIO.input(inp)==1: subprocess.Popen('sudo service hostapd start',shell=True) print "ON" time.sleep(2)
except: print "WiFi control using Cayenne, BYE!!!" finally: GPIO.cleanup()