Triggering an Alexa routine from the command line or an Arduino compatible device (ESP32)


By creating an auto-resetting virtual sensor on SmartThings.com, Alexa can execute a routine when the virtual sensor's state is triggered via a REST call to SmartThings.com

Steps to follow

Creating a virtual, auto-resetting sensor on SmartThings.com

  • Register an account with SmartThings.com
  • Go to My device handlers
  • Create a new device handler 
  • Switch to "From code" and paste in the auto-resetting sensor code from here.
  • Go to My Locations and add a new location (e.g., "Living Room")
  • Go to My Devices and add a new device
    • Name, Label, Zigbee Id, Device Network Id = MyVirtualButton
    • Type = Simulated Alexa Button (probably the last item in the dropdown)
    • Location = Living Room (or whatever you created)
    • Hub, Group = (empty)
  • Once you created the device, click on it and make sure to take a note of the GUID in the URL, which will look something like this (the last part, c00... is the GUID of the device): /device/show/c00ab333-71e6-4a62-9c39-5df269cddd95

Create a personal access token for SmartThings.com

Making the first call to SmartThings.com from the command line

To make sure everything works so far, execute the following curl command (substitute your GUIDs):

curl -H 'Authorization: Bearer: YOUR_PERSONAL_ACCESS_TOKEN' \
-d '[{"component":"main","capability":"switch","command":"on"}]' \
https://api.smartthings.com/v1/devices/YOUR_DEVICE_GUID/commands

Then something like this should come back:

{"results":[{"id":"fffb05cb-16fd-4c0c-a1de-24ab0e96b0ba","status":"ACCEPTED"}]}

Setting up Alexa

  • Install the SmartThings skills on your Alexa account
  • Discover new devices - this should find your new MyVirtualButton sensor (note: we had to create a sensor rather than a simple switch: Alexa cannot trigger routines based on switches, only based on sensors).
  • Create a new Alexa routine that is triggered by MyVirtualButton: "When MyVirtualButton is..." "Open"

Triggering the virtual sensor from your Arduino device

The last step is to write a simple HTTP POST call -replacing the curl command above- in C to call SmartThings.com with the right personal token and device id. The complete Arduino code to trigger an Alexa routine can be found here.



Comments

Popular posts from this blog

MurMurHash3, an ultra fast hash algorithm for C# / .NET

Octoprint as a systemd service - running it with high priority

ESP32 - send a push notification from the Arduino ESP32 device to your phone