Particle.variable() & Particle.function() not creating readable variable/callable function

I’m beyond frustration. I started with a Photon, added an Electron. I’m attempting to make a network accessible thermometer. I have a simple prototype wired up on a breadboard, and various versions of my firmware are successfully reading reasonable temperatures and reporting over the Serial connection to a local machine. I can (mostly) get Particle.publish() to put temperature values on the dashboard.

And when I try to create a variable or a function, I get (at best) intermittent results: sometimes the function appears via IFTTT or via my iOS sample program attempting to list devices and their variables and functions. Easily 90% of the time the registered variable/function does not appear. When it DOES appear, it disappears very quickly (within minutes), and rerunning the firmware or resetting the device does not help.

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

#define DHTPIN 5
#define DHTTYPE AM2301

#define version "v.8"


int one_second = 1000;
int one_minute = 60 * one_second;

int temperature = -1000;
double tempC = -1;
String message = "This is a message";

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.println(version);
    delay(5*one_second);       // allow time for setting up serial monitor

    dht.begin();

    bool registered = false;

    registered = Particle.variable("EvansIntTemp", temperature);
    reportOnRegistration(registered, "EvansIntTemp");
 
    registered = Particle.function("EvansGetTemp", getTemperature);
    reportOnRegistration(registered, "EvansGetTemp()");

    Serial.println(version);
}

void reportOnRegistration(bool registered, String name) {
    if (registered) {
        Serial.println(name + " registered");
    }
    else {
        Serial.println("Failed to register " + name);
    }
}


int getTemperature(String command) {
    temperature = dht.getTempFarenheit();
    Serial.println(String(temperature) + " F == getTemperature()");
    return temperature;    
}


void loop() {
    temperature = 0;
    temperature = getTemperature("");

    Serial.println(String(temperature) + " ºF");

    delay(one_minute);
}

The printed versions to the Serial port are to make sure that I’ve got the latest version of the program running.

What am I doing wrong?

Could you try commenting out the delay(one_minute); or reducing it to something < 10s? Just checking.

I’m not sure. I commented out the lines that interact with the DHT sensor and hardcoded 70 and it seems to work properly for me. The functions and variables are registered properly, and the code looks fine. The serial output is normal:

EvansIntTemp registered
EvansGetTemp() registered
v.8
70 F == getTemperature()
70 ºF
70 F == getTemperature()
70 ºF

particle list shows reasonable data:

test4 [xxxxxxx] (Photon) is online
  Variables:
    EvansIntTemp (int32)
  Functions:
    int EvansGetTemp(String args)

And the variables and functions seem to work properly.

particle get test4 EvansIntTemp
70
particle call test4 EvansGetTemp
70
1 Like

If there’s a delay in the setup() function, the functions/variables aren’t registered, because they have to be registered shortly after the cloud connects.

As a workaround, you can switch to SEMI_AUTOMATIC mode. Register your functions/variables, and then connect to the cloud via Particle.connect().

We plan to remove this limitation in a near future release of firmware.

4 Likes

With that in mind, you could place the function/variables before the delay, elimination the need to go with system modes. Though they’re worthwhile checking out!

1 Like

Removed all the delay in the setup(), moved the dht.begin() to after registration and it works the first time. And the second time.

That’s the first time that’s ever happened, and I think you’ve hit the problem.

Thanks very much!

2 Likes