Trouble reading values from particle.variable() [SOLVED]

Thanks, that makes sense. I went through and cleaned things up a bit. Everything is working great now.

#include "PietteTech_DHT/PietteTech_DHT.h"

// DHT parameters
#define DHTTYPE  DHT22  // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D1  // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   2000  // How often to sample in milliseconds

// Use uFL External Antenna
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

// Variables
int lastPub;
int pubDelay = 5000;  // How often to publish in milliseconds
int tempf;
int humidity;
int targettemp = 60;
int blue = D7;
int garageDoorRelay = D5;
int furnaceRelay = D6;

// furnaceHeat shows whether the furnace is running or not
int furnaceHeat = 0;
// furnaceActive enables the furnace to run
bool furnaceActive = false;


// Post Function Declarations
int garageDoor(String command);
int furnOnOff(String command);
int setFurTemp(String command);

// Declaration
void dht_wrapper();

// Lib Initialize
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// globals
unsigned int DHTnextSampleTime;  // Next time we want to start sample
bool bDHTstarted;  // flag to indicate we started acquisition
int n;  // counter

void setup() {
  // Hardware pin assignments
  pinMode(blue, OUTPUT);
  pinMode(garageDoorRelay, OUTPUT);
  pinMode(furnaceRelay, OUTPUT);

  // Variables Exposed to Cloud
  Particle.variable("temperature", tempf);
  Particle.variable("targettemp", targettemp);
  Particle.variable("humidity", humidity);
  Particle.variable("furnaceheat", furnaceHeat);

  // Functions exposed to Cloud
  Particle.function("garagedoor", garageDoor);
  Particle.function("furnonoff", furnOnOff);
  Particle.function("setfurtemp", setFurTemp);

  // DHT
  DHTnextSampleTime = 0;

  // Start out with our relays turned on
  digitalWrite(garageDoorRelay, HIGH);
  digitalWrite(furnaceRelay, HIGH);
}

void dht_wrapper() {
  DHT.isrCallback();
}

void loop() {
  // Check if we need to start the next sample
  if (millis() > DHTnextSampleTime) {
    if ( !bDHTstarted ) {  // start the sample
      DHT.acquire();
      bDHTstarted = true;
      digitalWrite(blue, HIGH);
    }

    if ( !DHT.acquiring() ) {  // has sample completed?
      humidity = DHT.getHumidity();
      tempf = DHT.getFahrenheit();

      // Turn the furnace on or off if necessary
      heatGarage();

      n++;  // increment counter
      bDHTstarted = false;  // reset the sample flag so we can take another
      DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;
    }
  }

  if ( (millis() - lastPub) > pubDelay ) {
    blinkPub();
    lastPub = millis();
  }
}

int blinkPub() {
  // Turn the built in LED on to indicate publishing
  digitalWrite(blue, HIGH);

  // Publish readings - uncomment/comment the values you'd like to publish
  Particle.publish("Humidity", String(humidity) + "%");
  Particle.publish("Temperature", String(tempf) + " Ā°F");

  // Turn the built in LED off indicate publishing finished
  digitalWrite(blue, LOW);
}

// POST functions
// These  function automagically get called upon a matching POST request
int garageDoor(String command) {
  if ( command == "activate" ) {
    activateGarageDoor();
    return 1;
  }
  return -1;
}

int furnOnOff(String command) {
  if ( command == "on" ) {
    furnaceOn();
    return 1;
  }
  if ( command == "off" ) {
    furnaceOff();
    return 1;
  }
  return -1;
}
int setFurTemp(String command) {
  if (command.length() > 0) {
    setFurnaceTemp(command);
    return 1;
  } else {
    return -1;
  }
}

// POST helper functions
int activateGarageDoor() {
  digitalWrite(garageDoorRelay, LOW);
  delay(250);
  digitalWrite(garageDoorRelay, HIGH);
  Particle.publish("Garage Door", "Active");
  return 1;
}
int heatGarage() {
  if (furnaceActive) {
    if (targettemp >= tempf) {
      digitalWrite(furnaceRelay, LOW);
      furnaceHeat = 1;
      Particle.publish("Furnace", "On");
      return 1;
    } else {
      digitalWrite(furnaceRelay, HIGH);
      furnaceHeat = 0;
      Particle.publish("Furnace", "Off");
      return -1;
    }
  } else {
    digitalWrite(furnaceRelay, HIGH);
    furnaceHeat = 0;
    Particle.publish("Furnace", "Off");
    return -1;
  }
}
int setFurnaceTemp(String command) {
  targettemp = command.toInt();
  return 1;
}
int furnaceOn() {
  furnaceActive = true;
  return 1;
}
int furnaceOff() {
  furnaceActive = false;
  return 1;
}
1 Like

Iā€™m having a similar if not exact same problem not being able to read variables. Iā€™m a noob and am struggling to understand how to apply the solution to my situation.
I have two DHT22 sensors reading temps from my refridgerator and interior space. Everything is working as expected except Iā€™m not able to query the particle variables. (and Iā€™m getting odd spikes in the readings, but thatā€™s for another post)
Can anyone help me apply the solution above to my code? Much appreciated;

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

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

//token for ubidots
#define TOKEN "abc123abc123abc123abc123" 

#define DHT1PIN 2                               // fridge temp
#define DHT2PIN 4                               // internal temp

#define DHTTYPE DHT22                           // DHT 22 (AM2302)

Ubidots ubidots(TOKEN);

DHT dhtFridge(DHT1PIN, DHTTYPE);
DHT dhtInternal(DHT2PIN, DHTTYPE);

// Define variables
double fridgeTemp = 0;
double internalTemp = 0;

void setup()
{
  Particle.variable("fridgeT", &fridgeTemp, DOUBLE);     //make fridge temps avail via var
  Particle.variable("internalT", &internalTemp, DOUBLE); //make internal temps avail via var
  dhtFridge.begin();
  dhtInternal.begin(); 
  Serial.begin(9600);
  delay(10000);
   } 

void loop()
{
  //Read humidity into humidity vars  
  float fridgHumid = dhtFridge.getHumidity();
  float internalHumid= dhtInternal.getHumidity();

  //Read Farenheit into Temp vars
  float fridgeTemp = dhtFridge.getTempFarenheit();
  float internalTemp = dhtInternal.getTempFarenheit();

  ubidots.add("fridgeTemp", fridgeTemp);	    //define fridgeTemp as ubidot var
  ubidots.add("internalTemp", internalTemp);	//define interalTemp as ubidot var
  ubidots.sendAll();                            //send vars to ubidot
  delay(60000);                                 //delay sixty sec

  // Publish temp var for reading remotely
  Particle.publish("fridgeTemp", String(fridgeTemp) + " Ā°F");
  Particle.publish("internalTemp", String(internalTemp) + " Ā°F");
}

This form is deprecated, try this instead

  Particle.variable("fridgeT", fridgeTemp);     //make fridge temps avail via var
  Particle.variable("internalT", internalTemp); //make internal temps avail via var

And don't redeclare the used variables locally, that just hides the global variable that you exposed and hence they won't be updated. Only the local ones will receive the value, but will vanish once loop() finishes.

void loop() {
  ...
  //Read Farenheit into Temp vars
  float fridgeTemp = dhtFridge.getTempFarenheit();
  float internalTemp = dhtInternal.getTempFarenheit();
  ...
}

Just remove float - BTW, your global vars are double, so why are the locals float anyway?

2 Likes

Perfect! that did the trick. Learn something everyday. I appreciate the help. As for the formatting of the variables, that was a leftover from some comments I saw earlier in the thread about ā€œoverloadingā€ the variables. Obviously didnā€™t work. Iā€™m not sure why I left the local varā€™s as float. Must have been another remnant of testingā€¦

1 Like