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
228 stars 121 forks source link

Basic relay inverted controls #237

Closed taharslan44 closed 2 years ago

taharslan44 commented 2 years ago

Hi all, I am a very new member to this community. Probably my problem is way too easy but i couldn't figured it out. I have an ESP-01 wifi and compatible relay (esp8266 wifi relay) like this. I have successfully established a connection with sinric pro relay example but my controls are inverted. When I press "Turn off" from dashboard it turns on and vice versa. Yes, I know if I rewire the relay from NO to NC it will solve the problem but still I need to know how it is done from the code. Thanks for the help and sorry for poor language.

Code;

 * If you encounter any issues:
 * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
 * - ensure all dependent libraries are installed
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
 * - open serial monitor and check whats happening
 * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
 * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
 */

// 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>
#ifdef ESP8266 
       #include <ESP8266WiFi.h>
#endif 
#ifdef ESP32   
       #include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProSwitch.h"

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

#define RELAY_PIN         0                  // Pin where the relay is connected (D5 = GPIO 14 on ESP8266)

bool onPowerState(const String &deviceId, bool &state) {
  digitalWrite(RELAY_PIN, state);             // set pin state
  return true;                                // request handled properly
}

void setup() {
  pinMode(RELAY_PIN, OUTPUT);                 // set relay-pin to output mode
  WiFi.begin(WIFI_SSID, WIFI_PASS);           // start wifi

  SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];   // create new switch device
  mySwitch.onPowerState(onPowerState);                // apply onPowerState callback
  SinricPro.begin(APP_KEY, APP_SECRET);               // start SinricPro
}

void loop() {
  SinricPro.handle();                         // handle SinricPro commands
}
kakopappa commented 2 years ago

digitalWrite(RELAY_PIN, !state);

Will invert the state

On Sun, 26 Dec 2021 at 9:16 AM taharslan44 @.***> wrote:

Hi all, I am a very new member to this community. Probably my problem is way too easy but i couldn't figured it out. I have an ESP-01 wifi and compatible relay (esp8266 wifi relay) like this http://www.dhgate.com/product/esp8266-esp-01s-5v-wifi-relay-module-things/437897148.html . I have successfully established a connection with sinric pro relay example but my controls are inverted. When I press "Turn off" from dashboard it turns on and vice versa. Yes, I know if I rewire the relay from NO to NC it will solve the problem but still I need to know how it is done from the code. Thanks for the help and sorry for poor language.

Code;

// 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 "SinricProSwitch.h"

define WIFI_SSID "xxx."

define WIFI_PASS "xxx"

define APP_KEY "xxx" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"

define APP_SECRET "xxx" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

define SWITCH_ID "xxx" // Should look like "5dc1564130xxxxxxxxxxxxxx"

define BAUD_RATE 9600 // Change baudrate to your need

define RELAY_PIN 0 // Pin where the relay is connected (D5 = GPIO 14 on ESP8266)

bool onPowerState(const String &deviceId, bool &state) { digitalWrite(RELAY_PIN, state); // set pin state return true; // request handled properly }

void setup() { pinMode(RELAY_PIN, OUTPUT); // set relay-pin to output mode WiFi.begin(WIFI_SSID, WIFI_PASS); // start wifi

SinricProSwitch& mySwitch = SinricPro[SWITCH_ID]; // create new switch device mySwitch.onPowerState(onPowerState); // apply onPowerState callback SinricPro.begin(APP_KEY, APP_SECRET); // start SinricPro }

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

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/237, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZTIAAW2O3OK2A3PMDDUSZ3HXANCNFSM5KYMVEGQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

taharslan44 commented 2 years ago

thank you so much

CrickCrock28 commented 1 year ago

digitalWrite(RELAY_PIN, !state); Will invert the state On Sun, 26 Dec 2021 at 9:16 AM taharslan44 @.> wrote: Hi all, I am a very new member to this community. Probably my problem is way too easy but i couldn't figured it out. I have an ESP-01 wifi and compatible relay (esp8266 wifi relay) like this http://www.dhgate.com/product/esp8266-esp-01s-5v-wifi-relay-module-things/437897148.html . I have successfully established a connection with sinric pro relay example but my controls are inverted. When I press "Turn off" from dashboard it turns on and vice versa. Yes, I know if I rewire the relay from NO to NC it will solve the problem but still I need to know how it is done from the code. Thanks for the help and sorry for poor language. Code; If you encounter any issues: - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md - ensure all dependent libraries are installed - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies - open serial monitor and check whats happening - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one / // 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 #endif #ifdef ESP32 #include #endif #include "SinricPro.h" #include "SinricProSwitch.h" #define WIFI_SSID "xxx." #define WIFI_PASS "xxx" #define APP_KEY "xxx" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" #define APP_SECRET "xxx" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" #define SWITCH_ID "xxx" // Should look like "5dc1564130xxxxxxxxxxxxxx" #define BAUD_RATE 9600 // Change baudrate to your need #define RELAY_PIN 0 // Pin where the relay is connected (D5 = GPIO 14 on ESP8266) bool onPowerState(const String &deviceId, bool &state) { digitalWrite(RELAY_PIN, state); // set pin state return true; // request handled properly } void setup() { pinMode(RELAY_PIN, OUTPUT); // set relay-pin to output mode WiFi.begin(WIFI_SSID, WIFI_PASS); // start wifi SinricProSwitch& mySwitch = SinricPro[SWITCH_ID]; // create new switch device mySwitch.onPowerState(onPowerState); // apply onPowerState callback SinricPro.begin(APP_KEY, APP_SECRET); // start SinricPro } void loop() { SinricPro.handle(); // handle SinricPro commands } — Reply to this email directly, view it on GitHub <#237>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZTIAAW2O3OK2A3PMDDUSZ3HXANCNFSM5KYMVEGQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you are subscribed to this thread.Message ID: **@.***>

yes, it's right, but at the first activation the RELAY_PIN is set to HIGH, ho i can solve this?

sivar2311 commented 7 months ago

Hello @CrickCrock28 !

If this is a new question, please open a new issue with detailed description of your problem. Thank you for your understanding.