Publish showing correct value but API isn't

I have some code I’m testing out and the idea is to expose the variables for temp/humdity/lux/ppm via the particle.variable api so I can poll the data from another system. So far i have gotten everything to work with the exception of the CO2 value when querying the API. If i look at the Particle console for the publish events I see the correct value assigned to the CO2 variable, but when I use the API it returns 0 instead (all the other variables are fine).

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


// DHT11 (Temp/Humidity) parameters
#define DHTPIN 5
#define DHTTYPE DHT11
// DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// MQ5 (GAS) parameters
int number = 0;
int state = 0;
MQ135 gasSensor = MQ135(1);
float rzero = gasSensor.getRZero();
int ppm = gasSensor.getPPM();

/* TSL2561 Light Sensor
   Connections
   ===========
   Connect SCL to analog D1
   Connect SDA to analog D0
   Connect VDD to 3.3V DC
   Connect GROUND to common ground

*/
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);



//variables
int temperature;
int humidity;
int lux;
int CO2;

void setup() {
    
    // Start serial port
    Serial.begin(9600);
    
    // Start DHT sensor
    dht.begin();
    
    // Start light sensor
    Serial.println("Light Sensor Test"); Serial.println("");
    
    /* Initialise the sensor */
    if(!tsl.begin())
    {
    /* There was a problem detecting the ADXL345 ... check your connections */
        Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    }
    
    /* Display some basic information on this sensor */
    displaySensorDetails();
    
    /* Setup the sensor gain and integration time */
    configureSensor();
    
    //Expose data to REST API
    Particle.variable("temperature", temperature);
    Particle.variable("humidity", humidity);
    Particle.variable("lux", lux);
    Particle.variable("CO2", CO2);
}

void loop() {
    
    int temperature = getTemperature();
    int humidity = getHumidity();
    int lux = getLux();
    int CO2 = getCO2();

}

int getTemperature(void)
{
    // Temperature measurement
    temperature = dht.getTempFarenheit();
    
    Particle.publish("temperature", String(temperature) + " °F");

    return temperature;
}
int getHumidity(void)
{
    // Humidity measurement
    humidity = dht.getHumidity();
    
    Particle.publish("humidity", String(humidity) + "%");

    return humidity;
}
int getLux(void)
{
    // Light level measurement
    sensors_event_t event;
    tsl.getEvent(&event);
    
    /* Display the results (light is measured in lux) */
    if (event.light)
    {
        lux = event.light;
    }
    else
    {
        /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
        lux = 0;
    }
    
    Particle.publish("light", String(lux) + " lux");

    return lux;
}
int getCO2(void)
{
    //Gas level measument
    float rzero = gasSensor.getRZero(); // baseline MQ sensor

    //Get gas levels correcting for temp/himidity
    int co2_ppm = gasSensor.getCorrectedPPM(temperature,humidity);
    int ppm = (co2_ppm / 4) * 100;
    
    Particle.publish("CO2", String(ppm) + " PPM");
    
    return ppm;
}
void configureSensor(void)
{
    /**************************************************************************/
    /*
        Configures the gain and integration time for the TSL2561
    */
    /**************************************************************************/
    /* You can also manually set the gain or enable auto-gain support */
    // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
    // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
    tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
    
    /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
    tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
    // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
    // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
    
    /* Update these values depending on what you've set above! */  
    Serial.println("------------------------------------");
    Serial.print  ("Gain:         "); Serial.println("Auto");
    Serial.print  ("Timing:       "); Serial.println("13 ms");
    Serial.println("------------------------------------");
}
void displaySensorDetails(void)
{
    /**************************************************************************/
    /*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
    */
    /**************************************************************************/
    sensor_t sensor;
    tsl.getSensor(&sensor);
    Serial.println("------------------------------------");
    Serial.print  ("Sensor:       "); Serial.println(sensor.name);
    Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
    Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
    Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
    Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
    Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
    Serial.println("------------------------------------");
    Serial.println("");
    delay(500);
}

You are re-declaring your variables in every loop even though you defined them in the header. Try making your loop() look like this:

void loop() {
    temperature = getTemperature();
    humidity = getHumidity();
    lux = getLux();
    CO2 = getCO2();
}
4 Likes

That did it.

Thank you.

1 Like