I am trying to create a simple circuit using two ESP8266.
I need a physical button connected to either one to activate an LED on both.
(See Diagram)
I am new to coding and was able to find some example projects to start from. I tried using the Blynk “Sync Physical Button” example code, and was able to successfully make a button toggle the LED on one of the two ESPs but it does not communicate with the second one even with both the same virtual pin.
I want to make use of the Blynk bridge to activate the LED on both ESPs from the button on either one. I am sure there is a fairly simple code to do this, but I am struggling to make that jump.
Here is the code I am currently using to toggle the LED on one ESP.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "aaaaaaaaaaaaaaaaaaaa";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "1111111111";
char pass[] = "2222222222";
// Set your LED and physical button pins here
const int ledPin = 5;
const int btnPin = 12;
BlynkTimer timer;
void checkPhysicalButton();
int ledState = LOW;
int btnState = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V2);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState != LOW) {
// Toggle LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Update Button Widget
Blynk.virtualWrite(V2, ledState);
}
btnState = LOW;
} else {
btnState = HIGH;
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
}