[Webhooks] Super Simple Motion Activated Push Notifications

You are about to witness the power of the Particle Cloud like you've never seen before!

Say you want to use a Motion Sensor to send yourself a Push Notification every time someone enters your office. Would you believe me if I told you all you needed was 4 lines of code? No really! I wouldn't lie to you :angel:


webooks-push.ino

void setup() {
    pinMode(D0, INPUT);
}

void loop() {
    if (digitalRead(D0) == HIGH) {
        Particle.publish("office-motion", "OFFICE", 60, PRIVATE);
        while (digitalRead(D0) == HIGH); // hang tight here until motion stops
    }
}

Hey you don't think that's 4 lines? I could make it one line but that's besides the point... :slight_smile: the meat of the program there is really just 4 lines.


Now let's set up our Push Notification service

To receive your push notification there is a little setup if you've never done it before, but once you do you'll never have to touch it again. There are a ton of services you could use, but the one I like is Pushover.net. Setup an account with Pushover then follow these instructions:

:one: Click on the Pushover logo to get to your dashboard.

:two: Copy your User Key. This is the value for the the user query field below.

:three: Add a device that is going to receive these push notifications. Primarily an iOS or Android phone/tablet... whatever you can download the Pushover app to.

:four: Send a test message and make sure it's working!

:five: Register an application. Call it whatever you want it doesn't really matter.

:six: When you are done click on the application's name from step 5 and copy the API Token/Key from that page. This is the value for the token query field below.


Now for the magical part, Webhooks!

I'm going to jump straight into the doing, but if you want to take a step back for learning hop over here: http://docs.particle.io/photon/webhooks/

:one: Copy the following JSON blob into a new text file and call it pushover.json:

{
  "eventName": "office-motion",
  "url": "https://api.pushover.net/1/messages.json",
  "requestType": "POST",
  "query":
  {
    "user": "YOUR_USER_KEY",
    "token": "YOUR_API_TOKEN_KEY",
    "title": "MOTION",
    "message": "{{SPARK_EVENT_VALUE}}"
  },
  "mydevices": true
}

:two: Paste your User Key and API Token/Key into the appropriate places in the JSON file.

:three: Save it!

:four: Now open up a terminal and navigate to where you saved the pushover.json file. You must be able to execute Particle CLI commands. Enter the following command and press enter:

particle webhook create pushover.json

You should see the following output:

Sending webhook request  { uri: 'https://api.particle.io/v1/webhooks',
  method: 'POST',
  json: true,
  form: 
   { event: 'office-motion',
     url: 'https://api.pushover.net/1/messages.json',
     deviceid: undefined,
     access_token: '12341234123412341234123412341234',
     requestType: 'POST',
     headers: undefined,
     json: undefined,
     query: 
      { user: '23452345234523452345234523452345',
        token: '34563456345634563456345634563456',
        title: 'MOTION',
        message: '{{SPARK_EVENT_VALUE}}' },
     auth: undefined,
     mydevices: true } }
Successfully created webhook!

:five: If you don't get this output.. or if you wish to delete your webhook and try again, do the following. Type in this command:

particle webhook list

Copy the long number associated with the webhook you want to delete and run this command, pasting the number at the end:

particle webhook delete 123412341234123412341234

You should see a message that it was successfully removed


Wire up your PIR Motion Sensor to D0, 3V3 and GND


Program your Photon/Core

:one: Copy and paste the simple code above into a new app in Particle Build or Particle Dev.

:two: If your Motion sensor outputs HIGH or +3.3V when motion is detected, you are all set! If it outputs LOW or 0V during motion, you should replace the two occurrences of "HIGH" in the code with "LOW".

:three: Most motion sensors have kind of a delayed output, where it will stay active HIGH or LOW after motion is detected for at least a second or so. If yours does not do that, you may want to also add a simple one second delay delay(1000); above the line containing the while() loop. This will prevent the push notifications from happening too fast.

:four: Flash the program to your Photon/Core.

Enjoy your super simple Motion activated Push Notifications!

That's it! Go play! or keep reading for boring stuff...


Some background

You might be thinking, why not just send the HTTPS request to Pushover directly from the Core? Well the Core can't actually send encrypted HTTPS requests, but it can do HTTP just fine. Because of that you would typically have to use a PHP Proxy or some other proxy service to forward your HTTP request as a HTTPS request. This gets complicated really fast... my wife BARELY let me explain to her all of the pieces needed for the PHP Proxy way (and I was trying to be quick and fun about it!) So then I explained this new webhooks method and showed her code, and she was like "OH, that's great!".

For a previous example of how to do this exact same thing with a PHP Proxy, check out this post and all of the code you need to accomplish it. To be honest, I over simplified this project share because I was short on time, and also because it's really lengthy on what you need to setup. If you know, then you know... if not... it's kind of too much for you to do.

Now forget all of that and remember... U :blue_heart: WEBHOOKS:grey_exclamation: Particle Cloud FTW!

29 Likes

What a terrific tutorial, I set up mine in as much time as it took to read!!!

if (lastState != phoneyState)
  {
    //notification(phoneyState);  // old http notification :(
    Spark.publish("phoneyMode", phoneyState? "ON" : "OFF", 60, PRIVATE); // Spark WebHook!!!
  }
  lastState = phoneyState;

Loving WebHooks!!!!

4 Likes

Awesome tutorial! Clear and easy to follow. Did the whole process in <30min :grin:

1 Like

I don’t know why it’s not working with my PIR. I had it working by a 5V output before using my Photon. Here is the diagram. The pins are reversed from your diagram. http://www.mpja.com/download/31227sc.pdf

What exactly is 'not working'? A description like that is a bit too vague to offer much help.
Also, please try not to cross-post over several topics, things tend to get messy, which will benefit no-one.

@collegeWaldo Your particular sensor (+Power) needs to be hooked to 5V (so you could use the VIN pin for that), output to D0 and GND to GND. It appears to output HIGH when it detects motion so the code should work as-is. If that’s not the case, try == LOW instead of == HIGH on both lines. You may have to play with the sensitivity and time delay adjustments. Watch the output of the sensor with a multimeter when you are playing with it, and it might be so sensitive that you have to completely leave the room for it to reset to it’s idle state. I have peaked around doorways before when testing mine… a funny site from the perspective of my Photon.

2 Likes

Man…I cannot believe how simple it is… :stuck_out_tongue_closed_eyes: Awesome POST.

Maybe it is easy to use web hooks. The tutorial is so simple almost anyone can do it. But my problem is, I am not an operating systems guru, and because particle-cli fails to install on my W7 system, I can get no further. When looking for a way to fix this, all I find is a lot of things that are sure to mess up my computer, such as installing Visual Studio, some version of Python etc. Sorry to dump this on this thread, but I so want to test web hooks.
Erlend

1 Like

@Erlend

Yeah…I can see where you’re coming from…It really sucks when issues with the computer prevents you from accomplishing certain things…

One idea is to try to use a Virtual Machine in which you could install another Operating System (e.g. Mac OS X, or another version of Windows). I know that this is not an easy thing to do, but thought i’d throw it in there just in case…

In terms of good tutorials that guide you through the process…Have you tried THIS ONE? (click on the link)

Also here’s another post on troubleshooting Particle CLI on Windows…HERE

I have the tutorial open on a page - node.js OpenSSL VisualStudio Python - this is what I have to install to get particle-cli. I just cant believe it. 20 years ago I said bye to CLI, and now I have to install gigas of other software to get a mandatory cli. To me this negates much of the thrill of the particle IoT adventure. I want to write just one thing - source code. The rest should be point&click. Surely this misery is a show stopper to more than me?

Apologize. Think I had a bad day yesterday. I honestly believe the Particle stuff and the movement it fuels is fantastic. Sorry I dumped my frustrations.

Erlend

3 Likes

9 posts were split to a new topic: Help Installing Particle CLI on Windows 10

Hey @Erlend @clarissa

I hope Particle hears your frustration. If the new dashboard can handle activating webHooks that would be very useful. I teach High School Robotics using the Photon and don’t want each student to have to setup the CLI . The Awesome Particle IDE is why I am using the Photon in the classroom.

1 Like

Hi, when I type “particle webhook create webhookchannel.json” in my command prompt, I get a output “Please specify a url”. I dont undersand what i’m doing wrong. I copy paste from the web site and I changed the Access Token and the #tag. Thanks for your help

Can you show the content of webhookchannel.json without the sensitive information?

{
“event”: “pushbulletHOME”,
“url”: “https://api.pushbullet.com/v2/pushes”,
“requestType”: “POST”,
“headers”: {
“Authorization”: “********************************”,
“Content-Type”: “application/json”
},
“json”: {
“type”: “note”,
“title”: “{{SPARK_EVENT_VALUE}}”,
“body”: “”,
“channel_tag”: “https://www.pushbullet.com/channel?tag=****”
},
“mydevices”: true
}

Are you in the directory containing the .json file? Be sure to CD to the directory before you run the webhook command: particle webhook create webhookchannel.json

Weird. Are you running v.1.11.0 for the CLI?

It should be webhookchannel.json without the .txt so:

particle create webhook webhookchannel.json