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
236 stars 125 forks source link

Reading a digital input #45

Closed Hansta98 closed 4 years ago

Hansta98 commented 4 years ago

I want monitor a digital signal over an input from my nodemcu. Therefore I tried the GarageDoor.ino and the ContactSensor.ino but in both cases I did not get any answer in Serial Monitor or rather on Alexa App and Sinric Dashboard overview. Serial Monitor tells me only strange strings like: ⸮⸮⸮⸮⸮!⸮D⸮⸮⸮ ⸮?)⸮⸮⸮⸮⸮@H I only needed the current state from a relay. Would also work to read the input over an interrupt (CHANGE), so every time the state changes, SinricPro synchronize it (or what´s the usefullest way)? sinric pro

sivar2311 commented 4 years ago

Seems, your baudrate is wrong. (I cant see this in screenshot). Please check for correct settings!

"only need state of relay" ... Can you tell more about this? What do you want to achieve, and why do you use a garagedoor device for this?

Hansta98 commented 4 years ago

I just tried a round... it's a unfortunate screenshot sorry. To my aim: I switched a relay in parallel to a load (roomlight) which I want monitore. With the contacts of the relay, I want send a HIGH or a LOW signal to SinricPro, so that it knows if it's on or off. Futuremore I will realize something like a cross-switch-relay with two other relays so that I can expand the existing toggle circuit with an additional switch position over my voice. For this, Alexa must know the current state

Hansta98 commented 4 years ago

Screenshot_20200302-224828_AutoCAD

sivar2311 commented 4 years ago

What's about serial monitor issue? Is this solved? (Even this is not related to SinricPro)

I think you will agree that using a garagedoor device is the the wrong way here ;) Please take a look at Light example. I think a SinricProLight is what you're looking for. Since you only want to turn it on and off, you only need to implement onPowerState callback for this. Remove the print statement, and exchange it with the code, needed to control your cicruit.

Hansta98 commented 4 years ago

Yeah, the point was/is that usually I had to know what's the current light state, else if I only toggle my "cross-switch-relay", once I switch one of the two other just existing switches, I had to switch the light inverted (if I want switch on light, I had to tell: "Alexa turn of the roomlight"). For this I would know how to read it, or rather to realize when it changes his state (read the input periodically with a timer or only once when state changed over an interrupt function). The main problem is that until now I not able to sinchronize any input signal with sinricPro.

sivar2311 commented 4 years ago

So, i guess Serial Monitor is not a problem anymore?

To update state on SinricPro Server, use sendPowerStateEvent function (see here) But please make sure, only send Events when state changed!

To check the current state of the relay, use a simple digitalRead. You can do this within the loop, or via interrupt. Are you familar with programming interrupts? This will request to set a flag and handles all other things outside the interrupt function!

If you have to say "turn off" to turn on your device, you have to invert your digitalWrite.

Hansta98 commented 4 years ago

In reality I dont need to know usually the current state and when it changed. (thinking) I must know it only when I ask Alexa. So an interrupt would be unnecessary. Always I switch the roomlight manually it doesnt interest Alexa, but when I command her to switch it she have to know it. So, as you told me, idea one (put it within the loop) sould work in practice

sivar2311 commented 4 years ago

If you don't update the current state to SinricPro server, the state will shown incorrect in Alexa App. If you have the possibillity to check the current state, you should update (only if there was a change), using the corresponding event function.

Anyways, your sketch have to know the current state (not Alexa) to control the relais correctly. Btw, the screenshot showing german words: I live near Hamburg ;)

Hansta98 commented 4 years ago

Hallo silvar2311, jetzt melde ich mich doch noch mal. Es geht um das Einlesen eines Eingangs (Pin D4) mittels Interrupt Routine. Wollte eigentlich den benötigen Wert bei Statusänderung auslesen und in eine Variable schreiben. Es scheint jedoch als ob die Interrupt-Routine kontinuierlich abgearbeitet wird. Der Serielle Monitor gibt jedenfalls nur "Mist" aus. Die Idee dahinter war, dass Func_Roomlight über den Interrupt Pin D4 aufgerufen und dort die Variable RoomlightState überschrieben wird.

kreuzschaltung

`#define Out_Roomlight 0 // GPIO WemosPin D3 für Raumlicht schalten (schalte Relais 1K01+1K02)

define In_Roomlight 2 // GPIO WemosPin D4 für Raumlicht Status (Relais 1K09)

define TestPin 5 //GPIO WemosPin D1 für Testzwecke

volatile byte RoomlightState = LOW; //Raumlichtstatus bool myPowerState = false;

bool onPowerState(const String &deviceId, bool &state) { Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state ? "on" : "off"); myPowerState = state; digitalWrite(Out_Roomlight, myPowerState); 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); } Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str()); }

void setupSinricPro() { SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
mySwitch.onPowerState(onPowerState);
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() { pinMode(TestPin, OUTPUT);
pinMode(Out_Roomlight, OUTPUT);
pinMode(In_Roomlight, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(In_Roomlight), Func_RoomlightState, CHANGE);

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

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

void Func_RoomlightState() { RoomlightState = digitalRead(In_Roomlight); if (RoomlightState == HIGH) { digitalWrite(TestPin, HIGH); } if (RoomlightState == LOW) { digitalWrite(TestPin, LOW); } }

//Func_Roomlight wird immer aufgerufen wenn In_Roomlight mit Masse verbunden wird oder von Masse getrennt wird //Func_Roomlight liest lediglich den aktuellen Raumlichtstatus aus //RoomlightState == HIGH wenn In_Roomlight NICHT mit Masse verbunden ist (Raumlicht = aus) //RoomlightState == LOW wenn In_Roomlight mit Masse verbunden ist (Raumlicht = ein) `

sivar2311 commented 4 years ago

Hi! Kannst du mich mal per E-Mail kontaktieren (sivar2311@googlemail.com). Dann können wir mal Daten zu WhatsApp oder Telegramm austauschen und direkt schreiben. Was hältst Du davon?