Monday, December 14, 2015

MAME4all-pi

I have no super interesting updates for now other than me receiving the Mayflash Arcade Stick and making sure it works on the Raspberry Pi running the MAME4all-pi emulator which you can download here.


Installation of MAME4all-pi is as simple as downloading the zip file, unzipping it (using the unzip command), then running mame. In between unzipping and running mame, you have to get the appropriate MAME ROMs you want to play (I won't mention any sources here so don't ask) and copy it to the roms directory under mame4all.

unzip mame4all_pi.zip mame4all
cd mame4all
./mame

Press ESC to exit mame4all and TAB if you want to get to the mame configuration menu. The joystick was plug and play except only the buttons would work but not the directional stick. To make sure the joystick is sending data when the joystick is moved or buttons are pressed, I download the joystick package which includes some useful diagnostic tools. If Raspbian sees the joystick correctly, there'll be a js0 device file in /dev/input representing the 1st joystick. To see the raw data read from this file, you can run the jstest command

sudo apt-get install joystick
sudo jstest --normal /dev/input/js0

As you move the joystick around and press the buttons you can see the values being displayed on the screen. In the out of box state, moving the joystick up and down modifies axis 2 and 3 whereas mame4all is looking for values under axis 0 and 1. Normally you'd need a update the source for mame4all-pi to get this to work and compile it but for this particular joystick, you just have to press the Auto and Clear button at the same time once and it'll toggle the mode to act like a traditional directional joystick. This state isn't persistent and you'll have to do this every time you connect the joystick the first time.

jstest output
Once all is set, then you can use the mame4all-pi configuration menu (by pressing the Tab key while a game is loaded) to map the desired buttons to the whatever you want.


Friday, December 11, 2015

Wifi Access Point

The Raspberry Pi (RPi from here on out) has a built in Ethernet port for network connectivity which is highly useful if you need Internet access or just generally want to add it to your network. If you want wifi capabilities, you can easily just plug in one of many low-cost USB wifi adapters that are natively supported by Raspbian (the preferred Linux build for RPi) for under $10. In this case I've chosen the highly popular Edimax USB wifi adapter.

Edimax USB WiFi adapter
Below is the configuration of /etc/network/interfaces that I use to connect the RPi to my WPA2 protected ASUS wireless access point that is using a hidden SSID and assigned my RPi a static IP. The values for wpa-ssid and wpa-psk are clearly changed and if you're going to use this you'll want to change those to your SSID and wireless password.

 # interfaces(5) file used by ifup(8) and ifdown(8)  
   
 # Please note that this file is written to be used with dhcpcd  
 # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'  
   
 # Include files from /etc/network/interfaces.d:  
 source-directory /etc/network/interfaces.d  
   
 auto lo  
 iface lo inet loopback  
   
 iface eth0 inet manual  
   
 # Static IP assignment connecting  
 # to access point with hidden ssid  
 allow-hotplug wlan0  
 iface wlan0 inet static  
     address 192.168.100.43  
     netmask 255.255.255.0  
     gateway 192.168.100.1  
     wpa-ap-scan 1  
     wpa-scan-ssid 1  
     wpa-ssid "THESSIDHERE"  
     wpa-proto RSN  
     wpa-pairwise CCMP  
     wpa-key-mgmt WPA-PSK  
     wpa-psk "THEWIRELESSPASSWORDHERE"  

All this is very nice and practical but for a project that I'll be elaborating on in a future post, I envisioned the need to be able to connect (via ssh, http, etc) to the RPi via wifi directly without having the need for a separate wireless router. The obvious straight forward approach is to set up the wireless adapter to use AdHoc mode. This was not as elegant as I'd like.

Instead, I wanted the RPi to act as a wireless hotspot or more simply a wifi access point with a broadcasting ssid, password, and a built in dhcp server to issue IP addresses to guests connecting. I found this page (http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router) detailing how you can use  HostAPD and isc-dhcp-server to turn the RPi into a wireless router. For what I wanted, that would be a good solution but since all I wanted was a simple AP, I could skip the routing and NATing parts of the setup. You can read that page for more info but I modified the steps slightly here to show what I did to get it to work. There were some errors with those instructions so I decided to write up all my steps even though there's some duplication. I'm assuming the wifi adapter is detected at this point and the RPi currently has an Internet connection (via the Ethernet port) in order to download and install the requisite software.

Install the DHCP server:
sudo apt-get install isc-dhcp-server

Install the custom version of HostAPD that includes modified network drivers. During compilation, I did see several warnings but it was mostly about unused variables so they can all be safely ignored.
git clone https://github.com/jenssegers/RTL8188-hostapd.git
cd RTL8818-hostapd/hostapd
sudo make
sudo make install

Now we have to prep dhcpd with the subnet and range of IPs to issue. We need to edit /etc/dhcp/dhcpd.conf for that. As mentioned in the referenced doc, comment out these options
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
and uncomment the "authoritative;" line to make it authoritative
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
Add this snippet to the end of the file to configure the dhcp server to issue up to 90 IP addresses in the 192.168.200.0/24 space. Admittedly this is overkill and reducing this to 10 IPs is probably sufficient in which case just change 192.168.200.100 to 192.168.200.20.
subnet 192.168.200.0 netmask 255.255.255.0 {
  range 192.168.200.10 192.168.200.100;
  option broadcast-address 192.168.200.255;
  option routers 192.168.200.1;
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "rpi-local";
}
You can see the complete dhcpd.conf file here for reference.

Next we have to configure the DHCP server to only respond to requests through the wifi adapter since we don't want it acting as a DHCP server over the wired connection. We do this by editing /etc/default/isc-dhcp-server and changing the INTERFACES line to wlan0
INTERFACES="wlan0"

We then have to assign a static IP to the wifi adapter. We'll use the first IP address of the subnet we're assigning to this interface so with the current configuration we're going to use 192.168.200.1. The address we choose in the 192.168.200.0/24 range isn't really important as long as its not in the range we configured for the dhcp server to issue.

Edit /etc/network/interfaces to look like the configuration below. The import part is the static configuration for wlan0 and optionally commenting out wlan1 if you don't plan on having a second wifi adapter plugged in at the same time.
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.200.1
    netmask 255.255.255.0

#allow-hotplug wlan1
#iface wlan1 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Next we have to configure HostAPD with the ssid and wifi password we want to advertise and the password to secure it with. To do this, edit /etc/hostapd/hostapd.conf and change the values for ssid and wpa_passphrase. In my case I used rpilocal as the ssid with a complex passphrase.

Now cycle the wlan0 interface so it picks up the new settings
sudo ifdown wlan0
sudo ifup wlan0

Enable the hostapd and isc-dhcp-server services to run at their configured runlevels
sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable

Start the services to make sure they start up with no errors. If you didn't enable the services in the step above, I found that the hostapd service throws a file not found error when trying to start it. This sequence was reversed in the page I was referencing so make sure you enable it before trying to start the services.
sudo service isc-dhcp-server start
sudo service hostapd start

If all is well then you should be able to see the SSID advertised and be able to connect to it using any wifi client like your laptop, tablet or phone with the password you set. Once connected, you should then be able to ping 192.168.200.1 from the client and connect to the RPi using any means you'd like such as ssh.

RPi Access Point now visible to my iPhone

Known Issues: 
I've observed that on occasion, especially from a cold boot, the RPi can hang when at the login prompt after boot if the wifi adapter is plugged in. I'm still trying to figure out if this is a hardware of software issue but the workaround is:
  1. Boot the RPi without the wifi adapter
  2. Once logged in, plug in the wifi adapter
  3. In a terminal, run ifconfig to make sure that wlan0 is up 
  4. Restart isc-dhcp-server
    sudo service isc-dhcp-server restart
  5. Restart hostapd
    sudo service hostapd restart

Introduction

As a young teenager about 2 decades ago, I got started playing around with computers because I wanted to become a pilot. I was fascinated with flight simulator games at the time and wanted to play them. Back in the mid-90s, playing games like flight simulators still wasn't as plug and play as you'd like and I found myself trying to figure out how to build/repair computers in pursuit of making things work. The most I knew about computers at that time was whatever I observed going to friends' houses to watch them use their parents' or family's computer. Unfortunately, I didn't really know too many people with computers so I spent a lot of time going back and forth to the library trying to teach myself and this was before widespread Internet so resources weren't so easy to come by.


Ironically, and perhaps thankfully,  I spent more time learning about computers and programming than actually playing games. This led to a hobby that would truly fascinate me and stir my imagination that altered the direction of my life to a degree in Computer Engineering rather then Aeronautics and one that would lead to a successful career of now 18 straight years in technology. Nothing really "just worked" back then, at least not in the PC world, so I spent many hours of my life toiling away in front of a computer trying to make sense of it all. The engineer in me loved trying to figure out how and why things worked as I struggled to make this oversized calculator do what I wanted it to do. It became my passion and there is nothing better than earning a solid living from something you don't even consider work.

Over the years, my role in IT has evolved quite a bit as I moved from industry to industry. I have quite literally touched all aspects of IT work from building racks, running cables, to managing large enterprise environments, writing software, publishing two technical books and now doing lots of interesting things in "the cloud" for a financial tech company. What has been lost over time is a little bit of that fire and imagination that once burned brightly in that teenager who felt there was no end to the kinds of things we could bring to life with the help of computers.

Why am I mentioning all this history and why is it relevant to the Raspberry Pi and this blog? Well, I started reading more and more about this super cheap microcomputer designed primarily as an educational tool to encourage a new generation to learn more about computers. I saw it getting picked up by computer hobbyists and then used in all sorts of applications from DIY media centers, to robotics to large parallel miniature compute clusters. This "microcomputer" was the Raspberry Pi (https://www.raspberrypi.org/) and it awoke the curiosity and thirst for tinkering that I once had. With as much compute capacity of a computer about a decade ago in a package no bigger than my wallet, powered by nothing more than a 5V mini-USB port and drawing less than 1 Amp and with GPIO (general purpose I/O) pins that allows you input and output control signals for interfacing with low powered electronics, it's opens up all kinds of opportunities.

This blog is really just for me to document various things I'm trying with the Raspberry Pi as I begin to explore its capabilities. If it's helpful to others, great, but it's not really my primary objective. In many cases I'm going to just try to reproduce some things that others have already written about and maybe document my own experiences with the process, in other cases, I may try to piece together something different. At times I'll be working with existing software packages, other times I may try to code some stuff on my own. It might just be software, it might be a combination of software and hardware (a throwback to my Computer Engineer days playing around with robotics and AI). In most cases, I won't really have a big purpose other than to learn new things. Here goes nothing...

I'll be committing configuration files and code in this general purpose git repository : https://github.com/steguis/hapiprojects