How to complete a HTTP POST from a Device Handler


I am completely new to Smartthings, picked one up yesterday and immediately wanted to get my hands dirty. I have a few Lenovo smart plugs which are incompatible with SmartThings so that seemed like a good place to start.

I sniffed the packets sent between my phone and the Lenovo app and pretty soon I had CURL requests which could turn the plug on and off.

I could not post the CURL requests as "new users can only create posts with two links in them. But happy to share

After a bit of searching I found some examples of device handlers and thought I’d give it a go. I created a handler and am attempting to run the HTTP POST, the code runs in the simulator, but I never get a response back from the API. Which, I’m guessing means I have not successfully sent the request.

Perhaps I am doing something wrong. I can code in C# and make sense of most C based languages but I’ve never really used Java or Groovy.

Here is the code I am having trouble with (I’ve tried this many different ways and a fair bit of googling but so far cannot figure this one out).

def cmdStr = new physicalgraph.device.HubAction([

    method: "POST",

    path: "",

    body: "postData=%7B%22devId%22%3A%222800434484f3ebc1b3af%22%2C%22dps%22%3A%22%7B%5C%221%5C%22%3Afalse%7D%22%2C%22gwId%22%3A%222800434484f3ebc1b3af%22%7D",

    headers: ["HOST": "",








//Send the command to the Hub

def CmdResponse = sendHubCommand(cmdStr)

log.debug cmdStr **// I see this**

log.debug CmdResponse **//There is never a response from the server here.**

I’ve tried (among other things) removing the hostname from the path but that seems to get me in the same situation

The hubAction you are using is for requests on your LAN (and is also still limited to just http I believe). You need to be looking at using httpPost() for external requests.

I don’t know anything about the Lenovo plugs, but I note that a lot of brands piggy back on the Tuya Smart system, and that is being integrated with SmartThings, with the first brand integration being Globe Electric. So that may be worth looking into. My superficial understanding is that there is interoperability between brands on Tuya.

@THeBingoKing, Just to add @orangebucket’s comment above. The Globe Suite integration is not ready yet. People are waiting for it for months. When it would be ready you could add Tuya devices to ST.

Otherwise, I wouldn’t post that much detailed code here. If you haven’t changed some part of it, then you just gave out a link to control your plug…
But I guess that method would use some sort of authentication like OAuth, so you need that for your DH too.

Some links regarding HTTP POST.

Hi @orangebucket / @GSzabados

Thanks for the responses, the HTTPPOST is exactly what I needed and I have it running now.

P.S. I did change my code, all of the DeviceID’s etc have been changed so no one can control the plug.

Thanks Again

1 Like