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
Other
230 stars 124 forks source link

PIN High/Low On/Off Controls Reversed #33

Closed footballguy05 closed 4 years ago

footballguy05 commented 4 years ago

This is probably a very simple fix for the pros out there, but I can't seem to understand what's happening. Was able to find some great code that basically meets my needs. Thanks to the OP on that one. However, what I can't figure out is why the controls are reversed. I have to switch the relay to 'off' on the control device to activate the relay and vice versa.

Have tried re-configuring a couple of ways, but basically the output never matches the Sinric.pro dashboard / Alexa device state. I've checked my relay NC/NO/GND wiring. All is good there. Any other ideas?

typedef struct {
  int pin;
  bool state;
} config_t;
std::map<String, config_t> deviceConfig = {
// SinricPro deviceId        ,  PIN, state (on/high=true, off/low = false) 
  {"5e3468**************", {D1, false}},
  {"5e3469**************", {D2, false}},
  {"5e35d5**************", {D3, false}},
  {"5e35d5**************", {D4, false}}
};

bool onPowerState(String deviceId, bool &state) {
  config_t config = deviceConfig[deviceId]; // get config for corresponding deviceId
  Serial.printf("DeviceId: %s, PIN: %d, state: %s\r\n", deviceId.c_str(), config.pin, state?"on":"off");
  digitalWrite(config.pin, state);
  deviceConfig[deviceId].state = state;
  return true;
}

// 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());
}

void setupSinricPro() {
  for (auto& config : deviceConfig) {
    const char* deviceId = config.first.c_str();
    SinricProSwitch &mySwitch = SinricPro[deviceId];
    mySwitch.onPowerState(onPowerState);
    pinMode(config.second.pin, OUTPUT);
  }

  SinricPro.begin(APP_KEY, APP_SECRET);
  SinricPro.restoreDeviceStates(true);
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
 }

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

What's your relays wiring? NO / GND or NC / GND? This code is for NO / GND.

footballguy05 commented 4 years ago

If you don’t mind me asking, which part of the code tells you that?

As for the wiring, I have a 12v source with the red line fed into the NC and the black into NO and jumpers to carry power to each channel. The actuators are wired into the GND on channels 1/2 and 3/4.

Thanks

Sent from my iPhone

On Feb 7, 2020, at 11:51 PM, Boris Jäger notifications@github.com wrote:



What's your relays wiring? NO / GND or NC / GND? This code is for NO / GND.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/sinricpro/esp8266-esp32-sdk/issues/33?email_source=notifications&email_token=AON2CFFK5CG37H45L7R3X5TRBZB5PA5CNFSM4KRYCVOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELFKLYI#issuecomment-583706081, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AON2CFD4B5CZFFRYHQYFURLRBZB5PANCNFSM4KRYCVOA.

sivar2311 commented 4 years ago

If you don’t mind me asking, which part of the code tells you that?

// SinricPro deviceId , PIN, state (on/high=true, off/low = false)

I am not an electronics expert but as far as i understand relays, youre wiring is wrong. NO = normally open NC = normally closed GND = GND (nothing to say about this)

NO means relay is normally open...and a high signal on signal pin will close the circuit betwen NO and GND and open the circuit between NC and GND.

NC means relay is closed and a high signal on signal pin will OPEN the circuit between NC and GND and close the circuit NO and GND

image See here

Edit: Or do you use another type of relays? Maybe you can provide a circuit diagram?

footballguy05 commented 4 years ago

Thanks for taking the time to answer.

Even assuming my relay wiring is wrong, which I’m not certain is the case given numerous tutorials I watched on how to wire the relay, that doesn’t explain why the switch is activated when the dashboard says off and vice versa.

Sent from my iPhone

On Feb 8, 2020, at 7:55 AM, Boris Jäger notifications@github.com wrote:



If you don’t mind me asking, which part of the code tells you that?

// SinricPro deviceId , PIN, state (on/high=true, off/low = false)

I am not an electronics expert but as far as i understand relays, youre wiring is wrong. NO = normally open NC = normally closed GND = GND (nothing to say about this)

NO means relay is normally open...and a high signal on signal pin will close the circuit betwen NO and GND.

NC means relay is closed and a high signal on signal pin will OPEN the circuit between NC and GND

[image]https://user-images.githubusercontent.com/2558564/74086426-fbdd2700-4a82-11ea-877a-7f1cd7ab7f35.png See herehttps://duino4projects.com/arduino-relay-control-tutorial/

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/sinricpro/esp8266-esp32-sdk/issues/33?email_source=notifications&email_token=AON2CFCT3CVJUA7ELJNU5S3RB22WXA5CNFSM4KRYCVOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELFSK4Q#issuecomment-583738738, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AON2CFAK6RZ3UTBPIU3EGULRB22WXANCNFSM4KRYCVOA.

sivar2311 commented 4 years ago

Ok, then whats the PIN state, when it is turned ON, HIGH or LOW? Edit: And what's the serial output? Is it "DeviceId: <deviceId>, PIN: <PIN> state: ON", or is it "DeviceId: <deviceId>, PIN: <PIN> state: OFF" ?

Edit2: By writing "your wiring is wrong" i mean, your circuit seems to be "inverted". I think your circuit requires a LOW signal to turn on, and a HIGH signal to turn off. If this is so, just simply invert the output to your PIN by inverting the state.

Change this: digitalWrite(config.pin, state);

to this: digitalWrite(config.pin, !state);

h16bill commented 4 years ago

I recently had an issue with this sort of thing. I am using a relay module attached to a d1 mini. It has status leds for power and active. Turns out this relay is active low. So it activates with an input of 0-1.2v . I was not expecting this but after figuring it out and looking back at the source where I bought it from, it did say active low. YMMV

footballguy05 commented 4 years ago

apologies for the delay in responding. spent a few days under the weather and unable to get back to my project. After a bit more tinkering, was able to conclude that it works as is. I still don't fully understand the relationship between the LED on/off and the dashboard, but I'm sure that's my shortcomings, nothing to do with the code. Marking as closed. Thanks for all the input.