Skip to main content

Castle Track-a-lot



As the wondrous, (totally) bona fide folklore of the British Isles describes it, atop the mythical hilltop of Glastonbury Tor, King Arthur convinced his brother from another mother Merlin to abandon sorcery, and embrace the power of single-board computers, GPS and cloud computing. While the other wise knights at the "totally inclusive" round table discussion continued to remain sceptical of the relevance of blockchain in resolving all of the medieval world's ailments, such as war, famine, plague and unbearable dandruff.  All except Percival, the head of marketing at Camelot Inc, for whom this quarter's bonus was contingent on him selling more distributed ledger solutions to stonemasons, roofers and ironmongers.  But that particular subreddit is for another day.

Because there was a number of matters the court did all unanimously agree on during one of their daily stand-ups, in front of their kanban board littered with squirrel hide.  The 2004 film of their deeds hilariously overplayed their accomplishments... Gladiator was approximately ×702 better.  Yes,  Dagonet was kind of an idiot.  And did Arthur really pull a sword out of a stone?  Oh, c'mon!  Nah! Yet they agreed to proof of concept a location-based engagement system across their restless kingdom that would allow their subjects to receive notifications from Groupon about 2-for-1 offers for stylish pouches available at the local tannery.  Perhaps Percival was the charismatic "influencer" everyone listened to, after all.

...Therefore, where better to demonstrate this magic fairy dust that is location based services than on the approaches to the Isle of Avalon.  Or to the unbelieving, that hill near Glastonbury with a ruined tower on top, and still called an island because it was once surrounded by a swamp.

Nope, let's stick with Avalon*.


*No, they did not sell beauty products on your doorstep... that was Avon.

This may be our first ever Rosie warning on the accuracy of historical events depicted in our blog post...

If you are currently embarking on a PhD on the Arthurian legend (that might or might not be true), or stumbled upon this post when trying to verify the authenticity of the great king himself (who might or might not have existed)... please abandon this post now.  Seriously.

This blog contains a wholly fictitious account of a period in British history based on the lyrics of a song that a friend of a friend's piano teacher's dog-walker was once heard singing in the shower by the Alsatian itself.  The words were - word for word - paw-printed on some nearby toilet paper using leftover shampoo that ended up being flushed down the loo, but has since been discovered and reworked for a new audience by Mills & Boon. Yes, our story is that true.

The Under Armoury:

When brave, legendary, perspiring warriors of questionable historical accuracy with no access to deodorant do battle, they inexplicably equip themselves with all sorts of holy and blessed artefacts forged out of endangered and exotic commodities. Like dragon teeth, mermaid scale, and extremely limited England football team World Cup winner's medals.  Lynx - on the other hand - isn't one of them.

  • First of all, we have in our clenched fist the indisputably legendary, almighty, single-board computer itself, the Sir Trackalot of small-factor computers: a Raspberry Pi running Raspbian OS.  We're using a Zero, since that is a number closest to the computing power required to take a measurement or two using a GPS receiver.  And it's smaller and lighter than its bigger, meaner siblings... which means it fits snugly underneath our Ted Baker chain mail.
  • Attached to the Pi using UART TX/RX connections is a U-Blox Neo-6 GPS receiver module.  It is kind of crucial, if we want to track stuff in open space such as a marshland which should never be crossed in full plate armour.  Beam Me Up Rosie! was where we first encountered these nosey devices that demand to know where we've been.
  • ...And that's about it.  Rest of the alchemy happens by doing battle with a mouse and keyboard.  Or more specifically, by interacting with the king's unicorn start-up that he forgot to declare to his fellow knights - Arthurian Web Services.

We're ready to do battle!  Oh wait.  The bard has started to pluck the lute. And he thinks it's paramount that we're furnished first with embellished stories of past glory...

Bard rate 9600:

This is the second instalment in our adventurous series - I-O-Mr-T - in which we decided to take our random assortment of electronic gadgetry outdoors.  Which means - rather anticlimactically - there has only been one tale of tragedy and woe to hum about to date: Gold Filling.

Arguably, looking further back into the Dark Ages like a true historian, Taking a Peak introduced us to our current obsession with tracking stuff, and Beam Me Up Rosie! was where we first encountered and used global positioning devices.

Ruby on grails:

Apart from the bit of walking at the end, there really isn't much that isn't accomplished here by bashing the keyboard frantically like an agitated orc with a war hammer.

Let's unwind our scroll and bellow out our royal orders.

  • First, we'll create ourselves a DynamoDB NoSQL table in which we will store our locations.  We'll add a single item to it - describing Glastonbury Tor - and furnish that record with additional attributes (like a description, GPS coordinates, entrance fee, Avalon Likelihood Rating, etc, etc).  Later, we did add further information URLs and location of images, as this makes the notification emails - aka spam - come to life more.
  • Then, we'll use the boto3 client to query our table using Python, and using the super useful haversine formula, investigate if we can establish the distance to Glastonbury Tor from our current location.  As friar Bob's inscriptions forewarn us: yes, we can!
  • With a bit of additional code, we can create a little Lambda function to email us (using AWS SES) when we are close enough to Glastonbury Tor, and update our AWS IoT thing's shadow document with a reference to the location.
  • Finally, let's go walking.

Random walk:

With the proliferation of smartphones packed with lots of clever chips that help us navigate and orienteer ourselves, such as GPS, Inertial Measurement Units and Wi-Fi, there has been a medieval siege weapon-grade explosion in location-based services that tailor our experience based on where we are physically positioned.  How else would Google Maps know when to ask us to rate our recent visit to the local greasy spoon cafe, where the spoons are quite (literally) greasy according to the reviews.  We wouldn't know... we only went there to use the loo.

Well, it's easy enough to establish our precise location on Earth using GPS - and this has become a familiar theme across many of our recent adventures.  But we need additional sources of information about what's around us in order to make judgements about where we are in relation to them.  And - most importantly - sell us stuff, lots and lots of stuff, based on where we are with our wallets.

All in all, we'll be using our old lieutenant DynamoDB to house metadata about surrounding locations with the view of making our re-enactment of the Arthurian legend more interactive.

Holy grail!  Is that the time?  Let's go.


Queen GUI-never?

We start our journey in the dark, secluded corner of the AWS console in which there is nothing else to see other than data.  Yes, it's DynamoDB, the true lady of the data lake.

We find ourselves here not because we want to fish a novelty Fisher-Price sword out of the muddy water, but because we want to create a simple reference database containing our locations of interest.  DynamoDB being a managed key-value NoSQL database service, it's ideal for storing these types of documents.  Percival says so.

Let's go ahead and create ourselves a not-so-round table.  We'll call it riot-geo-data.


Empty tables are pointless.  And a sign of peasantry.  In order to demonstrate our regality, we need some data.  Why not start by creating an entry for the Glastonbury Tor.


Unlike our ever-expanding cast of heroic knights, all getting their own spin-off shows, we're not being very ambitious, nor adventurous.  After all, we're shunning the API and congregating around the console to prod our records into life like part-time pikemen.  Oh well.  We're only creating an entry or two at this point.


As you can tell, we've decided that each location should have a few additional attributes.  Most important are the GPS coordinates.  Without these, this entire exercise becomes pretty fruitless.

That's in.  This monk has chronicled its existence in no uncertain terms.  It's official.  We have created our very own historical record of the mystical Somerset hilltop. We ought to throw ourselves a banquet with lots of jesters and noblemen to celebrate this momentous occasion before the plague hits (at which point targeted Groupon marketing becomes secondary to... well... survival).


But the DynamoDB table in itself isn't very useful.  We need to be able to query it.  Preferably, using Python.

Unfortunately for him, Boto wasn't one of the knights invited to serve in King Arthur's court (he wasn't available anyway, he later said, due to a well-deserved internship at a prestigious medieval distillery, Booze Allen Hamilton).  However, his third generation descendant - boto3 - grew up to outdo his family's achievements to become a rather useful AWS SDK for Python.  And importantly for us, it has a highly relevant DynamoDB interface.

Let's use it to query our table.

AWS_ACCESS_KEY = "holygrail"
AWS_ACCESS_SECRET = "holygrail"

import boto3

session = boto3.session.Session(
    aws_access_key_id=AWS_ACCESS_KEY,
    aws_secret_access_key=AWS_ACCESS_SECRET,
    region_name="eu-west-1"
)
dynamodb = session.resource("dynamodb")
table = dynamodb.Table("riot-geo-data")
geo_points = table.scan()["Items"]
print(geo_points)

There we have it.  A list of our locations, all neatly in one place.  In Python.

Incidentally, as our table grows, this method will quickly become unmanageable as we're pulling down all items in our table using scan().  In reality, we would want to tailor the query so we only bring back items of relevance.  This might, for example, require us to perform a preliminary calculation to filter out results using a range of "near enough" coordinates.


Now, if we think back to what we did back in Dis["orientated"], we learnt that the Earth isn't flat.  And that we need a special formula to calculate distance between two GPS coordinates.  Haversine is that formula.  And there is a Python library for it.

Let's pretend that we're in Glastonbury Abbey (51.145556, -2.714444).  And let's see how far away we're from the Tor.

The haversine.haversine() function only requires the two set of coordinates to return the distance in kilometres.

import haversine
current_pos = (51.145556, -2.714444)
target_pos = (51.144444, -2.698611)
haversine.haversine(current_pos, target_pos)

There we have it. Still 1.11km away, apparently. Not quite there yet.  There's a bit of walking to be done.


What we can also do at this point is cycle through our list of locations from DynamoDB, and test for each how far away we currently are (another reason why we ought to be filtering our DynamoDB query).  We then apply an arbitrary threshold (200m).  If we're closer than this distance, we'll tell ourselves that we've arrived. 

current_pos = (51.145556, -2.714444)
distance_threshold = 0.2
for geo_point in geo_points:
    _distance_km = haversine.haversine(
        (geo_point["latitude"], geo_point["longitude"]),
        current_pos
    )
    print("Distance (km) to:", geo_point["name"], round(_distance_km,3))
    if _distance_km < distance_threshold:
        print("You've arrived at:", geo_point["name"])
    else:
        print("Not at", geo_point["name"], "yet")

All a bit clunky.  And that threshold should probably be investigated further.  But we can now start to see how we have started to position ourselves in relation to the locations around us.


Let's now pick a random point on the approach up to the Tor.

current_pos = (51.144266, -2.699791)


We re-run our code and it has decided for us that we are there.


Now this only works because we're dealing with a specific point, which could be a person or a mountain summit.  But most locations span greater distances, and have complex shapes.  Imagine a park, or a city.  True art of geo-fencing would require us to deal with boundaries, and mathematically calculate if we are within the location's polygon.  Perfectly doable, but slightly more computationally intensive.

It's also important to remind ourselves that GPS is just one way of positioning objects.  In built-up areas, with lots of Wi-Fi hotspots, using a Wi-Fi based positioning system might be more useful.  But this relies on availability of Wi-Fi signals.  And the crowd sourcing, collection and sharing of Wi-Fi hotspot details.  Sounds a bit iffy for our use case.

Anyhow, what's left for us to do?

We never intended to wander around the Somerset countryside with a laptop.  So we'll transfer this code into a Lambda function to run in Arthur's cloud computing start-up, and plumb it into an AWS IoT rule.  In other words, for every positional payload that arrives from our intrepid thing, the Lambda function will run and evaluate its proximity against all locations in our DynamoDB table.


Notice how we had to download and zip up the haversine module alongside our code.  It's clearly an external dependency for our program to run.

All in all, we'll send ourselves an email notification using AWS Simple Email Service.  And also update the thing's device shadow with the location's code.  Clearly, it could be so much more.  Such as push notifications to a mobile app, or an old-school SMS.

This Lambda function forms part of a grander experiment, and can be found here.

The merry band of holy knights are approaching the tower, with a singing minstrel in tow.  How do we know?  Because Merlin has now renounced sorcery, and has fully embraced the power of cloud and IoT.


As we approach the Tor, we receive a friendly notification email.  Did we find the Holy Grail?  Oh yikes. It might be buried back in the Glastonbury Abbey, apparently.  Or according to Indiana Jones, somewhere else altogether.


Let's now fast forward a few centuries.  This location has absolutely nothing to do with the legend of King Arthur.  But it happened to be on our family's weekend itinerary (and one of our favourite locations to visit).  This is Dyrham Park, an impressive stately home to the north of Bath.


There's a bit of walking required from the car park to the estate and back.  So let's get recording with our home-made GPS tracker thingy.


As you might have guessed, we arrived on-site prepared.  We had entered into DynamoDB a small number of records related to some interesting locations around Dyrham Park.

Which is why our inbox had been visited by several emails during our trip.  If these timestamps look a little suspicious, it is because our payloads were cached using our technique described in Gold Filling.  In other words, we allowed the Pi to connect to the Internet - via our smartphone's hotspot - only twice during our visit.


Did we remember to lock our horse and carriage?  Probably.


Lastly, we reached the conclusion that it is a very good idea to store the current location of the thing in its shadow document.  Not only does this allow us to view which location the device might be in, but it also allows us to perform a simple test in our Lambda function to see if we're already in that location or not.  If we don't do this, we would continuously receive notifications for every payload that is sent back while our device remains in the same location.


Now we're acutely aware that this series is increasingly becoming a travel-blog, carefully disguised as a site about hands-on IoT shenanigans. And if we continue for much longer in this vain, we'll be accused by our audience of being part of a grand conspiracy involving the shady, secret society known as Visit Somerset.  Which is why before Dan Brown comes calling, we're going to call it a day and park our little Arthurian tale in the car park of one of many wonderful National Trust attractions available in the beautiful countryside of Somerset.  Your next amazing holiday destination.


Run these script-ures:

Ah yes, DynamoDB.  We missed you.  It's useful for storing data in, like location information:
Boto3 for DynamoDB is probably the best way to interact with the service if using Python:
Lambda features heavily here, there and everywhere in AWS:
Where else would we go, if not the Python haversine library, when trying to work out the distance between two coordinates:
We like spamming ourselves with lots of mail.  Some might call this Spamalot:
More generally, this entire blog continues to be about AWS IoT:

Comments

MOST VISITED (APPARENTLY)

LoRa-Wan Kenobi

In the regurgitated words of Michael BublĂ©: It's a new dawn .  It's a new day .  It's a new Star Wars film .  For me .  And I'm (George Lucas, and I'm) feeling good .  Unfortunately for Canadian Mike, the Grammy that year was won by the novelty disco classic with the famous refrain: We love IoT, even in Planet Tatooine * . *Not true. Clearly, the Star Wars producers didn't sincerely mean the last Jedi the previous time around.  Return of the Jedi, released during the decade that spearheaded cultural renaissance 2.0 with the mullet and hair-metal , was less economic with the truth.  Either way, we're going to take inspiration from the impressive longevity of the money-spinning space-opera and reboot our franchise with some Jedi mind tricks.  Except this particular flick doesn't require an ever-growing cast of unrecognisable characters, unless ASCII or UTF counts.  In place of an ensemble gathering of Hollywood stars and starlets, we will b

Battle of BLEtain

The trolling . The doxing . An army of perplexing emojis. And endless links to the same - supposedly funny - viral video of a cat confusing a reflection from a dangling key for a golden hamster, while taking part in the mice bucket challenge. Has social media really been this immense force for good? Has it actually contributed significantly to the continued enlightenment of the human (or feline) race? In order to answer these poignant existential questions about the role of prominent platforms such as Critter, StinkedIn and Binterest, employing exceptional scientific rigour equal to that demonstrated by Theranos , we're going to set up a ground-breaking experiment using the Bluetooth Low Energy feature of MicroPython v1.12, and two ESP32 development boards with inexplicable hatred for one another.  And let them hurl quintessentially British expressions (others call them abuse) at each other like two Wiltshire residents who have had their internet access curbed by the co

Hard grapht

You would all be forgiven for assuming that bar , pie and queue line are favourite pastimes of the British .  Yet, in fact – yes, we did learn this back in GCSE maths – they are also mechanisms through which meaningless, mundane data of suspect origin can be given a Gok Wan -grade makeover, with the prime objective of padding out biblical 187-page PowerPoint presentations and 871-page Word reports (*other Microsoft productivity tools are available).  In other words, documents that nobody has the intention of ever reading.  But it becomes apparent over the years; this is perhaps the one skill which serves you well for a lifetime in certain careers.  In sales.  Consultancy.  Politics.  Or any other profession in which the only known entry requirement is the ability to chat loudly over a whizzy graph of dubious quality and value, preferably while frantically waving your arms around. Nevertheless, we are acutely conscious of the fact that we have spent an inordinate amount