Button widget long loading times

Hie all.

I am new to Cayenne Iot and i am using a generic lolin esp8266. The point of my project at the moment is to control a relay with a button.

When i press the button widget the button is supposed to illuminate blue when pressed but the button just shows the loading annimation as shown in the picture until the page as been refreshed. This happens on the app version in which i have to exit the app and re-load it again. Is there a fix for this? Thank you kindly…

Image showing issue:

1 Like

delete this device from the dashboard and add a new one by following this steps:

  1. add new —>device/widgets —>arduino ----> copy and paste the MQTT credentials into the below code and upload —> this add a new device to your dashboard. Cayenne-MQTT-Arduino/ESP8266.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub
  2. add the green widget for channel 0 which pops up.
  3. add a new button widget:- add new —> device/widgets —> actuator —> generic —> digital output.

Hi when i do step 3 it won’t let me select device. Please see image below.

that is because you have not followed the step 1 correctly.

My apologies. i have redone step 1 i can now select the digital out put widget in step 3. The issue still persists as seen below:

Widget settings

Thanks again!

can you add the channel 0, by clicking on “+” and make sure that the device is online and sending data continously.

Im still having the same issue. The data is being sent to my arduino rp2040 connect but the button keeps loading on forever. I am not sure how to fix it. Some help pls

This is my code btw

#include <SPI.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include <Arduino_LSM6DSOX.h>


int hydrogen;
int temperature_deg;
float x, y, z;


//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "TELUSWiFi0280";        // your network SSID (name)
char pass[] = "8m4c8DbzKz";    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status


#define MQTT_SERVER     "mqtt.mydevices.com"
#define MQTT_SERVERPORT 1883
#define MQTT_USERNAME   "973c1410-f0b0-11ec-9f5b-45181495093e"
#define MQTT_PASSWORD   "ba071053e33cabd2a7fe0149e2b52c34c01323b1"
#define CLIENT_ID       "402c18d0-f314-11ec-a681-73c9540e1265"

//#define CLIENT_ID_SUB

#define MQTT_TOPIC_HYDROGEN  "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/data/1"
#define MQTT_TOPIC_X   "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/data/2"
#define MQTT_TOPIC_Y   "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/data/3"
#define MQTT_TOPIC_Z   "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/data/4"
#define MQTT_TOPIC_BOARD_TEMP   "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/data/5"
#define MQTT_TOPIC_LED   "v1/" MQTT_USERNAME "/things/" CLIENT_ID "/cmd/6"

WiFiClient espClient;
PubSubClient client(MQTT_SERVER, MQTT_SERVERPORT, espClient);

//Defineining a datatype to store the mqtt message, up to 50 bytes
char  msg[50];


long    timeStampNow;
long    timeStampLastMsg = 0;

void setup() {
  //Built-in LED Setup
  pinMode(LED_BUILTIN, OUTPUT);
  
  // Initialize serial monitor for debugging purposes.
  Serial.begin(9600);
  Serial.println("DEBUG: Entering setup ");

  // Attempt to connect to WiFi.
  WiFi.begin(ssid, pass);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

 // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

 // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
   // delay(2000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");

//Accelerometer setup
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in g's");
  Serial.println("X\tY\tZ");

//Onboard temperature sensor setup
if (IMU.temperatureAvailable()){
    int temperature_deg = 0;
    }

  // Initialize MQTT PubSub Library
  client.setServer(MQTT_SERVER, MQTT_SERVERPORT);
  // Function called in case of an incomming mqtt message from Cayenne Dashboard
  // (In this case is the dashboard button to turn on/off the heater)
  client.setCallback(mqttCallback);
  Serial.println("DEBUG: Setup Done! ");
  

}

// -------------------- LOOOP -------------------

void loop() {
  // Check if the esp8266 is connected to the mqtt server.
  if (!client.connected()) {
    reconnect();
  }

  // If connected, perform PubSubClient housekeeping function.
  client.loop();

  // Publish temperature readings every 2 seconds (2000ms)
  timeStampNow = millis();
  if (timeStampNow - timeStampLastMsg > 5000) {    // Publish interval in milliseconds
    // Reset the counter
    timeStampLastMsg = timeStampNow;
    // Jump to the actual function to read and publish temperatures.
    hydrogendata();
    accelerometer();
    board_temp();
  }
  client.subscribe(MQTT_TOPIC_LED);
  delay(1000);
  //Serial.println("DEBUG: Reached MAIN LOOP END --> Looping ");
}


// ------------------------------ OTHER FUNCTIONS

// Function that attempts to reconnect to the mqtt server.

void reconnect() {
  // If esp8266 is disconnected from the mqtt server, try to reconnect.
  // (*** I still need to think what wil happen to the boiler if connection is lost ***)
  while (!client.connected()) {
    Serial.print("DEBUG: Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect(CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
      Serial.println("connected");

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  //Serial.println("DEBUG: Quiting reconnect loop ");
}


// Function that handles temperature adquisition from sensors, and publishing to Cayenne Dashboard.
void hydrogendata(){
  hydrogen = analogRead(A0);
  // Construct the mqtt message following Cayenne's rules, according to the Docs..
  // Send Sensor data -> Topic: v1/username/things/clientID/data/channel

  //using a float rounded to 2 decimal places.
  String hydrogen_message = String(hydrogen);
  //Serial.println(hydrogen_message);
  mqtt_message(hydrogen_message);
  
  // Finally! Publish the temperature to Cayenne Dashboard, Channel 1.
  client.publish(MQTT_TOPIC_HYDROGEN,msg);
  }

void mqtt_message(String message){
  // Convert the string into a Char Array
  message.toCharArray(msg, 50);
  //Serial.print("Publish message: ");
  //Serial.println(msg);  
  }

void accelerometer(){
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
//    Serial.print(x);
//    Serial.print('\t');
//    Serial.print(y);
//    Serial.print('\t');
//    Serial.println(z);
  }
  String acc_x_message = String(x);
  String acc_y_message = String(y);
  String acc_z_message = String(z);

//Publishing accelerometer values to Cayenne Dashboard, Channel 2-4
  mqtt_message(acc_x_message);
  client.publish(MQTT_TOPIC_X,msg);
  mqtt_message(acc_y_message);
  client.publish(MQTT_TOPIC_Y,msg);
  mqtt_message(acc_z_message);
  client.publish(MQTT_TOPIC_Z,msg);
}
     
void board_temp(){
    IMU.readTemperature(temperature_deg);
    String onboard_temp = String(temperature_deg);
    mqtt_message(onboard_temp);
    client.publish(MQTT_TOPIC_BOARD_TEMP,msg);
}

// Function to intercept MQTT messages
void mqttCallback(char* topic, byte* payload, unsigned int length) {
 char receivedChar;
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    // Show the received message "stream" in the serial monitor.
    Serial.print((char)payload[i]);
    receivedChar = (char)payload[i];
    }
    
  Serial.print (receivedChar);
 //String str_msg = String(receivedChar);
 if(receivedChar == '1') {
   digitalWrite(LED_BUILTIN,HIGH);
 }else if (receivedChar =='0') {
   digitalWrite (LED_BUILTIN,LOW);
 }
  
  Serial.println();
}