Closed Dom2955774 closed 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) ?
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
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
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 NODEBUG_WEBSOCKETS
#define NDEBUG
#include <ESP8266WiFi.h>
#include <WiFi.h>
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
@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; }
Yeah, sorry parameter is called lockState
not state
I fixed the example code in my previous comment
@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
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
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#include <ESP8266WiFi.h>
#include <WiFi.h>
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(); }
You did not what you did in your first sketch:
void setup() {
pinMode(relay, OUTPUT); // <-- is missing !
}
Please use for posting code.
its working now!! thanks a lot guys for all the help and patience
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