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
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... 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:
Click on the Pushover logo to get to your dashboard.
Copy your User Key. This is the value for the the user query field below.
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.
Send a test message and make sure it's working!
Register an application. Call it whatever you want it doesn't really matter.
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/
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
}
Paste your User Key and API Token/Key into the appropriate places in the JSON file.
Save it!
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!
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
Copy and paste the simple code above into a new app in Particle Build or Particle Dev.
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".
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.
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 WEBHOOKS Particle Cloud FTW!