Blynk Virtual pin BLYNK_WRITE()

HI i am new on blynk app i have a magnetic door switch connected to a wemos d1 esp 8266 monitoring on the print screen the on off status normally open normally closed .How can i monitor this status thru blynk application i will appreciate if someone could help me . Here is the code that i use. I know after reading blynk forum that is have to do with BLYNK_WRITE() if (param.asInt()) but i don’t know how to put this in my code. Thank you!

#define BLYNK_PRINT Serial
const int buttonPin = D3;
const int ledPin = BUILTIN_LED;

int buttonState = 0;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial state, LED off
  digitalWrite(ledPin, buttonState);
}

void loop() {
  // read button state, HIGH when pressed, LOW when not
  buttonState = digitalRead(buttonPin);

  // if the push button pressed, switch on the LED
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);  // LED on
    Serial.println("ON");
  } else {
    digitalWrite(ledPin, LOW); // LED off
    Serial.println("OFF");
  }
}

There are few ways to achieve this. Please read this http://docs.blynk.cc/#blynk-main-operations-get-data-from-hardware

Since you want to monitor the status using the blynk app you should do something like this

BLYNK_READ(V2)
{
if(buttonState==HIGH)
  {
     Blynk.virtualWrite(2, HIGH);
  }
  else
  {
     Blynk.virtualWrite(2, LOW);
  }
}

This will send a HIGH or LOW signal to the blynk app at the virtual port V2. So if you put a button on the virtual port V2 and set it as push the app will show if it’s On or Off.

The param.asInt() you mentioned before would be used in case you want to control the motor from the app. Something like this:

BLYNK_WRITE(V1)
 {
 if(param.asInt()==1)
  {
     digitalWrite(ledPin, HIGH);
  }
  else
  {
     digitalWrite(ledPin, LOW);
  }
 }

It would check the button state associated to the virtual pin 1 and change the led state

Thank you i will give a try …i just want to monitor when my garage door is true close after presing the relay button on off:-)

1 Like

Just a reminder. This will show you if it is closing or opening. To check if it’s closed already you need some sensor to detect when it’s closed and than you use the same logic to detect the sensor state.

I will try with this magnetic sensor. Thank you!

2 posts were split to a new topic: Need help with virtual pins