Arduino Based PIR Motion Sensor with iOS Push Notification

We’ve been having some problems with our cat staying out late over the last few weeks, and expecting to be able to summon us at the back door. As I’m not keen on standing guard waiting for him, I’ve decided to revert to type and build a solution around one of my Arduino boards.

I’ve used various bits of example code, and it’s all working. First the hardware, which is a real thing of beauty(!)

PIR Sensor

PIR Sensor

So we have a PIR sensor in a yoghurt pot. I’ve enclosed the sensor in a tube to try to make it more directional. The sketch I’ve used is from the Arduino Playground. The only change I’ve made is to comment out every Serial.Print[ln] command, except for the one that identifies the end of the motion detection period.

Next, on the Raspberry Pi, I installed this variant of the standard serial port library for Perl.  I’ve used the example script to make a call to curl, using the system(“curl….”); command.

Next comes the slightly trickier part. I did a bit of a trawl around yesterday to see if there were any nice push notification services. This is a pretty complicated sport, but long and short of it is that I went for a free option from an organisation call PushApps, who have detailed instructions on how to set yourself up with the various certificate options on the Apple Developer Portal, and then how to integrate the actual code into your app. I just went for a very rough and ready single view app for now. It’s not doing anything other than displaying the remote notification.

The actual process of integrating the notification functionality into an app is very straightforward.

That said, there are two moderately tricky parts to this. The first is to make sure that you configure Xcode to pick up the right ‘provisioning profile’ that you have to go through in the instructions.

The second is – well, actually pretty straightforward with the benefit of hindsight, but it took me a while to translate the example provided in the documentation in PHP into something I could use – via curl. It’s a call to PushApps’ JSON interface, which brings us back to the perl script. When I read a string from the serial port equating to a motion detection event I call:

curl -H “Content-Type: application/json” -d ‘{“SecretToken”:”your-secret-token-here”, “Message”:”short message to be displayed”}’ https://ws.pushapps.mobi/RemoteAPI/CreateNotification

which, lo and behold, will send a push notification event. The example code takes care of the registering of the phone [well, the app installed on the phone] to pick up the notification.

I’ve left the curl command unescaped for readability purposes. When you put it inside the system() command, you have to escape all of the double quotes with a backslash.

if this doesn’t work, there’s nothing for it, the cat will have to go 🙂

I initially tried making the call using the web client on the Arduino WiFi shield. You can’t: there’s no support for TLS.

Jedi Cat

These aren't the whiskers you are looking for

I have absolutely no justification for this picture of Ping – and the internet can survive without yet another cat picture – other than it makes me giggle.

Our New Light Fitting

Ping, our Siamese, is obsessed with climbing on top of our kitchen cupboards, which are no doubt covered in dust. It’s not clear from the perspective, but the top of his ear is millimetres off the ceiling in this picture. I bounced the flash to try to minimise the shadow. It took quite a few attempts to get the manual exposure down. This is F13 at 1/80th second, with the flash tuned down by -1/3.

Ping