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

Pin (Gpio) #342

Closed Neu59 closed 7 months ago

Neu59 commented 8 months ago

sorry forgive my ignorance.

How do I tell the garage code which gpio is going to use?

kakopappa commented 8 months ago

You can use any GPIO except 0.

How do you control the garage door without sinric pro?

On Sat, 14 Oct 2023 at 10:36 PM Neu59 @.***> wrote:

sorry forgive my ignorance.

How do I tell the garage code which gpio is going to use?

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/342, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZQV3BGWQ5RQWUQ342DX7KWPNAVCNFSM6AAAAAA6AKSB3GVHI2DSMVQWIX3LMV43ASLTON2WKOZRHE2DGMZUGI4TKMA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Neu59 commented 8 months ago

It is controlled with a remote control and a physical button on the wall.

Neu59 commented 8 months ago

Thanks for answering but in your sketch there is nothing written for the gpio.

I have tried to set it up by copying a switch, Alexa controls it but it does not activate the gpio

kakopappa commented 8 months ago

https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/Relay/Relay/Relay.ino

On Sat, 14 Oct 2023 at 11:32 PM Neu59 @.***> wrote:

Thanks for answering but in your sketch there is nothing written for the gpio.

I have tried to set it up by copying a switch, Alexa controls it but it does not activate the gpio

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

Neu59 commented 8 months ago

Thank you very much friend, I'm going to study to see if I understand it. This drives me crazy all afternoon.

Neu59 commented 8 months ago

The code is fine, I understand it, but what if I have to control 2 relays for the garage door and the light? On the Sinric pro page I have 2 IDs

define WIFI_SSID "sidd"

define WIFI_PASS "pass"

define APP_KEY "xxxxxxxxxxxxxxxxxxxxxee"

define APP_SECRET "17db51bd-52xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

define SWITCH_ID_1 "652aa1xxxxxxxxxxxxxxxx"

define RELAYPIN_1 13

define SWITCH_ID_2 "652xxxxxxxxxxxxxxxxxxxxxxxx"

define RELAYPIN_2 4

kakopappa commented 8 months ago

Then, you just have to do the same again for the other relay

#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 "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID         "YOUR-WIFI-SSID"    
#define WIFI_PASS         "YOUR-WIFI-PASSWORD"
#define APP_KEY           "YOUR-APP-KEY"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "YOUR-APP-SECRET"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID_1       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

#define RELAYPIN_1 13
#define RELAYPIN_2 4

#define BAUD_RATE         115200                // Change baudrate to your need

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.printf("Device 1 turned %s\r\n", state?"on":"off");
  digitalWrite(RELAYPIN_2, state);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.printf("Device 2 turned %s\r\n", state?"on":"off");
   digitalWrite(RELAY_PIN, state);
  return true; // request handled properly
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

  #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP); 
    WiFi.setAutoReconnect(true);
  #elif defined(ESP32)
    WiFi.setSleep(false); 
    WiFi.setAutoReconnect(true);
  #endif

  WiFi.begin(WIFI_SSID, WIFI_PASS); 

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }

  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  // add devices and callbacks to SinricPro
  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
  mySwitch1.onPowerState(onPowerState1);

  SinricProSwitch& mySwitch2 = SinricPro[SWITCH_ID_2];
  mySwitch2.onPowerState(onPowerState2); 

  // 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);
}

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

  pinMode(RELAYPIN_1, OUTPUT);                 // set relay-pin to output mode
  pinMode(RELAYPIN_2, OUTPUT);                 // set relay-pin to output mode

  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}
Neu59 commented 8 months ago

friend, thank you for your help but when you configure it as a garage in Siric Pro, apparently it does not send the opening data. I have tried it and the light turns on and off ok, the garage Alexa responds ok but the relay does not work

Neu59 commented 8 months ago

In the Sinric PRo App there is an open button, a close button and a third button like power on, well if I press that button it works. On the power off and on button it does nothing.

App Sinsric Garage

kakopappa commented 8 months ago

You can’t use Switch sketch with Garage .

For garage use this

https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/GarageDoor/GarageDoor.ino

Turn on/off the relay in onDoorState

On Sun, 15 Oct 2023 at 12:48 AM Neu59 @.***> wrote:

[image: App Sinsric Garage] https://user-images.githubusercontent.com/61716192/275270108-50eaed7f-f5ae-4ce7-8094-ae7fd66a348b.jpg

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

Neu59 commented 8 months ago

Correct, that is the one I want to use but I don't know how to configure the gpio

bool onDoorState(const String& deviceId, bool &doorState) { Serial.printf("Garagedoor is %s now.\r\n", doorState?"closed":"open"); return true; Here how to configure the esp8266 gpio?

Neu59 commented 8 months ago

ok adding the pin works now I have to figure out how to make it so that the relay only acts for a second and doesn't always remain connected.

bool onDoorState(const String& deviceId, bool &doorState) { Serial.printf("Garagedoor is %s now.\r\n", doorState? "open":"closed"); digitalWrite(RELAYPIN_1, doorState ?LOW:HIGH); if(digitalRead(RELAYPIN_1)==HIGH) { timer.setTimeout(500L, []() { // Run after 0.5 seconds second digitalWrite(RELAYPIN_1, LOW);}); // END Timer Function }

got it thanks for your help ;-)

kakopappa commented 8 months ago

Great.

you can use a delay() as well. Eg

digitalWrite(RELAYPIN_1, doorState ?LOW:HIGH); if(digitalRead(RELAYPIN_1)==HIGH) { delay(500); digitalWrite(RELAYPIN_1, LOW); }

What’s the module /product are you using to control the door? Can you please share more details about the project ? Like to setup. Tutorial around this so it would be helpful for someone else

Neu59 commented 8 months ago

It is an esp8266. With Alexa app Blnyk, Wifi manager. I control the garage door with a 0.5 second pulse both in opening and closing, and I control a light plus a physical button in the garage.

ESP8266 APP BLYNK

Neu59 commented 8 months ago
#define BLYNK_TEMPLATE_ID "XXXXXXXXXXXXXX-R"
#define BLYNK_TEMPLATE_NAME "Porton Finca"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXXXXXXXXXXXXXX"

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#define RELAYPIN_1        4
#define RELAYPIN_2        13
const int btnPin = 12;
bool ledState = 0;
bool btnState = 1;
int Resett = 3;
unsigned long tiempoAnterior;
unsigned long tiempoAnterior2;
#define APP_KEY           "05dXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "17XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define GARAGEDOOR_ID     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define FOCO_ID           "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 

#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp8266.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include "SinricPro.h"
#include "SinricProGarageDoor.h"
#include "SinricProSwitch.h"
#include <SimpleTimer.h>
#include <Arduino.h>
#include <SimpleTimer.h>
#include <Arduino.h>
//BlynkTimer timer;
SimpleTimer timer;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool onDoorState(const String& deviceId, bool &doorState) 
{
  Serial.printf("Garagedoor is %s now.\r\n", doorState? "closed":"open");
  digitalWrite(RELAYPIN_1, doorState ?LOW:LOW);
  Blynk.virtualWrite(V0, doorState);
  if(digitalRead(RELAYPIN_1)==LOW) 
  {       
   timer.setTimeout(1000L, []() {  // Run after 0.5 seconds second
   digitalWrite(RELAYPIN_1, HIGH);  // Set  Pin 4 LOW
   Blynk.virtualWrite(V0, 0);    
   });  // END Timer Function      
  }
  return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool onPowerState(const String& deviceId, bool &focoState) 
{
  Serial.printf("Garagedoor is %s now.\r\n", focoState? "open":"closed");
  digitalWrite(RELAYPIN_2, focoState ?HIGH:LOW); 
  Blynk.virtualWrite(V1, focoState); 
  return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setupSinricPro() 
{

   pinMode(RELAYPIN_1, OUTPUT);
   pinMode(RELAYPIN_2, OUTPUT);
   pinMode(btnPin, INPUT_PULLUP);
   pinMode(Resett, INPUT);
   digitalWrite(RELAYPIN_1, HIGH );

  SinricProGarageDoor &myGarageDoor = SinricPro[GARAGEDOOR_ID];
  myGarageDoor.onDoorState(onDoorState);

  SinricProSwitch &Fococh1 = SinricPro[FOCO_ID];
  Fococh1.onPowerState(onPowerState);

  // 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);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V0)
{   
  int value = param.asInt(); // Get value as integer

  if (value == 1)
  {
     digitalWrite(RELAYPIN_1, LOW ); // Set GPIO 4 HIGH     
     timer.setTimeout(500L, []() {  // Run after 0.5 seconds second
     digitalWrite(RELAYPIN_1, HIGH);  // Set  Pin 4 LOW
     Blynk.virtualWrite(V0, 0);
     });  // END Timer Function
  }
} 
///////////////////////////////////////////////////////////////////////////////////////////////////////

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V1);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);

}

////////////////////////////////////////////////////////////////////////////////////////////////////
// When App button is pushed - switch the state
BLYNK_WRITE(V1) { 
  ledState = param.asInt();
  digitalWrite(RELAYPIN_2, ledState);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void checkPhysicalButton()
{

if (digitalRead(btnPin) == 0) {
    // btnState is used to avoid sequential toggles
    if (btnState != 0) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(RELAYPIN_2, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V1, ledState);
    }
    btnState = 0;
  } else {
    btnState = 1;
  }
 }
//////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
    // WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
    // it is a good practice to make sure your code sets wifi mode how you want it.

    // put your setup code here, to run once:
    Serial.begin(9600);Serial.printf("\r\n\r\n");

    //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wm;
    setupSinricPro();
    // reset settings - wipe stored credentials for testing
    // these are stored by the esp library
    // wm.resetSettings();     
    // Automatically connect using saved credentials,
    // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
    // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
    // then goes into a blocking loop awaiting configuration and will return success result

    bool res;
    // res = wm.autoConnect(); // auto generated AP name from chipid
    // res = wm.autoConnect("AutoConnectAP"); // anonymous ap
    res = wm.autoConnect("AutoConnectAP"); // password protected ap

    if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
    } 
    else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
    }
    delay(5000);
     Blynk.begin(BLYNK_AUTH_TOKEN, WiFi.SSID().c_str(), WiFi.psk().c_str());

     timer.setInterval(100L, checkPhysicalButton);
     timer.setInterval(1000, IMPRIMIR);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() 
{
   SinricPro.handle();
   timer.run();
   Blynk.run();
    // put your main code here, to run repeatedly:  

    if(digitalRead(Resett)==LOW)
    {
      WiFiManager wm;
      wm.resetSettings(); 
    }
}

Edited by sivar2311 (code-blocks inserted for better readability)

Neu59 commented 8 months ago

Friend, to make it perfect, you need to inform Alexa of the status of the light bulb (Spotlight) and the garage door, when I activate it with blink or the physical button.

sivar2311 commented 8 months ago

@Neu59

See sendDoorStateEvent and sendPowerStateEvent

Neu59 commented 8 months ago

for example like this?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// BLYNK_WRITE(V0) {
int value = param.asInt(); // Get value as integer

if (value == 1) { digitalWrite(RELAYPIN_1, LOW ); // Set GPIO 4 HIGH
timer.setTimeout(500L, []() { // Run after 0.5 seconds second digitalWrite(RELAYPIN_1, HIGH); // Set Pin 4 LOW Blynk.virtualWrite(V0, 0); }); // END Timer Function } sendDoorStateEvent (closed); } ///////////////////////////////////////////////////////////////////////////////////////////////////////

kakopappa commented 8 months ago

SinricProGarageDoor &myGarageDoor = SinricPro[GARAGEDOOR_ID]; myGarageDoor.sendDoorStateEvent (closed);

On Mon, 16 Oct 2023 at 9:46 PM Neu59 @.***> wrote:

for example like this?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// BLYNK_WRITE(V0) { int value = param.asInt(); // Get value as integer

if (value == 1) { digitalWrite(RELAYPIN_1, LOW ); // Set GPIO 4 HIGH timer.setTimeout(500L, { // Run after 0.5 seconds second digitalWrite(RELAYPIN_1, HIGH); // Set Pin 4 LOW Blynk.virtualWrite(V0, 0); }); // END Timer Function } sendDoorStateEvent (closed); }

///////////////////////////////////////////////////////////////////////////////////////////////////////

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

Neu59 commented 8 months ago

The series does not compile for me due to the lack of quotes ("closed"), so it does compile

kakopappa commented 8 months ago

Hi,

You have to use the sendDoorStateEvent for SinricProGarageDoor and sendPowerStateEvent for SinricProSwitch to let the server know when you are doing a physical change

I have never used Blynk. so you have to send the event to the server at the right place.

image

SinricProGarageDoor &myGarageDoor = SinricPro[GARAGEDOOR_ID];

myGarageDoor.sendDoorStateEvent(true); // when closed
myGarageDoor.sendDoorStateEvent(false); // when opened
  SinricProSwitch &Fococh1 = SinricPro[FOCO_ID];
  Fococh1.sendPowerStateEvent(true); // device turned on
  Fococh1.sendPowerStateEvent(false);  //  device turned off

do not send the same state in a loop repeatedly.

btw, Why do you need Blynk and Sinric Pro both?

Neu59 commented 8 months ago

Good question because Sinric Pro already has its app. I discovered it later.

Can a physical button be installed with sinric pro?

kakopappa commented 8 months ago

https://help.sinric.pro/pages/tutorials/switch/part-2

On Tue, 17 Oct 2023 at 3:29 AM Neu59 @.***> wrote:

Good question because Sinric Pro already has its app. I discovered it later.

Can a physical button be installed with sinric pro?

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

sivar2311 commented 7 months ago

It's been more than 14 days since anything happened on this issue, so I'm going to close it. If you have any further questions about this issue, please feel free to reopen it.