sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
227 stars 121 forks source link

garage status #376

Closed Neu59 closed 1 month ago

Neu59 commented 2 months ago

Hello friends, my garage only works with a pulse generated by a relay, How can I change the state of the door from open to closed in the app without it acting on the relay?

sivar2311 commented 1 month ago

Hi @Neu59 !

You can use sendDoorStateEvent to send the current state or update the state on the server.

Neu59 commented 1 month ago

Thanks for answering, I use this line to send the status of the door but it gives me an error when compiling. SinricPro.sendDoorStateEvent(GARAGEDOOR_ID_2, true);

'class SINRICPRO_3_0_1::SinricProClass' has no member named 'sendEvent'; did you mean 'sendQueue'?

sivar2311 commented 1 month ago

Unfortunately, this is wrong.

SinricPro object does not have such a function, but SinricProGarageDoor has!

// get GarageDoor object
SinricProGarageDoor& myGarageDoor = SinricPro[GARAGEDOOR_ID_2];
// send event
myGarageDoor.sendDoorStateEvent(true);
Neu59 commented 1 month ago

Thanks friend for the help but I continue with the compilation problem. 'class SINRICPRO_3_0_1::SinricProClass::Proxy' has no member named 'sendDoorStateEvent' SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(false);

Neu59 commented 1 month ago
#include <Arduino.h>
#include <WiFiManager.h>
#include "SinricPro.h"
#include "SinricProGarageDoor.h"

#define APP_KEY           "xxxxxxxxxxxxxxxxxxxxxxxxx0"
#define APP_SECRET        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxee-48e7-860b-7df34825a937"
#define BAUD_RATE         115200

const int GARAGE_DOOR_PIN_1 = 22;
const int GARAGE_DOOR_PIN_2 = 23;
const char* GARAGEDOOR_ID_1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* GARAGEDOOR_ID_2 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;

bool doorState = false;
unsigned long lastOpenTime = 0;
const unsigned long doorCloseDelay = 20000; // 20 seconds delay before closing the door

bool onDoorState(const String& deviceId, bool &doorState) {
  Serial.printf("Garagedoor %s is %s now.\r\n", deviceId.c_str(), doorState?"closed":"open");

  if (deviceId == GARAGEDOOR_ID_1) {
    digitalWrite(GARAGE_DOOR_PIN_1, LOW); // Activa el relé
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Desactiva el relé
  } else if (deviceId == GARAGEDOOR_ID_2) {
    digitalWrite(GARAGE_DOOR_PIN_2, LOW); // Activa el relé
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Desactiva el relé
  }

  // Send the event to the Sinric Pro server to update the status of the door

  if (doorState) {
    // Si la puerta está cerrada, enviar evento de puerta abierta
    SinricPro[GARAGEDOOR_ID_1].sendDoorStateEvent(true);
    SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(true);
  } else {
    // Si la puerta está abierta, enviar evento de puerta cerrada
    SinricPro[GARAGEDOOR_ID_1].sendDoorStateEvent(false);
    SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(false);
  }

  return true;
}

void setupSinricPro() {
  SinricProGarageDoor &myGarageDoor1 = SinricPro[GARAGEDOOR_ID_1];
  myGarageDoor1.onDoorState(onDoorState);

  SinricProGarageDoor &myGarageDoor2 = SinricPro[GARAGEDOOR_ID_2];
  myGarageDoor2.onDoorState(onDoorState);

  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

  WiFiManager wifiManager;
  wifiManager.autoConnect("GarageDoorAP");

  pinMode(GARAGE_DOOR_PIN_1, OUTPUT);
  pinMode(GARAGE_DOOR_PIN_2, OUTPUT);

  digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Set initial state to off
  digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Set initial state to off

  setupSinricPro();
}

void loop() {
  SinricPro.handle();

  if (doorState && (millis() - lastOpenTime >= doorCloseDelay)) {
    // If the door was opened and it's been more than 20 seconds, close the door
    doorState = false;
    Serial.println("Closing garage door...");
    // You can add your code here to trigger the event to close the door in the Sinric Pro app
    // SinricPro[GARAGEDOOR_ID_1].sendDoorStateEvent(false);
    // SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(false);
  }
}

error when compiling 'class SINRIC PRO_3_0_1::Sinric Pro Class::Proxy' has no member named 'sendDoorStateEvent'

Neu59 commented 1 month ago

I just want the open door indicators in the application to show a closed door again after 20 seconds after opening.

sivar2311 commented 1 month ago

let me check

sivar2311 commented 1 month ago
  if (doorState) {
    // Si la puerta está cerrada, enviar evento de puerta abierta
    SinricPro[GARAGEDOOR_ID_1].sendDoorStateEvent(true);
    SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(true);
  } else {
    // Si la puerta está abierta, enviar evento de puerta cerrada
    SinricPro[GARAGEDOOR_ID_1].sendDoorStateEvent(false);
    SinricPro[GARAGEDOOR_ID_2].sendDoorStateEvent(false);
  }

This code is wrong and makes no sense inside the onDoorState callback. Please remove this.

I don't understand your setup and what you want to acheive.

Please explain your setup in detail and what you're trying to acheive.

I'm off for now and will answer to your reply in about two hours.

Neu59 commented 1 month ago
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
  #define DEBUG_ESP_PORT Serial
  #define NODEBUG_WEBSOCKETS
  #define NDEBUG
#endif 

#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
  #include <WiFi.h>
#endif

#include <WiFiManager.h>            // Include the library
#include "SinricPro.h"
#include "SinricProGarageDoor.h"
#include <SimpleTimer.h>

#define APP_KEY           "xxxxxxxxxxxxxxxxxxx-f6be5a8e2f40"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "xxxxxxxxxxxxxxxxxx-9e514e9b7f21-eec12790-4aee-48e7-860b-7df34825a937"   // Should look like "
#define BAUD_RATE         115200                     // Change baudrate to your need

SimpleTimer timer;
bool paso1 = true;
bool paso2 = true;
const int GARAGE_DOOR_PIN_1 = 22;
const int GARAGE_DOOR_PIN_2 = 23;
const char* GARAGEDOOR_ID_1 = "xxxxxxxxxxxxxxxxxxxxxx32";  // Should look like "5dc1564130xxxxxxxxxxxxxx"
const char* GARAGEDOOR_ID_2 = "xxxxxxxxxxxxxxxxxxxxf537ddf";  // Should look like "5dc1564130xxxxxxxxxxxxxx"
int Segundos;
int Segundos2;
bool onDoorState(const String& deviceId, bool &doorState) {
  Serial.printf("Garagedoor %s is %s now.\r\n", deviceId.c_str(), doorState?"closed":"open");

  if (deviceId == GARAGEDOOR_ID_1) {
    digitalWrite(GARAGE_DOOR_PIN_1, LOW); // Activa el relé
    paso1 = false;
    Segundos = 0;
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Desactiva el relé
  } else if (deviceId == GARAGEDOOR_ID_2) {
    digitalWrite(GARAGE_DOOR_PIN_2, LOW); // Activa el relé
    paso2 = false;
    Segundos2 = 0;
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Desactiva el relé
  }

  return true;
}

void setupSinricPro() {
  SinricProGarageDoor &myGarageDoor1 = SinricPro[GARAGEDOOR_ID_1];
  myGarageDoor1.onDoorState(onDoorState);

  SinricProGarageDoor &myGarageDoor2 = SinricPro[GARAGEDOOR_ID_2];
  myGarageDoor2.onDoorState(onDoorState);

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);

  Segundos ++;
  Segundos2 ++;
  if(Segundos >= 20 && paso1 == false)
  {
   Serial.println("envio estado P11111111111111111");
   myGarageDoor1.sendDoorStateEvent(true);   
   paso1 = true;
  }
   if(Segundos2 >= 20 && paso2 == false)
  {
  Serial.println("envio estado P222222222222222222");
   myGarageDoor2.sendDoorStateEvent(true);    
   paso2 = true;
  }
  if(Segundos >=25)
  {
    Segundos = 0;
  }
  if(Segundos2 >=25)
  {
    Segundos2 = 0;  
  }
}

void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

  WiFiManager wifiManager;        // Create an instance of WiFiManager
  wifiManager.autoConnect("GarageDoorAP"); // Start WiFiManager

  pinMode(GARAGE_DOOR_PIN_1, OUTPUT);
  pinMode(GARAGE_DOOR_PIN_2, OUTPUT);

  // Invert logic for relay control
  digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Set initial state to off
  digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Set initial state to off

  timer.setInterval(1000, setupSinricPro);
}

void loop() {
  SinricPro.handle();
   timer.run();
}

Thanks friend for your information, I solved it that way

my goal was. After pressing open in the app, it went back to closed after 20 seconds and it worked fine.

sivar2311 commented 1 month ago

Ok, I think I begin understand what you want to acheive and your approach looks promising.

But don't do this in setupSinricPro! Write an extra function for this and call it in setIntervall ! Otherwise SinricPro.begin() is called everytime the timer fires! -> not good.

Neu59 commented 1 month ago
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
  #define DEBUG_ESP_PORT Serial
  #define NODEBUG_WEBSOCKETS
  #define NDEBUG
#endif 

#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
  #include <WiFi.h>
#endif

#include <WiFiManager.h>            // Include the library
#include "SinricPro.h"
#include "SinricProGarageDoor.h"
#include <SimpleTimer.h>

#define APP_KEY           "d0465cxxxxxxxxxxxxxxxx"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"   // Should look like 
"5f36xxxxxxxxxxxxxxxxxx-333d65xxxxxx"
#define BAUD_RATE         115200                     // Change baudrate to your need

SimpleTimer timer;
WiFiManager wifiManager;

bool paso1 = true;
bool paso2 = true;
const int GARAGE_DOOR_PIN_1 = 22;
const int GARAGE_DOOR_PIN_2 = 23;
const int RESET = 21;
const char* GARAGEDOOR_ID_1 = "6xxxxxxxxxxxxxxxxxxxxxxxx";  // Should look like "5dc1564130xxxxxxxxxxxxxx"
const char* GARAGEDOOR_ID_2 = "6xxxxxxxxxxxxxxxxxxxxxxx";  // Should look like "5dc1564130xxxxxxxxxxxxxx"
int Segundos;
int Segundos2;

bool onDoorState(const String& deviceId, bool &doorState) {
  Serial.printf("Garagedoor %s is %s now.\r\n", deviceId.c_str(), doorState?"closed":"open");

  if (deviceId == GARAGEDOOR_ID_1) {
    digitalWrite(GARAGE_DOOR_PIN_1, LOW); // Activa el relé
    paso1 = false;
    Segundos = 0;
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Desactiva el relé
  } else if (deviceId == GARAGEDOOR_ID_2) {
    digitalWrite(GARAGE_DOOR_PIN_2, LOW); // Activa el relé
    paso2 = false;
    Segundos2 = 0;
    delay(500); // Espera 0.5 segundos
    digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Desactiva el relé
  }

  return true;
}

void setupSinricPro() {

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);

}
 void SetInterval()
 {
  SinricProGarageDoor &myGarageDoor1 = SinricPro[GARAGEDOOR_ID_1];
  myGarageDoor1.onDoorState(onDoorState);

  SinricProGarageDoor &myGarageDoor2 = SinricPro[GARAGEDOOR_ID_2];
  myGarageDoor2.onDoorState(onDoorState);

  Segundos ++;
  Segundos2 ++;

  if(Segundos >= 20 && paso1 == false)
  {
   Serial.println("envio estado P11111111111111111");
   myGarageDoor1.sendDoorStateEvent(true);   
   paso1 = true;
  }
   if(Segundos2 >= 20 && paso2 == false)
  {
  Serial.println("envio estado P222222222222222222");
   myGarageDoor2.sendDoorStateEvent(true);    
   paso2 = true;
  }
  if(Segundos >=25)
  {
    Segundos = 0;
  }
  if(Segundos2 >=25)
  {
    Segundos2 = 0;  
  }
 }
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

//  WiFiManager wifiManager;        // Create an instance of WiFiManager
  wifiManager.autoConnect("GarageDoorAP"); // Start WiFiManager

  pinMode(GARAGE_DOOR_PIN_1, OUTPUT);
  pinMode(GARAGE_DOOR_PIN_2, OUTPUT);  
  pinMode(RESET, INPUT_PULLUP);

  // Invert logic for relay control
  digitalWrite(GARAGE_DOOR_PIN_1, HIGH); // Set initial state to off
  digitalWrite(GARAGE_DOOR_PIN_2, HIGH); // Set initial state to off

  setupSinricPro ();
  timer.setInterval(1000, SetInterval);

}

void loop() {
  SinricPro.handle();
   timer.run();
  if(digitalRead(RESET) == LOW) 
  {
    wifiManager.resetSettings();
  }
}

is ok??

sivar2311 commented 1 month ago

Looks better now :)

This is how I would do it (without Timer library)

#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProGarageDoor.h>

const char* WIFI_SSID = "";
const char* WIFI_PASS = "";

const char* APP_KEY = "";
const char* APP_SECRET = "";
const char* GARAGEDOOR_ID_1 = "";
const char* GARAGEDOOR_ID_2 = "";

const int doorPin1 = 22; 
const int doorPin2 = 23;

unsigned long timerDoor1 = 0;
unsigned long timerDoor2 = 0;

const int DELAY_TIME = 2000;

void startDoorTimer(unsigned long& timerReference) {
    timerReference = millis();
}

void triggerDoor(const int doorPin) {
    digitalWrite(doorPin, LOW);
    delay(500);
    digitalWrite(doorPin, HIGH);
}

bool onDoorState(const String& deviceId, bool& state) {
    if (state) {
        if (deviceId == GARAGEDOOR_ID_1) {
            triggerDoor(doorPin1);
            startDoorTimer(timerDoor1);
        }

        if (deviceId == GARAGEDOOR_ID_2) {
            triggerDoor(doorPin2);
            startDoorTimer(timerDoor2);
        }
    }

    return true;
}

void handleDoorTimer(unsigned long& timer, const String& deviceId) {
    if (timer && millis() - timer >= DELAY_TIME) {
        SinricProGarageDoor& myGarageDoor = SinricPro[deviceId];
        myGarageDoor.sendDoorStateEvent(false);
        timer = 0;
    }
}

void handleDoorTimers() {
    handleDoorTimer(timerDoor1, GARAGEDOOR_ID_1);
    handleDoorTimer(timerDoor2, GARAGEDOOR_ID_2);
}

void setupGarageDoors() {
    pinMode(doorPin1, OUTPUT);
    digitalWrite(doorPin1, HIGH);

    pinMode(doorPin2, OUTPUT);
    digitalWrite(doorPin2, HIGH);
}

void setupWiFi() {
    Serial.println("Connecting WiFi");
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    while (WiFi.status() != WL_CONNECTED) {
        delay(100);
        Serial.print(".");
    }
    Serial.println("Connected");
}

void setupSinricPro() {
    SinricProGarageDoor& myGarageDoor1 = SinricPro[GARAGEDOOR_ID_1];
    SinricProGarageDoor& myGarageDoor2 = SinricPro[GARAGEDOOR_ID_2];
    myGarageDoor1.onDoorState(onDoorState);
    myGarageDoor2.onDoorState(onDoorState);
    SinricPro.onConnected([](){ Serial.println("SinricPro connected");});
    SinricPro.onDisconnected([](){ Serial.println("SinricPro disconnected");});
    Serial.println("Connecting SinricPro...");
    SinricPro.begin(APP_KEY, APP_SECRET);
}

void setup() {
    Serial.begin(115200);
    setupGarageDoors();
    setupWiFi();
    setupSinricPro();
}

void loop() {
    SinricPro.handle();
    handleDoorTimers();
}
Neu59 commented 1 month ago
#include <Arduino.h>
#include <WiFiManager.h>
#include <SinricPro.h>
#include <SinricProGarageDoor.h>

const char* APP_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxx";
const char* APP_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdf34825a937";
const char* GARAGEDOOR_ID_1 = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* GARAGEDOOR_ID_2 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

const int doorPin1 = 22;
const int doorPin2 = 23;

unsigned long timerDoor1 = 0;
unsigned long timerDoor2 = 0;
const int RESET = 21;

const int DELAY_TIME = 2000;

WiFiManager wifiManager;

void startDoorTimer(unsigned long& timerReference) {
    timerReference = millis();
}

void triggerDoor(const int doorPin) {
    digitalWrite(doorPin, LOW);
    delay(500);
    digitalWrite(doorPin, HIGH);
}

bool onDoorState(const String& deviceId, bool& state) {
    if (state) {
        if (deviceId == GARAGEDOOR_ID_1) {
            triggerDoor(doorPin1);
            startDoorTimer(timerDoor1);
        }

        if (deviceId == GARAGEDOOR_ID_2) {
            triggerDoor(doorPin2);
            startDoorTimer(timerDoor2);
        }
    }

    return true;
}

void handleDoorTimer(unsigned long& timer, const String& deviceId) {
    if (timer && millis() - timer >= DELAY_TIME) {
        SinricProGarageDoor& myGarageDoor = SinricPro[deviceId];
        myGarageDoor.sendDoorStateEvent(false);
        timer = 0;
    }
}

void handleDoorTimers() {
    handleDoorTimer(timerDoor1, GARAGEDOOR_ID_1);
    handleDoorTimer(timerDoor2, GARAGEDOOR_ID_2);
}

void setupGarageDoors() {
    pinMode(doorPin1, OUTPUT);
    digitalWrite(doorPin1, HIGH);

    pinMode(doorPin2, OUTPUT);
    digitalWrite(doorPin2, HIGH);
}

void setupWiFi() {
    wifiManager.autoConnect("GarageDoorAP");
}

void setupSinricPro() {
    SinricProGarageDoor& myGarageDoor1 = SinricPro[GARAGEDOOR_ID_1];
    SinricProGarageDoor& myGarageDoor2 = SinricPro[GARAGEDOOR_ID_2];
    myGarageDoor1.onDoorState(onDoorState);
    myGarageDoor2.onDoorState(onDoorState);
    SinricPro.onConnected([](){ Serial.println("SinricPro connected");});
    SinricPro.onDisconnected([](){ Serial.println("SinricPro disconnected");});
    Serial.println("Connecting SinricPro...");
    SinricPro.begin(APP_KEY, APP_SECRET);
    //  WiFiManager wifiManager;        // Create an instance of WiFiManager
    wifiManager.autoConnect("GarageDoorAP"); // Start WiFiManager

}

void setup() {
    Serial.begin(115200);
    pinMode(RESET, INPUT_PULLUP);   

    setupGarageDoors();
    setupWiFi();
    setupSinricPro();
}

void loop() {
    SinricPro.handle();
    handleDoorTimers();
     if(digitalRead(RESET) == LOW) 
  {
    wifiManager.resetSettings();
  }
}

https://github.com/sinricpro/esp8266-esp32-sdk/assets/61716192/5e6dea92-f296-48c4-bb35-8d67ecfe6f41

Thank you teacher for the code with much more class than mine. I have added Wifi Manager. The problem is that the relay only works when I press close, when I press open the relay does not work.

sivar2311 commented 1 month ago

Sorry, misunderstanding on my side. I'm not on my PC right now. I'll send you the code later.

Neu59 commented 1 month ago

Perfect, no problem, thank you very much Remember that after pressing open after 20 seconds I need the app to be closed again without activating the relay.

sivar2311 commented 1 month ago

You need to change

const int DELAY_TIME = 20000;

and

bool onDoorState(const String& deviceId, bool& state) {
    if (deviceId == GARAGEDOOR_ID_1) {
        triggerDoor(doorPin1);
        if (state) startDoorTimer(timerDoor1);
    }

    if (deviceId == GARAGEDOOR_ID_2) {
        triggerDoor(doorPin2);
        if (state) startDoorTimer(timerDoor2);
    }

    return true;
}
Neu59 commented 1 month ago

Friend, your help is a great honor for me, the code is solved now the relays work well. The problem is the text Closed and open in the app. The first time after a restart you press open and after 20 seconds the text in the app says closed, perfect, but if you press open again the relay works ok but the text in the app After 20 seconds it does not change to closed.

For it to change from open to closed you must first press close and then open, so if it works after 20 seconds it indicates closed.

In summary you have to cycle press open close and open the text changes to closed ok, but if you only press open several times only the first one works well. I hope you understood me.

sivar2311 commented 1 month ago

Can you please explain your system. Why should the status be automatically closed again after 20 seconds? Does your door close again automatically after 20 seconds? I don't quite understand that. Perhaps you can show a video of the system that clearly shows how it works.

Neu59 commented 1 month ago

Friend Sivar. My system closes the door after 20 seconds, so I don't need to press close, just open. With your code after a reset of the esp32 it works fine by pressing open 20 seconds later the app changes its text from open to close, but if I press open again after 20 seconds it does not change the text from open to close.

Well, your code is great, very professional. I will use mine if it works the way I want it to, thank you

https://github.com/sinricpro/esp8266-esp32-sdk/assets/61716192/5670d07b-606b-430d-8c05-65d775997899

Neu59 commented 1 month ago

As you can see in the video, the first time it works well, it changes the state, but the second time it does not change the state. The only option for it to change state is by clicking close and then open.

sivar2311 commented 1 month ago

Hi @Neu59 Sorry, I confused myself with the status (open / closed).

I have completely revised the sketch

The code consist of 2 files now:

You can rename the "main.cpp" to anything you want, like "GarageDoor.ino" - whatever you prefer

Neu59 commented 1 month ago

Master, thank you for generating the code for this situation.

In the main code I can't find the lines

const char GARAGEDOOR_ID_1 = " "; const char GARAGEDOOR_ID_2 = " "

sivar2311 commented 1 month ago

See lines 14-15

sivar2311 commented 1 month ago

If you want to add another garagedoor, simply expand the array.

Neu59 commented 1 month ago

Garage Door.h: No such file or directory

Friend, do I need a library?

sivar2311 commented 1 month ago

You need to create a file called "GarageDoor.h" Copy the content from the gist file into your local "GarageDoor.h" file.

sivar2311 commented 1 month ago

If this is too complicated change the main.cpp like so:

#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProGarageDoor.h>

const char* WIFI_SSID = "WIFI_SSID_HERE";
const char* WIFI_PASS = "WIFI_PASS_HERE";

const char* APP_KEY    = "APP_KEY_HERE";
const char* APP_SECRET = "APP_SECRET_HERE";

struct GarageDoor {
    GarageDoor(const String& name, const String& deviceId, const int pin, const int delayTime) : name(name), deviceId(deviceId), pin(pin), delayTime(delayTime), timer(0) {}
    const String        name;
    const String        deviceId;
    const int           pin;
    const unsigned long delayTime;
    unsigned long       timer;
};

GarageDoor garageDoors[]{
//   name           deviceId               pin  delay time (ms)
    {"GarageDoor1", "DEVICE_ID_DOOR1_HERE", 22, 20000},
    {"GarageDoor2", "DEVICE_ID_DOOR2_HERE", 23, 20000}
};

// rest of the code ....

See Multiple Garagedoors (single file version)

Neu59 commented 1 month ago

Master, now it works perfectly, thank you very much.

sivar2311 commented 1 month ago

You're welcome.

I highly recommend to use a (hardware) switch to detect the close state.

The current implementation for the closed state is only based on the timers and does not reflect the real door state.

Neu59 commented 1 month ago

I have added wifimanager and in the gpio21 I press LOW and reset the wifi configuration

#include <Arduino.h>
#include <WiFiManager.h> // Incluimos la biblioteca WiFiManager
#include "GarageDoor.h"
#include <SinricPro.h>
#include <SinricProGarageDoor.h>

const char* APP_KEY    = "d0465c32-b7e1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* APP_SECRET = "c9bb2cab-5213-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const int RESET = 21;
WiFiManager wifiManager;

GarageDoor garageDoors[]{
    //   name           deviceId               pin  delay time (ms)
    {"GarageDoor1", "66354xxxxxxxxxxx", 22, 20000},
    {"GarageDoor2", "66354xxxxxxxxxxx", 23, 20000}
};

void triggerGarageDoor(GarageDoor& garageDoor) {
    digitalWrite(garageDoor.pin, LOW);
    delay(500);
    digitalWrite(garageDoor.pin, HIGH);
    Serial.printf("[%s]: triggered\r\n", garageDoor.name.c_str());
}

void startGarageDoorTimer(GarageDoor& garageDoor) {
    Serial.printf("[%s]: timer started\r\n", garageDoor.name.c_str());
    garageDoor.timer = millis();
}

void stopGarageDoorTimer(GarageDoor& garageDoor) {
    Serial.printf("[%s]: timer stopped\r\n", garageDoor.name.c_str());
    garageDoor.timer = 0;
}

void openGarageDoor(GarageDoor& garageDoor) {
    Serial.printf("[%s]: opening door\r\n", garageDoor.name.c_str());
    triggerGarageDoor(garageDoor);
    startGarageDoorTimer(garageDoor);
}

void closeGarageDoor(GarageDoor& garageDoor) {
    Serial.printf("[%s]: closing door\r\n", garageDoor.name.c_str());
    triggerGarageDoor(garageDoor);
    stopGarageDoorTimer(garageDoor);
}

bool garageDoorTimerIsExpired(GarageDoor& garageDoor) {
    if (garageDoor.timer && millis() - garageDoor.timer >= garageDoor.delayTime) {
        Serial.printf("[%s]: timer expired\r\n", garageDoor.name.c_str());
        return true;
    }
    return false;
}

void sendGarageDoorCloseEvent(GarageDoor& garageDoor) {
    SinricProGarageDoor& myGarageDoor = SinricPro[garageDoor.deviceId];
    myGarageDoor.sendDoorStateEvent(true);
    Serial.printf("[%s]: close event sent\r\n", garageDoor.name.c_str());
}

void handleGarageDoorTimer(GarageDoor& garageDoor) {
    if (garageDoorTimerIsExpired(garageDoor)) {
        stopGarageDoorTimer(garageDoor);
        sendGarageDoorCloseEvent(garageDoor);
    }
}

void handleGarageDoorTimers() {
    for (auto& garageDoor : garageDoors) handleGarageDoorTimer(garageDoor);
}

bool onDoorState(const String& deviceId, bool& state) {
    for (auto& garageDoor : garageDoors) {
        if (deviceId == garageDoor.deviceId) {
            if (state) {
                closeGarageDoor(garageDoor);
            } else {
                openGarageDoor(garageDoor);
            }
            return true;
        }
    }

    return false;
}

void setupGarageDoor(GarageDoor& garageDoor) {
    int pin = garageDoor.pin;
    pinMode(pin, OUTPUT);
    digitalWrite(pin, HIGH);

    SinricProGarageDoor& door = SinricPro[garageDoor.deviceId];
    door.onDoorState(onDoorState);
}

void setupGarageDoors() {
    for (auto& garageDoor : garageDoors) setupGarageDoor(garageDoor);
}

void setupWiFi() {
    // Creamos un objeto WiFiManager
//    WiFiManager wifiManager;

    // Conectamos al WiFi, si no se puede conectar, se abrirá un portal de configuración de WiFi
    if (!wifiManager.autoConnect("GarageDoorAP")) {
        Serial.println("failed to connect and hit timeout");
        // Reiniciar y volver a intentar la conexión si no se puede conectar
        ESP.restart();
        delay(1000);
    }
    Serial.println("connected... :)");
}

void setupSinricPro() {
    SinricPro.onConnected([]() { Serial.println("[SinricPro]: connected"); });
    SinricPro.onDisconnected([]() { Serial.println("[SinricPro]: disconnected"); });
    Serial.println("[SinricPro]: connecting...");
    SinricPro.begin(APP_KEY, APP_SECRET);
}

void setup() {
    Serial.begin(115200);
    setupGarageDoors();
    setupWiFi();
    setupSinricPro();
    pinMode(RESET, INPUT_PULLUP);

}

void loop() {
    SinricPro.handle();
    handleGarageDoorTimers();
     if(digitalRead(RESET) == LOW) 
  {
    wifiManager.resetSettings();
  }
}

Garage Door

Neu59 commented 1 month ago

Are you referring to a magnetic sensor to know when it opens and closes?

sivar2311 commented 1 month ago

setupWiFi looks okay!

Suggestion: put the code for the reset button into a separate function "handleResetButton" and call this from the loop.

Neu59 commented 1 month ago

ok I will

In my case it is a community of neighbors, the door has a photo cell to stop due to obstruction and automatic closing.

reset_wifi

Is that okay, Master?

sivar2311 commented 1 month ago

Better... I'll correct your code later today to make it perfect. I have to leave now.

Neu59 commented 1 month ago

Friend, is it possible to share the app with the family?

sivar2311 commented 1 month ago

cc @kakopappa :

Friend, is it possible to share the app with the family?

kakopappa commented 1 month ago

You can share the same email/password.

On Sun, 5 May 2024 at 4:16 PM sivar2311 @.***> wrote:

cc @kakopappa https://github.com/kakopappa :

Friend, is it possible to share the app with the family?

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/376#issuecomment-2094700794, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZSRNJPGDTIQKW2MG73ZAX2HLAVCNFSM6AAAAABHGFRDC6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJUG4YDANZZGQ . You are receiving this because you were mentioned.Message ID: @.***>

Neu59 commented 1 month ago

Sinric pro is great, thank you very much

sivar2311 commented 1 month ago

@Neu59

Here comes the final sketch Multiple garage doors (single file version) with WiFiManager and reset button

Neu59 commented 1 month ago

thanks friend now I have a professional code