virtualWrite not triggering my relay

Hello there.
I’m doing a school project about controlling the classroom lights through relays. The relays will turn on/off at certain times. While writing the code, I faced an issue: the virtualWrite function does change the state in the dashboard but doesn’t turn on the relay. If I trigger the switch from the dashboard the relays turn on/off, but not when using the “prova” function. So the BLYNK_WRITE() functions are working and switch the relay, but not when I write it in the code.

Here’s information about my project. Thanks in advance.

Hardware model + communication type: Arudino UNO + ESP8266
Smartphone OS (iOS or Android) + version: I’m using the web dashboard, Chrome on Windows 10.
Blynk server or local server: Blynk server
Blynk Library version: 1.1.0


#define BLYNK_TEMPLATE_ID           "TMPLQg3tv8ug"
#define BLYNK_DEVICE_NAME           "TDR"
#define BLYNK_AUTH_TOKEN            "zJW_cvA-2Df0GGk8-dX7XRKPuQUFHk08"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Casa_MV_3";
char pass[] = "06877211451234";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 38400

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

WidgetRTC rtc;


#define llum1 V8
#define llum2 V9
#define AppleTV V10
#define projector V11
#define SensorLlum A0

int h = hour();
int m = minute();
int s = second();

// 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() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();

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

BLYNK_CONNECTED() {
  Blynk.syncAll();
  rtc.begin();
}

// Definir els interruptors:
// V8 = Llums 1
BLYNK_WRITE(V8) {
  if (param.asInt() == 1) {
    digitalWrite(8, HIGH);
  } else {
    digitalWrite(8, LOW);
  }
}
// V9 = Llums 2
BLYNK_WRITE(V9) {
  if (param.asInt() == 1) {
    digitalWrite(9, HIGH);
  } else {
    digitalWrite(9, LOW);
  }
}
// V10 = Projector
BLYNK_WRITE(V10) {
  if (param.asInt() == 1) {
    digitalWrite(10, HIGH);
  } else {
    digitalWrite(10, LOW);
  }
}
// V11 = Apple TV
BLYNK_WRITE(V11) {
  if (param.asInt() == 1) {
    digitalWrite(11, HIGH);
  } else {
    digitalWrite(11, LOW);
  }
}

void prova(){
  Serial.println(String(hour()) + ":" + minute() + ":" + second());
  if(second()%2==0){
    Serial.println("a");
      Blynk.virtualWrite(llum1, 1);
    } else {
      Blynk.virtualWrite(llum1, 0);
      }
 }

void setup() {
  pinMode(SensorLlum, INPUT);
  pinMode(llum1, OUTPUT);
  pinMode(AppleTV, OUTPUT);
  pinMode(projector, OUTPUT);
  pinMode(llum2, OUTPUT);

  // Debug console
  Serial.begin(115200);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);

  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

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

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

That’s how Blynk is designed to function.
A Blynk.virtualwrite does not trigger the corresponding BLYNK_WRITE() function. This is done to avoid an endless loop situation.

You’re also doing strange things with the aliases that you’ve created for your virtual ins. You’re using the alias in some places, but the V number elsewhere.

Pete.

Thanks for replying, Pete.

I had not idea about that, thanks for letting me know.
True, I didn’t use the aliases I set because I was testing if that would fix the problem.

Is there a way I could create a function to trigger both Blynk.virtualWrite and digitalWrite?
I tried the following code but it only works for turning it on, not off:

void turnOn(String pin){
    Blynk.virtualWrite(pin, 1);
    digitalWrite(pin, HIGH);
 }

void turnOff(String pin){
   Blynk.virtualWrite(pin, 0);
   digitalWrite(pin, LOW);
}

Here’s the error I’m getting:

I think that’s because you’re using a String, not an integer in your parameter.

Pete.

Correct, that was it. Thanks Pete.

1 Like