OK, here’s my code. It uses either the Blynk Button or the Physical Button to swap which one of the ESP8266 LEDs (Red or Blue) is lit. When ever either button is pushed, the lit LED is turned off and the other one is turned on.
When the LEDs are swapped, the sketch code changes the color and label of the Blynk Button using Blynk.setProperty() to match which LED on the ESP8266 is lit.
As long as the Blynk Project is running, everything stays in sync perfectly. But, if I stop the Blynk Project, use the Physical Button to swap the LEDs, and then restart the Blynk Project then the Blynk Button does not update to the current LED color. Things only go back in sync again when I press either Blynk or Physical Button.
Running Blynk on an Android phone and I’ve attached a screen shot of my Button setup.
Thanks again for your help.
Greg
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example runs directly on ESP8266 chip.
*
* You need to install this for ESP8266 development:
* https://github.com/esp8266/Arduino
*
* Please be sure to select the right ESP8266 module
* in the Tools -> Board menu!
*
* Change WiFi ssid, pass, and Blynk auth token to run :)
*
**************************************************************/
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define BLYNK_GREEN "#23C48E"
#define BLYNK_BLUE "#04C0F8"
#define BLYNK_YELLOW "#ED9D00"
#define BLYNK_RED "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"
#define redLED 0
#define blueLed 2
#define ledOFF 1
#define ledON 0
#define PHYSICAL_BUTTON 14
#define BOUNCE_TIME 200
enum ledSelect {red, blue};
void checkPhysicalButton(void);
void initLEDs(void);
void setLeds(void);
void swapLeds(void);
void labelButton(ledSelect);
uint8_t btnState = HIGH;
uint32_t lastButtonPush = 0;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "yyyyyyyyyyy";
char pass[] = "zzzzzzzzzzz";
ledSelect litLED;
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
initLEDs();
pinMode(PHYSICAL_BUTTON, INPUT_PULLUP);
}
void loop() {
Blynk.run();
checkPhysicalButton();
}
void initLEDs() {
pinMode(redLED, OUTPUT);
pinMode(blueLed, OUTPUT);
litLED = blue;
setLeds();
}
void swapLeds() {
switch (litLED) {
case red:
litLED = blue;
setLeds();
break;
case blue:
litLED = red;
setLeds();
break;
default:
break;
}
}
void setLeds() {
switch (litLED) {
case red:
digitalWrite(redLED, ledON);
digitalWrite(blueLed, ledOFF);
labelButton(red);
break;
case blue:
digitalWrite(redLED, ledOFF);
digitalWrite(blueLed, ledON);
labelButton(blue);
break;
default:
break;
}
}
void checkPhysicalButton()
{
if (digitalRead(PHYSICAL_BUTTON) == LOW) {
// btnState is used to avoid sequential toggles BOUNCE_TIME check provides debounce
if ((btnState != LOW) && (millis()-lastButtonPush > BOUNCE_TIME)) {
// Swap states of LEDs
swapLeds();
lastButtonPush = millis();
}
btnState = LOW;
} else {
btnState = HIGH;
}
}
BLYNK_WRITE(V0) {
int value = param.asInt();
if (value) {
swapLeds();
}
}
BLYNK_CONNECTED() {
labelButton(litLED);
}
BLYNK_READ(V0) {
labelButton(litLED);
}
void labelButton(ledSelect color) {
switch (color) {
case red:
Blynk.setProperty(V0, "label", "Red");
Blynk.setProperty(V0, "color", BLYNK_RED);
break;
case blue:
Blynk.setProperty(V0, "label", "Blue");
Blynk.setProperty(V0, "color", BLYNK_BLUE);
break;
default:
break;
}
}