1

I'm using Blynk along with a WeMos D1 R2 and i can change modes through an ON/OFF button.

I can succesfully change mode and make my application go from mode 1 (irrigation) to mode 2 (Fertilization), but through the code i manage to switch from mode 2 to mode 1(after my code does what it is meant to do).

However, i cannot make the button show up as being in mode 1 again, if the application is working properly in mode 1. Any ideas on how to update button condition?

My code follows(below the code you can see the app and the button Using the V1 virtual pin):

void setup()
{
 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
 Blynk.begin(auth, ssid, pass);
 pinMode(Relay1, OUTPUT); // Setup Relay pin as output pin
 pinMode(Relay2, OUTPUT); // Setup Relay pin as output pin
//pinMode(led_pin, OUTPUT);
 pinMode(sensor_pin, INPUT);
 digitalWrite(Relay1, LOW);
 digitalWrite(Relay2, LOW);
 digitalWrite(Relay1,HIGH);
 digitalWrite(Relay2,HIGH);
}
BLYNK_READ(V8)
{
 Blynk.virtualWrite(8,level); // virtualpin 8 distance
}
BLYNK_WRITE(V1) 
{
 pinValue = param.asInt(); 
}
BLYNK_READ(V5)
{
 Blynk.virtualWrite(5,RH); // virtualpin 8 distance
}
BLYNK_READ(V6)
{
 Blynk.virtualWrite(6,temp); // virtualpin 8 distance
}
void loop()
{
 Blynk.run();
 timer.run();
 if (pinValue==0)
 { 
 // some code here
 }
 else //(digitalRead(sensor_pin) == 1)
 {
 // some other code
 }}
else {
 // a lot of code here
 pinValue=0; // here i set pinValue to 0 again in order to return to mode1
}
}
BLYNK_READ(V1)
{
 Blynk.virtualWrite(1,pinValue); // virtualpin 1 state
}
//End of program

The button in the app Button is on top left

jsotola
1,5342 gold badges12 silver badges20 bronze badges
asked Jan 28, 2018 at 1:04

1 Answer 1

2

BLYNK_READ() is documented as:

BLYNK_READ(vPIN)

BLYNK_READ defines a function that is called when device is requested to send it’s current value of Virtual Pin to the server.

That means it is called when the app or the Blynk server requests a state from the hardware/sketch, such as when the app first connects or reconnects to the Blynk server, or when the hardware reconnects to the Blynk server due to some network problem.

But if you just want to update the virtual pin V1 at an arbitrary time in your sketch, just call:

Blynk.virtualWrite(V1, pinValue);

at the point in your code where you want to "change" the button's current state. Not in the BLYNK_READ() function, but in your main code, such as:

[...]
else {
 // a lot of code here
 pinValue=0; //here i set pinValue to 0 again in order to return to mode1
 Blynk.virtualWrite(V1, pinMode);
}
[...]
answered Jan 28, 2018 at 2:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.