Yes.
Have a look at my messy code
#define BLYNK_PRINT Serial
#include "settings.h"
#include "wifimanagment.h"
#include "shiftwrite.h"
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
/* SN74HC595 Connections */
/* Controls the internal transference of data in SN74HC595 internal registers */
const int LATCH_PIN = 16; // D0 - Connected to ST_CP
/* Generates the clock signal to control the transference of data */
const int CLOCK_PIN = 5; // D1 - Connected to SH_CP
/* Outputs the byte to transfer */
const int DATA_PIN = 4; // D2 - Connected to DS
/* NodeMCU to Push Button switch */
// D3, D4, D5, D6, D7, RX
int BTN_PIN[] = {0, 2, 14, 12, 13, 3};
/* Blynk Virtual Pin Assignment */
#define VPIN_BTN_0 V0
#define VPIN_BTN_1 V1
#define VPIN_BTN_2 V2
#define VPIN_BTN_3 V3
#define VPIN_BTN_4 V4
#define VPIN_BTN_5 V5
void checkPhysicalButton();
int relayState[] = { 1, 1, 1, 1, 1, 1 };
int btnState[] = { 1, 1, 1, 1, 1, 1 };
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
//for (int i = 0; i <= 5; i++) {
// Blynk.syncVirtual(VPIN_BTN[i]);
//}
Blynk.syncVirtual(VPIN_BTN_0, VPIN_BTN_1, VPIN_BTN_2, VPIN_BTN_3, VPIN_BTN_4, VPIN_BTN_5);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BTN_0) {
relayState[0] = param.asInt();
shiftWrite(0, relayState[0]);
}
BLYNK_WRITE(VPIN_BTN_1) {
relayState[1] = param.asInt();
shiftWrite(1, relayState[1]);
}
BLYNK_WRITE(VPIN_BTN_2) {
relayState[2] = param.asInt();
shiftWrite(2, relayState[2]);
}
BLYNK_WRITE(VPIN_BTN_3) {
relayState[3] = param.asInt();
shiftWrite(3, relayState[3]);
}
BLYNK_WRITE(VPIN_BTN_4) {
relayState[4] = param.asInt();
shiftWrite(4, relayState[4]);
}
BLYNK_WRITE(VPIN_BTN_5) {
relayState[5] = param.asInt();
shiftWrite(5, relayState[5]);
}
void checkPhysicalButton() {
if (digitalRead(BTN_PIN[0]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[0] != 0) {
// Toggle RELAY state
relayState[0] = !relayState[0];
shiftWrite(0, relayState[0]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_0, relayState[0]);
}
btnState[0] = 0;
} else {
btnState[0] = 1;
}
if (digitalRead(BTN_PIN[1]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[1] != 1) {
// Toggle RELAY state
relayState[1] = !relayState[1];
shiftWrite(1, relayState[1]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_1, relayState[1]);
}
btnState[1] = 0;
} else {
btnState[1] = 1;
}
if (digitalRead(BTN_PIN[2]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[2] != 0) {
// Toggle RELAY state
relayState[2] = !relayState[2];
shiftWrite(2, relayState[2]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_2, relayState[2]);
}
btnState[2] = 0;
} else {
btnState[2] = 1;
}
if (digitalRead(BTN_PIN[3]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[3] != 0) {
// Toggle RELAY state
relayState[3] = !relayState[3];
shiftWrite(3, relayState[3]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_3, relayState[3]);
}
btnState[3] = 0;
} else {
btnState[3] = 1;
}
if (digitalRead(BTN_PIN[4]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[4] != 0) {
// Toggle RELAY state
relayState[4] = !relayState[4];
shiftWrite(4, relayState[4]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_4, relayState[4]);
}
btnState[4] = 0;
} else {
btnState[4] = 1;
}
if (digitalRead(BTN_PIN[5]) == 0) {
// btnState[] is used to avoid sequential toggles
if (btnState[5] != 0) {
// Toggle RELAY state
relayState[5] = !relayState[5];
shiftWrite(5, relayState[5]);
// Update Button Widget
Blynk.virtualWrite(VPIN_BTN_5, relayState[5]);
}
btnState[5] = 0;
} else {
btnState[5] = 1;
}
}
void setup() {
EEPROM.begin(memalloc);
Serial.begin(115200);
delay(100);
// Set the three SPI pins to be outputs
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
for (int i = 0; i <= 5; i++) {
// Turn off all RELAYS
shiftWrite(i, HIGH);
// Restore previous RELAYS states
pinMode(BTN_PIN[i], INPUT_PULLUP);
shiftWrite(i, relayState[i]);
}
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
if(connectWifi()){
Blynk.begin(auth, ssid, password);
} else {
startAP();
Blynk.begin(auth, ssid, password);
}
}
void loop() {
if(WiFi.status() == WL_CONNECTED){
Blynk.run();
timer.run();
} else{
startAP();
}
}