Can someone help me with my code?

Mission not accomplished yet :no_entry: You’ve posted code with lots of modifications. Attention to details is one of the things we are going to achieve with this “interactive tutorial”

I also don’t see the status in the Serial Monitor, saying:
Connected to Blynk
or something similar

    /**************************************************************
     * Blynk is a platform with iOS and Android apps to control
     * Arduino, Raspberry Pi and the likes over the Internet.
     * You can easily build graphic interfaces for all your
     * projects by simply dragging and dropping widgets.
     *
     *   Downloads, docs, tutorials: http://www.blynk.cc
     *   Blynk community:            http://community.blynk.cc
     *   Social networks:            http://www.fb.com/blynkapp
     *                               http://twitter.com/blynk_app
     *
     * Blynk library is licensed under MIT license
     * This example code is in public domain.
     *
     **************************************************************
     * This example runs directly on ESP8266 chip.
     *
     * You need to install this for ESP8266 development:
     *   https://github.com/esp8266/Arduino
     *
     * Change WiFi ssid, pass, and Blynk auth token to run :)
     *
     **************************************************************/
    
    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "34c49****************3";
    
    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth, "P*****e", "t********s");
    }
    
    void loop()
    {
      Blynk.run();
    }

OK, connected board. flashed LED with the above code. No serial monitor enabled on example code so I cannot send what it says.

These lines should get Serial Monitor printing the connection status. Could you please double-check?
It was printing something using your previous code. So it should work the same

This is what serial print shows:

[25803] Connecting to P******e
[29826] Connected to WiFi
[29826] Blynk v0.3.0
[29826] Connecting to cloud.blynk.cc:8442
[29962] Ready (ping: 0ms).

Great! So Blynk IS working. :tada:
Now you can blink LED connected to D2 (I assume) with the button in the app attached to D2.

Next: Mission 2:
Control LED, connected to pin D2 on your ESP8266 using Button Widget attached to Virtual Pin V1. But before that, we need to understand how Virtual Pins work. If they do work…

Use your code as a foundation. Look at the GetData example. You’ll see that this code is very similar to yours.

  1. Main difference is that it was written for a different connection type (Ethernet Shield)
  2. Secondly, you’ll see this construction:
BLYNK_WRITE(V1)
{
  BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
}

Since Button widget is sending HIGH(1) or LOW(0), which is neither a String nor Double, you should use: param.asInt() Correct?

So your code should look like this:

BLYNK_WRITE(V1)
{
   BLYNK_LOG("Got a value: %s", param.asInt());
}

Try incorporate this construction in your code and:

  • Post your code
  • Try pressing the button in the app
  • Post your Serial Monitor

Notice, that we are still using only examples, not your initial code.

This is where things fall apart. However, adding that BLYNK_LOG seemed to make a difference. First, here is the code:

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "34c499cba10046a38045279e5ba3c973";
    //const int LED_PIN = 2; //sets GPIO pin number
    int widgetState; //int used to relay widget state
    //#define BLYNK_PIN V1 // sets virtual pin used
    //#define BLYNK_DEBUG // Optional, this enables lots of prints
    //#define BLYNK_PRINT Serial
    
    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth, "Paulie", "twofieros");
      pinMode(2, OUTPUT);  //define LED pin as output
    }
    
    //This constructions is used to get a value from a Widget in the app
    //which WRITES values to Virtual Pin BLYNK_PIN:
    BLYNK_WRITE(V1)
    {
      BLYNK_LOG("Got a value: %s", param.asInt());
      // You can also use: asInt() and asDouble()
      //int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
    
       if (param.asInt() == 1) {          //if Button Widget value is 1 (HIGH)...
          Blynk.virtualWrite(2, HIGH);//switch your LED ON
       } else {                         //otherwise..
          Blynk.virtualWrite(2, LOW);   //switch your LED OFF
       }
    }
    void loop()
    {
    Blynk.run();
    //digitalWrite(LED_PIN, widgetState);
     if (2 == HIGH) {
      Serial.println("LED on");
    }else{
      Serial.println("LED off");
     }
    }

Now the serial monitor.
[29071] Connecting to Paulie
[33093] Connected to WiFi
[33094] Blynk v0.3.0
[33094] Connecting to cloud.blynk.cc:8442
LED off
LED off
LED off
LED off
way down the line
LED off
[38142] Connecting to cloud.blynk.cc:8442
LED off
[38231] Ready (ping: 0ms).
LED off
then further down the line
LED off
[40942] Got a value:
LED off
LED off
as soon as I changed the widget state on the app it seemed to crash
LED off
[42042]
Exception (28):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000001 depc=0x00000000

ctx: cont
sp: 3ffea650 end: 3ffeaa50 offset: 01a0

stack>>>
3ffea7f0: 00000008 00000001 00000000 3ffea8d0
3ffea800: 3ffea8e0 40100188 3ffeaaa8 3ffe8c39
3ffea810: 3ffe9694 00000001 3ffea840 40205940
3ffea820: 3ffeaaa8 00000001 4023f58a 40206b6d
3ffea830: 3ffe9694 3ffe8c39 3ffeaaa8 4020252d
3ffea840: 20746f47 61762061 3a65756c 40104c20
3ffea850: 4000050c 3fffc278 40104ef0 3fffc200
3ffea860: 00000022 02809a92 3fff2c50 40104ce0
3ffea870: 402073aa 00000030 0000001c ffffffff
3ffea880: 40202c8d 3ffe96c8 3ffea9a0 00000005
3ffea890: 00000000 00000000 00080000 ffdfffff
3ffea8a0: 0000000a 3fffc6fc 00000001 00000000
3ffea8b0: 3ffe96c8 3ffea9a0 00000005 00000001
3ffea8c0: 3ffea8e0 3ffea8d0 00000004 4020101c
3ffea8d0: 3ffea996 00000001 00000001 00000009
3ffea8e0: 00ff0000 ff000000 3ffe9694 4020102c
3ffea8f0: 3ffea994 fffffffb 3ffea930 40202608
3ffea900: 402109a1 3fff4890 00000001 4020102c
3ffea910: 40209dce 3ffea990 00000001 40202c1e
3ffea920: 402048f1 3fff4838 3fff4890 3ffea990
3ffea930: 3ffea995 00000001 00000001 402044ea
3ffea940: 00000001 00000000 3ffe9694 3ffea995
3ffea950: 3ffea996 3ffea990 40201fda 3ffe9a30
3ffea960: 00000005 3ffea990 40201fda 3ffea990
3ffea970: 3ffea990 00000001 3ffe9694 40202e46
3ffea980: 00000000 3ffe96b4 3ffe9694 40202c94
3ffea990: 31007776 00003100 3ffe9694 3ffe96b4
3ffea9a0: 06257d14 00000000 3ffe9694 3ffe96b4
3ffea9b0: 00000006 00000006 40201fda 3ffe9a30
3ffea9c0: 3ffeaaa8 00000001 3ffe8fcc 00000001
3ffea9d0: 00000000 3ffe96b4 3ffe9694 4020302d
3ffea9e0: 3fffdc20 00000007 3ffeaaa8 40206261
3ffea9f0: 3ffe8fcb 00000000 3ffeaaa8 40206261
3ffeaa00: 3ffe8d0f 0000000a 3ffeaaa8 40206bf4
3ffeaa10: 00000000 00000000 00000016 3ffeaa7c
3ffeaa20: 3fffdc20 00000000 3ffeaa74 40202643
3ffeaa30: 00000000 00000000 3ffeaa74 40202056
3ffeaa40: 00000000 00000000 3ffe9a30 40100378
<<<stack<<<
��_�c_��

This is progress actually, before I was never able to get a change in the serial monitor with the app.

And now I just saw your notice, that we are still using only examples, not your initial code let me go back and try this again. I’ll still post this in case it shows something for you or anyone else.

Could you just NOT USE your old code and stick to the instructions? Otherwise, it doesn’t make any sense!

So frustrating. Here is my code.

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "34c499*******************3";
    
    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth, "********", "*********");
    }
    BLYNK_WRITE(V1)
    {
      BLYNK_LOG("Got a value: %s", param.asInt());
      // You can also use: asInt() and asDouble()
      //int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
    if (param.asInt() == 1){
      Blynk.virtualWrite(2, HIGH);
    }else{
      Blynk.virtualWrite(2, LOW);
    }
    }
    
    
    void loop()
    {
      Blynk.run();
    }

here is serial monitor. It connects but then gibberish and the board locks up, drawing excess power. I didn’t put serial prints in to keep it simple for now. I’m not even sure I would get the Blynk prints right anyway.

[238] Connecting to Paulie
[4262] Connected to WiFi
[4262] Blynk v0.3.0
[5001] Connecting to cloud.blynk.cc:8442
[10002] Connecting to cloud.blynk.cc:8442
[10097] Ready (ping: 1ms).
[13658] 
Exception (28):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000001 depc=0x00000000

ctx: cont 
sp: 3ffea640 end: 3ffeaa40 offset: 01a0

>>>stack>>>
3ffea7e0:  00000008 00000001 00000000 3ffea8c0  
3ffea7f0:  3ffea8d0 40100188 3ffeaa98 3ffe8c39  
3ffea800:  3ffe9688 00000001 3ffea830 00000001  
3ffea810:  3ffeaa98 00000001 4023f576 40206b59  
3ffea820:  3ffe9688 3ffe8c39 3ffeaa98 4020252d  
3ffea830:  20746f47 61762061 3a65756c 00080020  
3ffea840:  40104b5d 3fff1800 00000147 3ffec68c  
3ffea850:  00000000 00d06799 3fff2c40 40104ce0  
3ffea860:  3ffec650 00000000 00000000 3ffec674  
3ffea870:  00d06799 4010513e 00000100 3ffec650  
3ffea880:  7fffffff 3ffec650 00000001 00000001  
3ffea890:  0000000a 00d06799 00002200 3ffec65c  
3ffea8a0:  4000050c 3fffc278 40104ef0 00000001

… more gibberish continues until I turned it off.

I get that param is where the value is stored as a string. What confuses me is all the stuff in void setup which runs once. I’m imagining that what I setup in void setup is sent to Blynk server for processing. I just keep wanting to put all the function in void loop but the documentation says not to use any Blynk. command in void loop or I will get booted from the Blynk server. Do I have that logic correct? Can I call param in void loop and get the correct value? My guess is no and that is stored only on the server and out of reach from the programing on the local board.

OK, I’ve been on the phone with my parents sorting this out. I have the BLYNK_LOG working now. There was an issue with the suggested code. You had suggested:

BLYNK_LOG(“Got a value: %s”, param.asInt());

That doesn’t work. It only worked with param.asStr(). I had to change %s to %i for it to work. The correct code is below. This returns either 0 or 1. Perhaps the supporting documentation needs to explain that change. It’s apparently common in C++ but I was not aware of what was going on or why the change.

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "34c499*******************3";
    
    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth, "Paulie", "t*********s");
    }
    BLYNK_WRITE(V1)
    {
      BLYNK_LOG("Got a value: %i", param.asInt());
    }
    
    void loop()
    {
      Blynk.run();
    }

I’m now going to try to write param to a pin. I hope I have this finally figured out.

Sorry, I missed it, my bad. Glad you found it out.

Now, since you finally got the output from Virtual Pin, you can start using it for your needs. Change this part of the code:

BLYNK_WRITE(V1)
{
  BLYNK_LOG("Got a value: %i", param.asInt());
}

to this (don’t change anything else):

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); //1 or 0 will be written to this variable

  if (pinValue == 1){ // If Button Widget is pressed
      digitalWrite(2, HIGH);
  } else {
      digitalWrite(2, LOW);
  }

}

You might also need to set of your pin D2 pinMode to OUTPUT in void setup()

If you are talking about BLYNK_WRITE() – it’s not part of the void setup(). It’s a different entity. You can place it after void loop() if you want

This is very correct. Avoid putting stuff to void loop()

OK, I got that far at the same time you replied. Here is the latest code.

void setup()
{
  Serial.begin(9600);
  pinMode (2, OUTPUT);
  Blynk.begin(auth, "Paulie", "t*********s");
}
BLYNK_WRITE(V1)
{
  int button = param.asInt();
  BLYNK_LOG("Got a value: %i", button);
  if (button == 1)
{
  digitalWrite(2, HIGH);
  BLYNK_LOG("HIGH");
}else{
  digitalWrite(2, LOW);
  BLYNK_LOG("LOW");
 }
}

now I’m going to try to incorporate more. Just keeping you updated as I go along and sharing what I find for anyone else following along.

OK, I got it working. Boy, did it take baby steps. It sure is cool to see it working as planned. Thank you Pavel for your patience and guidance.

For anyone else who was following this thread or who wants to make what I did. Here is my final code.

//This program is a three-way switch for Blynk and a real switch.
//This was written for ESP8266. tested and confirmed to work.
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
int button;  //global value for blynk widget

void setup()
{
  Serial.begin(9600);
  pinMode (2, OUTPUT); //enable pin for lamp
  pinMode (0, INPUT); //enable pin for switch
  Blynk.begin(auth, "ssid", "password");
}
BLYNK_WRITE(V1)
{
  button = param.asInt();
  BLYNK_LOG("Got a value: %i", param.asInt());
  if (button == 1)
{
  BLYNK_LOG("HIGH");
}else{
  BLYNK_LOG("LOW");
 }
}


void loop()
{
  Blynk.run();
  if (button == digitalRead(0))
  {
  digitalWrite(2, HIGH);
  Serial.println("lamp on");
}else{
  digitalWrite(2, LOW);
  Serial.println("lamp off");
}
}

I’m now going to construct the final circuit and install it into my 3d printed lamp. I learned a lot from this project. I hope to make many more using Blynk. I’m one happy kickstarter backer!

You can substitute all the BLYNK_LOG with regular Serial.print
When you are done - remove all the prints for better performance

Thank you Pavel. I will do that.

Hi Twofieros,
Thank you for your Code and I was also looking for a similar solution. I tried yours and it almost works. Now I can control the ESP’s led both from app and from the Switch connected with ESP
But I want to see the status of the ESP switch using the apps Display icon LED. i.e., The LED icon has to be ON or OFF reflecting Blynk_LOG’s Low and High status.
Please suggest the changes.
Thank You and Regards.

I plan to add that also. I’m waiting on some more parts to show up for my circuit. I’ll see if I can find some time soon to get that additional feature working soon. I’ll post the code here when I do. I’d love it if you could show what you made with it. I will show my final project when it’s all done.

I wrote the code to include an LED widget. It was pretty simple to add. It uses Virtual Pin 2 to save the status and push it to the dashboard on the phone. Here is the code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//setup instructions:  This runs on ESP8266-01. GPIO0 is mechanical switch. GPIO2 is output relay.
//Blynk setup: Make a button widget for Virtual Pin 1. Make an LED setting widget for virtual pin 2.
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "34c49*************************3";
int button;  //global value for blynk widget

void setup()
{
  Serial.begin(9600);
  pinMode (2, OUTPUT); //enable pin for lamp
  pinMode (0, INPUT); //enable pin for switch
  Blynk.begin(auth, "P******e", "********");
}
BLYNK_WRITE(V1)
{
  button = param.asInt(); //assign button to widget value
  BLYNK_LOG("Got a value: %i", param.asInt());
  if (button == 1)
{
//  digitalWrite(2, HIGH);
  BLYNK_LOG("HIGH");
}else{
//  digitalWrite(2, LOW);
  BLYNK_LOG("LOW");
 }
}


void loop()
{
  Blynk.run();
  int lamp = 2;
  int relay = 2; //pin number for output relay
  if (button == digitalRead(0))
  {
  digitalWrite(lamp, HIGH);
  Blynk.virtualWrite(V2, 1); //send value to virtual pin 2
  Serial.println("lamp on");
}else{
  digitalWrite(lamp, LOW);
  Blynk.virtualWrite(V2, 0); //send value to virtual pin 2
  Serial.println("lamp off");
}
}

Hey twofieros,
Do you have a circuit diagram for your project?
Can I have a 3 way switch for every output pin?

Thank you

Hello, I’m trying to follow this example to turn a relay on and off with an Uno + Ethernet Shield. I’ve tried quite a few variations, and I have inserted print statements so I see it’s going all the way through the code. But I can’t get reliable signals from Blynk to my relay to turn it on and off. It works - both ON and OFF work. But not every time. Maybe half the time, there is no response. Can you help?

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
int digitalPin=9;
int value;

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

void setup()
{
  Serial.begin(9600);
  pinMode(digitalPin, OUTPUT);
  Blynk.begin(auth);
}

BLYNK_WRITE(V1){
    value=param.asInt();
    BLYNK_LOG("Got a value: %i", param.asInt());
    if (value==1){       
        BLYNK_LOG("HIGH"); 
    } else {
        BLYNK_LOG("LOW"); 
    }
}
void loop() {
    Blynk.run();
    if (value == 1)
  {
    digitalWrite(digitalPin, HIGH);
    Serial.println("relay on");
} else{
    digitalWrite(digitalPin, LOW);
    Serial.println("relay off");
}
}