ESP8266 goes offline when processing a function

Hie all

I am using a wemos d1 mini and an node mcu board. I have tried this on both boards with the same issue. I am using both the android and web dashboards.

The problem i am having is that when i run a scheduled task or manually activate a button in the dashboard, the system will run for about 2 minutes before "going offline until said event/process has finished. Example: I have a button in my dashboard which activates a relay for 3 minutes and then shuts off the relay. When i press the button, the relay switches on as expected but about 1.5 to 2 minutes into the relay being on, the dashboard shows as offline and i get an email notification saying my device is offline. (I set an alert to tell me when the device goes offline.)

here is my code

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

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


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


int soillevel = analogRead(A0);


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(5,OUTPUT); //Set pin 5 as OUTPUT pin, to send signal to relay
  
}


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(0)
{

  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.

  int x  = analogRead(A0);
  int w = (x-24)/10;
  
  Cayenne.virtualWrite(0, w);
  
}

CAYENNE_OUT(1)

{

      Cayenne.virtualWrite(2, digitalRead(5));

}

// This function will be called every time a Dashboard widget writes a value to Virtual Channel 4.
CAYENNE_IN(2)
{
  int val1 = getValue.asInt();
  digitalWrite(5, val1);
  delay(180000);
}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
}

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

}

My Dash

you cannot use delay in the loop. try to use millis to count the amount of time.

Hi thank you for your help.

I have tried using millis but the relay witches on and stays on. Please see my code below:

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <SimpleTimer.h>
 SimpleTimer timer;

int  State = LOW; //initial state of relay pin
unsigned long previousMillis = 0;  
const long interval = 5000; 



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


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

int waterlevel = digitalRead(5);
int soillevel = analogRead(A0);


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(5,OUTPUT); //Set pin 5 as OUTPUT pin, to send signal to relay
  
}


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(0)
{

//Analog Sensor

  int x  = analogRead(A0);
  int w = (x-24)/10;
  
  Cayenne.virtualWrite(0, w);
  
}

CAYENNE_OUT(1)

{
      //Read State of pin 5
      Cayenne.virtualWrite(2, digitalRead(5));

}

// This function will be called every time a Dashboard widget writes a value to Virtual Channel 2.
CAYENNE_IN(2)
{


unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis+5000UL;
    
      int val1 = getValue.asInt();

if (getValue.asInt() == 1)// if the button on dash has been turned on
{
State = HIGH; // Switch on Relay
}

 else
 {
      State = LOW; //Turn off relay


 }
   
      digitalWrite(5, State);
   previousMillis = millis();

  }


}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
}

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

}

Any advice is greatly appreciated. Thank you!

this function is only called when there is message received from cayenne hence the code inside the function will run only once and the counting of time wont happen. Use an variable and count the time in the main loop.

Thank you. Is there an example that i can follow? or pseudo code? Im new to the millis function and it seems to be a bit complicated. I appreciate you assistance you have been very helpful.

Something like this:

void loop()
{
if (start){
   if(millis() - lastMillis > 10000) {
      digitalWrite(5, 0);
      start = false;
}
}
}

CAYENNE_IN(2)
{
 start = true;
digitalWrite(5, 1);
lastMillis = millis();
}
1 Like

Hi thank you i have managed to get it working. Thank you very much for your help. The ESP8266 stays on now.

2 Likes