Categories
Raspberry Tank

Tank Day 22: I Occidentally a Whole Access Point

On day 21 of the build diary, I put up a list of things to do that would turn this now remote-controlled tank into a fully autonomous one. The first software task was to replace the Raspbian operating system on my Raspberry Pi with Adafruit’s Occidentalis, an almost identical OS that also includes pre-built kernel drivers for I2C.

Installing Occidentalis

Installing Occidentalis was very easy indeed – identical to installing Raspbian, as you might expect. Day 19 covered the move from the original Raspberry Pi software to Debian, and the same procedure was followed this time to move from Raspbian to Occidentalis.

As before, Raspbian’s raspi-config utility popped up on first run, offering the ability to expand the image to fill the SD card, enable ssh, and so on.

raspi-config
raspi-config

Setting up the new install was simply a matter of copying files back to their original locations, and reinstalling libjpeg8-dev and lighttpd. As we are now moving from one armhf environment to another, I didn’t even need to recompile rt_http or mjpg_streamer – they simply worked once they were copied into place.

WiFi Upgrades

At this point I also took the opportunity to upgrade the tank’s WiFi capabilities. I replaced the tiny Edimax EW-7811UN adapter with an Edimax EW-7711UAN, which has a large aerial that should improve coverage. It uses the rt2800usb driver, which is supported out of the box on Raspbian and Occidentalis.

Rear of Lower Chassis, showing new WiFi Adapter
Rear of Lower Chassis, showing new WiFi Adapter

I also suggested on day 17 that I would like the Raspberry Tank to be its own WiFi access point, rather than having to create a hotspot with the control device and have the tank connect to that. Whilst configuring networking on the new Occidentalis install, I figured that it would be a good time to set that up.

Configuring the Access Point

To set the Raspberry Pi up as its own access point, we first need a package called hostapd which will manage the broadcast of the SSID and will allow other clients to connect. For convenience, we also need a DHCP server that will allow connecting clients to automatically be given an IP address. One of the easiest DHCP servers to configure is the one built into dnsmasq, so that’s what we used.

Setup went roughly along the lines of this Raspberry Pi Access Point guide.

I deviated a little from the guide in that I chose to run the Raspberry Tank’s WiFi network in the 192.168.0.0/24 range, and chose to run an open WiFi network instead of a WPA-protected one to ensure compatibility with the widest range of devices.

My eventual configuration was as follows:

/etc/defaults/hostapd

DAEMON_CONF="/etc/hostapd/hostapd.conf"

/etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=RaspberryTank
hw_mode=g
channel=8
beacon_int=100
auth_algs=3
wmm_enabled=1

/etc/dnsmasq.conf

interface=wlan0
no-hosts
dhcp-range=192.168.0.50,192.168.0.150,12h

/etc/network/interfaces

auto lo
iface lo inet loopback

iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.0.0
gateway 192.168.0.1

With these files set up as above, and both hostapd and dnsmasq restarted, my laptop could see and connect to the tank’s wireless network. However, I did encounter one odd problem with this configuration, which was that the laptop was not being given an IP address by dnsmasq’s DHCP server.

Running tail /var/log/syslog revealed messages of the form:

DHCP request received on wlan0 which has no address

Even though wlan0 had an address set in /etc/network/interfaces, that address was no longer assigned to the interface after hostapd bound to it. Running sudo ifconfig wlan0 192.168.0.1 reassigned the right IP address to wlan0, at which point the laptop could then connect and be given a proper IP address.

Laptop Connected to WiFi Network
Laptop Connected to WiFi Network

This was a repeatable issue, however – even on boot, wlan0 would be given its IP address, but as soon as hostapd ran, that address would be removed, causing the DHCP server to fail. I could not find anything useful on the internet about this issue. (It seems that this may not be a problem when using hostapd to bridge between two connections, and that is how most people use it.)

I “fixed” the problem with the ugly hack of editing the init script for hostapd, and inserting the command ifconfig wlan0 192.168.0.1 such that it runs after the hostapd daemon.

My /etc/init.d/hostapd file now reads:

#!/bin/sh

### BEGIN INIT INFO
# Provides:		hostapd
# Required-Start:	$remote_fs
# Required-Stop:	$remote_fs
# Should-Start:		$network
# Should-Stop:
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Advanced IEEE 802.11 management daemon
# Description:		Userspace IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP
#			Authenticator
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd
DAEMON_DEFS=/etc/default/hostapd
DAEMON_CONF=
NAME=hostapd
DESC="advanced IEEE 802.11 management"
PIDFILE=/var/run/hostapd.pid

[ -x "$DAEMON_SBIN" ] || exit 0
[ -s "$DAEMON_DEFS" ] && . /etc/default/hostapd
[ -n "$DAEMON_CONF" ] || exit 0

DAEMON_OPTS="-B -P $PIDFILE $DAEMON_OPTS $DAEMON_CONF"

. /lib/lsb/init-functions

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
		--pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
	log_end_msg "$?"
        ifconfig wlan0 192.168.0.1
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN" \
		--pidfile "$PIDFILE"
	log_end_msg "$?"
	;;
  reload)
  	log_daemon_msg "Reloading $DESC" "$NAME"
	start-stop-daemon --stop --signal HUP --exec "$DAEMON_SBIN" \
		--pidfile "$PIDFILE"
	log_end_msg "$?"
	;;
  restart|force-reload)
  	$0 stop
	sleep 8
	$0 start
	;;
  status)
	status_of_proc "$DAEMON_SBIN" "$NAME"
	exit $?
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
	exit 1
	;;
esac

exit 0

This works, but is a very ugly hack, so I welcome any comments teaching me how to do this properly!

However, at the end of day 22, we do now have a Raspberry Tank that functions properly as its own WiFi access point, including a DHCP server.

33 replies on “Tank Day 22: I Occidentally a Whole Access Point”

I got the same problem in my project to connect the RPi with my PC. Thanks to your ugly hack it works now…

You’re welcome! I’m still waiting for some kind person to explain to me how to do this properly, but when I find out I’ll let you know!

Thanks, I was also stuck at the “DHCP request received on wlan0 which has no address” issue. A more elegant solution is to add the line auto wlan0 to /etc/network/interfaces

ok now, now I got a ipaddress and a gate address but I still can’t SSH into the raspberry pi, putty keep giving me the connection refused error

when I try to connect using the telnet mode in putty it give me the error “connection close by remote host” what could I be doing wrong please help

Do you have an SSH (or Telnet) server running on your Raspberry Pi? Depending on what operating system you are using, you may not have an SSH server running by default.

If you’re using the “Raspbian” distro, the easiest way to enable SSH is to insert the SD card into another computer. You should see two volumes — one small “boot” volume and one larger “root” volume. Inside the “boot” volume there should be a file named bootenablessh.rc. If you rename that to boot.rc, eject the SD card and put it back into your Pi, you should have an SSH server running whenever the Pi starts up.

If you have a different operating system on your Pi, let me know and I’ll see if I can work out the instructions for your particular OS.

yes mine SSH is running since I can SSH into raspberry pi without any problem through the Ethernet method. by the way do I need to set static address on mine laptop in order for the SSH into raspberry pi through wifi to be successful? The OS I am using for mine raspberry pi is occidentalis v0.2

If you’re using the configuration above, your laptop should be assigned an IP address automatically using DHCP. First of all, try going into your network settings in Windows 7 and making sure that your laptop has an IP address in the right range. Then, try pinging the RPi from the command line in Windows.

If you can’t ping the RPi, you probably have something wrong in your network configuration on the Pi. Try setting a static address on the laptop and see if that works.

If you can ping the RPi, it’s probably configured to only listen for SSH connections on the Ethernet port (for some reason). On your Pi, edit /etc/ssh/sshd_config and look for a line that should read ListenAddress 0.0.0.0. If there’s something other than “0.0.0.0” in there, that might be your problem.

kk I do as you suggest but there is no such file as /etc/ssh/ssdh_config in occidentalis v0.2

ok I go in ssh.conf but I can’t seem to find the line saying listening address so what do I do now?

Occidentalis may name things a little differently, I don’t have the Raspberry Tank around at the moment so I can’t check with a proper Occidentalis install — all I have is a vanilla Debian box.

ssh.conf sounds like config for the SSH client, so you won’t find ListenAddress there.

If you don’t have any files like /etc/ssh/sshd.conf or /etc/ssh/sshd_config (the “d” is the important bit that lets you know it’s configuration for the daemon — i.e. the server) then perhaps Occidentalis stores the file somewhere else. I will power up the tank when I get a chance and see if I can find it.

ok I found the file and uncomment the line that said listen address 0.0.0.0, save it and reboot but I still get connection refused when I try to ssh into raspberry pi using putty

Just checking because you didn’t give a definite answer earlier – can you definitely ping the Raspberry Pi on the IP address that you’re entering into PuTTY?

yes I can definitely ping to mine pi when I connect to it wirelessly, all the packet send was receive without error

Now I am quite confused! Your Pi responds to ping but not to ssh, you’re running ssh because you can use it via Ethernet, and you’ve made sure it’s listening to connections from all addresses.

Can you attach a keyboard and monitor to the Pi while it’s in the state where you can’t ssh in, and from the keyboard, type:

sudo lsof -i | grep ssh

This will hopefully show an ssh server and the address it’s listening on – could you comment here with the output of that command?

sorry I don have a monitor and a spare keyboard on mine hand right now, when I try the above comment while I SSH on mine raspberry pi while it is connected to mine router using Ethernet, there is an error saying lsof command not found what could this means?

ok…now I notice one thing, mine gateway is difference form mine ip address assigned to mine raspberry does it matter?

That shouldn’t matter — if you can ping the RPi over Wifi, then routing (including gateways) isn’t your problem.

If you don’t have lsof, you can install it from your distro’s repositories using something like sudo apt-get install lsof, or manually if necessary. However, if you don’t have any way of running this command while connected via SSH, it won’t help us diagnose the issue.

Is there any chance of getting a monitor and keyboard connected to your Pi soon? I’m sorry to say that I’m running out of ideas here 🙁

How I wish there is a pre-config occidentalis img for me to upload on the SD card,try so many ways of config and none works……sign

Hi Ian. First of all very nice work!
I know the conversation has kind of ended some days ago, but I hope you don’t mind me bringing it up again, because I have the same issue with hostapd + trying to ssh from my win7 laptop via the raspi wifi AP.

  1. I can ssh via ethernet
  2. when connected to the raspi AP, ping goes ok but I can’t ssh (connection refused)
  3. the sshd config file is set to listen from all addresses
  4. I did have run lsof command with the raspi connected to a monitor and keyboard, and this is the result:
    sshd 12807 root 3u IPv4 2489 0t0 TCP *:ssh (LISTEN)
  5. If I make an ad-hoc wireless network using win7, and connect to it from the raspi (after stoping hostapd and dnsmasq services) I can ssh from win7 to the raspi via wifi.

So I still don’t know what’s happening, but I guess there is some problem mixing hostapd and sshd services… Do you have any idea?

Thank you!

I guess there’s some configuration problem between hostapd and sshd, yes — but I’m sorry to say that I still have no idea what the problem is. I have tried to recreate the problem on my Pi but it works without a problem for me.

When I get a chance I will grab the /etc/init.d/hostapd and /etc/ssh/sshd_config from my tank so you and low yi liang can compare yours against what I have.

I use a very similar setup to yours. I use the RPi to provide an information station, a wireless local content server at events. No Internet is involved.

I have not had the issue you describe.

I see that the netmask you show in your network/interfaces and the network shown in your Windows connection illustration are different. The Windows one seems correct. Shouldn’t the netmask for the address you specified be 255.255.255.0, not 255.255.0.0 as your file currently reads?

I also don’t understand the idea of dnsmasq assigning the laptop an IP address. You have hard coded an address in network/interfaces, so the one you put there should stick.

I’m not sure I understand what you mean. The entries in /etc/network/interfaces are for the Raspberry Pi, which gets wlan0 set up with IP 192.168.0.1, netmask 255.255.0.0.

This is nothing to do with the laptop’s IP, which is allocated by DHCP by dnsmasq from the range 192.168.0.50 to 192.168.0.150. The laptop’s netmask of 255.255.255.0 is not specifically set anywhere, so this would appear to be a default provided by either dnsmasq or Windows.

Although it makes sense for them both to have the same netmask, there’s no reason why this has to be the case.

low yi liang wrote:

How I wish there is a pre-config occidentalis img for me to upload on the SD card,try so many ways of config and none works……sign

Do you have ip address confict? I had the same problem, i.e. ping succeed, but ssh failed. And I found my vmware virtual adapter have same ip address with the one got from wifi.

hi,
i bought the Edimax EW-7711UAN, too. But i have troubles with set up an access point.
when i want to test my hostapd.conf file i get the following error:

invalid/unkown driver ‘nl80211’

do you have a solution?

I’m not sure — I didn’t have this problem with mine, so perhaps there are two different versions of the EW-7711UAN, or perhaps something has changed to break support in newer versions of Raspbian.

Do you know which driver your device uses? I’ve seen some posts such as this one suggesting that Realtek has a special version of hostapd that supports their devices, so that could be worth a try.

I also had a problem with the wlan0 interface losing its configured address when starting hostapd.

Instead of changing the ifplugd configuration, I got rid of the problem by NOT setting DAEMON_CONF in /etc/default/hostapd. Instead I added hostapd /etc/hostapd/my-hostapd.conf to the file /etc/network/interfaces (after iface wlan0 inet static). See /usr/share/doc/hostapd/README.Debian for details.

Leave a Reply

Your email address will not be published. Required fields are marked *