kakopappa / arduino-esp8266-alexa-multiple-wemo-switch

multiple belkin wemos switch emulator using ESP8266
https://sinric.pro
MIT License
308 stars 196 forks source link

How to make the state returned to Alexa from another gpio pin. #48

Open Ynot1 opened 6 years ago

Ynot1 commented 6 years ago

Lots of success with this code today after changing the persistent_uuid line in switch.cpp as pointed out by others. That's really helpful guys, thanks. My Alexa Plus is discovering the devices without any additional skills, and controlling the resulting relays just fine.

My use case is a garage door control, that wants 1 sec pulses to either open or close the door. I have modified the code in this repo to read a gpio input pin on the esp2866 that is connected to a switch so I know the state of the door. Simple then to gate the Alexa on and off commands via that garageDoorState Boolean to only produce a close pulse when the door is open and vice versa.

The only problem with this, is that Alexa doesn't know the actual state of the door, only the state of the door control relays , and as they are always short pulses, when I look at the belkin smart switch device display in Alexas list of devices, it's always off.

Where in the code could I make changes such that the state of the door is returned to Alexa instead of the state of the door controls? I would like to see those smart switch displays in Alexa say "on" when the door is open. (This could be really useful if I ever want to know remotely if I have left the door open)

I can see regular " got binary state request" messages in the serial monitor which is probably where I need to make changes, but I lack the understanding of exactly how...

Apart from the tweak above, and removing most of the debug messages to make life quieter, I haven't touched the library files (yet).

kakopappa commented 6 years ago

Don't think it's possible

I am not sure what are you trying to do. Check whether sinric.com can support

On Wed, Apr 25, 2018 at 12:12 PM Ynot1 notifications@github.com wrote:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch/issues/48, or mute the thread https://github.com/notifications/unsubscribe-auth/AHIM5kFsD7FdbqElGeVhq1SP81UqT-z8ks5tsAXVgaJpZM4Tiyf2 .

Ynot1 commented 6 years ago

include

include

include

include

include "switch.h"

include "UpnpBroadcastResponder.h"

include "CallbackFunction.h"

// prototypes boolean connectWifi();

//on/off callbacks bool garageDoorOpenOn(); bool garageDoorOpenOff(); bool garageDoorCloseOn(); bool garageDoorCloseOff();

// Change this before you flash const char ssid = "JustBePatient_VirusDownloading"; const char password = "HH563dggrtvvxz";

boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

Switch garagedooropen = NULL; Switch garagedoorclose = NULL;

bool isgarageDoorOpenOn = false; bool isgarageDoorCloseOn = false; bool GarageDoorState = false; bool PrevGarageDoorState = false;

const int garageDoorRelay = 0; // GPIO0 pin. const int GPIO2 = 2; // GPIO2 pin. this would be better if it were the RXD pin, it would make restarts with the door open possible...

void setup() { pinMode(garageDoorRelay, OUTPUT); pinMode(GPIO2, OUTPUT); // to start with, it changes later...

Serial.begin(9600);

Serial.println("Booting..."); delay(2000);

//flash fast a few times to indicate CPU is booting digitalWrite(GPIO2, LOW); delay(100);
digitalWrite(GPIO2, HIGH); delay(100); digitalWrite(GPIO2, LOW); delay(100);
digitalWrite(GPIO2, HIGH); delay(100);
digitalWrite(GPIO2, LOW); delay(100);
digitalWrite(GPIO2, HIGH);

Serial.println("Delaying a bit..."); delay(2000);

// Initialise wifi connection wifiConnected = connectWifi();

if(wifiConnected){

//flash slow a few times to indicate wifi connected OK digitalWrite(GPIO2, LOW); delay(1000);
digitalWrite(GPIO2, HIGH); delay(1000); digitalWrite(GPIO2, LOW); delay(1000);
digitalWrite(GPIO2, HIGH); delay(1000); digitalWrite(GPIO2, LOW); delay(1000);
digitalWrite(GPIO2, HIGH);

upnpBroadcastResponder.beginUdpMulticast();

// Define your switches here. Max 10
// Format: Alexa invocation name, local port no, on callback, off callback
garagedooropen = new Switch("garage door open", 80, garageDoorOpenOn, garageDoorOpenOff);
garagedoorclose = new Switch("garage door close", 81, garageDoorCloseOn, garageDoorCloseOff);

Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*garagedooropen);
upnpBroadcastResponder.addDevice(*garagedoorclose);

}

digitalWrite(garageDoorRelay, LOW); // turn off relay 
digitalWrite(GPIO2, HIGH); // turn off LED 

Serial.println("Making GPIO2 into an INPUT"); // used to detect garage door current state pinMode(GPIO2, INPUT);

PrevGarageDoorState = GarageDoorState; // edge detection of garage door state

}

void loop() { GarageDoorState = digitalRead(GPIO2); //either GPIO 2 or RX Pin depending on variable used... delay(100);
if (GarageDoorState == LOW) { if (PrevGarageDoorState == HIGH){ Serial.println("GarageDoor State has just opened (Logic LOW)"); } }

if (GarageDoorState == HIGH) { if (PrevGarageDoorState == LOW){ Serial.println("GarageDoor State has just closed (Logic HIGH)"); } }

PrevGarageDoorState = GarageDoorState; // remember prev state for next pass

if(wifiConnected){

  upnpBroadcastResponder.serverLoop();

  garagedoorclose->serverLoop();
  garagedooropen->serverLoop();

} }

bool garageDoorOpenOn() { Serial.println("Request to Open door received ...");

  if (GarageDoorState == HIGH) { // only pulse relay if door is currently closed
      Serial.println("Door is closed - pulsing relay to open it");

      Serial.println("XXX Pulsing Relay on ...");
      digitalWrite(garageDoorRelay, HIGH); // turn on relay 
      delay(2000);    ;
      Serial.println("XXX Pulsing Relay off again ...");
      digitalWrite(garageDoorRelay, LOW); // turn off relay 
      }
  else {
      Serial.println("Door is already open - not pulsing relay!");
  }

isgarageDoorOpenOn = false;    
return isgarageDoorOpenOn;

}

bool garageDoorOpenOff() { // nothing ever calls this.... Serial.println("Switch 1 turn off ...");

digitalWrite(garageDoorRelay, LOW); // turn off relay 

isgarageDoorOpenOn = false;
return isgarageDoorOpenOn;

}

bool garageDoorCloseOn() { Serial.println("Request to Close door received");

  if (GarageDoorState == LOW) { // only pulse relay if door is currently open
      Serial.println("Door is open - pulsing relay to close it");

      Serial.println("XXX Pulsing Relay on ...");
      digitalWrite(garageDoorRelay, HIGH); // turn on relay 
      delay(2000);    ;
      Serial.println("XXX Pulsing Relay off again ...");
      digitalWrite(garageDoorRelay, LOW); // turn off relay
  }
  else {
      Serial.println("Door is already closed, not pulsing relay...");
  }

isgarageDoorCloseOn = false;
return isgarageDoorCloseOn;

}

bool garageDoorCloseOff() { // nothing ever calls this.... Serial.println("Switch 2 turn off ...");

digitalWrite(garageDoorRelay, LOW); // turn off relay

isgarageDoorCloseOn = false; return isgarageDoorCloseOn; }

// connect to wifi – returns true if successful or false if not boolean connectWifi(){ boolean state = true; int i = 0;

WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); Serial.println("Connecting to WiFi Network");

// Wait for connection Serial.print("Connecting ..."); while (WiFi.status() != WL_CONNECTED) { delay(5000); Serial.print("."); if (i > 10){ state = false; break; } i++; }

if (state){ Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } else { Serial.println(""); Serial.println("Connection failed. Bugger"); }

return state; }

Ynot1 commented 6 years ago

Cant use sinric, its not released in NZ. But its not the skill i am having an issue with, its the values returned to Alexa in the ESP-2866 code.

I want to return the state of GPIO2 (which i have read into the booleen variable "GarageDoorState") to Alexa so it appears as the smart switch state.

"I am not sure what are you trying to do. Check whether sinric.com can support"

Ynot1 commented 6 years ago

Anything is possible given time and perseverance!

I had to hack switch.cpp about to do it, but Alexa is now getting her state of my garage door from the door position switch itself, not from the relaystate variable that tracks if the last command was an On or off.

I had to expose the garage door state to switch.cpp via an extern bool declaration ( GarageDoorState is defined in the main.ino) and then fire that Boolean state into the getbinarystate logic ( I just overwrote switchstatus)

This is really crude, but as I only have two controls on that ESP-01 (door open and door close) having the same door state sent back for both of them makes sense. It wouldn't work if you wanted to have several different inputs pins reporting for different functions.

While I was about it, I changed the door state to be read from the rx pin instead of GPIO2 - now the ESP-01 can startup reliably with the garage door either open or shut. Previously, it would hang if it started with the door open.

Sent from my iPad

On 25/04/2018, at 5:35 PM, Aruna Tennakoon notifications@github.com wrote:

Don't think it's possible

I am not sure what are you trying to do. Check whether sinric.com can support

On Wed, Apr 25, 2018 at 12:12 PM Ynot1 notifications@github.com wrote:

― You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch/issues/48, or mute the thread https://github.com/notifications/unsubscribe-auth/AHIM5kFsD7FdbqElGeVhq1SP81UqT-z8ks5tsAXVgaJpZM4Tiyf2 .

― You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.