Event Troubleshooting

Greetings to the Particle community!

I’m trying to send the readings from my DHT22 sensor to a Particle Console Event. Even though I succesfully compile the firmware, neither temperature nor humidity display in the event’s section of the console. Could someone help me to tell if something is wrong with the syntax within the following code?:

#include <JsonParserGeneratorRK.h>
#include <Adafruit_DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht (DHTPIN, DHTTYPE);

int temp, humidity;
double temp_dbl, humidity_dbl;
const unsigned long UPDATE_INTERVAL = 2000;
unsigned long lastUpdate = 0;

void setup() 
{
    dht.begin();

    Particle.variable("temp", temp_dbl);
    Particle.variable("humidity", humidity_dbl);
}

void loop() 
{
    unsigned long currentMillis = millis();
    
    if(currentMillis - lastUpdate >= UPDATE_INTERVAL)
    {
        lastUpdate = millis();
    }

    temp = dht.getTempCelcius();
    humidity = dht.getHumidity();
    
    temp_dbl = temp;
    humidity_dbl = humidity;
    
    Serial.printlnf("Temp: %.f", temp_dbl);
    Serial.printlnf("Humid: %.f", humidity_dbl);
}

void createEventPayload(int temp, int humidity)
{
    JsonWriterStatic <256> jw;
    {
        JsonWriterAutoObject obj(&jw);
        jw.insertKeyValue("temp",temp);
        jw.insertKeyValue("humidity", humidity);
    }
    
    Particle.publish("dht-vals", jw.getBuffer(), PRIVATE);
}

Hi,
Welcome to the community ! :slight_smile:
it's your entire code ????
firstly:

with this you just update/overvrite your lastUpdate variable nothing else and then call the rest stuff, as fast as loop is,
should be:

void loop() 
{
    unsigned long currentMillis = millis();
    
    if(currentMillis - lastUpdate >= UPDATE_INTERVAL)
    {
        lastUpdate = millis();
  

    temp = dht.getTempCelcius();
    humidity = dht.getHumidity();
    
    temp_dbl = temp;
    humidity_dbl = humidity;
    
    Serial.printlnf("Temp: %.f", temp_dbl);
    Serial.printlnf("Humid: %.f", humidity_dbl);
     }
}

to update your data every 2000ms

Secondly:
could you point me where in your loop is a call for function :

createEventPayload(temp_dbl, humidity_dbl );

if you not going to do that you never gonna get any Particle.publish();
:stuck_out_tongue_winking_eye:
Update:
also this part is a little wrong

try dirlectrly assign sensor values:
temp_dbl = (double)dht.getTempCelcius();
humidity_dbl = (double)dht.getHumidity();

and you function call constructor:

void createEventPayload(double temp, double humidity)
and you can remove global int temp, humidity; from the beginning of your code

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.