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
Other
231 stars 124 forks source link

sinric pro momentary relay switch #152

Closed Dom2955774 closed 3 years ago

Dom2955774 commented 3 years ago

hi, i am new to coding and i am using arduino IDE, i am trying to get a example sketch which switches a relay on and then off after a specified time just like a momentary push button would do. i am trying to do this with sinric pro to open a door, can anyone point me in the right direction please

sivar2311 commented 3 years ago

SinricPro is only about communication (recive commands, send state) - not about controlling the hardware. For communication, functions like onPowerState or sendPowerStateEvent are used.

For your project you should be familiar with Arduino programming and be able to realize your project without SinricPro - first-

Do you have a working sketch for your project (without SinricPro) ?

Dom2955774 commented 3 years ago

ok i see, this is the sketch i have so far without sinric pro int relay = D7;

void setup() { pinMode(relay, OUTPUT); }

void loop() { digitalWrite(relay, HIGH); delay(3000); digitalWrite(relay, LOW); }

now i want to convert it so sinric pro runs it when i say unlock

kakopappa commented 3 years ago

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

sivar2311 commented 3 years ago

Use the lock example. Change https://github.com/sinricpro/esp8266-esp32-sdk/blob/b5b9ea490352517eaea36325fd29222b32ceef38/examples/Lock/Lock/Lock.ino#L42-L45

To something like this:

bool onLockState(String deviceId, bool &lockState) {
  if (lockState == false) {
   digitalWrite(relay, HIGH);
   delay(3000);
   digitalWrite(relay, LOW);
  }
  Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked");
  return true;
}

Additionally check the full documentation

Dom2955774 commented 3 years ago

iv tried altering this code this code https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/Relay/Relay/Relay.ino to this #ifdef ENABLE_DEBUG

define DEBUG_ESP_PORT Serial

   #define NODEBUG_WEBSOCKETS
   #define NDEBUG

endif

include

ifdef ESP8266

   #include <ESP8266WiFi.h>

endif

ifdef ESP32

   #include <WiFi.h>

endif

include "SinricPro.h"

include "SinricProLock.h"

define WIFI_SSID ""

define WIFI_PASS ""

define APP_KEY ""

define APP_SECRET ""

define SWITCH_ID ""

define BAUD_RATE 115200

define RELAY_PIN D7

bool onLockState(String deviceId, bool &lockState) { digitalWrite(RELAY_PIN, state);
return true;
}

void setup() { pinMode(RELAY_PIN, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASS);

SinricProLock &myLock = SinricPro[LOCK_ID]; myLock.onLockState(onLockState);
SinricPro.begin(APP_KEY, APP_SECRET);
}

void loop() { SinricPro.handle();
} and it gives a error "state was not declared in the scope" i also tried the lock sketch like Sivar2311 suggested and it gives the same error

kakopappa commented 3 years ago

@Dom2955774

bool onLockState(String deviceId, bool &lockState) { // Dont know your pin settings, set to HIGH or LOW if (lockState== false) { digitalWrite(RELAY_PIN, HIGH); } else { digitalWrite(RELAY_PIN, LOW); } Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked"); return true; }

sivar2311 commented 3 years ago

Yeah, sorry parameter is called lockState not state I fixed the example code in my previous comment

sivar2311 commented 3 years ago

@kakopappa the given original code

  ...
   digitalWrite(relay, HIGH);
   delay(3000);
   digitalWrite(relay, LOW);
...

looked to me if the device should unlock, pin must set HIGH for 3 seconds, then LOW

Dom2955774 commented 3 years ago

ok cool thanks, iv now got this sketch which compiled fine, it shows online, switches on and off in sinric pro but the relay doesnt respond at all

// 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

ifdef ESP8266

   #include <ESP8266WiFi.h>

endif

ifdef ESP32

   #include <WiFi.h>

endif

include "SinricPro.h"

include "SinricProLock.h"

define WIFI_SSID ""

define WIFI_PASS ""

define APP_KEY ""

define APP_SECRET ""

define LOCK_ID ""

define BAUD_RATE 115200

define RELAY_PIN D7

bool onLockState(String deviceId, bool &lockState) { if (lockState == false) { digitalWrite(RELAY_PIN,HIGH); delay(3000); digitalWrite(RELAY_PIN,LOW); } Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked"); return true; }

void setupWiFi() { Serial.printf("\r\n[Wifi]: Connecting"); WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } IPAddress localIP = WiFi.localIP(); Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); }

void setupSinricPro() { SinricProLock &myLock = SinricPro[LOCK_ID]; myLock.onLockState(onLockState);

// 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 setup() { Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); setupWiFi(); setupSinricPro(); }

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

sivar2311 commented 3 years ago

You did not what you did in your first sketch:

void setup() {
  pinMode(relay, OUTPUT); // <-- is missing ! 
}

Please use image for posting code.

Dom2955774 commented 3 years ago

its working now!! thanks a lot guys for all the help and patience