10

I am working on a RPi radio project, and I'd like to be able to switch between a "setup mode" where the RPi acts as a Wi-Fi access point, allowing the user to send his/her home Wi-Fi credentials via a dedicated mobile app then write them into the wpa_supplicant file, and a "normal mode" where the board becomes a Wi-Fi client and connects to the Wi-Fi network the user specified while using the setup mode.

Is it possible?

Greenonline
  • 2,740
  • 4
  • 23
  • 36
Zbam
  • 103
  • 1
  • 1
  • 6

1 Answers1

9

It is possible, I suggest you to use a lighttpd with a webservice for your app. After that - just switch between hostapd(AP mode) and wpa_supplicant(client mode). It is as simple as stop one service and start another wia shell command.

UPDATE: Some tips. What is the simplest way is to use a systemd as a triggerhappy switcheroo, and let it be done like this :

  • use wpa_supplicant as a service
  • a hostapd is already a service in ubuntu/debian, so you don't have tо do anything about it.

Next, configure ISC-DHCPD and hostapd for your AP mode, check them to be working as AP, i.e. you can join a network and obtain an IP address, DNS from your DHCP is pingable from a device and working. After that use this cheatsheet on SystemD to make dhcpd and hostapd not start automatically on boot :

#systemctl disable hostapd.service
#systemctl disable isc-dhcp-server.service

and stop thoose SoB's ;) just like that :

#systemctl stop hostapd.service
#systemctl stop isc-dhcp-server.service

after that by a link I've pointed upstrings make wpa_supplicant ( install it like that : apt-get install wpasupplicant ) a service too, and set it up to be working as you wish, i.e. your RPi associates with AP you're about to use, receives IP+DNS, can ping/wget, e.t.c. After that - disable and stop the service just like we did it upstrings :

#systemctl stop wpa_supplicant.service
#systemctl disable wpa_supplicant.service   

After that here goes the magic :) to switch from mode to another just stop the services from a previous mode and start the needed ones(i.e. stop wpa_supplicant and start hostapd and isc-dhcp-server) like this:

#systemctl stop wpa_supplicant.service && systemctl start hostapd.service && systemctl start isc-dhcp-service.service

Yes, in one single string. The secret here is in "&&" construction - it's a type of queue when the next element will be executed only if the previous one haven't failed. So wherever from you'll make a call for this pipe, check for exitcode 0. It will guarantee you that everything was OK switching the modes. That's it, feel free to ask questions if you need more help!

Alexey Vesnin
  • 926
  • 9
  • 16
  • Thanks for your answer. That's what I was thinking of, but I am not that good with shell scripting. I'll try to find some examples, because I think I'll have to fiddle with dhcpd.conf, /etc/default/isc-dhcp-server and /etc/network/interfaces as suggested in this article link – Zbam Mar 16 '16 at 13:28
  • @Zbam need more info to help you out? – Alexey Vesnin Mar 16 '16 at 14:10
  • some insight on the way I could switch from one mode to the other would be great, because from what I've seen it involves many files – Zbam Mar 16 '16 at 17:23
  • @Zbam take a look at my edit, I've updated my answer – Alexey Vesnin Mar 16 '16 at 20:20
  • This has been very useful for my own "turtles" project... but I am still unclear on one detail: I ideally want to "try" to connect to a known wifi connection, and if it fails, then default to AP mode. Is there an easy way to do this with SystemD, or should I be writing a shell script that will attempt the connection, and then do the above? Any guidance would be helpful! (Or let me know if you would prefer this as a separate question) – Aerophilic Nov 20 '16 at 20:11
  • @Aerophilic it will be an external agent - in SystemD or in SysV.init - and it better be a shellscript rather than a C/C++ binary =) The main motto here is that there's nobody in this world, including a SystemD developers, who knows exactly what you need in your very special case. So - make a shellscript and be 101% sure that your task will be done exactly as you want it to be done.You can make this script as a SystemD service - but it's a question of taste, generally. Shellscripts may be not so straightforward as C or Python code, but it's low payload and on-the-fly nature pays off all – Alexey Vesnin Nov 21 '16 at 02:26
  • 1

    after that by a link I've pointed upstrings make wpa_supplicant What do you mean by "upstrings"? Where is the link on how to make wpa_suppplicant a service?

    – Dave Thomas Feb 20 '17 at 23:27
  • it is already a service in Debian systems like Raspbian. A link is in the list upstrings, here it is http://w1.fi/cgit/hostap/tree/wpa_supplicant/systemd/wpa_supplicant.service.arg.in – Alexey Vesnin Feb 21 '17 at 12:15