I am having trouble getting two programs to talk together.
I am receiving the following error when running my current programs:
error groovyx.net.http.HttpResponseException: Unauthorized @line 72 (sendToDevice)
How do I fix this?
Here is my SmartThings Device Type:
preferences {
section("Your Particle credentials and device Id:") {
input("token", "text", title: "Access Token")
input("deviceId", "text", title: "Device ID")
input("query", "text", title: "Query Name")
}
}
metadata {
definition (name: "Blinds", namespace: "luxetbono", author: "John Bono") {
capability "Window Shade"
}
// tile definitions
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "open", label: '${name}', action: "switch.off", icon: "st.Home.home9", backgroundColor: "#ffc300", nextState: "opening"
state "close", label: '${name}', action: "switch.on", icon: "st.Home.home9", backgroundColor: "#00a9ff", nextState: "closing"
// state "opening", label:'${name}', icon:"st.Home.home9", backgroundColor:"#ffeaa5", nextState: "closing"
// state "closing", label:'${name}', icon:"st.Home.home9", backgroundColor:"#b7e1f7", nextState: "opening"
}
main "switch"
details "switch"
}
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
// TODO: handle 'windowShade' attribute
}
// handle commands
def open() {
log.debug "Executing 'on'"
sendEvent(name: 'switch', value: 'open')
// TODO: handle 'open' command
sendToDevice 'open'
}
def close() {
log.debug "Executing 'off'"
sendEvent(name: 'switch', value: 'close')
// TODO: handle 'close' command
// sendToDevice 'close'
}
/*ENABLE THIS WHEN YOU HAVE YOUR PARTICLE PHOTON. */
private sendToDevice(cmd) {
// Particle API call to our photon device
// "deviceId" will be replaced with our actual device name
// FanControl is the name of our published function exposed by the device
// state is our input parameter: 'on' or 'off'
log.info(deviceId)
httpPost(
uri: "https://api.particle.io/v1/devices/${deviceId}/${query}",
body: [access_token: token, command: cmd],
) {response -> log.debug (response.data)}
}
Here is my Particle Photon code:
boolean state = true;
void setup() {
Particle.function("blind", blind); // Register cloud function called blind that controls blinds.
pinMode(D7, OUTPUT);
}
int blind(String command)
{
if(command == "open"){
// Open window blinds.
RGB.control(true);
RGB.color(34, 102, 75);
delay(4000);
RGB.control(false);
state = true;
// Add state change call for Smartthings using webhooks and the CoRE SmartApp.
} else if (command == "close") {
// Close window blinds.
RGB.control(true);
RGB.color(34, 68, 102);
delay(4000);
RGB.control(false);
state = false;
// Add state change call for Smartthings using webhooks and the CoRE SmartApp.
}
}
void loop() {
}