Control same pin virtually and digitally

Hi,
using an ESP8266 and Blynk I want to control the state of two relais. I want to control them in the same way with both designated buttons in Blynk and two hardware buttons. The two buttons (each software and hardware) should activate the first or the second relais as long as the respective button is pressed. With just the virtual buttons everything works fine, but I struggle with properly implementing the code for the hardware buttons. Ideally I want to use interrupts to avoid that the state of the pins is checked every loop.

Just out of curiosity: How does the Blynk-App do this with regular buttons? Also with interrupts, or something else I might not be aware of ?

Heres my code so far.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "***";

char ssid[] = "***";
char pass[] = "***";

int Taster = 13;
int Auf = 12;
int Zu = 14;
int val = 0;

BlynkTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V2);

// Digital clock display of the time
void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute();
  Serial.print("Current time: ");
  Serial.print(currentTime);

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
}

void ManualMode()
{
  if(digitalRead(Auf) == HIGH)
  {
    digitalWrite(0, HIGH);
  }
  else
  {
    digitalWrite(0, LOW);
  }
  if(digitalRead(Zu) == HIGH)
  {
    digitalWrite(16, HIGH);
  }
  else
  {
    digitalWrite(16, LOW);
  }
}

BLYNK_CONNECTED() 
{
  // Synchronize time on connection
  rtc.begin();
}

BLYNK_WRITE(V3)
{
  digitalWrite(0, param.asInt());
}

BLYNK_WRITE(V4)
{
  digitalWrite(16, param.asInt());
}

BLYNK_WRITE(V2)
{
  if (String("Status") == param.asStr())
  {
    val = digitalRead(Taster);
    if (val == HIGH)
    {
      terminal.print("Klappe ist geschlossen");
    }
    else
    {
      terminal.print("Klappe ist offen");
    }
    terminal.println();    
    terminal.flush();
  }
}

void setup()
{
  Serial.begin(9600);

  pinMode(Taster, INPUT);
  pinMode(Auf, INPUT);
  pinMode(Zu, INPUT);

  Blynk.begin(auth, ssid, pass);
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, clockDisplay);
  terminal.clear();
}

void loop()
{
  Blynk.run();
  timer.run();
  ManualMode();
}

Nobody got an idea?

Basically what I want is to run software and hardware buttons in parallel. They each should perform in the same way, as the respective relay should be activated as long as the button is pressed. This should be possible right ? Unfortunately I can’t get it to work.

Has someone got an idea of how to do this ?

Greetings

Try that by add or replace parts of code below (see my comments ADD or REPLACE LINE).
Buttons V3, V4 in PUSH mode with values 0 (not push) and 1 (push).
I have not tried to compile, it is possible that some corrections are required.

int aufState = 0; // ADD
int zuState = 0; :// ADD

void ManualMode()
{
  if(digitalRead(Auf) == HIGH || aufState) // REPLACE LINE
  {
    digitalWrite(0, HIGH);
  }
  else
  {
    digitalWrite(0, LOW);
  }
  if(digitalRead(Zu) == HIGH || zuState) // REPLACE LINE
  {
    digitalWrite(16, HIGH);
  }
  else
  {
    digitalWrite(16, LOW);
  }
}
BLYNK_WRITE(V3)
{
  aufState = param.asInt(); // REPLACE LINE
}

BLYNK_WRITE(V4)
{
  zuState = param.asInt(); // REPLACE LINE
}
1 Like

Thanks for the quick reply. I got it working by also including the BLYNK_WRITE Function for each V3 and V4 as you can see in the now compiled and working code.

Cheers,
Gerrit

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "***";

char ssid[] = "***";
char pass[] = "***";

int Taster = 13;
int Auf = 12;
int Zu = 14;
int val = 0;

int aufState = 0;
int zuState = 0; 

BlynkTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V2);

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute();
  Serial.print("Current time: ");
  Serial.print(currentTime);

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
}

void ActuationMode()
{
  if(digitalRead(Auf) == HIGH || aufState)
  {
    digitalWrite(0, HIGH);
  }
  else
  {
    digitalWrite(0, LOW);
  }
  if(digitalRead(Zu) == HIGH || zuState)
  {
    digitalWrite(16, HIGH);
  }
  else
  {
    digitalWrite(16, LOW);
  }
}


BLYNK_CONNECTED() 
{
  // Synchronize time on connection
  rtc.begin();
}

BLYNK_WRITE(V2)
{
  if (String("Status") == param.asStr())
  {
    val = digitalRead(Taster);
    if (val == HIGH)
    {
      terminal.print("Klappe ist geschlossen");
    }
    else
    {
      terminal.print("Klappe ist offen");
    }
    terminal.println();    
    terminal.flush();
  }
}

BLYNK_WRITE(V3)
{
  aufState = param.asInt(); 
}

BLYNK_WRITE(V4)
{
  zuState = param.asInt(); 
}

void setup()
{
  Serial.begin(9600);

  pinMode(Taster, INPUT);
  pinMode(Auf, INPUT);
  pinMode(Zu, INPUT_PULLUP);

  Blynk.begin(auth, ssid, pass);
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, clockDisplay);
  terminal.clear();
}

void loop()
{
  Blynk.run();
  timer.run();
  ActuationMode();
}
1 Like

Well done :wink: