Friday 10 July 2015

A new project!

Finally! I am working on something fun.

I haven't posted on here in what seems forever, not because I haven't been tinkering, but because most of the things I have been making have just been schoolwork related. I haven't had a good personal project worthy of sharing.

But now I am making a Weasley Clock!

Some people have made working Weasley Clocks before, like this beautiful thing and This LED whatsit, but I wanted to have a go at making my own and solving the problem myself. I don't know how to make the intricate mechanical workings that would be needed for the multi-handed clock that shows the location of all family members, so mine is just going to have one hand and show my location.

The project is broken down like this:

  1. Make a smartphone app that periodically sends location data to a central location, preferably a database under my control rather than Twitter or some such.
  2. Write a program for a Raspberry Pi that monitors this database and retrieves the location.
  3. Create a mechanical clock, controlled by the Pi, that turns hands to the correct location.
So far I have pretty much completed the first two steps. Step 1 was surprisingly tough. I have some experience making Android apps using the MIT App Inventor, but this has limitations. Specifically, I need my app to run constantly, in the background, checking location and sending out the data. App Inventor apps can't do this. You have to have the app open on the screen to function correctly, so that was no good.
I have only ever played with making proper Android apps once, and only got as far as the Hello World-style "My First App" on the developer site. This made me realise how painful it can be to get even a simple app up and running on a smartphone. I don't know if I was doing it wrong, but it seems that you need to jump through a lot of hoops to even get one button on a screen talking to another item. There's a complex system of messages being sent back and forth, called Intents, which I don't fully understand. I guess I could spend a bit longer looking into it, but I just wanted to get this thing working without spending the whole summer on it.

My app needed to be location-aware, so I needed to access the GPS functions of the phone. Google have integrated location services into their ecosystem, one feature of which is something called Geofencing. This sounds like exactly what I need. You set up a series of areas, by defining a location and a radius around it. This forms a "fence". Whenever the phone moves into or out of this fence, your app can be notified of it. Perfect! All I needed to do was declare "home", "work" and any other places I might like to appear on my clock and it should work. Except for some reason it didn't. I spent a day playing with this, but the notifications were very sporadic and unreliable.

In the end, I gave up this approach and looked around for other methods. I ended up going with Repeating Alarms. These can be set to go off at set intervals, and can trigger a process in your app. The app doesn't need to wake up the phone, or display anything. It can just do its job and go back to sleep until the next alarm.

The app checks the location, then works out the distance to each of a hard-coded set of places using this algorithm. For each location I have also stored a radius, representing the size of the area I consider being "home" or "work" etc. The program decides if you are in each area, then sends a single ID number to the central database, with 0  being "home", 1 being "work" etc. I decided to only send an ID number, rather than the exact location, for privacy reasons. The interface is almost non-existent, but I would eventually like to add the ability to customise the list of locations. This would not be used very often, as it has to match the set of locations physically printed on the clock, but it would be a nice feature. Then I could maybe make the app available to other people, once I have added some security features, so they could use it in their own projects.

So, Step 1 is done. It's running now and storing my location ID every 10 minutes. You can see it in its testing phase now at this page.
It will eventually require a password to access, but since I am only using index numbers the information is of little use to bad people.

Programming Step 2 was trivial. I made a small PHP web page on my own web server that creates an API for storing and retrieving the data in a number of ways. Then I wrote a Python program that retrieves the location. This is just 3 lines of code. Accessing web pages through Python is easy (are you listening, Android?)

Step 3 is the next challenge. I have to work out how to program a motor using the GPIO pins of the Raspberry Pi to turn to particular angles, accurately and reliably over time, then mount that in some sort of clock casing. I have bought a very cheap stepper motor, which I think will give me the accuracy I need, and I am now looking into how to set it up.

More details when I have them!



No comments:

Post a Comment