Get Particle Photon Variable and Display Value in Tile

I’m trying to get a particle photon variable to display on value tile. I have a voltage input at pin A0 on the photon and code to read the analog input, see code below. I then want the device handler set up to get the particle variable and display it on the value tile. However, I cannot seem to get the variable from the cloud. I tried several variations of httpGet() but with no luck. Any help would be appreciated!!

Particle Code:

int read = A0;
int voltageIN;

void setup()
{
   pinMode(A0, INPUT);
   Particle.variable("voltageIN", voltageIN);
}

void loop()
{
 voltageIN = analogRead(read);
 delay(100);
}

Device Handler:

preferences {
    input("deviceId", "text", title: "Device ID")
    input("token", "text", title: "Access Token")
}
metadata {
	definition (
		name: "valueDeviceTile", author: "zyx") {
	}
	tiles() {
		valueTile("voltage", "device.voltage", width: 2, height: 2) {
			state "default", label: '${currentValue} Volts'
		}
	}
}
def voltage(){
    log.debug "get invoked"
    httpGet("https://api.particle.io/v1/devices?access_token=${token}", voltageIN) { resp ->
        resp.headers.each {
        log.debug "${it.name} : ${it.value}"
    	}
	}
}
def parse(String description) {
}

This is how i’m doing it.

private detect(car) {
   //Spark Core API Call
	httpPost(
		uri: "https://api.spark.io/v1/devices/${deviceId}/Read",
        body: [access_token: token, command: car],  
	) {response -> 
    	def sensorValue = response.data      
        log.debug sensorValue.return_value
        
        if (sensorValue.return_value > 1450 & sensorValue.return_value < 1850){
        	arrived()
        	sendEvent(name: "distance", value: "Toyota Prius V" as String, unit: "")	
        }else if (sensorValue.return_value < 2400 & sensorValue.return_value > 1100){
        	arrived()
        	sendEvent(name: "distance", value: "Honda Pilot" as String, unit: "")	
        }else if (sensorValue.return_value > 2500){
        	departed()
            sendEvent(name: "distance", value: "No Car" as String, unit: "")
            log.debug "No Car In Parked Stall"
        }
    }
}
1 Like

I’m not too educated on the HTTP methods. Intuitively, I would think the httpPost would be to send a command and the httpGet would be used to retrieve data. It looks like your example is intending to pull data using httpPost? Additionally, do you know how the “response” works? Excuse my lack of knowledge…just a newbie here :slight_smile:

Yeah, I have to poll the photon to get the data. I’ve been meaning to have the photon push data to the DTH (need to figure out how to do a REST API in DTH). The response I am getting is DAC count of the analog value from an ultrasonic sensor that is measuring the distance from the sensor to the car.

I have not worked on my Particle integration for a long time but I did get it to work by polling like your doing. I also got it working with hooks so particle could send events to ST. It is a bit custom and a pain to wrap your head around. I would love to get this integration working better but free time is the problem right now.