Help with mqtt

im using mqtt and the buttons work slowly. If i use button one after other arduino goes offline. I have succesfully converted code from old to new MQTT sensor also lag. please help i have to show project on monday.

#include <CayenneMQTTESP8266Shield.h>
#include <Ultrasonic.h>
#include <CayenneTemperature.h>
char ssid = “zzzzz”;
char wifiPassword = “zzzzz”;
char username = “zzzzz-6624-11e7-9c17-adc0b1d0cf53”;
char password = “zzzzz”;
char clientID = “z-16f7-z-z-z”;
#define EspSerial Serial
ESP8266 wifi(&EspSerial);
const int thermistorPin = 0;
const float resistance = 10000;
Thermistor thermistor(A0, 10000);
int led = LOW;
int led1 = LOW;
int led2 = LOW;
int led3 = LOW;
Ultrasonic ultrasonic(12, 13);

void setup()
{
pinMode(3, OUTPUT); // button
pinMode(2, OUTPUT); // LED or relay
pinMode(4, OUTPUT);
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

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

CAYENNE_IN(1) {
int value = getValue.asInt();
digitalWrite(2, value);
}

CAYENNE_IN(2) {
int value = getValue.asInt();
digitalWrite(3, value);
}
CAYENNE_IN(3) {
int value = getValue.asInt();
digitalWrite(4, value);
}
CAYENNE_OUT(5)
{
Cayenne.celsiusWrite(5, thermistor.getCelsius());
delay(10);
}
CAYENNE_OUT(6)
{

Cayenne.virtualWrite(6, ultrasonic.distanceRead());
delay(10);
}
CAYENNE_OUT(7)
{
Cayenne.kelvinWrite(7, thermistor.getKelvin());
delay(10);
}
CAYENNE_OUT(8)
{
Cayenne.fahrenheitWrite(8, thermistor.getFahrenheit());
delay(10);
}

try this code. i made some small changes.

#include <CayenneMQTTESP8266Shield.h>
#include <Ultrasonic.h>
#include <CayenneTemperature.h>
char ssid[] = “zzzzz”;
char wifiPassword[] = “zzzzz”;
char username[] = “zzzzz-6624-11e7-9c17-adc0b1d0cf53”;
char password[] = “zzzzz”;
char clientID[] = “z-16f7-z-z-z”;
#define EspSerial Serial
ESP8266 wifi(&EspSerial);
const int thermistorPin = 0;
const float resistance = 10000;
Thermistor thermistor(A0, 10000);
int led = LOW;
int led1 = LOW;
int led2 = LOW;
int led3 = LOW;
Ultrasonic ultrasonic(12, 13);
unsigned long lastMillis = 0;

void setup()
{
pinMode(3, OUTPUT); // button
pinMode(2, OUTPUT); // LED or relay
pinMode(4, OUTPUT);
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();{
Cayenne.celsiusWrite(5, thermistor.getCelsius());

Cayenne.virtualWrite(6, ultrasonic.distanceRead());

Cayenne.kelvinWrite(7, thermistor.getKelvin());

Cayenne.fahrenheitWrite(8, thermistor.getFahrenheit());
}
}

CAYENNE_IN(1) {
int value = getValue.asInt();
digitalWrite(2, value);
}

CAYENNE_IN(2) {
int value = getValue.asInt();
digitalWrite(3, value);
}
CAYENNE_IN(3) {
int value = getValue.asInt();
digitalWrite(4, value);
}
2 Likes

I’ll try it once I’m free