Speedy GPS Mapping Using Your Pi
YOU’LL NEED THIS
USB WI-FI ADAPTER WITH MONITOR MODE Such as a TP-Link TL-WN72N.
USB GPS MODULE We’re using a U-Blox 7.
ONE OF THE GREAT THINGS ABOUT THE RASPBERRY PI is that it’s so small and portable. To illustrate that fact while doing something interesting, this tutorial reveals how to configure a Raspberry Pi 3 to run from a car cigarette lighter socket while enabling the driver to collect Wi-Fi location and other data using a USB GPS and an external USB Wi-Fi module [ Image A]. We then explain how to map this data using Google Maps.
Start off by grabbing the Raspbian Lite image file from www.raspberrypi.org/downloads/raspbian. Download and unzip the ZIP file. Ensure that only the image file is extracted. There should only be one file ending in .img at this point. Use Etcher (downloadable from www.etcher.io) to write out the image. Insert the microSD card and open Etcher. First, select the destination card, then click “Start.” It takes several minutes to write and verify the image.
1 SET UP THE PI Once the image is written to the card, unmount, remove, and reinsert it. Mount it as a fat32 disk. At this point, there should be a /boot partition mounted. To enable remote access, create an empty file called “ssh” in the boot folder with this command: touch /media/ boot/ssh
>> Now unmount the card and place it in the Pi, then turn on the power. By default, the password for logging into the Raspberry Pi is “raspberry.” The first item to sort is to enable the Pi to boot without the HDMI cable being plugged in. You need to log in to the Pi directly. Do this by editing the /boot/config.txt config file ( sudo nano / boot/ config.txt ).Locate the line with hdmi_ force_hotplug=1 and uncomment it, then save on exit. It’s also best practice to update the Pi’s software. 2 TOTALLY WIRED To set up remote access, the next step is to establish the wired Ethernet connection to install all the packages that this project requires. If you’re pushed for time, it’s possible to use the onboard Wi-Fi, but later on that is used to connect directly to the Pi, so it’ll need to be reconfigured if you use wireless. Change the wired network configuration by using this command: sudo nano /etc/network/interfaces
>> Locate and change the “eth0” setting to a local network address using the lines below, substituting your IP addresses for the ones listed. auto eth0 iface eth0 inet static address 10.0.0.177 netmask 255.255.255.0 gateway 10.0.0.254 dns-nameservers 22.214.171.124
>> If you’ve not already done so, plug in the cable. Make the changes live using this command: sudo service networking restart
>> Check the IP address is working by using ifconfig eth0 to make sure the changes have taken. Raspbian provides a tool for managing the Pi: raspi-config. This has several applications above and beyond updating. To use it, run the following command: sudo raspi-config
>> Select option eight (update). While in raspi-config, set a new password and hostname if desired, by using the appropriate options. When finished, move to the “Finish” button, and press Enter. A reboot may be required at this point. If so, reboot and reconnect using SSH, as done above. Now comes the interesting bits, such as configuring the GPS and USB Wi-Fi. Plug in the GPS receiver [ Image B]. After a few seconds, it should have registered. To check that it can be seen properly, use this command: sudo lsusb
3 CHECK THE GPS RECEIVER This should show the GPS device as being listed (for this tutorial, the device is shown as a U-blox AG). If you’re not sure, remove the device and repeat the command above, comparing the out result. For the GPS device to work, it needs to seen as a serial device. To see which device has been registered, you can use the following command: sudo dmesg | grep -i usb
With our current setup, the device shows as “ttyACM0”: cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device.
To check the correct device is being received, use this command to output the GPS data to the console.
sudo cat /dev/ttyACM0
>> There are several ways to view the GPS data, including a command called gpsd . It has a few dependencies, some
of which are shared with the Kismet Wi-Fi scanner. First, install the following dependencies:
sudo apt-get install -y screen gpsd libncurses5-dev libpcapdev tcpdump libnl-dev gpsd-clients python-gps ntp
>> There are several options that need to be set for the GPS service to run properly. Edit the /etc/default/gpsd file ( sudo nano /etc/default/gpsd ) and modify the file in line with what’s shown below. Some of the comment lines have been moved for brevity...
START_DAEMON=”true” USBAUTO=”true” DEVICES=”/dev/ttyACM0” GPSD_OPTIONS=”-n” GPSD_SOCKET=”/var/run/gpsd.sock”
To turn on GPS location on boot, use the command sudo systemctl enable gpsd.socket . >> To check that it’s working as expected, you can use the cgps command. This should show a nice formatted console screen, with a lot of GPS data, including your current co-ordinates and height above sea-level. Use Ctrl-C to exit it. If it looks as though it’s not working, we advise checking the service is running as a first point (and potentially carry out a reboot if it’s problematic). Use systemctl status gpsd . In addition, if the GPS doesn’t pick up any co-ordinates, try hanging the device out the window. We found that double glazing stops the signal getting in! 4 TWEAK THE INTERFACE Now is a good time to configure the WLAN1 Wi-Fi interface. First, ensure that the new controller can be seen by using the same lsusb command that was used earlier. Unfortunately, the out-of-the-box Wi-Fi built into the Pi 3 doesn’t currently work for our purposes, because it doesn’t support Monitor mode. For more on this, see the boxout on page 63.
>> It should be noted that Monitor mode does just that. It monitors the network and never connects to any wireless networks—nor is it able to! Next, you need to install the Kismet software. This is the Wi-Fi scanning tool that we’ll be using. Kismet has to be built from source; it can’t just be installed because it’s not available in the repos or a package. We’re using the latest release at time of writing. Follow the commands below to download and install Kismet:
wget http://www.kismetwireless.net/code/ kismet-2016-07-R1.tar.xz tar -xvf kismet-2016-07-R1.tar.xz cd kismet-2016-07-R1/ make dep sudo make install
5 CONFIGURE KISMET The whole process takes a long time (30 minutes or more, potentially) as it builds all the programs from source code. It can be compiled to run without using root, but that is beyond this introductory project. There are a lot of warnings, but they are nothing to worry about. Eventually, the system returns to the command prompt. Kismet can take command-line arguments, but in this scenario, the article uses the Kismet configuration file. The only item to be set is the interface to use. Edit the kismet.conf file: sudo nano /usr/ local/etc/ kismet.conf
>> Uncomment the line that reads ncsource=wlan0 and also change wlan0 to wlan1 . It’s now possible to run Kismet, and it’ll detect all the various local network points and their geolocations. While it’s not advisable to run Kismet as root, for our purposes it’s fine. Run it by using the command sudo kismet . This loads the Kismet GUI and asks several questions. It’s possible to use Tab to navigate around.
>> The best startup procedure is to leave the settings in place. When asked if it’s desired to connect to the Kismet server, make sure you press “Yes.” The console log should hopefully start showing all the networks that it can currently locate [ Image C]. Behind the scenes, the Kismet server also creates several groups of log files. Assuming
the test run goes as expected, and there are log files in /home/pi, it’s time to shut down the Raspberry Pi, and do it for real. To exit Kismet, use Ctrl-C. 6 ACCESS FOR ALL The last big configuration item is setting up the wlan1 to be an access point. These configuration steps are based on the official Raspberry Pi guide ( www.raspberrypi.org/documentation/ configuration/wireless/access-point.md). This means that your laptop can see and connect to the Pi without having to worry about IP addresses. To set up this configuration, there are two items of software that are needed: dnsmasq and hostapd. Again, install them with:
sudo apt-get install -y dnsmasq hostapd
>> The dnsmasq tool takes care of the DHCP angle, while the hostapd utility provides the items that are needed to configure the wireless access point. First, we need to change the way wlan0 works. Edit the DHCP configuration file ( sudo nano /etc/dhcpcd.
conf ) and add the following entry to the bottom of the file: denyinterfaces wlan0 7 CONFIGURE INTERFACE You also need to set up the interfaces configuration for wlan0. Just like was done earlier when we set a static IP address for the wired network, the same needs to be done for the wlan0 interface. Edit it to use the following ( sudo nano /etc/ ): network/interfaces allow-hotplug wlan0 iface wlan0 inet static address 172.16.0.1 netmask 255.255.255.0 network 172.16.0.0
>> Once set up, save the changes, and exit. Restart the services as detailed below. If you’re not using a wired connection, the interface restart would cause issues. It’s better to do a restart ( sudo reboot now ). sudo service dhcpcd restart sudo ifdown wlan0 sudo ifup wlan0 >> Configure the DNS settings by editing the dnsmasq
file ( sudo nano /etc/dnsmasq.conf ), and add the interface details as shown below:
interface= wlan0 # Use the require wireless interface - usually wlan0 dhcp-range=172.16.0.2,172.16.0.20,255.255.255.0,24h >> Finally, set up the AP configuration file ( sudo nano /etc/
), adding the items shown below: hostapd/ hostapd.conf interface= wlan0 driver=nl80211 ssid=MyNetwork hw_mode=g channel=7 wmm_enabled=0 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=MyPassword wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
>> Edit the hostapd file, changing the DAEMON_CONF line to reflect that below ( sudo nano /etc/default/ hostapd): DAEMON_CONF=”/etc/ hostapd/ hostapd.conf”
>> Turning on the services at boot is achieved with: sudo service hostapd start sudo service dnsmasq start
>> Restart the service to instigate the changes: sudo service ntp restart 8 TEST THE SETUP To test the environment, shut down the Pi, and remove the Ethernet and HDMI cables. Power it up, connect to the newly created AP, and, from your laptop, access a remote shell into the AP point. Shut down, then place the Pi in your car. Be aware that the Pi may not boot properly if there’s insufficient power. It may look powered on, but it doesn’t fully boot. Check power requirements before doing anything. At a push, you can power it from your laptop’s USB port. External battery packs are an option, too—be sure to read the manual, to ensure it can provide enough power. >> Assuming the Pi has powered up, and you can access it via ssh , run the Kismet application using: ssh firstname.lastname@example.org sudo kismet
>> Follow the earlier routine to access the Kismet console. 9 CONVERT THE DATA Go for a short drive, then come back. All the data will have been collected automatically, and placed in files within the /home/pi folder. Use the laptop to gracefully shut down the Pi using the command sudo shutdown -h now . These files contain GPS data and Wi-Fi base station data, but are currently not in a format that Google Maps can understand. To do the conversion, we’re going to use a script by Scott Helme to convert it into a mapping file. To do this, use the command: wget https://gist.githubusercontent.com/ScottHelme/5c6869e1 7c3e9c8b2034dc8fc13e180b/raw/31c2d34f66748b6bd26415fd7d1 20c06b3d92eaf/netxml2kml.py -O netxml2kml.py
Or the following shortened link: wget http:// bit.ly/LXF230netxml -O netxml2kml.py
>> To create the mapping file, run the command: cd / home/pi python netxml2kml.py --kml -o output *xml
It may take a minute or two, but it eventually delivers a summary of the Wi-Fi base stations found.
>> Obviously, the data is still on the Pi, so it needs to be copied across. Copy the file using scp to your local PC, substituting values as needed. Use the following command to copy the data across from the Pi: scp / home/pi/*.xml myuser@myworkstation:/ home/myuser
10 IMPORT INTO GOOGLE MAPS At this point, the data file should be in the current directory from which the command was run. The final step is to import the XML file into Google Maps ( www.google.com/maps/d/home). Select “Create New Map.” Add a layer. This will show a window that will enable the user to add the newly created file. This could potentially take several minutes to complete. Once done, you should be able to see all those Wi-Fi spots mapped, alongside various details of the protocols supported, and other interesting items [ Image D].
>> Create a raw Google Map. To import the data, select “Add New Layer,” then click “Import.” Drag in the saved file. It’ll take a moment to upload and import the data. Once completed, the map shows the location, Wi-Fi station name, encryption schemes, and other data.
>> We hope that this guide has been useful. There are many other additions and configurations that can be applied upon the foundation of this fun project, but that’s for you to experiment with. Please let us know how you get on—this is your chance to inspire other MaximumPC readers and the entire Raspberry Pi community.