URL shortcut to turn Smartthings switch on/of?

Forgive me if this is not the right place for this question but I thought this fit best. Can someone help enlighten me on the proper way to do the following?

My goal is to replace IFTTT as a service that was doing webhooks/API integration with Smartthings. I just need to replace what IFTTT was doing as i have the other parts solved. What I need is the ability for my iphone (via an iphone “Shortcut”) to visit a URL that then causes Smartthings to turn on a switch/light. I only do this while I’m on local wifi so if the solution requires being on the same LAN, that is fine.

I presume that Smartthings does not offer a way for me to create a web URLthat I can simply visit from my phone that will make this work directly so what I need is a web server that loads a webpage and then i can have that do the other more complicated POST (or whatever actions are needed) over to Smartthings to have it flip the switch??? I have a Raspberry Pi with apache2 and php installed and working. I am not familiar with Groovy but I am familiar with HTML and PHP.

If the above is correct, are the required steps to create a SmartApp on my account and get the Client ID and Secret, then from the Apache server have it do some oauth stuff to generate an access token. Then with this access token I have what i need to start connecting to smartthings to do this?

I tried the above using a sample php oath script and it gave me an access token but I am stuck getting this to work. Can someone point me in the right direction. Just linking to the Api documentation doesn’t help as I got lost. I’m not looking to build something complicated here. :slight_smile:
Thanks!

Try this. Should be able to get the URL from the App called Cloud Interface. Don’t know if you can install it via the New App but should be able to with the Classic.

You can call a webCoRE piston by URL but I’m only saying that because if I don’t someone else will.

Anyway, you don’t need all that guff.

You just need to be able to send an HTTP POST request to the REST API with bearer authorization e.g. using the curl library with PHP. It can be less tedious to execute a scene as you don’t have to specify a command.

The bearer token comes from https://account.smartthings.com/tokens so you don’t need to jump through oauth hoops.

3 Likes

Thanks for the info. I have attempted to set this up but I am apparently missing something in relation to the bearer auth. Possibly I am not setting it in the header correctly. Attached is a screenshot of my code and error. I replaced the last few characters of the bearer with xxxx for the screenshot. I am attempting a GET to start with so that I can get the scene ids and then I was planning on trying a POST to one of those IDs as a test.

UPDATE: I found a method to get the header to work for GET and POST to a scene. Yipee. Now I just need to figure out how to get the execute to a particular switch to work. Thanks for the pointers folks.

I think the problem with your initial try was simply that $crl had sneaked in to the curl_setopt instead of $ch and so there was no header.

Hmm, now I’m scratching my head at how to send a POST that turns on or off a switch. Can someone help me with this code. I used GET in order to obtain my device ID but now I clearly have something wrong in sending the POST in a way that includes all the necessary details.

I really feel like I have this correct but it is not working. I cant find anything on this “commands must have a size between 1 and 10” error online. The commands array is in json format and I output at the top. The device ID is valid. Anyone have ideas of what else I can try?

You haven’t actually sent a ‘commands’ array. In JSON terms you need to end up with:

{
    "commands": [
        {
            "component": "main",
            "capability": "switch",
            "command": "on"
        }
    ]
}

Thanks I finally found one method that works and that is to put the entire string of commands directly into the body area of the POSTFIELDS. Although everything online stated this to be an array, just injecting the text worked like a champ. Obviously, you have to put \ in front of quotes or any escape characters in PHP.