I have created a project using a WEMOS mini D1V2 and a 2 channel relay that allows me to control two different lights with separate momentary switches, the blynk app, or Amazon Alexa. The problem I am having is that when I turn on relay2 using any of the above mentioned methods, relay1 randomly turns on. However, when I turn on relay1 using any of the above mentioned methods, relay2 functions as intended (i.e. it does not turn on).
Here is my code. Does anyone see any logic errors I might be missing. I have checked all hardware and wiring connects and everything seems in order. Thanks in advance for the help.
//Common libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"
//WiFiManager specific libraries
#include <DNSServer.h> //Local DNS server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve as the configuration portal
#include <WiFiManager.h> //Core library
//ArduinoOTA specific libraries
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
#define VPIN1 V7 //Use a unique virtual pin for each device using the same token / dashboard
//In order to control a multitude of devices with a single Blynk dashboard, each ESP8266
//should be programmed with a unique virtual pin assignment, corresponding to a Blynk switch.
#define VPIN2 V8 //Use a unique virtual pin for each device using the same token / dashboard
//In order to control a multitude of devices with a single Blynk dashboard, each ESP8266
//should be programmed with a unique virtual pin assignment, corresponding to a Blynk switch.
char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //Get token from Blynk
//ON and OFF callbacks
void light1On();
void light1Off();
void light2On();
void light2Off();
WemoManager wemoManager;
WemoSwitch *relay1 = NULL;
WemoSwitch *relay2 = NULL;
boolean LEDState1 = 1;
boolean LEDState2 = 1;
SimpleTimer timer; // allocate a name (timer) to the timer
const int relay1Pin = D1;
const int relay2Pin = D4;
const int button1 = D6;
const int button2 = D7;
const int led1 = D2;
const int led2 = D5;
int relayState1 = LOW;
int relayState2 = LOW;
int buttonState1 = HIGH;
int buttonState2 = HIGH;
void setup()
{
Serial.begin(115200);
//WiFi.disconnect(); //uncomment to clear SSID and PASSWORD from EEPROM
WiFi.hostname("FOBWilson_07fc8b"); //uncomment to change hostname for device. Replace XXXX with your own unique name
WiFiManager wifi; //WiFiManager local initialization
//Attempt to get SSID and PASSWORD from EEPROM and connect to network
//If it does not connect it creates an Access Point with the
//Specified name
wifi.autoConnect("WilsonWemo"); //Replace XXXX with your own unique name
wemoManager.begin();
// Format: Alexa invocation name, local port no, on callback, off callback
relay1 = new WemoSwitch("West Deck Light", 80, light1On, light1Off);
relay2 = new WemoSwitch("Dining Room Outlet", 81, light2On, light2Off);
wemoManager.addDevice(*relay1);
wemoManager.addDevice(*relay2);
pinMode(relay1Pin, OUTPUT);
pinMode(button1,INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(button2,INPUT_PULLUP);
pinMode(led2, OUTPUT);
digitalWrite(relay1Pin, relayState1);
digitalWrite(led1, !relayState1);
digitalWrite(relay2Pin, relayState2);
digitalWrite(led2, !relayState2);
Blynk.config(auth);
ArduinoOTA.begin();
timer.setInterval(100L, checkPhysicalButton1); // Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton2); // Setup a function to be called every 100 ms
}
void loop()
{
wemoManager.serverLoop();
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
// Toggle relay1 on
void light1On()
{
digitalWrite(relay1Pin, HIGH);
digitalWrite(led1, LOW);
Blynk.virtualWrite(VPIN1, HIGH); // Sync the Blynk button widget state
}
// Toggle relay1 off
void light1Off()
{
digitalWrite(relay1Pin, LOW);
digitalWrite(led1, HIGH);
Blynk.virtualWrite(VPIN1, LOW); // Sync the Blynk button widget state
}
BLYNK_WRITE(VPIN1)
{
relayState1 = param.asInt(); // Get the state of the relay
digitalWrite(relay1Pin, relayState1);
digitalWrite(led1, !relayState1);
}
void checkPhysicalButton1()
{
//Relay1
if (digitalRead(button1) == LOW)
{
if (buttonState1 != LOW)
{
relayState1 = !relayState1;
Blynk.virtualWrite(VPIN1, relayState1);
digitalWrite(relay1Pin, relayState1);
digitalWrite(led1, !relayState1);
}
buttonState1 = LOW;
}
else
{
buttonState1 = HIGH;
}
}
// Toggle relay2 on
void light2On()
{
digitalWrite(relay2Pin, HIGH);
digitalWrite(led2, LOW);
Blynk.virtualWrite(VPIN2, HIGH); // Sync the Blynk button widget state
}
// Toggle relay2 off
void light2Off()
{
digitalWrite(relay2Pin, LOW);
digitalWrite(led2, HIGH);
Blynk.virtualWrite(VPIN2, LOW); // Sync the Blynk button widget state
}
BLYNK_WRITE(VPIN2)
{
relayState2 = param.asInt(); // Get the state of the relay
digitalWrite(relay2Pin, relayState2);
digitalWrite(led2, !relayState2);
}
void checkPhysicalButton2()
{
//Relay2
if (digitalRead(button2) == LOW)
{
if (buttonState2 != LOW)
{
relayState2 = !relayState2;
Blynk.virtualWrite(VPIN2, relayState2);
digitalWrite(relay2Pin, relayState2);
digitalWrite(led2, !relayState2);
}
buttonState2 = LOW;
}
else
{
buttonState2 = HIGH;
}
}