ESP8266 periodically disconnects

Hi
I’m using an arduino uno in combination with an ESP8266-01 module to connect to Cayenne. With the code below, the ESP does in fact connect to Cayenne, but every minute or so, it disconnects.

#define EspSerial Serial
ESP8266 wifi(&EspSerial);

void setup()
{
Serial.begin(115200);
delay(10);

// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);

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

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

If I take a look in the serial monitor, the following shows up:
15:33:47.619 -> AT+CIPSEND=1,40
15:33:52.648 -> AT+CIPSEND=1,40
15:33:57.643 -> AT+CIPSEND=1,40
15:34:02.661 -> AT+CIPSEND=1,40
15:34:17.673 -> AT+CIPSEND=1,40
15:34:32.526 -> AT+CIPSEND=1,2
15:34:37.524 -> AT+CIPSEND=1,2
15:34:42.556 -> AT+CIPCLOSE=1
15:34:47.550 -> [639025] Disconnected

When the ESP is disconnected, a new connection is established and the above repeats itself.
I’m totally new to arduino and Cayenne, so I have no idea what I’m talking about, but does anybody know how to fix this?
Help would be grately appreciated!

Kind regards

if you read the into for this code it mentions that this code is not suited for Arduino and won’t be stable.

Anything you would recommend instead?

you can use software serial.

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

#define VIRTUAL_CHANNEL 1


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

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


SoftwareSerial EspSerial(2,3);

ESP8266 wifi(&EspSerial);

void setup()
{
  //Serial.begin(9600);   
  //delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(9600);
  delay(10);
  Serial.begin(115200);
  Serial.println("connecting to wifi");
  WiFi.begin(ssid, wifiPassword);
  Serial.println("connected to wifi");
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("disconnect to wifi");
    WiFi.disconnect();
    Serial.println("disconnected");
    Serial.println("connecting to cayenne");
    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());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

Thanks a lot!

though this is also not stable, but give it a try,

I get a bunch of error messages, including " ‘WiFi’ was not declared in this scope"…
Maybe a change in hardware is a better idea?

ohh, remove all the wifi parts.

Now I get this as an error:

no matching function for call to ‘CayenneMQTTESP8266Client::begin(char [37], char [41], char [37], char [14], char [11])’

use this one:

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

char ssid[] = "AP-E16C00";
char wifiPassword[] = "mydevices@gemtek";

SoftwareSerial mySerial(10, 11);

char username[] = "";
char password[] = "";
char clientID[] = "";


#define EspSerial mySerial

ESP8266 wifi(&EspSerial);


void setup() {
  //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_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

I’ve uploaded your code without errors, but the serial monitor doesn’t show anything… Cayenne also says that the arduino is offline.

uncomment this lines

Still nothing I’m afraid…

also this :man_facepalming:

Haha, woops, didn’t even saw it myself :smile:
Now the serial monitor does something, but it still doesn’t connect correctly.
If I check at 9600 baud, it says:
“Failed to enable MUX”
“Failed to set STA mode”
And occasionally “Failed to disable Echo”
Btw, just to be sure: I connected the RX of the ESP to pin 11(TX of Arduino) and the TX of the ESP to pin 10 (RX of Arduino), correct?
With the hardware serial, I had to upload the sketch first and then connect the wires, is that necessary here?

i hope you are using a voltage divider.

I am not actually, some posts online said that it would’nt be a problem… Could that in fact be it?

it is always better to be on the safer side.

But that still won’t fix my code I’m afraid :smile:
Could it be that the Uno provides insufficient power for the ESP?

let’s test it out whether the esp is working and we can communicate with it. upload this code and send AT and see if esp send okay response.

// Basic serial communication with ESP8266
// Uses serial monitor for communication with ESP8266
//
//  Pins
//  Arduino pin 2 (RX) to ESP8266 TX
//  Arduino pin 3 to voltage divider then to ESP8266 RX
//  Connect GND from the Arduiono to GND on the ESP8266
//  Pull ESP8266 CH_PD HIGH
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the ESP8266
//
 //AT+CIOBAUD=9600
//AT+IPR=9600
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(10, 11); // RX | TX
 //AT+CIOBAUD=9600
 
void setup() 
{
    Serial.begin(9600);     // communication with the host computer
    //while (!Serial)   { ; }
 
    // Start the software serial for communication with the ESP8266
    ESPserial.begin(115200);  
 
    Serial.println("");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("Ready");
    Serial.println("");    
}
 
void loop() 
{
    // listen for communication from the ESP8266 and then write it to the serial monitor
    if ( ESPserial.available() )   {  Serial.write( ESPserial.read() );  }
 
    // listen for user input and send it to the ESP8266
    if ( Serial.available() )       {  ESPserial.write( Serial.read() );  }
}