Thank you Paul for the image linking info.
Yes, I am using Pull-up.
LED is supposed to be on when relay is off. And when Relay is on LED is supposed to be off. If I power it using only the buck converter, dimmed LED is always on and button does not do anything. If I add the USB power to the mix, then everything works including the button.
Here is my code.
// Fully Working - 2019-11-17 (with dual power)
// Two Relays, one LED, one manual push/momentary switch, Wifi/Alexa Control
// Project for: Alexa Control mains light; + Button
#include <ESP8266WiFi.h>
#include <Bounce2.h>
#include "fauxmoESP.h"
const byte ButtonPin = D6;
const byte LedPin = D5;
Bounce button;
#define RELAY_PIN_1 D7
#define RELAY_PIN_2 D8
#define SERIAL_BAUDRATE 115200
#define WIFI_SSID "ssid"
#define WIFI_PASS "password"
#define LAMP_1 "Kitchen Light"
#define LAMP_2 "Extra Light"
fauxmoESP fauxmo;
// Wi-Fi Connection
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void setup() {
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
// Wi-Fi connection
wifiSetup();
pinMode(RELAY_PIN_1, OUTPUT);
digitalWrite(RELAY_PIN_1, HIGH);
pinMode(RELAY_PIN_2, OUTPUT);
digitalWrite(RELAY_PIN_2, HIGH);
// Manual button
button.attach(ButtonPin, INPUT_PULLUP);
pinMode(LedPin, OUTPUT);
// By default, fauxmoESP creates it's own webserver on the defined port
// The TCP port must be 80 for gen3 devices (default is 1901)
// This has to be done before the call to enable()
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
// You can use different ways to invoke alexa to modify the devices state:
// "Alexa, turn lamp two on"
// Add virtual devices
fauxmo.addDevice(LAMP_1);
fauxmo.addDevice(LAMP_2);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
// Callback when a command from Alexa is received.
// You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
// State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
// Just remember not to delay too much here, this is a callback, exit as soon as possible.
// If you have to do something more involved here set a flag and process it in your main loop.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if ( (strcmp(device_name, LAMP_1) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 1 switched by Alexa");
//digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
if (state) {
digitalWrite(RELAY_PIN_1, LOW);
digitalToggle(LedPin);
Serial.print("LED should be off");
} else {
digitalWrite(RELAY_PIN_1, HIGH);
digitalToggle(LedPin);
Serial.print("LED should be on");
}
}
if ( (strcmp(device_name, LAMP_2) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 2 switched by Alexa");
if (state) {
digitalWrite(RELAY_PIN_2, LOW);
} else {
digitalWrite(RELAY_PIN_2, HIGH);
}
}
});
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}
// Manually trigger Relay using momentary push button
button.update();
if (button.fell()) {
digitalToggle(LedPin);
digitalWrite(RELAY_PIN_1, digitalRead(LedPin));
Serial.println(digitalRead(LedPin) ? F("on") : F("off"));
}
}
inline void digitalToggle(byte pin) {
digitalWrite(pin, !digitalRead(pin));
}
Thanks.