Giter VIP home page Giter VIP logo

Comments (14)

sivar2311 avatar sivar2311 commented on June 9, 2024

What exact is the issue?
I don't see any servo related code.

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

I tried adding servo related code but it didn't work.
I am new to using Arduino so can you please help me to add the servo code to it

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on June 9, 2024

I suggest first to finish your servo code without SinricPro.
If you got it running, implement SinricPro.

from esp8266-esp32-sdk.

khorner116 avatar khorner116 commented on June 9, 2024

That is very good advice. Implement using a button or even just an on/off loop using standard servo library code. When you figure out how to do it, then copy the servo code into the onLockState function in the code you supplied. When you get an "open", rotate your servo in one direction. When you get a "close" rotate it in the other direction. But I think you're apt to have a problem, as at power off, the servo could return to 0 deg. That could open your door lock. But I have no idea how you've set up your hardware.

from esp8266-esp32-sdk.

kakopappa avatar kakopappa commented on June 9, 2024

I think you going to need a reed switch to check the status of the door
https://www.sparkfun.com/products/13247

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

i am aware of the reed switch,thanks @kakopappa .
Also thanks for your advice @khorner116 , will try to implement and will share the update of it.

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

Hello, @khorner116 , with your advice I have try to update the code made by @kakopappa, but I am having some issues with it, after Unlocking the door, it cannot be locked again by pressing the Lock button and also just after pressing the unlock button, it get disconnects from Sinric server after some amount of time, can you help me to overcome this issue.

''
#define ENABLE_DEBUG

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

#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProLock.h"

#define WIFI_SSID "Mitali"
#define WIFI_PASS "mitalibakliwal"
#define APP_KEY "0xxxxxc-xxxx-4500-xxxx-954xxxxxx40a"
#define APP_SECRET "6exxxx5f-xxxx-48f9-86aa-4f73bff23861-xxxxxxxx-2738-xxxx-9940-3e47xxxxxxf0"
#define LOCK_ID "5e6e0exxxxxxx22d498dd56b76"
#define BAUD_RATE 9600
#include <Servo.h>
#define LOCK_PIN D4// PIN where the lock is connected to: HIGH = locked, LOW = unlocked
#define LOCK_STATE_PIN D2 // PIN where the lock feedback is connected to (HIGH:locked, LOW:unlocked)
Servo myservo;
bool lastLockState;

bool onLockState(String deviceId, bool lockState) {
int pos;
for (pos = 0; pos <= 180;) // goes from 0 degrees to 180 degrees
{
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(0); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0;) // goes from 180 degrees to 0 degrees
{
myservo.write(90); // tell servo to go to position in variable 'pos'
delay(0); // waits 15ms for the servo to reach the position
}

Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked");
digitalWrite(LOCK_PIN, lockState);
return true;

}

void checkLockState() {
bool currentLockState = digitalRead(LOCK_STATE_PIN); // get current lock state
if (currentLockState == lastLockState) return; // do nothing if state didn't changed
Serial.printf("Lock has been %s manually\r\n", currentLockState?"locked":"unlocked"); // print current lock state to serial
lastLockState = currentLockState; // update last known lock state
SinricProLock &myLock = SinricPro[LOCK_ID]; // get the LockDevice
myLock.sendLockStateEvent(currentLockState); // update LockState on Server
}

void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
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());
}

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");

pinMode(LOCK_PIN, OUTPUT);
pinMode(LOCK_STATE_PIN, INPUT);

setupWiFi();
setupSinricPro();
myservo.attach(D4);
}

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

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on June 9, 2024

Remove the for loops... They do nothing except blocking the complete code. Thats why after unlocking nothing is working anymore. Only the delay keeps the ESP from crashing.

Just do a simple myServo.write depending to the lockState.

I cant deliver complete code right now because i am on my Mobile Phone right now.

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on June 9, 2024
#define POSITION_LOCK     0       // define servo lock position
#define POSITION_UNLOCK   90      // define servo unlock position

bool onLockState(const String &deviceId, bool &lockState){
  myServo.write(lockState?POSITION_LOCK:POSITION_UNLOCK);
  Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState ? "locked" : "unlocked");
  lastLockState = lockState;
  return true;
}
  1. deviceId is const String& !

  2. lockState is bool& !

  3. myServo.write(lockState?POSITION_LOCK:POSITION_UNLOCK);
    is the same as:

if (lockState == true) {
  myServo.write(POSITION_LOCK);
} else {
  myServo.write(POSITION_UNLOCK);
}

See c++ conditional operator

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

Thanks @sivar2311, your code helped me to overcome several issues.
Now the Lock gets Locked and Unlocked withot crashing, but I am facing a issue in the Sinric Pro app, when I press Unlock the device gets unlocked but the system shows that its still locked. Can you help me that what I am missing out?

''
#define ENABLE_DEBUG

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

#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProLock.h"
#include <Servo.h>
#define WIFI_SSID "Mitali"
#define WIFI_PASS "mitalibakliwal"
#define APP_KEY "01b92a6c-ead4-4500-bf5d-9543867f840a"
#define APP_SECRET "6e892e5f-df0a-48f9-86aa-4f73bff23861-52ce696e-2738-4251-9940-3e47bca923f0"
#define LOCK_ID "5e6e0e2586622d498dd56b76"
#define BAUD_RATE 9600
#define LOCK_PIN D4// PIN where the lock is connected to: HIGH = locked, LOW = unlocked
#define LOCK_STATE_PIN D2 // PIN where the lock feedback is connected to (HIGH:locked, LOW:unlocked)
#define POSITION_LOCK 90 // define servo lock position
#define POSITION_UNLOCK 0 // define servo unlock position
Servo myservo;
bool lastLockState;

bool onLockState(const String &deviceId, bool &LockState){
if (LockState == true) {
myservo.write(POSITION_LOCK);
} else {
myservo.write(POSITION_UNLOCK);
}
Serial.printf("Lock has been %s manually\r\n", LockState?"locked":"unlocked");
lastLockState = LockState;
return true;
}

void checkLockState() {
bool currentLockState = digitalRead(LOCK_STATE_PIN); // get current lock state
if (currentLockState == lastLockState) return; // do nothing if state didn't changed
Serial.printf("Lock has been %s manually\r\n", currentLockState?"locked":"unlocked"); // print current lock state to serial
lastLockState = currentLockState; // update last known lock state
SinricProLock &myLock = SinricPro[LOCK_ID]; // get the LockDevice
myLock.sendLockStateEvent(currentLockState); // update LockState on Server
}

void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
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());
}

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");
pinMode(LOCK_PIN, OUTPUT);
pinMode(LOCK_STATE_PIN, INPUT);

setupWiFi();
setupSinricPro();
myservo.attach(D4);
}

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

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on June 9, 2024

Please check if this issue happens to Dashboard and AlexaApp too. Or is it just SinricPro-App?

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

It happens with the Dashboard too.
I haven't checked it with the Alexa yet but will let you know.

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on June 9, 2024

Your code makes use of checkLockState() which reads the current lockstate back from pin D2.
Is there anything connected to D2 which returns the current lock state?
Otherwise you have to remove this code.

from esp8266-esp32-sdk.

bakliwalp avatar bakliwalp commented on June 9, 2024

Yes, you were right. Thanks for your help.
Also thanks to all of you.

from esp8266-esp32-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.