{"id":1156,"date":"2014-08-04T21:28:52","date_gmt":"2014-08-04T21:28:52","guid":{"rendered":"https:\/\/zogspat.tk\/blog\/?p=1156"},"modified":"2014-08-04T21:28:52","modified_gmt":"2014-08-04T21:28:52","slug":"arduino-based-pir-motion-sensor-with-ios-push-notification","status":"publish","type":"post","link":"https:\/\/the-plot.com\/blog\/?p=1156","title":{"rendered":"Arduino Based PIR Motion Sensor with iOS Push Notification"},"content":{"rendered":"<p>We&#8217;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&#8217;m not keen on standing guard waiting for him, I&#8217;ve decided to revert to type and build a solution around one of my Arduino boards.<\/p>\n<p>I&#8217;ve used various bits of example code, and it&#8217;s all working. First the hardware, which is a real thing of beauty(!)<\/p>\n<div id=\"attachment_1158\" style=\"width: 160px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/the-plot.com\/blog\/wp-content\/uploads\/2014\/08\/IMG_24991.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1158\" class=\"size-thumbnail wp-image-1158\" alt=\"PIR Sensor\" src=\"https:\/\/the-plot.com\/blog\/wp-content\/uploads\/2014\/08\/IMG_24991-150x150.jpg\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-1158\" class=\"wp-caption-text\">PIR Sensor<\/p><\/div>\n<p>So we have a PIR sensor in a yoghurt pot. I&#8217;ve enclosed the sensor in a tube to try to make it more directional. The sketch I&#8217;ve used is from the <a href=\"http:\/\/playground.arduino.cc\/Code\/PIRsense\">Arduino Playground<\/a>. The only change I&#8217;ve made is to comment out every Serial.Print[ln] command, except for the one that identifies the end of the motion detection period.<\/p>\n<p>Next, on the Raspberry Pi, I installed this <a href=\"https:\/\/github.com\/syxanash\/Device-SerialPort-Arduino\">variant<\/a> of the standard serial port library for Perl. \u00a0I&#8217;ve used the example <a href=\"https:\/\/metacpan.org\/pod\/release\/SYXANASH\/Device-SerialPort-Arduino-0.07\/lib\/Device\/SerialPort\/Arduino.pm\">script<\/a>\u00a0to make a call to curl, using the system(&#8220;curl&#8230;.&#8221;); command.<\/p>\n<p>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 <a href=\"https:\/\/wiki.pushapps.mobi\/display\/PUSHAPPS\/iOS+Getting+Started\">detailed instructions<\/a> 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&#8217;s not doing anything other than displaying the remote notification.<\/p>\n<p>The actual process of integrating the notification functionality into an app is very straightforward.<\/p>\n<p>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 &#8216;provisioning profile&#8217; that you have to go through in the instructions.<\/p>\n<p>The second is &#8211; 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 &#8211; via curl. It&#8217;s a call to PushApps&#8217; 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:<\/p>\n<p>curl -H &#8220;Content-Type: application\/json&#8221; -d &#8216;{&#8220;SecretToken&#8221;:&#8221;your-secret-token-here&#8221;, &#8220;Message&#8221;:&#8221;short message to be displayed&#8221;}&#8217;\u00a0<a href=\"https:\/\/ws.pushapps.mobi\/RemoteAPI\/CreateNotification\">https:\/\/ws.pushapps.mobi\/RemoteAPI\/CreateNotification<\/a><\/p>\n<p>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.<\/p>\n<p>I&#8217;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.<\/p>\n<p>if this doesn&#8217;t work, there&#8217;s nothing for it, the cat will have to go \ud83d\ude42<\/p>\n<p>I initially tried making the call using the web client on the Arduino WiFi shield. You can&#8217;t: there&#8217;s no support for TLS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;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&#8217;m not keen on standing guard waiting for him, I&#8217;ve decided &hellip; <a href=\"https:\/\/the-plot.com\/blog\/?p=1156\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1156","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1156"}],"version-history":[{"count":2,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1156\/revisions"}],"predecessor-version":[{"id":1160,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1156\/revisions\/1160"}],"wp:attachment":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}