Multiple controllers on one GPIO

It’d be nice if I could add multiple controllers to one GPIO instead of getting the “busy” notification.

Use case:
I have my ESP8266 controlling a relay (GPIO 2) with a button control for manual control but I would like to add a couple timers to turn the relay on at certain times of the day.

In this situation you need to use virtual pins. A simple OR statement in your program should suffice.

Thanks @snowman.

Any chance you could elaborate on that? Currently my setup and loop look like this:
void setup()
{
Blynk.begin(auth);
}

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

Protip for Blynk forum: put 3x ``` (top left key on most keyboards) before and after your code so it looks like this:

code

So virtual pins are a communication channel between the app and the hardware. If you assign, say V0 to your button, V1 to one timer widget and V2 to another timer widget, you need to add a handler to your code for each vPin:

BLYNK_WRITE(V0)
{
  buttonState = param.asInt();
}
BLYNK_WRITE(V1)
{
  timer1State = param.asInt();
}
BLYNK_WRITE(V2)
{
  timer2State = param.asInt();
}

The BLYNK_WRITE handlers go outside your setup() and loop() (as they’re declaring functions) and are called whenever those vPins change state. I’ve used the ‘_State’ variables to store that state so we can use them in our sketch.

Then you need a construct like this to switch the actual physical pin:

if (buttonState || timer1State || timer2State)
{
  digitalWrite(RELAY_PIN, 1);
}
else
{
  digitalWrite(RELAY_PIN, 0);
}

That switches the RELAY_PIN pin high any of the signals are 1, otherwise it switches it low. That code would go in your loop(), although the proper way to do it would be to use SimpleTimer to run it at a set frequency depending on your application so you don’t use all your CPU time running that check.

2 Likes

Thanks @snowman, that code works perfectly.