# Particle httpGet help?

**URL:** <https://community.smartthings.com/t/particle-httpget-help/29213>\
**Category:** Writing Device Types\
**Created:** [November 16, 2015, 10:16pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213 "2015-11-16T22:16:04Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dome](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dome](https://community.smartthings.com/u/dome)\
**Post date:** [November 16, 2015, 10:16pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/1 "2015-11-16T22:16:04Z")

</div>

I’ve been working on a device type for the Particle platform. The idea is an extension of my Smart Room Controller project I developed for the ThingShield.

I’m making great progress in how the Particle receives commands from SmartThings, but I’m finding that my lack of coding experience is impeding my progress in getting published events from the Particle into SmartThings. The key first step I’m looking to do is add a local switch to the Photon. I was planning to use httpGet and point it at my particle device’s API output, but I can’t get it working.

@juano2310, do you have any advice?

Device type code:

```
/**
 * SmartThings/Particle Automation Node
 *	powered by Particle
 *
 * Author: @dome
 * Big credit to: juano23@gmail.com
 * Date: 2014-01-23
 */
 
 preferences {
    input("deviceId", "text", title: "Device ID")
    input("token", "text", title: "Access Token")
}
 
 // for the UI
metadata {
    // Automatically generated. Make future change here.
	definition (name: "SPAN", author: "dome") {
    	capability "Switch"
        capability "Valve"
		capability "Alarm"
		capability "Water Sensor"
		capability "Temperature Measurement"

		command "red"
		command "green"
		command "blue"
		command "dark"
		command "white"
		command "redalert"
		command "flashgreen"
		command "flashred"
		command "flashblue"
		command "flashorange"
		command "flashyellow"
		command "flashpurple"
		command "flashwhite"
		command "fadered"
		command "fadegreen"
		command "fadeblue"
		command "fadeorange"
		command "fadeyellow"
		command "fadepurple"
		command "fadewhite"
		command "strobered"
		command "strobegreen"
		command "strobeblue"
		command "strobewhite"
		command "strobeorange"
		command "strobeyellow"
		command "strobepurple"
		command "persistred"
		command "persistgreen"
		command "persistblue"
		command "persistwhite"
		command "persistorange"
		command "persistyellow"
		command "persistpurple"
		command "dimred"
		command "dimgreen"
		command "dimblue"
		command "dimwhite"
		command "dimorange"
		command "dimyellow"
		command "dimpurple"
		command "alertred"
		command "alertgreen"
		command "alertblue"
		command "alertwhite"
		command "alertorange"
		command "alertyellow"
		command "alertpurple"
        command "beep1"
		command "beep2"
        command "beep3"
		command "beep4"
		command "siren1"
		command "siren2"
		command "siren3"
		command "siren4"
		command "modehome"
		command "modeaway"
		command "modenight"
		command "modeguest"
		command "modedebug"
}

    // tile definitions
	tiles {
		standardTile("switch", "device.switch", width: 1, height: 1, canChangeIcon: true) {
			state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
			state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
		}
		standardTile("valve", "device.valve", width: 1, height: 1, canChangeIcon: true) {
			state "open", label: '${name}', action: "valve.close", icon: "st.valves.water.open", backgroundColor: "#53a7c0"
			state "closed", label: '${name}', action: "valve.open", icon: "st.valves.water.closed", backgroundColor: "#ffffff"
		}

        standardTile("alarm", "device.alarm", canChangeIcon: false) {
			state "off", label:'off', action:'alarm.both', icon:"st.alarm.alarm.alarm", backgroundColor:"#ffffff"
			state "strobe", label:'strobe!', action:'alarm.off', icon:"st.alarm.alarm.alarm", backgroundColor:"#e86d13"
			state "siren", label:'siren!', action:'alarm.off', icon:"st.alarm.alarm.alarm", backgroundColor:"#e86d13"
			state "both", label:'alarm!', action:'alarm.off', icon:"st.alarm.alarm.alarm", backgroundColor:"#e86d13"
		}

		main "switch"
		details "switch","alarm","valve"
	}
}

def parse(String description) {
	if(message == "switch.on") {
        sendEvent(name: "switch", value: "on")
		descriptionText = "${device.displayName} is switched on"        
    }

	log.error "This device does not support incoming events"
	return null 
    }
    

def on() {
	put '1'
    sendEvent(name: "switch", value: "on")
    log.debug "${device.displayName} is on"
}

def off() {
	put '0'
    sendEvent(name: "switch", value: "off")
    sendEvent(name: "valve", value: "closed")
    sendEvent(name: "alarm", value: "off")
    log.debug "${device.displayName} is off"
}
def both() {
	put '9'
    sendEvent(name: "switch", value: "on")
    sendEvent(name: "alarm", value: "both")
    log.debug "${device.displayName} is in alarm"
}
def open() {
	put '2'
    sendEvent(name: "valve", value: "open")
    log.debug "${device.displayName} has begun watering"

}
def close() {
	put '3'
    sendEvent(name: "valve", value: "closed")
    log.debug "${device.displayName} has ceased watering"
}

def modehome() {
	put '41'
    log.debug "${device.displayName} has changed mode to Home"    
}

def modenight() {
	put '42'
    log.debug "${device.displayName} has changed mode to Night"    
}

def modeaway() {
	put '43'
    log.debug "${device.displayName} has changed mode to Away"    
}

def modeguest() {
	put '44'
    log.debug "${device.displayName} has changed mode to Guest"    
}

def modedebug() {
	put '45'
    log.debug "${device.displayName} has changed mode to Debug"    
}

private put(span) {
    //Spark Core API Call
	httpPost(
		uri: "https://api.particle.io/v1/devices/${deviceId}/spancommand",
        body: [access_token: token, command: span],  
	) {response -> log.debug (response.data)}
}

private get() {
    //Spark Core API Call
    log.debug "get invoked"
    httpGet("https://api.spark.io/v1/devices?access_token=${token}", spansend) { resp ->
        resp.headers.each {
        log.debug "${it.name} : ${it.value}"
    	}
	}
}

def poll() {
	log.debug "Executing 'poll'"
    get()
	checkSpark()
}

def refresh() {
	log.debug "Executing 'refresh'"
    get()
	checkSpark()
}

// Get the sensor reading
private getReading() {
    //Spark Core API Call
    def readingClosure = { response ->
	  	log.debug "Reading request was successful, $response.data.result"
      	sendEvent(name: "rssi", value: response.data.result)
	}

    httpGet("https://api.spark.io/v1/devices/${deviceId}/${sparkVar}?access_token=${token}", readingClosure)
}

// Check the Spark status on the polling schedule so not realtime unless refreshed
private checkStatus() {
	int stat = 0
	//Spark Core API Call
    def readingClosure = { response -> response.data.each { core ->
    	if (core.id == deviceId) {
		  	log.debug "Spark status check was successful, $core.connected"
	      	sendEvent(name: "switch", value: core.connected ? "on" : "off")
            stat = core.connected ? 1 : 0
            }
        }
	}

    httpGet("https://api.spark.io/v1/devices?access_token=${token}", readingClosure)
    stat

}

/*
def params = [
    uri: "https://api.particle.io",
    path: "/v1/devices/events?access_token=${token}"
]
*/

void spanget() {
 // httpGet(params) { resp ->
 	log.debug "spanget invoked"
 	httpGet("https://api.spark.io/v1/devices?access_token=${token}", spansend) { resp ->
        resp.headers.each {
        log.debug "${it.name} : ${it.value}"
    	}
    log.debug "response contentType: ${resp.contentType}"
    log.debug "response data: ${resp.data}"
    if (resp == 1) {
   		log.debug "home"
    	}
	} 
}    

def checkSpark() {
    if (checkStatus()) {
		getReading()
	}
        else {
    sendEvent(name: "rssi", value: "-200")
    }
}

```

Particle code:

```
int SPANcommand(String command);

int V1 = 7;
int sirenPin = 1;

bool test = FALSE;
bool light = FALSE;

String deviceType = "SRC";
//Options: SRC, SYC

String watering = "false";
String alarm = "false";
int startCountdown = 20; //In minutes
//Before conversion, 40000 = 1 minute
int countdown = 0;

void setup()
{
	Particle.function("spancommand", SPANcommand);
//	Particle.variable("watering", watering, STRING);
	pinMode(V1, OUTPUT);
	pinMode(sirenPin, OUTPUT);
	digitalWrite(V1, LOW);
	delay(10);
	RGB.control(true);
	//RGB.brightness();
	RGB.color(0, 0, 255);
	delay(500);
	beep1();
	//Particle.publish("restart");
	Particle.publish("spansend", "restart");
	Particle.publish("deviceType", deviceType);
	RGB.color(0, 0, 0);
	//TODO: set valve and switch to closed and off
}

void loop()
{
	if (watering == "true")
	{
		countdown = countdown - 10;

		if (countdown <= 0)
		{
			digitalWrite(V1, LOW);
			watering = "false";
			Particle.publish("spansend","closed");
			RGB.color(0, 0, 0);
			beep4();
		}
	}
	delay(10);
	
	if (test == TRUE) 
	{
	    if (light == TRUE) {
	        delay(10000);
	        RGB.color(0, 0, 0);
	        light = FALSE;
	    }

	   else if (light == FALSE) {
	        delay(10000);
	        RGB.color(255, 255, 255);
	        light = TRUE;
	    }

	}
}

int SPANcommand(String command)
{
	if (command == "1") {
		digitalWrite(V1, HIGH);
		watering = "true";
		RGB.color(0, 255, 0);
		countdown = startCountdown * 40000;
		beep3();
		beep3();
		Particle.publish("spansend","open");
		return 1;
	} 
	else if (command == "0") {
		digitalWrite(V1, LOW);
		watering = "false";
		beep4();
		beep4();
		Particle.publish("spansend","closed");
		RGB.color(0, 0, 0);
		return 0;
	}
	else if (command == "9") {
	    //alarm activation protocol
		digitalWrite(V1, HIGH);
		watering = "false";
		beep4();
		beep4();
		Particle.publish("spansend","alarm");
		RGB.color(255, 0, 0);
		return 0;
	}

	else if (command == "41") {
		Particle.publish("Mode","home");
		RGB.color(0, 255, 0);
		delay(500);
		RGB.color(0, 0, 0);
		return 0;
	}

	else if (command == "42") {
		Particle.publish("Mode","night");
		RGB.color(0, 0, 255);
		delay(500);
		RGB.color(0, 0, 0);
		return 0;
	}

	else if (command == "43") {
		Particle.publish("Mode","away");
		RGB.color(255, 0, 0);
		delay(500);
		RGB.color(0, 0, 0);
		return 0;
	}

	else if (command == "44") {
		Particle.publish("Mode","guest");
		RGB.color(0, 255, 100);
		delay(500);
		RGB.color(0, 0, 0);
		return 0;
	}

	else if (command == "45") {
		Particle.publish("Mode","debug");
		RGB.color(255, 0, 255);
		delay(500);
		RGB.color(0, 0, 0);
		return 0;
	}

	else {
		beep2();
	}

}

void beep1()
{
  beep(4000, 50, 50);
	beep(4000, 50, 0);
}

void beep2()
{
  beep(4000, 50, 50);
	beep(4000, 1000, 0);
}

void beep3()
{
  beep(4000, 200, 50);
	beep(4650, 500, 0);
}

void beep4()
{
  beep(4125, 100, 50);
	beep(4000, 500, 0);
}

void beep(int freq, int duration, int wait)
{
  tone(sirenPin, freq);
  delay(duration);
  noTone(sirenPin);
  delay(wait);
}
```

---

<div class="post-metadata">

**Author:** ![tgauchat](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tgauchat/32/49516_2.png) [@tgauchat](https://community.smartthings.com/u/tgauchat)\
**Post date:** [November 16, 2015, 10:41pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/2 "2015-11-16T22:41:47Z")

</div>

Check out a few other Photon projects already posted for clues, perhaps?

Search 🔍 “Photon”.

> [@Automated Window Blinds - Spark Core](https://community.smartthings.com/t/automated-window-blinds-spark-core/5681):
>
> So I purchased a Spark Core a few weeks back and decided to have some fun. First and foremost a big thank you to @juano2310, I borrowed and updated your Spark Core device type. It now serves as a dimmer switch for blinds. So the core of the code is located here: Things you will need: A Spark Core A servo (preferably analog) Standard horizontal slat window blinds (plantation style will work too) DIY mentality wink Steps Wire up your spark core with the servo. Create your Spark Core Blinds device type using the github code Create your window Blinds device Load the Spark code to [www.spark.io/build](http://www.spark.io/build) Add the Spark Core device id and access token to the device this can be done on the smart app under preferences for the device. Put on your DIY hat finally and with your wife’s per…

---

<div class="post-metadata">

**Author:** ![dome](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dome](https://community.smartthings.com/u/dome)\
**Post date:** [November 16, 2015, 10:42pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/3 "2015-11-16T22:42:43Z")

</div>

Thanks, I have been doing that all along… as far as I’ve seen, no one else is using httpGet, instead relying on devices that only communicate one way. I want two way communication.

---

<div class="post-metadata">

**Author:** ![juano2310](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/juano2310/32/150581_2.png) [@juano2310](https://community.smartthings.com/u/juano2310)\
**Post date:** [November 17, 2015, 12:42pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/4 "2015-11-17T12:42:08Z")

</div>

Hi Dome,  
Some time ago I helped posting this library on Particle platform “HTTPCLIENT” whit this library you can make http calls directly from the Photon.  
If you enable Oauth and endpoints on your SmartApp you would be able to send values directly from the Photon to the SmartApp without having to query the Photon regularly.

---

<div class="post-metadata">

**Author:** ![jjhtpc](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jjhtpc/32/95671_2.png) [@jjhtpc](https://community.smartthings.com/u/jjhtpc)\
**Post date:** [November 17, 2015, 1:37pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/5 "2015-11-17T13:37:43Z")

</div>

I am very interested in this myself. I currently don’t use this much as I am using an enerwave 7 button controller with one setup of lights and I don’t really look in the app to see status. I would like this for the spark/photon blinds project I had setup as it would allow me to create a smart app that is a little more intuitive.

I think what you are looking for is here:  
[https://docs.particle.io/reference/javascript/#publishing-event](https://docs.particle.io/reference/javascript/#publishing-event)

I would think you would need to publish an event (var) to the particle cloud and then do an httpGet to grab that variable.

---

<div class="post-metadata">

**Author:** ![dome](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dome](https://community.smartthings.com/u/dome)\
**Post date:** [November 17, 2015, 2:01pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/6 "2015-11-17T14:01:52Z")

</div>

@juano2310 @jjhtpc Thanks, I will keep hacking at it. I’m a self-taught coder and am much more comfortable developing hardware than software. Java is not in my comfort zone but maybe this is my incentive to branch out and learn something new (or at least fail spectacularly!).

---

<div class="post-metadata">

**Author:** ![jjhtpc](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jjhtpc/32/95671_2.png) [@jjhtpc](https://community.smartthings.com/u/jjhtpc)\
**Post date:** [November 17, 2015, 2:32pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/7 "2015-11-17T14:32:01Z")

</div>

Oh and I thought of something else. I don’t know if you use IFTTT, but I find that it is often a great first step to prototype integration as direct connection sometimes requires coding and that is not always intuitive. IFTTT can look for a published event or look for a variable change and you can setup to trigger an event off that and then make something happen on ST or send yourself a text message.

This will help you at least determine if your publish is working properly.

---

<div class="post-metadata">

**Author:** ![dome](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dome](https://community.smartthings.com/u/dome)\
**Post date:** [November 17, 2015, 2:42pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/8 "2015-11-17T14:42:02Z")

</div>

I might just do that. When I developed my first Spark/Particle device I did use IFTTT as an intermediary for ST-\>P events, but did not see the need for the other direction at the time. I developed further and was able to remove the IFTTT segment for a direct link between ST and Particle. As for why I didn’t think to try this for the other direction, that’s beyond me. I guess I didn’t eat my Wheaties this morning.

---

<div class="post-metadata">

**Author:** ![dome](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dome](https://community.smartthings.com/u/dome)\
**Post date:** [November 17, 2015, 3:36pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/9 "2015-11-17T15:36:32Z")

</div>

Thank you for your suggestion. I think I got lost in the forest and couldn’t find the trees! I got it working with IFTTT. I would eventually like to take off those training wheels but this at least gets me moving with a workable solution today.

---

<div class="post-metadata">

**Author:** ![jjhtpc](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jjhtpc/32/95671_2.png) [@jjhtpc](https://community.smartthings.com/u/jjhtpc)\
**Post date:** [November 17, 2015, 3:52pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/10 "2015-11-17T15:52:45Z")

</div>

Ha that is exactly how I look at IFTTT training wheels, because all IFTTT is, is a simplified in between for two separate entities that need to be coded. If you can get it working that way there is a way through the API to directly code it.

---

<div class="post-metadata">

**Author:** ![jdetmold](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdetmold](https://community.smartthings.com/u/jdetmold)\
**Post date:** [February 26, 2016, 6:37am UTC](https://community.smartthings.com/t/particle-httpget-help/29213/11 "2016-02-26T06:37:59Z")

</div>

@juano2310 any chance you could point me in the direction of this library?  
thanks!

---

<div class="post-metadata">

**Author:** ![juano2310](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/juano2310/32/150581_2.png) [@juano2310](https://community.smartthings.com/u/juano2310)\
**Post date:** [February 26, 2016, 4:41pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/12 "2016-02-26T16:41:17Z")

</div>

Hi @jdetmold,  
If you go to [https://build.particle.io/build](https://build.particle.io/build) after you log in, in the left hand column there is a one option that is libraries. Use the search tool to spot “HTTPCLIENT”. There are examples there too.  
Also you can find it here. [https://github.com/nmattisson/HttpClient](https://github.com/nmattisson/HttpClient)

---

<div class="post-metadata">

**Author:** ![jdetmold](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdetmold](https://community.smartthings.com/u/jdetmold)\
**Post date:** [February 26, 2016, 6:05pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/13 "2016-02-26T18:05:18Z")

</div>

> [@juano2310](#):
>
> examples

Thanks a ton!  
20char

---

<div class="post-metadata">

**Author:** ![jjhtpc](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jjhtpc/32/95671_2.png) [@jjhtpc](https://community.smartthings.com/u/jjhtpc)\
**Post date:** [February 29, 2016, 2:22pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/14 "2016-02-29T14:22:58Z")

</div>

@dome  
Hey dome by chance could you post the code that worked both on spark and on smartthings.

I am planning on adding something similar to the blinds project and possibly the rgb solution and I am not one to reinvent the wheel. Any help is appreciated.

Thanks,

jjhtpc

---

<div class="post-metadata">

**Author:** ![jdetmold](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdetmold](https://community.smartthings.com/u/jdetmold)\
**Post date:** [March 1, 2016, 3:22am UTC](https://community.smartthings.com/t/particle-httpget-help/29213/15 "2016-03-01T03:22:39Z")

</div>

I agree if anyone has a demo of this working that would be very helpful!

---

<div class="post-metadata">

**Author:** ![jjhtpc](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jjhtpc/32/95671_2.png) [@jjhtpc](https://community.smartthings.com/u/jjhtpc)\
**Post date:** [March 5, 2016, 5:11pm UTC](https://community.smartthings.com/t/particle-httpget-help/29213/16 "2016-03-05T17:11:54Z")

</div>

OK I worked this out. I talk about it here.

> [@Automated Window Blinds - Spark Core](https://community.smartthings.com/t/automated-window-blinds-spark-core/5681/204):
>
> Hello, So I worked on this pretty extensively over the last week. I took my code back to the original code because I felt it stayed closer to what I wanted. Feel free to modify to your hearts content as axyr did above. The main bonus of this code is two way communication. I figured out the particle API commands for an httpGet to grab a variable set on particle. So head on over to github and get the original branch: @a6pack @keithcroshaw @hawesg @Pete I know this was something you will be looking for. The main advantage of this is that now polling and refresh will show status if an outside smart app opens the blinds or if the slider is used… (which is working again)! Enjoy! Justin Oh and on a side note I think this opens the possibility of having a general Spark device type …

---

<div class="post-metadata">

**Author:** ![jdetmold](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdetmold](https://community.smartthings.com/u/jdetmold)\
**Post date:** [March 19, 2016, 3:41am UTC](https://community.smartthings.com/t/particle-httpget-help/29213/17 "2016-03-19T03:41:38Z")

</div>

I have thus far had no luck getting this working with oauth and smartthings, any change you have or have seen any example code with this working with smart things?
