Sending JSON to arduino with hubaction


(Peter Sun) #1

I want to send JSON data to my arduino from my SmartThings hub. I set up my arduino as a web server and tested that it will receive messages. I used POSTMAN to confirm.

I can confirm that the header data is there with this log entry

Below is my code that sets up my header data… and the post function:

def updated() {
    logger("updated()","trace")

    
    // Set Server Host variables config:
    state.serverHost = settings.prefHost
    state.serverPort = settings.prefPort
	state.serverAddress = state.serverHost + ":" + state.serverPort
    logger("Setting Server Variables: Server Address ${state.serverAddress}","info")
	
	
    state.headers = [:] 
    state.headers.put("HOST", "${state.serverAddress}\body")
    state.headers.put("Content-Type", "application/json")
    }

def postToServer(data) {
    logger("postToServer(): Posting data to Server: Host: [${state.headers}] Data: [${data}]","info")
    logger("Setting Server Variables: Server Address ${state.serverAddress}","info")
    try {
        def hubAction = new physicalgraph.device.HubAction(
            method: "POST",
            path: "",
            body: data,
            headers: state.headers,
        )
		
        hubAction
    }
    catch (Exception e) {
		logger("postToServer(): Exception ${e} on ${hubAction}","info")
    }
}

I am not receiving any messages from the hub. Any suggestions?


#2

Webcore allows you to send we calls.


(Edwin Weston) #3

Would this work?
http://docs.smartthings.com/en/latest/ref-docs/smartapp-ref.html?highlight=httppostjson#httppostjson


#4

I’d say HOST must contain only host:port combination. The rest should be in path. If you want you can use uri for all that. I’d also add application/json for “Content-type”.


#5

That is info for building a SmartApp and not a DTH. You are building a DTH to trigger the action from your smartapp right?