Difference between revisions of "EBC Exercise 32 ifttt"

From eLinux.org
Jump to: navigation, search
m (Set up the Bone: Adding install instructions)
m (Set up ifttt)
 
(8 intermediate revisions by the same user not shown)
Line 16: Line 16:
 
# enter "sms" for the Event Name and click "Create Trigger"
 
# enter "sms" for the Event Name and click "Create Trigger"
 
# click on the blue "that"
 
# click on the blue "that"
# enter "sms" and click on "SMS"
+
# enter "sms" and click on "ClickSend SMS"
# click on the "Send me an SMS"
+
# click on the "Send SMS"
 +
# Enter a To phone, but leave the From field blank.
 
# enter a message to send.  You can click on the '''Add ingredient''' to insert things into the message.
 
# enter a message to send.  You can click on the '''Add ingredient''' to insert things into the message.
# click "Create action"
+
# click "Continue"
 
# edit the Recipe Title if you want.  (I remove "update phone number")
 
# edit the Recipe Title if you want.  (I remove "update phone number")
 
# click "Finish"
 
# click "Finish"
Line 25: Line 26:
 
=== Set up the Bone ===
 
=== Set up the Bone ===
  
The file '''send.js''' on [https://github.com/MarkAYoder/BeagleBoard-exercises/tree/master/iot/ifttt github] is a simple example that triggers ifttt events.   
+
The file '''send.py''' on [https://github.com/MarkAYoder/BeagleBoard-exercises/tree/master/iot/ifttt github] is a simple example that triggers ifttt events.   
 
# Go to [https://ifttt.com/services/maker_webhooks/settings https://ifttt.com/services/maker_webhooks/settings] channel and copy your key. It's the string following '''URL https://maker.ifttt.com/use/'''
 
# Go to [https://ifttt.com/services/maker_webhooks/settings https://ifttt.com/services/maker_webhooks/settings] channel and copy your key. It's the string following '''URL https://maker.ifttt.com/use/'''
# edit '''send.js''' and paste in your key.
+
# edit '''send.py''' and make sure the '''event''' matches the event name you entered ("sms").
# make sure the '''event''' matches the event name you entered ("sms").
 
 
# save your file and make sure it's executable.
 
# save your file and make sure it's executable.
 +
# edit '''setup.sh''' put your key in it.
 
# source the '''setup.sh'''.   
 
# source the '''setup.sh'''.   
 
  bone$ ''' source setup.sh'''
 
  bone$ ''' source setup.sh'''
In it you'll find
 
sudo npm install -g request
 
export NODE_PATH=$NODE_PATH:/usr/lib/node_modules
 
The first command loads the '''request''' modules and the second fixes '''NODE_PATH''' to find it.
 
  
 
=== Run the script ===
 
=== Run the script ===
 
Once everything is set up all you do is run:
 
Once everything is set up all you do is run:
  bone$ '''./send.js'''
+
  bone$ '''./send.py'''
  https://maker.ifttt.com/trigger/sms/with/key/b...4?value1=My&value2=Test%202&value3=BeagleBone
+
  <Response [200]>
 
  Congratulations! You've fired the sms event
 
  Congratulations! You've fired the sms event
  
You are allowed to pass on three parameters (value1, value2 and value3), which the script does.  You can set your recipe to send these values in the sms message.
+
You are allowed to pass three parameters (value1, value2 and value3), which the script does.  You can set your recipe to send these values in the sms message.
 +
 
 +
=== Triggering Events with curl ===
 +
You can also trigger the event with curl.
 +
bone$ '''curl -X POST -H "Content-Type: application/json" -d '{"value1":"My","value2":"Test 2","value3":"BeagleBone"}' https://maker.ifttt.com/trigger/sms/with/key/EG33hBxy7L7W3DvKnNoCi'''
 +
 
 +
It's easy to see that '''sms''' is the event to trigger.  The string following '''/key/''' is the key and the three values
 +
follow the '''-d'''.  The curl command must be all one line.
  
 
== Getting triggers events from ifttt ==
 
== Getting triggers events from ifttt ==
Line 58: Line 62:
 
  host$ '''./portForward.sh eth0 9090'''
 
  host$ '''./portForward.sh eth0 9090'''
 
Now when you access your host on port 9090, it will forward the request to the Bone.
 
Now when you access your host on port 9090, it will forward the request to the Bone.
 +
 +
This may be messy if you are running a virtual machine.  If so, you need to have the virtual machine's host
 +
(Windows I assume) port forward to the virtual machine and then have the VM forward to the bone.
  
 
=== Setting up ifttt ===
 
=== Setting up ifttt ===
# Browse to [https://ifttt.com/myrecipes/personal https://ifttt.com/myrecipes/personal] and click "Create a Recipe"  
+
# Browse to [https://ifttt.com/myrecipes/personal https://ifttt.com/myrecipes/personal] and click "New Applet"  
 
# click the blue "if"
 
# click the blue "if"
# search and click gmail
+
# search and click Gmail
 
# click "Any new email in inbox
 
# click "Any new email in inbox
# click "Create Trigger"
 
 
# click "that"
 
# click "that"
# search and click "Maker"
+
# search and click "Webhooks"
 
# click "Make a web request"
 
# click "Make a web request"
# if in the URL for your Host computer, port 9090.  Mine is '''137.112.41.36:9090/on'''
+
# Fill in the URL for your Host computer, port 9090.  Mine is '''137.112.41.36:9090/on'''
 
# select Method: GET, Content Type: text/plain and put in the Body where you want
 
# select Method: GET, Content Type: text/plain and put in the Body where you want
 
# click "Create Action"
 
# click "Create Action"
 
# clean up the Recipe Title and click "Create Recipe"
 
# clean up the Recipe Title and click "Create Recipe"
 +
# Go to https://ifttt.com/gmail
 +
# Click on '''Settings'''
 +
# Click on '''Edit connection'''
 +
# Select the gmail account to use
 +
# Click '''Allow'''
 +
# Wait a bit
  
 
=== Start the server and test ===
 
=== Start the server and test ===

Latest revision as of 10:00, 18 October 2021

thumb‎ Embedded Linux Class by Mark A. Yoder


ifttt (If This Then That) is a slick web service that lets you attach event triggers to actions. See the ifttt website for examples of what it can do. Here I'll show you how to use the Maker channel to trigger events from the Bone and have the Bone receive events.

Sending an SMS message from the Bone

There are three steps needed to send SMS messages, set up ifttt, edit a script on the Bone, and then run the script.

Set up ifttt

  1. Create an account on ifttt and
  2. Go to https://ifttt.com/my_applets
  3. then click New Applet
  4. next click the big blue this
  5. type "web" in the search box and click "Webhooks"
  6. click the blue "Receive a web request"
  7. enter "sms" for the Event Name and click "Create Trigger"
  8. click on the blue "that"
  9. enter "sms" and click on "ClickSend SMS"
  10. click on the "Send SMS"
  11. Enter a To phone, but leave the From field blank.
  12. enter a message to send. You can click on the Add ingredient to insert things into the message.
  13. click "Continue"
  14. edit the Recipe Title if you want. (I remove "update phone number")
  15. click "Finish"

Set up the Bone

The file send.py on github is a simple example that triggers ifttt events.

  1. Go to https://ifttt.com/services/maker_webhooks/settings channel and copy your key. It's the string following URL https://maker.ifttt.com/use/
  2. edit send.py and make sure the event matches the event name you entered ("sms").
  3. save your file and make sure it's executable.
  4. edit setup.sh put your key in it.
  5. source the setup.sh.
bone$  source setup.sh

Run the script

Once everything is set up all you do is run:

bone$ ./send.py
<Response [200]>
Congratulations! You've fired the sms event

You are allowed to pass three parameters (value1, value2 and value3), which the script does. You can set your recipe to send these values in the sms message.

Triggering Events with curl

You can also trigger the event with curl.

bone$ curl -X POST -H "Content-Type: application/json" -d '{"value1":"My","value2":"Test 2","value3":"BeagleBone"}' https://maker.ifttt.com/trigger/sms/with/key/EG33hBxy7L7W3DvKnNoCi

It's easy to see that sms is the event to trigger. The string following /key/ is the key and the three values follow the -d. The curl command must be all one line.

Getting triggers events from ifttt

ifttt can be set up to access a web page when a trigger occurs. Here will run a web server on the Bone and have ifttt access it when you get an email.

The web server

The file receive.js on github is a simple web server. If the page /on is accessed, the LED on P9_14 is turned on. If /off is accessed the LED is turned off.

Setting up your host computer

These instructions assume your Bone is attached to your host over the USB so the Bone isn't visible to the world. You can make it accessible by using port forwarding.

host$ cd exercises/setup
host$ ifconfig

Note which interface your host is using. It may be eth0 or wlan0, or something like it.

host$ ./portForward.sh eth0 9090

Now when you access your host on port 9090, it will forward the request to the Bone.

This may be messy if you are running a virtual machine. If so, you need to have the virtual machine's host (Windows I assume) port forward to the virtual machine and then have the VM forward to the bone.

Setting up ifttt

  1. Browse to https://ifttt.com/myrecipes/personal and click "New Applet"
  2. click the blue "if"
  3. search and click Gmail
  4. click "Any new email in inbox
  5. click "that"
  6. search and click "Webhooks"
  7. click "Make a web request"
  8. Fill in the URL for your Host computer, port 9090. Mine is 137.112.41.36:9090/on
  9. select Method: GET, Content Type: text/plain and put in the Body where you want
  10. click "Create Action"
  11. clean up the Recipe Title and click "Create Recipe"
  12. Go to https://ifttt.com/gmail
  13. Click on Settings
  14. Click on Edit connection
  15. Select the gmail account to use
  16. Click Allow
  17. Wait a bit

Start the server and test

bone$ ./receive.js
Listening on 9090

Send an email to your gmail account and the bone should respond with

path: /on
{}




thumb‎ Embedded Linux Class by Mark A. Yoder