Arduino MQTT lots of bugs

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#include <dht.h>
dht DHT;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
float gas_value;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
  pinMode(24, OUTPUT);
  pinMode(28, OUTPUT);
}

void loop() {
	Cayenne.loop();
}

//Temperatuur DHT11 Channel 0 & Pin 6
CAYENNE_OUT(0)
{
  int chk = DHT.read11(6);
  Cayenne.celsiusWrite(0, DHT.temperature);
}

//Koolstofmonoxide Channel 1 & Pin A0
CAYENNE_OUT(1)
{
  gas_value=analogRead(A0);
  Cayenne.virtualWrite(1, gas_value, "co", "ppm");
}
//Wastafel Lamp Channel 2 Pin 24
CAYENNE_IN(2)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(24, HIGH);
  }
  else {
    digitalWrite(24, LOW);
  }
}

//Vochtigheid Channel 3 Pin 6
CAYENNE_OUT(3)
{
  int chk = DHT.read11(6);
   Cayenne.virtualWrite(3, DHT.humidity, "rel_hum", "p");
}
//Ledstrip Channel 4 Pin 28
CAYENNE_IN(4)
{
  if (getValue.asInt() == 0) {
    digitalWrite(28, HIGH);
  }
  else {
    digitalWrite(28, LOW);
  }
}

//Led Strip Rood
CAYENNE_IN(5)
{
  int currentValue = getValue.asInt(); 
  analogWrite(2, currentValue); 
}

//Led Strip Groen
CAYENNE_IN(6)
{
  int currentValue = getValue.asInt(); 
  analogWrite(3, currentValue); 
}

//Led Strip Blauw
CAYENNE_IN(7)
{
  int currentValue = getValue.asInt(); 
  analogWrite(4, currentValue); 
}

//Lamp Kamer Channel 8 Pin ?
CAYENNE_IN(8)
{
//  if (getValue.asInt() == 0) {
//    digitalWrite(?, HIGH);
//  }
//  else {
//    digitalWrite(?, LOW);
//  }
}

//Rolluik Open Channel 9 Pin ?
CAYENNE_IN(9)
{
//  if (getValue.asInt() == 0) {
//    digitalWrite(?, HIGH);
//  }
//  else {
//    digitalWrite(?, LOW);
//  }
}

//Rolluik Dicht Channel 10 Pin ?
CAYENNE_IN(10)
{
//  if (getValue.asInt() == 0) {
//    digitalWrite(?, HIGH);
//  }
//  else {
//    digitalWrite(?, LOW);
//  }
}

Led strip colors sliders works on pc but takes long. doesn’t work on android at all but thats an common bug

code look fine. what are problem you are facing?

unless the humitidy the only problem is the speed. Before my update to MQTT everything was perfect. When i changed colors of my led strip it changed immediately, now it takes 8 seconds.

And my internet connection is fine

i am not sure why there is a delay. there is a max of 2 sec delay at my end.

i checked it out.
he says DHT sensor library by Adafruit version 1.2.3 is being used but when i use that version or any other version it gives errors about his code.

for example:
DHT dht(DHTPIN, DHTTYPE);

exit status 1
‘DHT’ does not name a type

according to this: Sending MQTT messages within rate limits
Cayenne only uses intervals about 15 seconds.
But that should be with actuators so i don’t think that is it

download and install GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors.
next in examples open DHTtester.ino and run.

btw, according to my delay issue. i see here that you fixed it for somebody else :stuck_out_tongue:

did it work for him? i think thats the same issue as me.

that is a different case.

is it? his case is delay on a button>relay. so is mine.

But ok, back to DHT11,

Test works
DHTxx test!
Humidity: 46.00 % Temperature: 27.00 *C 80.60 *F Heat index: 27.18 *C 80.93 *F

i will try to fix it according to this script. i will be back soon.

he wants a return response from the board once the button is pressed.

ok, i changed my code.
my iot script now displays the humitidy in serial but not to the dashboard:

float h, t;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
  pinMode(24, OUTPUT);
  pinMode(28, OUTPUT);
  dht.begin();
}
    void loop() {
    	Cayenne.loop();
     
      delay(2000);
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print(" %\t");
      Serial.print("Temperature: ");
      Serial.print(t);
    }

    //DHT11 Channel 0-3 & Pin 6
    CAYENNE_OUT_DEFAULT()
    {
      Cayenne.celsiusWrite(0, t);
      Cayenne.virtualWrite(3, h, "rel_hum", "p");
    }

what is wrong with my code what makes it sent “0”

float h, t;
 unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
  pinMode(24, OUTPUT);
  pinMode(28, OUTPUT);
  dht.begin();
}
    void loop() {
    	Cayenne.loop();
     
      delay(2000);
      h = dht.readHumidity();
      t = dht.readTemperature();
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print(" %\t");
      Serial.print("Temperature: ");
      Serial.print(t);
    
 if (millis() - lastMillis > 10000) {
    lastMillis = millis();
      Cayenne.virtualWrite(0, t, "temp", "c")
      Cayenne.virtualWrite(3, h, "rel_hum", "p");
    }
 }
1 Like

should i remove my whole function or only the virtualwrite?

//DHT11 Channel 0-3 & Pin 6
CAYENNE_OUT_DEFAULT()
{
  Cayenne.celsiusWrite(0, t);
  Cayenne.virtualWrite(3, h, "rel_hum", "p");
}

you can use both.

nevermind it works

1 Like

thanks for your fast support.
this was something that never worked for me until now!

2 Likes