Giter VIP home page Giter VIP logo

Comments (4)

kakopappa avatar kakopappa commented on August 9, 2024

from arduino-esp8266-alexa-multiple-wemo-switch.

Ynot1 avatar Ynot1 commented on August 9, 2024

#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;
}

from arduino-esp8266-alexa-multiple-wemo-switch.

Ynot1 avatar Ynot1 commented on August 9, 2024

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"

from arduino-esp8266-alexa-multiple-wemo-switch.

Ynot1 avatar Ynot1 commented on August 9, 2024

from arduino-esp8266-alexa-multiple-wemo-switch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.