sinricpro / non-sdk-issues

Report non sdk related issues here (Alexa, Google Home, SmartThings, IFTTT, API)
2 stars 0 forks source link

How to add logs to activity logs. #41

Closed mbsyaswanth closed 2 years ago

mbsyaswanth commented 2 years ago

Hello Sinric pro team and other devs,

So, I made a smart valve. It can be controlled through the app and I also added a physical button.

When I turn on the device using physical button, I am getting a notification from the mobile app but I don't see it in the activity logs.

I am observing that the valve is automatically getting turned on and in order to find out why, I need these logs. Also I don't see the messages that are printed using Serial.sprintf("message here") in the logs. Is there a way to check it without connecting to computer ?

Observed that the auto-off doesn't kick in when the valve is turned on using physical button.

And suppose I have set a 5 min timer to auto off device using sinric pro. So, If I turn it off before 5 mins manually, still the timer is running and it tries to turn it off.

Please help me out.

mbsyaswanth commented 2 years ago

Here is my code for controlling the bi-stable valve :

/*
   Example for how to use SinricPro Switch device:
   - setup a switch device
   - handle request using callback (turn on/off builtin led indicating device power state)
   - send event to sinricPro server (flash button is used to turn on/off device manually)

*/

// 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 <Arduino.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID         "Wifi SSID HERE"
#define WIFI_PASS         "<Password here>"
#define APP_KEY           "<API KEY HERE>"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "<APP Secret Here>"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define SWITCH_ID         "<Switch ID Here>"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         9600                // Change baudrate to your need

#define CONTROL_PIN1 D1          // GPIO for control input 1
#define CONTROL_PIN2 D2          // GPIO for control input 2
#define BUTTON_PIN D8            // GPIO for manual BUTTON control
#define WIFI_PIN   LED_BUILTIN   // GPIO for WIFI LED (inverted)
#define VALVE_STATUS_PIN D7      // Pin that indictes valve status
#define TIMEOUT 30               // valve power timeout
#define BUTTON_TIMEOUT 500      // minimum time the button should wait to change the state back
#define EMERGENCY_TIMEOUT 400 // The valve will automatically be turned off after this timeout (10 mins) - in seconds.

Ticker valvePowerCutOffTicker;
Ticker emergencyOffTicker;

bool isValveOpen = false;     // valve status
unsigned long lastBtnPress = 0;

void cutSupplyToValve() {
  digitalWrite(CONTROL_PIN1, LOW);
  digitalWrite(CONTROL_PIN2, LOW);
  Serial.println("Valve supply is cut off");
  valvePowerCutOffTicker.detach();
}

// Change D1, D2 to 1 0 for 25ms
void openValve() {
  Serial.println("Opening valve");
  digitalWrite(CONTROL_PIN1, HIGH);
  digitalWrite(CONTROL_PIN2, LOW);
  digitalWrite(VALVE_STATUS_PIN, HIGH);
  isValveOpen = true;
  valvePowerCutOffTicker.attach_ms(TIMEOUT, cutSupplyToValve);

  emergencyOffTicker.attach(EMERGENCY_TIMEOUT, emergencyAutoOff);
}

// Chnage D1, D2 to 0 1 for 25ms
void closeValve() {
  Serial.println("Closing valve");
  digitalWrite(CONTROL_PIN1, LOW);
  digitalWrite(CONTROL_PIN2, HIGH);
  digitalWrite(VALVE_STATUS_PIN, LOW);
  isValveOpen = false;
  valvePowerCutOffTicker.attach_ms(TIMEOUT, cutSupplyToValve);

  emergencyOffTicker.detach();
}

/* bool onPowerState(String deviceId, bool &state)

   Callback for setPowerState request
   parameters
    String deviceId (r)
      contains deviceId (useful if this callback used by multiple devices)
    bool &state (r/w)
      contains the requested state (true:on / false:off)
      must return the new state

   return
    true if request should be marked as handled correctly / false if not
*/
bool onPowerState(const String &deviceId, bool &state) {
  //Serial.printf("Power state param %s", state);
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state ? "on" : "off");
  state ? openValve() : closeValve();
  return true; // request handled properly
}

void handleButtonPress() {
  unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
  if (digitalRead(BUTTON_PIN) == HIGH && actualMillis - lastBtnPress > BUTTON_TIMEOUT)  { // is button pressed (inverted logic! button pressed = LOW) and debounced?
    if (isValveOpen) {     // flip myPowerState: if it was true, set it to false, vice versa
      closeValve();
    } else {
      openValve();
    }
    // get Switch device back
    SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
    // send powerstate event
    mySwitch.sendPowerStateEvent(isValveOpen); // send the new powerState to SinricPro server
    Serial.printf("Device %s turned %s (manually via touchbutton)\r\n", mySwitch.getDeviceId().c_str(), isValveOpen ? "on" : "off");

    lastBtnPress = actualMillis;  // update last button press variable
  }
}

// Automatically turns off after EMERGENCY_TIMEOUT
void emergencyAutoOff() {
  if ( isValveOpen ) {
    closeValve();
    // get Switch device back
    SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
    // send powerstate event
    mySwitch.sendPowerStateEvent(isValveOpen); // send the new powerState to SinricPro server
    Serial.printf("Device %s turned %s (automatically via emergency off)\r\n", mySwitch.getDeviceId().c_str(), isValveOpen ? "on" : "off");
  }
}

// setup function for WiFi connection
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());
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];

  // set callback function to device
  mySwitch.onPowerState(onPowerState);

  // setup SinricPro
  SinricPro.onConnected([]() {
    Serial.printf("Connected to SinricPro\r\n");
  });
  SinricPro.onDisconnected([]() {
    Serial.printf("Disconnected from SinricPro\r\n");
  });
  SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server.
  SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function
void setup() {
  pinMode(BUTTON_PIN, INPUT); // GPIO 0 as input, pulled high
  pinMode(WIFI_PIN, OUTPUT); // define LED GPIO as output
  pinMode(CONTROL_PIN1, OUTPUT); // control pin
  pinMode(CONTROL_PIN2, OUTPUT); // control pin
  pinMode(VALVE_STATUS_PIN, OUTPUT); // valve status pin

  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

  setupWiFi();
  setupSinricPro();
}

void loop() {
  handleButtonPress();
  SinricPro.handle();
}
sivar2311 commented 2 years ago

Hello @mbsyaswanth

In the setupSinricPro() function you call the function SinricPro.restoreDeviceStates(true);. This will cause your valve to be automatically switched on at restart if the last known server state was "switched on". To change this, remove the line or set the parameter to false.

The functions Serial.print() and Serial.printf() can only print output on the serial port. The activity log on the dashboard only contains the server side logs. The library is not able to add entries to this log. You can achieve the functionality you want by using an external library such as RemoteDebug.

kakopappa commented 2 years ago

Hello @mbsyaswanth

Thank you for creating this issue.

auto-off doesn't kick in when on/off using the physical button (event) will be fixed in the next production release. It will be tomorrow (Monday 02/May/2022) along with other issue fixes.

kakopappa commented 2 years ago

Hello @mbsyaswanth

I have rolled out the latest version that contains the fix for your issue.. Please check and let me know if this issue persists.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.