Cayenne & WiFi do not stay connected

Using an Arduino Zero based processor and WiFi101 Shield. I have been able to connect and use my dashboard in the past, but now every time I have tried to connect it will connect at first, but then nothing happens, no sensor values are read or written, and then a minute later I get the message on the dashboard “Offline” - even though the WiFi shield displays an LED meaning it is connected.

After my program passes this line:
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

I can see in the Serial Monitor the success messages:
[3213] Connecting to WiFi_Henway
[4287] Connected to WiFi
[4288] IP: ___________
[4290] Connecting to mqtt.mydevices.com:1883
[4886] Connected

I have tried my old sketches and other hardware, I have tried reworking everything through the tutorials available in the documentation, and I have tried the luminosity troubleshooting, but I cannot manage to get past the line:
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

I can connect but not maintain it, and it seems that even though I am getting successful connection messages - something is going wrong.

I have tried to put a simple test message after the line:
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
SerialUSB.println(“test”); //SerialUSB for SAMD21

but nothing shows up in the Serial Monitor after the successful Cayenne connection messages.

Any ideas on what I’m doing wrong?

this message means it has already connected and passed Cayenne.begin(). Can you share the entire code you are using.

'/*
Cayenne Luminosity Example

This sketch sample file shows how to change the brightness on an LED using Cayenne Dashboard.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:

  1. In the Cayenne Dashboard add a new Luminosity widget.
  2. Select a virtual channel number.
  3. Select a slider widget.
  4. Set the slider widget min value to 0 and max value to 255.
  5. Set the VIRTUAL_CHANNEL value below to the virtual channel number you selected.
  6. Connect the LED’s legs to GND and to a PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
    Use a 1k resistor if possible.
    Schematic:
    [Ground] – [LED] – [1k-resistor] – [PWM Pin]
  7. Set the ACTUATOR_PIN value below to the digital PWM pin number you selected.
  8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  9. Compile and upload this sketch.
  10. Once the Arduino connects to the Dashboard you can use the slider to change LED brightness.
    */

#define CAYENNE_PRINT SerialUSB // Comment this out to disable prints and save space
#include <CayenneMQTTWiFi101.h> // Cayenne library to connect to transmit data
#include <Wire.h>

/*********************** EDIT THIS SECTION TO MATCH YOUR INFO *************************/
// WiFi network information
char ssid = “-”; // your network SSID (name)
char wifiPassword = “-”; // your network password

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “-”;
char password = “-”;
char clientID = “-”;

#define VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN 13

void setup()
{
SerialUSB.begin(9600);
Wire.begin();
WiFi.setPins(8, 2, A3, -1); // VERY IMPORTANT FOR TINYCIRCUITS WIFI SHIELD

while (!SerialUSB); // Will halt program until the Serial Monitor is opened

// Connect to WiFi and Cayenne
// connectWifi();
// Cayenne.begin(username, password, clientID);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
SerialUSB.println(“test”);
}

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

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
digitalWrite(ACTUATOR_PIN, value);
}’

I got the connected message and it did connect to the dashboard. But then it just disconnected a minute later and did nothing. Tried multiple times with multiple programs - so even getting this basic example to work would be great.

WiFi shield maintained connection LED, but Cayenne shows disconnection a minute later and no updated timestamp after the intial upload and timestamp.

solution is you need to send data to cayenne to show the device is connected. add this to your code:

 CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
}

this will show a green widget to your dashboard. add it by clicking on + of the widget.

After adding the code, the same behavior continued: No widget appeared, that slider was added manually when I started working with this example.

After the initial connection and timestamp, the connection goes offline.

I will paste the original example I have been trying to get working again (as it worked in the past, about a year ago)

#include <Wire.h> // For I2C communication with sensor
//#include <WiFi101.h> // For connecting to WiFi

#define CAYENNE_PRINT SerialUSB
#include “BMA250.h” // For interfacing with the accel. sensor
#include <CayenneMQTTWiFi101.h> // Cayenne library to connect to transmit data

/*********************** EDIT THIS SECTION TO MATCH YOUR INFO *************************/
// WiFi network information
char ssid = “-”; // your network SSID (name)
char wifiPassword = “-”; // your network password

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “-”;
char password = “-”;
char clientID = “-”;
/*************************************************************************************/

// Accelerometer sensor variables for the sensor and its values
BMA250 accel_sensor;
int x, y, z;
double temp;

void setup() {
SerialUSB.begin(9600);
Wire.begin();
WiFi.setPins(8, 2, A3, -1); // VERY IMPORTANT FOR TINYCIRCUITS WIFI SHIELD

while (!SerialUSB); // Will halt program until the Serial Monitor is opened

// Connect to WiFi and Cayenne
// connectWifi();
// Cayenne.begin(username, password, clientID);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

// Set up the BMA250 acccelerometer sensor
// SerialUSB.print(“Initializing BMA…”); SerialUSB.println();
accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms);
}

void loop() {
// Take sensor reading and print to Serial Monitor
accel_sensor.read(); //This function gets new data from the acccelerometer

// Get the acceleration values from the sensor and store them into global variables
// (Makes reading the rest of the program easier)
x = accel_sensor.X;
y = accel_sensor.Y;
z = accel_sensor.Z;
temp = ((accel_sensor.rawTemp * 0.5) + 24.0);

// Call function to display sensor readings on Serial Monitor
showSerial();

// Report sensor reading to Cayenne
Cayenne.loop();
delay(2000); // delay before looping again to take new reading
}

// Prints the sensor values to the Serial Monitor (found under ‘Tools’)
void showSerial() {
SerialUSB.print("X = ");
SerialUSB.print(x);

SerialUSB.print(" Y = ");
SerialUSB.print(y);

SerialUSB.print(" Z = ");
SerialUSB.print(z);

SerialUSB.print(" Temperature© = ");
SerialUSB.println(temp);
}

// This function goes through the steps to connect to WiFi using the WiFi101 library
void connectWifi(void){
// Connect to Wifi network:
SerialUSB.print("Connecting Wifi: ");
SerialUSB.println(ssid);
WiFi.begin(ssid, wifiPassword);

// Loop until connection is secured
while (WiFi.status() != WL_CONNECTED)
SerialUSB.print(".");
delay(500);

// Success message and put WiFi in low power mode to save energy
WiFi.maxLowPowerMode();
SerialUSB.println("");
SerialUSB.println(“WiFi connected”);
}

// Default function for sending sensor data at intervals to Cayenne.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here:
Cayenne.virtualWrite(1, x);
Cayenne.virtualWrite(2, y);
Cayenne.virtualWrite(3, z);
Cayenne.virtualWrite(4, temp);
}

can you verify first with a basic code that the device gets connected to cayenne. Upload this code and check the serial monitor:

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

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

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

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

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

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Got successful connection, as I have said before, at first when uploading the progam:

Then it goes offline:

can you try this code and check whether gets connected to other broker

// This example uses an Arduino/Genuino Zero together with
// a WiFi101 Shield or a MKR1000 to connect to shiftr.io.
//
// IMPORTANT: This example uses the new WiFi101 library.
//
// You can check on your device after a successful
// connection here: https://shiftr.io/try.
//
// by Gilberto Conti
// https://github.com/256dpi/arduino-mqtt

#include <WiFi101.h>
#include <MQTT.h>

const char ssid[] = "ssid";
const char pass[] = "pass";

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nconnecting...");
  while (!client.connect("arduino", "try", "try")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");

  client.subscribe("/hello");
  // client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  // Note: Do not use the client in the callback to publish, subscribe or
  // unsubscribe as it may cause deadlocks when other things arrive while
  // sending and receiving acknowledgments. Instead, change a global variable,
  // or push to a queue and handle it in the loop after calling `client.loop()`.
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  // by Arduino. You need to set the IP address directly.
  client.begin("broker.shiftr.io", net);
  client.onMessage(messageReceived);

  connect();
}

void loop() {
  client.loop();

  if (!client.connected()) {
    connect();
  }

  // publish a message roughly every second.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    client.publish("/hello", "world");
  }
}

Seems to be working!

it looks like it seems to going into connect() function again and again, due to some connection issue.

What is it supposed to do?

it checks whether connected or not and then loops in to connect again. which should not happen.