Change the value of a running exit

Hello, I am a beginner in arduino, I am at a crossroads, the situation is as follows, I am automating the access to a door with the Sinric.pro library and the esp8266 board, I am modifying the example code that is in the library, the case is that this function must send a true for 5 seconds without stopping the program and then send a false, I don't know much about the arduino syntax, thank you in advance.

bool onLockState(String deviceId, bool &lockState) {
  Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked");
  return true;
}

the complete code in case you need it.

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

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

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

#define WIFI_SSID         "----"    
#define WIFI_PASS        "----"
#define APP_KEY           "-----"                                        // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "-----"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define LOCK_ID           "------"                                                    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         9600                                                                          // Change baudrate to your need

#define RELAY_PIN         5

bool onLockState(String deviceId, bool &lockState) {
  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(); 
}

I'm sorry. By 'send a true' did you mean something other than the "return true;" that is already in the function?

hello, sorry for my bad explanation, I can't modify that true since it goes with the basic flow of the program, the main idea is to change the state, when sending the unlock command the state takes unlocked it returns a true for 5 seconds later returns to locked, for this I am trying to capture the commands that I send from the Sinric page but it is getting quite complicated and the code does not help me much.

:smiling_face_with_horns: Muahaha!

Are you modifying lockState in function? if not then there is no reason to pass bool by reference

Can the state be changed there? If so, how?

It says so in documentation


/**
 * @brief Callback definition for onLockState function
 * 
 * Gets called when device receive a `setLockState` request \n
 * @param[in]   deviceId    String which contains the ID of device
 * @param[in]   state       `true` = device is requested to lock \n `false` = device is requested to unlock
 * @param[out]  state       `true` = device has been locked \n `false` = device has been unlocked
 * @return      the success of the request
 * @retval      true        request handled properly
 * @retval      false       request was not handled properly because of some error
 * 
 * @section LockStateCallback Example-Code
 * @code
 * bool onLockState(const String &deviceId, bool &lockState) {
 *   Serial.printf("Device is %s\r\n", lockState?"locked":"unlocked");
 *   return true;
 * }
 * @endcode
 **/

after all lockState must be modified by function and passed back as result via reference. the other bool is just to inform caller on success of the operation

I'm trying to modify, I'm sorry I don't understand how, if I don't pass the lockedState how do I capture it?

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

I’m sorry I don’t have hardware so my help is very limited. You can try examples, there are plenty that come with the library and see if you can find something that almost does what you want

'lockState' is a boolean, not a String.

If the goal is to automatically lock 5 seconds after an unlock, I think you want something like:

bool onLockState(String deviceId, bool &lockState) 
{
  Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked");

  if (!lockState)
  {
    // Do the requested unlocking for 5 seconds
    Serial.println("Unlocking");
    digitalWrite(RELAY_PIN, LOW);
    delay(5000);
    Serial.println("Relocking");
    digitalWrite(RELAY_PIN, HIGH); // Relock
    lockState = true; // Lock is now locked
  }

  return true; // Event handled successfully
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.