Device (Wemos D1 mini) offline in cayenne


#1

I have two wemos d1 devices on the same network.
Of the two I have one with a slight problem on app side (I need to force quit and reopen twice for the buttons to work)
The second device has a more severe problem: It appears offline. But if I blindly open it and press a button, it returns online.

Could you please check my code? thanks `// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <Adafruit_BMP085_U.h>
#include <SimpleTimer.h>
#include <SPI.h> 

//BUTTON declarations
SimpleTimer timer;
const int buttonPin = D8;  // The number of the Pushbutton pin
int buttonState = 0;  // Variable for reading the pushbutton status
long previousMillis = 0;        // will store last time LED was updated
long interval = 3000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;


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


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


//DOOR RELAY
#define VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN D1 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//PANIC RELAY
#define VIRTUAL_CHANNEL 2
#define ACTUATOR_PIN D2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//ALARM RELAY
#define VIRTUAL_CHANNEL 3
#define ACTUATOR_PIN D3 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//BIG GATE GATE RELAY
#define VIRTUAL_CHANNEL 4
#define ACTUATOR_PIN D4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

unsigned long lastMillis = 0;

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


  //SETUP DOOR AS LOW (default locked)
  digitalWrite(D1, LOW);
  pinMode(D1, OUTPUT);
  
     //SETUP PANIC AS HIGH (default locked)
  digitalWrite(D2, HIGH);
  pinMode(D2, OUTPUT);

  //SETUP ALARM AS HIGH (default locked)
  digitalWrite(D3, HIGH);
  pinMode(D3, OUTPUT);

        //SETUP BIG GATE AS HIGH (default locked)
  digitalWrite(D4, HIGH);
  pinMode(D4, OUTPUT);

    timer.setInterval(1200L, emergencybtn);
}

void loop()  {
  Cayenne.loop(); 
    timer.run();

}

void emergencybtn()
  {   
   //REDBUTTON EMERGENCY
   previousMillis = millis();
 
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);

   //The button is pushed
    while (buttonState == HIGH) 
           

  {

       Serial.println(buttonState);
     
     currentValue = millis() - previousMillis;
    
     if (currentValue > interval)  //If the button has been pressed for over 7 secs, open it
     {          
       // save the last time relays has been turned on
       previousMillis = millis();   
       
       digitalWrite(D1, HIGH);      //opendoor
       delay(2000);     //give time to get in
      digitalWrite(D1, LOW);    //close it
     
     }
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  yield();
    }
  
  }


// DOOR RELAY   This function is called when data is sent from Cayenne.
CAYENNE_IN(1)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(D1, HIGH);
    delay(2000);
     digitalWrite(D1, LOW);
  }
  else {
    digitalWrite(D1, LOW);
  }
}


// PANIC RELAY This function is called when data is sent from Cayenne.
CAYENNE_IN(2)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(D2, LOW);
    delay(3000);
    digitalWrite(D2, HIGH);
  }
  else {
    digitalWrite(D2, HIGH);
    
  }
}

// ALARM RELAY This function is called when data is sent from Cayenne.
CAYENNE_IN(3)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(D3, LOW);
    delay(1200);
    digitalWrite(D3, HIGH);
  }
  else {
    digitalWrite(D3, HIGH);
    
  }
}

// AUTOMATIC PORCH GATE RELAY This function is called when data is sent from Cayenne.
CAYENNE_IN(4)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(D4, LOW);
    delay(2900);
    digitalWrite(D4, HIGH);
  }
  else {
    digitalWrite(D4, HIGH);
    
  }
}

#2

this is because you are not sending any data to cayenne to make in online. add this code in your code:

CAYENNE_OUT (13)
{
Cayenne.virtualWrite(0, millis());
}