Speedy GPS Map­ping Us­ing Your Pi

Maximum PC - - R&D - –STU­ART BURNS



USB GPS MODULE We’re us­ing a U-Blox 7.

ONE OF THE GREAT THINGS ABOUT THE RASPBERRY PI is that it’s so small and por­ta­ble. To il­lus­trate that fact while do­ing some­thing in­ter­est­ing, this tu­to­rial re­veals how to con­fig­ure a Raspberry Pi 3 to run from a car cig­a­rette lighter socket while en­abling the driver to col­lect Wi-Fi lo­ca­tion and other data us­ing a USB GPS and an ex­ter­nal USB Wi-Fi module [ Im­age A]. We then ex­plain how to map this data us­ing Google Maps.

Start off by grab­bing the Rasp­bian Lite im­age file from www.rasp­ber­rypi.org/down­loads/rasp­bian. Down­load and un­zip the ZIP file. En­sure that only the im­age file is ex­tracted. There should only be one file end­ing in .img at this point. Use Etcher (down­load­able from www.etcher.io) to write out the im­age. In­sert the mi­croSD card and open Etcher. First, se­lect the des­ti­na­tion card, then click “Start.” It takes sev­eral min­utes to write and ver­ify the im­age.

1 SET UP THE PI Once the im­age is writ­ten to the card, un­mount, re­move, and rein­sert it. Mount it as a fat32 disk. At this point, there should be a /boot par­ti­tion mounted. To en­able re­mote ac­cess, cre­ate an empty file called “ssh” in the boot folder with this com­mand: touch /me­dia/ boot/ssh

>> Now un­mount the card and place it in the Pi, then turn on the power. By de­fault, the pass­word for log­ging into the Raspberry Pi is “raspberry.” The first item to sort is to en­able the Pi to boot without the HDMI cable be­ing plugged in. You need to log in to the Pi di­rectly. Do this by edit­ing the /boot/con­fig.txt con­fig file ( sudo nano / boot/ con­fig.txt ).Lo­cate the line with hd­mi_ force_hot­plug=1 and un­com­ment it, then save on exit. It’s also best prac­tice to up­date the Pi’s soft­ware. 2 TO­TALLY WIRED To set up re­mote ac­cess, the next step is to es­tab­lish the wired Eth­er­net con­nec­tion to in­stall all the pack­ages that this project re­quires. If you’re pushed for time, it’s pos­si­ble to use the on­board Wi-Fi, but later on that is used to con­nect di­rectly to the Pi, so it’ll need to be re­con­fig­ured if you use wire­less. Change the wired net­work con­fig­u­ra­tion by us­ing this com­mand: sudo nano /etc/net­work/in­ter­faces

>> Lo­cate and change the “eth0” set­ting to a lo­cal net­work ad­dress us­ing the lines be­low, sub­sti­tut­ing your IP ad­dresses for the ones listed. auto eth0 iface eth0 inet static ad­dress net­mask gate­way dns-name­servers

>> If you’ve not al­ready done so, plug in the cable. Make the changes live us­ing this com­mand: sudo ser­vice net­work­ing restart

>> Check the IP ad­dress is work­ing by us­ing if­con­fig eth0 to make sure the changes have taken. Rasp­bian pro­vides a tool for man­ag­ing the Pi: raspi-con­fig. This has sev­eral ap­pli­ca­tions above and be­yond up­dat­ing. To use it, run the fol­low­ing com­mand: sudo raspi-con­fig

>> Se­lect op­tion eight (up­date). While in raspi-con­fig, set a new pass­word and host­name if de­sired, by us­ing the ap­pro­pri­ate op­tions. When fin­ished, move to the “Fin­ish” but­ton, and press En­ter. A re­boot may be re­quired at this point. If so, re­boot and re­con­nect us­ing SSH, as done above. Now comes the in­ter­est­ing bits, such as con­fig­ur­ing the GPS and USB Wi-Fi. Plug in the GPS re­ceiver [ Im­age B]. Af­ter a few sec­onds, it should have reg­is­tered. To check that it can be seen prop­erly, use this com­mand: sudo lsusb

3 CHECK THE GPS RE­CEIVER This should show the GPS de­vice as be­ing listed (for this tu­to­rial, the de­vice is shown as a U-blox AG). If you’re not sure, re­move the de­vice and re­peat the com­mand above, com­par­ing the out re­sult. For the GPS de­vice to work, it needs to seen as a se­rial de­vice. To see which de­vice has been reg­is­tered, you can use the fol­low­ing com­mand: sudo dmesg | grep -i usb

With our cur­rent setup, the de­vice shows as “ttyACM0”: cd­c_acm 1-1.2:1.0: ttyACM0: USB ACM de­vice.

To check the cor­rect de­vice is be­ing re­ceived, use this com­mand to out­put the GPS data to the con­sole.

sudo cat /dev/ttyACM0

>> There are sev­eral ways to view the GPS data, in­clud­ing a com­mand called gpsd . It has a few de­pen­den­cies, some

of which are shared with the Kis­met Wi-Fi scan­ner. First, in­stall the fol­low­ing de­pen­den­cies:

sudo apt-get in­stall -y screen gpsd lib­n­curses5-dev libp­capdev tcp­dump libnl-dev gpsd-clients python-gps ntp

>> There are sev­eral op­tions that need to be set for the GPS ser­vice to run prop­erly. Edit the /etc/de­fault/gpsd file ( sudo nano /etc/de­fault/gpsd ) and mod­ify the file in line with what’s shown be­low. Some of the com­ment lines have been moved for brevity...

START_DAEMON=”true” USBAUTO=”true” DE­VICES=”/dev/ttyACM0” GPSD_OPTIONS=”-n” GPSD_SOCKET=”/var/run/gpsd.sock”

To turn on GPS lo­ca­tion on boot, use the com­mand sudo sys­tem­ctl en­able gpsd.socket . >> To check that it’s work­ing as ex­pected, you can use the cgps com­mand. This should show a nice for­mat­ted con­sole screen, with a lot of GPS data, in­clud­ing your cur­rent co-or­di­nates and height above sea-level. Use Ctrl-C to exit it. If it looks as though it’s not work­ing, we ad­vise check­ing the ser­vice is run­ning as a first point (and po­ten­tially carry out a re­boot if it’s prob­lem­atic). Use sys­tem­ctl sta­tus gpsd . In ad­di­tion, if the GPS doesn’t pick up any co-or­di­nates, try hang­ing the de­vice out the win­dow. We found that dou­ble glaz­ing stops the sig­nal get­ting in! 4 TWEAK THE IN­TER­FACE Now is a good time to con­fig­ure the WLAN1 Wi-Fi in­ter­face. First, en­sure that the new con­troller can be seen by us­ing the same lsusb com­mand that was used ear­lier. Un­for­tu­nately, the out-of-the-box Wi-Fi built into the Pi 3 doesn’t cur­rently work for our pur­poses, be­cause it doesn’t sup­port Mon­i­tor mode. For more on this, see the box­out on page 63.

>> It should be noted that Mon­i­tor mode does just that. It mon­i­tors the net­work and never con­nects to any wire­less net­works—nor is it able to! Next, you need to in­stall the Kis­met soft­ware. This is the Wi-Fi scan­ning tool that we’ll be us­ing. Kis­met has to be built from source; it can’t just be in­stalled be­cause it’s not avail­able in the re­pos or a pack­age. We’re us­ing the lat­est re­lease at time of writ­ing. Fol­low the com­mands be­low to down­load and in­stall Kis­met:

wget http://www.kismetwire­less.net/code/ kis­met-2016-07-R1.tar.xz tar -xvf kis­met-2016-07-R1.tar.xz cd kis­met-2016-07-R1/ make dep sudo make in­stall

5 CON­FIG­URE KIS­MET The whole process takes a long time (30 min­utes or more, po­ten­tially) as it builds all the pro­grams from source code. It can be compiled to run without us­ing root, but that is be­yond this in­tro­duc­tory project. There are a lot of warn­ings, but they are noth­ing to worry about. Even­tu­ally, the sys­tem re­turns to the com­mand prompt. Kis­met can take com­mand-line ar­gu­ments, but in this sce­nario, the ar­ti­cle uses the Kis­met con­fig­u­ra­tion file. The only item to be set is the in­ter­face to use. Edit the kis­met.conf file: sudo nano /usr/ lo­cal/etc/ kis­met.conf

>> Un­com­ment the line that reads nc­source=wlan0 and also change wlan0 to wlan1 . It’s now pos­si­ble to run Kis­met, and it’ll de­tect all the var­i­ous lo­cal net­work points and their ge­olo­ca­tions. While it’s not ad­vis­able to run Kis­met as root, for our pur­poses it’s fine. Run it by us­ing the com­mand sudo kis­met . This loads the Kis­met GUI and asks sev­eral ques­tions. It’s pos­si­ble to use Tab to nav­i­gate around.

>> The best startup pro­ce­dure is to leave the set­tings in place. When asked if it’s de­sired to con­nect to the Kis­met server, make sure you press “Yes.” The con­sole log should hope­fully start show­ing all the net­works that it can cur­rently lo­cate [ Im­age C]. Be­hind the scenes, the Kis­met server also cre­ates sev­eral groups of log files. As­sum­ing

the test run goes as ex­pected, and there are log files in /home/pi, it’s time to shut down the Raspberry Pi, and do it for real. To exit Kis­met, use Ctrl-C. 6 AC­CESS FOR ALL The last big con­fig­u­ra­tion item is set­ting up the wlan1 to be an ac­cess point. Th­ese con­fig­u­ra­tion steps are based on the of­fi­cial Raspberry Pi guide ( www.rasp­ber­rypi.org/doc­u­men­ta­tion/ con­fig­u­ra­tion/wire­less/ac­cess-point.md). This means that your lap­top can see and con­nect to the Pi without hav­ing to worry about IP ad­dresses. To set up this con­fig­u­ra­tion, there are two items of soft­ware that are needed: dns­masq and hostapd. Again, in­stall them with:

sudo apt-get in­stall -y dns­masq hostapd

>> The dns­masq tool takes care of the DHCP an­gle, while the hostapd util­ity pro­vides the items that are needed to con­fig­ure the wire­less ac­cess point. First, we need to change the way wlan0 works. Edit the DHCP con­fig­u­ra­tion file ( sudo nano /etc/dhcpcd.

conf ) and add the fol­low­ing en­try to the bot­tom of the file: deny­in­ter­faces wlan0 7 CON­FIG­URE IN­TER­FACE You also need to set up the in­ter­faces con­fig­u­ra­tion for wlan0. Just like was done ear­lier when we set a static IP ad­dress for the wired net­work, the same needs to be done for the wlan0 in­ter­face. Edit it to use the fol­low­ing ( sudo nano /etc/ ): net­work/in­ter­faces al­low-hot­plug wlan0 iface wlan0 inet static ad­dress net­mask net­work

>> Once set up, save the changes, and exit. Restart the ser­vices as de­tailed be­low. If you’re not us­ing a wired con­nec­tion, the in­ter­face restart would cause is­sues. It’s bet­ter to do a restart ( sudo re­boot now ). sudo ser­vice dhcpcd restart sudo if­down wlan0 sudo ifup wlan0 >> Con­fig­ure the DNS set­tings by edit­ing the dns­masq

file ( sudo nano /etc/dns­masq.conf ), and add the in­ter­face de­tails as shown be­low:

in­ter­face= wlan0 # Use the re­quire wire­less in­ter­face - usu­ally wlan0 dhcp-range=,,,24h >> Fi­nally, set up the AP con­fig­u­ra­tion file ( sudo nano /etc/

), adding the items shown be­low: hostapd/ hostapd.conf in­ter­face= wlan0 driver=nl80211 ssid=MyNet­work hw_­mode=g chan­nel=7 wm­m_en­abled=0 macad­dr_acl=0 au­th_algs=1 ig­nore_broad­cast_s­sid=0 wpa=2 wpa_­passphrase=MyPass­word wpa_key_mgmt=WPA-PSK wpa_­pair­wise=TKIP rsn_­pair­wise=CCMP

>> Edit the hostapd file, changing the DAEMON_CONF line to re­flect that be­low ( sudo nano /etc/de­fault/ hostapd): DAEMON_CONF=”/etc/ hostapd/ hostapd.conf”

>> Turn­ing on the ser­vices at boot is achieved with: sudo ser­vice hostapd start sudo ser­vice dns­masq start

>> Restart the ser­vice to in­sti­gate the changes: sudo ser­vice ntp restart 8 TEST THE SETUP To test the en­vi­ron­ment, shut down the Pi, and re­move the Eth­er­net and HDMI ca­bles. Power it up, con­nect to the newly created AP, and, from your lap­top, ac­cess a re­mote shell into the AP point. Shut down, then place the Pi in your car. Be aware that the Pi may not boot prop­erly if there’s in­suf­fi­cient power. It may look pow­ered on, but it doesn’t fully boot. Check power re­quire­ments be­fore do­ing any­thing. At a push, you can power it from your lap­top’s USB port. Ex­ter­nal bat­tery packs are an op­tion, too—be sure to read the man­ual, to en­sure it can pro­vide enough power. >> As­sum­ing the Pi has pow­ered up, and you can ac­cess it via ssh , run the Kis­met ap­pli­ca­tion us­ing: ssh pi@ sudo kis­met

>> Fol­low the ear­lier rou­tine to ac­cess the Kis­met con­sole. 9 CON­VERT THE DATA Go for a short drive, then come back. All the data will have been col­lected au­to­mat­i­cally, and placed in files within the /home/pi folder. Use the lap­top to grace­fully shut down the Pi us­ing the com­mand sudo shut­down -h now . Th­ese files con­tain GPS data and Wi-Fi base sta­tion data, but are cur­rently not in a for­mat that Google Maps can un­der­stand. To do the con­ver­sion, we’re go­ing to use a script by Scott Helme to con­vert it into a map­ping file. To do this, use the com­mand: wget https://gist.githubuser­con­tent.com/Scot­tHelme/5c6869e1 7c3e9c8b2034d­c8fc13e180b/raw/31c2d34f66748b6b­d26415fd7d1 20c06b3d92eaf/netxm­l2kml.py -O netxm­l2kml.py

Or the fol­low­ing short­ened link: wget http:// bit.ly/LXF230netxml -O netxm­l2kml.py

>> To cre­ate the map­ping file, run the com­mand: cd / home/pi python netxm­l2kml.py --kml -o out­put *xml

It may take a minute or two, but it even­tu­ally de­liv­ers a sum­mary of the Wi-Fi base sta­tions found.

>> Ob­vi­ously, the data is still on the Pi, so it needs to be copied across. Copy the file us­ing scp to your lo­cal PC, sub­sti­tut­ing val­ues as needed. Use the fol­low­ing com­mand to copy the data across from the Pi: scp / home/pi/*.xml myuser@my­work­sta­tion:/ home/myuser

10 IM­PORT INTO GOOGLE MAPS At this point, the data file should be in the cur­rent di­rec­tory from which the com­mand was run. The fi­nal step is to im­port the XML file into Google Maps ( www.google.com/maps/d/home). Se­lect “Cre­ate New Map.” Add a layer. This will show a win­dow that will en­able the user to add the newly created file. This could po­ten­tially take sev­eral min­utes to com­plete. Once done, you should be able to see all those Wi-Fi spots mapped, along­side var­i­ous de­tails of the pro­to­cols sup­ported, and other in­ter­est­ing items [ Im­age D].

>> Cre­ate a raw Google Map. To im­port the data, se­lect “Add New Layer,” then click “Im­port.” Drag in the saved file. It’ll take a mo­ment to up­load and im­port the data. Once com­pleted, the map shows the lo­ca­tion, Wi-Fi sta­tion name, en­cryp­tion schemes, and other data.

>> We hope that this guide has been use­ful. There are many other ad­di­tions and con­fig­u­ra­tions that can be ap­plied upon the foun­da­tion of this fun project, but that’s for you to ex­per­i­ment with. Please let us know how you get on—this is your chance to in­spire other Max­i­mumPC read­ers and the en­tire Raspberry Pi community.




Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.