Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Ynot1 opened this issue Apr 25, 2018 · 4 comments
Open

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

Ynot1 opened this issue Apr 25, 2018 · 4 comments

Comments

@Ynot1
Copy link

Ynot1 commented Apr 25, 2018

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
Copy link
Owner

kakopappa commented Apr 25, 2018 via email

@Ynot1
Copy link
Author

Ynot1 commented Apr 25, 2018

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#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
Copy link
Author

Ynot1 commented Apr 25, 2018

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
Copy link
Author

Ynot1 commented Apr 28, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants