1

Sometimes I have to take my headless system to places that I don't know the WiFi SSID/Password before hand. SSH does not reliably work via usb, which is a pain anyways with my enclosure.

Is there a wireless way to change SSID/Password settings on the fly. I'm a mechanical Engineer, I don't know complex linux concepts, maybe this is trivial.

Some consumer IoT devices broadcast there own network for initial config of home SSID/Password. Is that possible? Necessary equipment/software/code?

I have Debian 3.8 on Pi 2, and use /etc/network/interfaces for setting SSID/Password "manually"

  • `SSH does not reliably work via usb` - I'd be interested to know, reliability aside, how this is achieved at all ... I do have a kernel of a solution in mind, but nee to get to my raspberry pi to test it before I even propose a solution :p are you able to plug in a thumb drive with your enclosure? – Jaromanda X Jul 06 '16 at 05:16
  • This is a prototype, while I could do I USB drive in development,( i see where you're going) I'd like to avoid potential end user infecting own equipment with bad usb drive. – ihavequestions Jul 06 '16 at 06:25
  • Does your wifi device allow AP mode? – Jaromanda X Jul 06 '16 at 06:34
  • As for infecting with bad USB drive, I don't think linux is vulnerable to that if you do it right (I'm not 100% sure on that though) – Jaromanda X Jul 06 '16 at 06:35
  • What about a basic serial cable to the system console? – Ronny Nilsson Jul 06 '16 at 13:58
  • Exactly as @RonnyNilsson says. How about a USB To RS232 TTL UART PL2303HX cable, it costs around 1.5 Euros in Aliexpress. I have 3 SBCs, and this cable for each one of them, which I do not ever take off. – Rui F Ribeiro Jul 07 '16 at 12:23

3 Answers3

3

guess you are looking for a configuration mode on the Pi. With a SPDT switch connected to GPIO pins you can trigger some scripts to change the PI's config and start in AP mode.

for both modes i would use different network interface config in /etc/network: interface.ap and interface.normal. the switching script just will set a symlink to the specific interface file

in normal mode the pi will connect its wlan0 to one of the WIFIs configured in the wpa_supplicant.conf. IP will be obtained automatically by DHCP.

The /etc/network/interfaces.ap to:

iface wlan0 inet dhcp
    allow-hotplug wlan0
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

in AP mode, the wlan0 needs to be configured with a static IP, bring up hostapd and dhcp.

you will need a installation of hostapd and dhcp server (the following is from eLinux: install software:

sudo apt-get install hostapd udhcpd

configure udhcpd: /etc/udhcpd.conf

start 192.168.42.2 # This is the range of IPs that the hostspot will give to client devices.
end 192.168.42.20
interface wlan0 # The device uDHCP listens on.
remaining yes
opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use.
opt subnet 255.255.255.0
opt router 192.168.42.1 # The Pi's IP address on wlan0 which we will set up shortly.
opt lease 864000 # 10 day DHCP lease time in seconds

infact all the dns settings will be irrelevant because the Pi has no internet connection during AP mode. Then configure the /etc/network/interfaces.ap to:

iface wlan0 inet static
  address 192.168.42.1
  netmask 255.255.255.0

and the last step for AP mode is the configuration of hostapd at /etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=My_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=My_Passphrase
wpa_key_mgmt=WPA-PSK
#wpa_pairwise=TKIP  # You better do not use this weak encryption (only used by old client devices)
rsn_pairwise=CCMP

wiring the switch here i can't bring up all details because i cant test the wiring at the moment. in general the configuration should be like this tutorial. the switch will be connected on two GPIO pins.

scripting a phyton script will check the position of the switch and fire the changes if the mode changes. the status of the mode can be checked with a variable that will be set on status changes or just read where the symlink is pointing to... it would be basically like this GPIO.input(21) is for AP mode and GPIO.input(17) is for normal mode:

while True:
  if (GPIO.input(17) and my_state=='AP'):
    my_state = 'normal'
    os.system("rm /etc/network/interfaces")
    os.system("ln -s /etc/network/interfaces.normal /etc/network/interfaces")
    os.system("ifdown wlan0 && ifup wlan0")
    os.system("systemctl stop hostapd")
    os.system("systemctl stop udhcpd")
    os.system("systemctl disbale hostapd")
    os.system("systemctl disbale udhcpd")

  if (GPIO.input(21) and my_state==normal):
    my_state = 'ap'
    os.system("rm /etc/network/interfaces")
    os.system("ln -s /etc/network/interfaces.ap /etc/network/interfaces")
    os.system("ifdown wlan0 && ifup wlan0")
    os.system("systemctl enbale hostapd")
    os.system("systemctl enbale udhcpd")
    os.system("systemctl start hostapd")
    os.system("systemctl start udhcpd")

okay this script snipplet is a quick example and very dirty. (I am not an expert in python and could not test it.) Maybe some others can help to improve it.

Joe Frambach
  • 103
  • 3
Joe Platano
  • 852
  • 8
  • 19
  • Would it be more efficient to put the groups of os.system calls into two bash scripts, so os.system only gets called once? I have strep, so can't test this for a few days. -OP – ihavequestions Jul 08 '16 at 15:40
  • @NasaWelder, yes of course the sytsem calls can be handled in bash scripts that will be triggered by one syscall. if it is more efficient than in phyton I dont know. I am not python expert) – Joe Platano Jul 08 '16 at 16:55
  • @JoePlatano I would like to use something like your concept, but my question is in AP Mode, is the Pi has to connect to ethernet cable? If not, How can I give wifi ssid and password to Pi? – ton1 Oct 30 '16 at 13:03
  • @JoePlatano so If the Pi has not ethernet cable, I can't use your way, right? – ton1 Oct 30 '16 at 13:07
  • 1
    @Juntae, the Pi is running completely without Ethernet cable connected. In the use case of the NasaWelder, it is not possible to connect the Pi to Ethernet to configure it. Therefore the AP mode. If you can connect the Pi to Ethernet and are able to SSH to the Pi, AP Mode is not necessary. The SSID is provided by Hostapd (see the config in my awnser ssid=My_AP so the password for the AP) the IP Addresses in AP mode are provided by udhcpd. – Joe Platano Oct 30 '16 at 15:50
  • @JoePlatano Thanks, but I want to do like NasaWlder. I want to make headless pi (I asked few days ago, http://raspberrypi.stackexchange.com/questions/56928/is-that-possible-to-connect-laptop-to-raspberry-pi-3-through-micro-usb-power-por/56929#56929). – ton1 Oct 30 '16 at 15:59
  • @JoePlatano So If the pi in AP mode, **how can access to it and give the wifi ssid/password information to the pi?** In AP mode, can I connect the pi in Mobile Phone or Laptop? so If can, it is not a internet, just a local network, right? – ton1 Oct 30 '16 at 16:00
  • @Juntae, un AP mode, SSID, password and IP addresses are preconfigured. Check the config examples in my answer. Of course this need to be done as before – Joe Platano Oct 30 '16 at 16:04
  • @JoePlatano Thanks, but I don't understand well. He asked he don't know ssid/password settings until arrive to place. but you said SSID, and password should preconfigured. I don't understand how can do it... I just want to know If I have the pi that has switch to ap/normal mode, and everything you wrote working well, If I bring it to my friend's house, how can I connect my friend's home's wifi? – ton1 Oct 30 '16 at 16:14
  • @JoePlatano Sorry to bother you... I didn't intend, just want to know what is best way : ap/normal mode and bluetooth in headless iot device. I spend whole days on it. Please give me some hints...Thanks. – ton1 Oct 30 '16 at 16:16
  • @Juntae for your smart mirror project: 1) in your home, where you can access the pi, configure the ap/normal mode. Outcome: you know the SSID,password and ip address of the pi and can connect it while in AP mode. 2) in your GF's place, switch the Raspberry to AP mode configure the WiFi credentials of your gf. 3) switch back to Normal mode. The Pi should now connect to the SSID of your gf and get an IP. You can access the pi, if you are in the WiFi of your GF. – Joe Platano Oct 30 '16 at 23:17
  • @JoePlatano Thnx. I tried your way today, I think the `if` statement in your code maybe should turn each other. `status==ap` and `status==normal`. is that correct? – ton1 Nov 05 '16 at 13:28
  • @Juntae, no this is logical correct. The Variable gives the state of system. If state = ap, and you push the button, the script should switch to Normal mode. There is missing to set the new variable state in the IF blocks. I add this to the code example – Joe Platano Nov 05 '16 at 15:24
1

Some consumer IoT devices broadcast there own network for initial config of home SSID/Password. Is that possible?

Yes.

Necessary equipment

You need a wifi adapter that supports AP (access point) mode. I think most but not all of them do. You could check with

sudo iw list | grep -A 12 -i "supported interface modes"

This may not be definitive in that it gives you a blind 12 lines of context after the search term, so if the list is longer than that you won't see all of them, in which case you could just scroll through the entire output of iw list (if iw isn't installed, apt install iw). However if the list is that long it probably includes AP.

Software?

hostapd and probably dhcpd. The latter is a server that hands out IP addresses to clients that want to connect. Unfortunately my approach to networking is unorthodox so I can't tell you the canonical approach to this,1 however, you will find many guides to configuring this software for use as a wifi hotspot online (including some specifically dedicated to the Pi/Raspbian; the quality of that stuff varies greatly and tends toward the low side IMO, but when it is correct and complete it is the most straightforward to follow).

Once the hotspot is running you'll have to connect to it, configure the ESSID, password, etc. You may be able to do this without losing your own connection to it for reasons implied in the footnote; if not you'll have to have a script you can trust to complete the task after you are disconnected.

This will take a bit of trial and error experimentation. ;)


1. This is related to the larger issue of having the system start up this way only if it cannot find a known network to connect to. It might not be too complicated to have it do both, since technically an AP (access point) is connected to a network, and managing connections to it. However, I've only used it to create a hotspot. Having it run normally like that is probably at best pointless, so you would want to at least turn off hostapd when you are connected as a normal client/end point.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • I don't see the answer here, just a few leads. Not very helpful for a mechanical engineer. – fcm Jul 08 '16 at 01:39
  • @fcm Well, I definitely think Joe Platano's answer went a step beyond in terms of detail. *I also think it is a shame I am the only person to have upvoted that answer thus far.* – goldilocks Jul 08 '16 at 03:09
0

Which version of the Pi are you running? If you have a Pi 3 you could maybe configure the Bluetooth to expose a virtual serial port that you could use as a console for configuring the SSID/Password.

Perhaps something like this could work (just with the built-in Bluetooth rather than a plug-in module.

  • It is a PI 2. No bluetooth. – ihavequestions Jul 06 '16 at 04:20
  • Mozilla documented that approach quite nicely in a 2017 blog post at https://hacks.mozilla.org/2017/02/headless-raspberry-pi-configuration-over-bluetooth/. Security-wise this approach is a bit...questionable, though. – Marcel Stör May 21 '20 at 16:06