Write Outputs

Hello, i dont understand why its not working. I’m using a WeMos D1 Mini. To me it seems like Blynk is overwriting my output status. How can i change that?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Blynk.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXX";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXX";
char pass[] = "XXXX";
int pinValue = 0;

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
}

void setup()
{
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
Blynk.run();

  if (pinValue == 1){
    digitalWrite(D8, HIGH);
    }
  else
    digitalWrite(D8, LOW);
}

You cant put on “loop”. Read the documentation and Will understand.
But, try Just like that :point_down:

#include <BlynkSimpleEsp8266.h>
#include <Blynk.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXX";

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

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1) {
    digitalWrite(D8, HIGH);
  }
  else
  {
    digitalWrite(D8, LOW);
  }
}
void setup()
{
  Blynk.begin(auth, ssid, pass);
pinMode(D8, OUTPUT);
}

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

Read this, Will help you too!

Well, thanks for your reply.
I tried the program, but it still does not work.
I don’t get it any more. I know that I can directly write outputs with blynk, but if I do that, the output is HIGH right form startup until I give an input with blynk. I tried all outputs as some pins are HIGH at start but it did not help. Until now, blynk has more been a nightmare to me than anything useful. I don’t want Blynk to be that and I really want to use blynk but it never works for me :frowning:. It’s so frustrating…

Your original code contained two major problems that would stop it working, plus the void loop issue that isnt good practice with Blynk.

Issue 1 - No pinMode declaration
When you are using digitalWrite commands, you first need to add a pinMode statement to your void setup, to initialise the pin as an output.

Issue 2 - Variable Scope
You’ve declared pinValue as an integer variable type at the top of your code, and assigned a default value of 0. Putting this declaration at the top og your coded makes the variable global, and accessible from anywhere within your code:

int pinValue = 0;

However, this line of code in BLYNK_WRITE(V1) re-declares a local copy of pinValue which is only available within the BLYNK_WRITE(V1) function:

int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

The ‘int’ at the beginning of the line of code means you are re-declaring pinValue as an integer.

Remove the int from that line of code and the value assigned to pinValue within BLYNK_WRITE(V1) will be available within the rest of your code.

The code supplied by @juninhuhlima overcomes both of these issues, and the issue of the cluttered void loop, so should work if used in its entirety.
I’m guessing that you’ve not used the full code when you tested it?

Pete.

To add to this, if you want the pin to start in a particular state, then do a digitalWrite immediately after your pin declaration in the setup

void setup()
{
  Blynk.begin(auth, ssid, pass);
pinMode(15, OUTPUT);
digitalWrite(15, LOW); // start pin D8 as LOW
}

Also, I prefer to use the actual GPIO numbers instead of the “D” numbers that are silk-screened on the board.

Additionally, D8 is GPIO 15, which is a “special” pin. If you are not careful with your wiring, using this pin can affect the boards boot mode. Although, this is more common of an issue when using it as an input.

Thank you so much guy’s!
I am so stupid. I know now what I’ve done wrong. I have selected the wrong board in the Blynk app. When uploading your code @juninhuhlima it finally worked. Now I’m almost done with all I need, but I still have one issue. I read in the blynk docs how to use timers and blynk.connected() and created a code according to how I interpreted the whole thing. I tried to write GPIO13, HIGH when I have no connection and a LOW when it established a connection. Now I have the issue that it’s not working :grimacing:. What do i have to change?

#include <BlynkSimpleEsp8266.h>
#include <Blynk.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXX";
char ssid[] = "XXXXXX";
char pass[] = "XXXXXX";
BlynkTimer timer;

void Event(void){
  if(Blynk.connected()==true){
    digitalWrite(13, LOW);
    Serial.println("Connected");
  } else if ((Blynk.connected()== false)){
    digitalWrite(13, HIGH);
    Serial.println("Not connected");
}
  
  
  }

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1) {
    digitalWrite(15, HIGH);
  }
  else
  {
    digitalWrite(15, LOW);
  }
}
void setup()
{
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  timer.setInterval(2000L, Event);
pinMode(15, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(15, LOW);
digitalWrite(13, HIGH);
}

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

Best regards and thank you for your help!

There is a topic that shows exactly how to do that.

Try read this :point_up_2: