[SOLVED] Some confusion around an API call for a Distance Sensor

Hello. I’m working through the book “Programming the Photon” and I ran into a hitch that I can’t seem to find any information about. The project calls for wiring up an HC-SR04 and calling it using the following API: "https://api.particle.io/v1/devices/[deviceID]/distanceCM." This gives me a 404 error. A similar API call for a light sensor using “https://api.particle.io/v1/devices/[deviceID]/volts” works just fine with the same device information and access code and nearly identical JavaScript code. Since this API call works, I’m wondering if the API call changed or if the book has a typo.

The Photon code for the HC-SR04 looks like this:

#include "HC_SR04/HC_SR04.h"

double cm = 0.0;
double inches = 0.0;
int trigPin = D4;
int echoPin = D5;

HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

void setup() {
    Particle.variable("cm", &cm, DOUBLE);
    Particle.variable("inches", &inches, DOUBLE);
}

void loop() {
    cm = rangefinder.getDistanceCM();
    inches = rangefinder.getDistanceInch();
    delay(100);
}

Heh… as I typed this, I tried changing the URL call to “cm” and I’m no longer getting a 404 error. I’ve run into a few other such situations with this book, but they at least seem solvable. So it appears that the call in the URL should tie to a specific Particle variable.

1 Like

Yup so API call for variable is directly related to the variable name created in firmware:

Particle.variable("analogvalue", &analogvalue, INT);
                   ^^^^^^^^^

In this example. the variable is called analogvalue and the corresponding call is: "https://api.particle.io/v1/devices/[deviceID]/analogvalue

1 Like