Calculating the distance and bearing between two GPS points

I have been searching for a solution to calculate the distance and bearing of a target gps location for a while now. This page always popped up but my brain hurt every time I tried to convert this to lua.

I am happy to say I have cracked it and here is my code.

function Geo_Angle( lat1, lon1, lat2, lon2)
  local dLon = math.rad(lon2-lon1);
  local y = math.sin(dLon) * math.cos(math.rad(lat2));
  local x = math.cos(math.rad(lat1)) * math.sin(math.rad(lat2)) - math.sin(math.rad(lat1)) * math.cos(math.rad(lat2)) * math.cos(dLon);
  local brng = math.deg(math.atan2(y, x));
  return ((brng + 360) % 360);

The distance code was created by roaminggamer else on the Corona forums (can’t find their name).

function Geo_Distance(lat1, lon1, lat2, lon2)
  if lat1 == nil or lon1 == nil or lat2 == nil or lon2 == nil then
    return nil
  local dlat = math.rad(lat2-lat1)
  local dlon = math.rad(lon2-lon1)
  local sin_dlat = math.sin(dlat/2)
  local sin_dlon = math.sin(dlon/2)
  local a = sin_dlat * sin_dlat + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * sin_dlon * sin_dlon
  local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
  -- 6378 km is the earth's radius at the equator.
  -- 6357 km would be the radius at the poles (earth isn't a perfect circle).
  -- Thus, high latitude distances will be slightly overestimated
  -- To get miles, use 3963 as the constant (equator again)
  local d = 6378 * c
  return d

Here is how I call the code

print("Bearing between Tamworth and Armidale: " .. tonumber(GeoAngle(-31.1063157, 150.9307341,-30.5143425, 151.66696439999998)))
print("Distance between Tamworth and Armidale: " .. tonumber(GeoDistance(-31.1063157, 150.9307341,-30.5143425, 151.66696439999998)) .. "km")


Corona Simulator[4155:693467] Bearing between Tamworth and Armidale: 47.07677793374
Corona Simulator[4155:693467] Distance between Tamworth and Armidale: 96.419986257454km

When comparing my results with this site my bearing results are within 0.18677793374 degree and 0.078686257454 km for distance (good enough for my simple augmented reality app).

I tried to trick it by putting the source/dest either side of GMT and it did not measure the longest route around the globe.

Corona Simulator[4288:936166] Bearing between -179 lon and 179 lon: 270
Corona Simulator[4288:936166] Distance between -179 lon and 179 lon: 222.6341993844km

Also putting a source/dest near each pole calculated the approximate right distance between the coordinates.

Corona Simulator[4288:936166] Bearing between -89 lat and 89 lat: 180°
Corona Simulator[4288:936166] Distance between 89 lat and 89 lat: 19814.443745211km

Setting up eth0 and wlan0 on a Raspberry PI RevB (no drivers)

Recently I setup a raspberry pi to talk to the internet via ethernet here. I asked myself how do you configure Ethernet and wireless on the radpberry pi?

Today I added a wireless card and set it up so it can talk to the network too.

Step 1: Wireless Dongle.

I purchased 1x EDUP Model EP-N8537 Wireless LAN 150Mbps USB Adapter from eBay for $7.95 ( , item).


Step 2: Configuration.

I plugged my new Wireless card into Windows and ran the following command to get the MAC Address for the new card. Write the Mac Address down.

    ipconfig /all

I then connected to my ADSL router and went to the Static IP section under LAN/DHCP.

I added a static IP reservation for this mac address to my router and gave it an IP that I was happy with.

I then booted the Raspberry Pi (don’t connect the Wireless card yet). I typed the following command to open the network interfaces file.

    sudo nano /etc/network/interfaces

I deleted the contend of my interfaces file and typed this in.

    auto lo
    face io inet loopback

    iface eth0 inet static

    auto wlan0
    allow-hotplug wlan0
    iface wlan0 inet static
    wpa-ssid yourwirelessnetworknamehere
    wpa-psk yourwirelesspasswordhere

If you are unsure of what to type above go and check out the hundreds of awesome and free security/networking podcasts here

Step 3:Reboot.

I ran a safe shutdown, inserted the WLAN adapter and rebooted.

Now I can use the ethernet or WIFI.


No drivers or non fee DEB packages were installed to get this NIC working.

Bonus tip: I am using a Ethernet over Power adapter connected to a wireless extender to allow my arduino/raspberry pi to connect to my router.

Arduino/Pi -> Gigabit Switch -> EoP A -> 240v (50m) -> EoP B -> 100MB Switch -> WiFi (30m) Extender A -> WiFI -> WiFi Extender B -> Router -> Internet 🙂

How to create custom partitions on a usb key (boot key) on Windows 8.1

Windows 8.1 does not allow you to create a custom partition size on a USB key (needed for some netbooks to boot from USB key).

    1. Open Elevated Command Prompt
    2. Run diskpart
    3. Type Rescan
    4. Type “list disk” to show all disks.
    5. Type the disk that you want to partition (in my case partition “4”). Make sure you select the right disk or your will delete your data with no prompts).

Enter these commands (note my custom partition size if 3072, change to your custom size).


Create Part Primary size=3072



Format fs=fat32 quick Label=”Boot”

Create part primary


Format fs=fat32 quick Label=”Deploy”


Setting up an iPod for a non-adult (unofficial guide)

Apple for legal reasons do not allow minors to setup an Apple ID and purchase music, apps, games etc. If you try and create an Apple ID for a minor apple will say thanks but no thanks. The next best solution is to setup the iPod with a parents Apple ID and share music and content to the device on a file by file basis (this is a pain), but what happens when the child grows up and leaves home ? There is no way you can split years of paid music/videos into a different account. Also sharing a parents Apple ID causes issues with purchasing content as an adult and child, Apple Gift cards received at XMAS would have to be handed to the parent to then make purchases.

This guide is not the official method for setting up an iPod for a (responsible) child but I see this as the best way to setup a new iPod for a minor.

Steps to Setup an iPod for a minor (“joe blogs”) but with age appropriate restrictions

  1. Create a separate desktop (Windows or Mac) login for “joe blogs”.
  2. Download the latest version of iTunes.
  3. Plugin the iPod into the computer.
  4. Allow the iPod to backup and update the iPod operating system to the latest version (e.g 7.0.4).
  5. Create an email address for “joe blogs” (I recommend you buy your own domain (e.g ) they you can create email accounts for your family (e.g [email protected])
  6. On the Desktop (Windows or Mac) open iTunes and click the Store then Create Apple ID Account. Link the apple ID to the new email account and set the age to the parents age (not the child’s). Set a VISA Debit as the payment type (don’t worry we won’t be making any purchases) this is just a temporary measure to verify the account is an adults.
  7. Verify the apple ID email that was sent to the email address.
  8. Open the Music app on the iPod, Click store then scroll to the bottom of the screen. Click Sign In. Sign in with the apple ID.
  9. Redeem a gift card ($30 would be fine).
  10. Go to the Apple Music store and make a purchase, Apple will ask you to verify the debit card CCV before making purchase. The purchase should come from the gift card credit and not the debit card.
  11. Open the music app, click store, scroll down to payment information and remove VISA card from payment information (select none).
  12. Open iTunes on the desktop and open the store menu then view account, sign in and click Payment Information. Remove and debit cards from the payment list.
  13. Now on the iPod go to Settings, general, Restrictions, select a password that only the adults will know).
  14. Specify age appropriate settings in the restrictions screen (Go hard and lock things down, worst case you can unlock features later).
  15. Go back to settings then general and set a passcode lock.
  16. Educate you kids on using gift cards, not buying in app purchases and virtual goods.
  17. Monitor your kids often.

Now the minor can have a locked down age appropriate iPod but still receive gift cards and purchase under their own name instead of searching for shifty/free content.
Hope this helps someone.

PiFace, Python and controlling basic circuits

Setting up the PiFace

Source Setup Script:

    sudo apt-get install -y python-dev python-gtk2-dev git
    pushd ~/
    git clone
    pushd piface/python
    sudo python install
    sudo piface/scripts/spidev-setup

Quick Circuit Setup


Sample Python code to power my 5x 5v lights and 1 speaker.

    from time import sleep
    import piface.pfio as p
    # while True:

    # Front LED On

    # Speaker Test

    for intloop in range(3):

    # Red Led Test

    # Yellow Led Test

    # Green Led Test

    #2 light Test


    #1 Ligth test


    # Speaker Test

    #Turn All On


    #Turn Front Led Off

Custom Pi Case and Basic Circuits

Custom 5 1/4 Floppy Pi Housing (w/basawood floor).

Custom 5 1/4 Floppy Pi Housing (Front).

Basic LED circuit for my 3yo.

3 LED circuit for my 3yo.

Breadboard mounted in my Custom 5 1/4 Floppy Pi Housing.

Breadboard mounted in my Custom 5 1/4 Floppy Pi Housing (Top).