Simple ESP32 project with 2 LED won't work!

Device: ESP32
Communication: wifi
Purpose: switch on/off two LEDs with app and web interface.
Phone: android 12.0
Blynk server region: India
Blynk Library verion: 1.3.2

Managed to get the device online. Web interface as well as app interface are in sync with each other.

  • The intention is to switch on and off LEDs connected to D26 and D33 on the ESP32 board.
  • Button 1 doesn’t seem to be working.
  • Button 2 switches on an internal blue LED.
  • Serial monitor keeps printing message. " : 2 is reserved" which O am not printing intentionally.
  • I am using Datastream 1 and 2.
  • See the images attached.
  • Why won’t it correctly operate D26 and D33 on the board?

Code:

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "..." // details are correctly given in the code
// #define BLYNK_DEVICE_NAME "..." // details are correctly given in the code
#define BLYNK_TEMPLATE_NAME "..." // details are correctly given in the code
#define BLYNK_AUTH_TOKEN "..." // details are correctly given in the code


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


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.

//Home Wifi Network
char ssid[] = "...";  // details are correctly given in the code
char pass[] = "...";  // details are correctly given in the code

BlynkTimer timer;


#define led1_pin 26
#define led2_pin 33


int led1_state = 0;
int led2_state = 0;
 
#define button1_vpin    V1
#define button2_vpin    V2

BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
}
 
BLYNK_WRITE(button1_vpin) {
  led1_state = param.asInt();
  digitalWrite(led1_pin, led1_state);
  Blynk.virtualWrite(led1_pin,led1_state);
  Serial.print("ledpin1 =");
  Serial.print(led1_pin);
  Serial.print(" : ");
  Serial.println(led1_state);
}
 
BLYNK_WRITE(button2_vpin) {
  led2_state = param.asInt();
  digitalWrite(led2_pin, led2_state);
  Blynk.virtualWrite(led2_pin,led2_state);
  Serial.print("ledpin2 =");
  Serial.print(led2_pin);
  Serial.print(" : ");
  Serial.println(led2_state);

}
 
void setup()
{
 
  Serial.begin(115200);
  
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);
  
  Blynk.begin(auth, ssid, pass);
 
}
void loop()
{
  Blynk.run();
  timer.run();
  
  listen_push_buttons();
}
 
void listen_push_buttons(){
      digitalWrite(led1_pin, led1_state);
      delay(200);
      Blynk.virtualWrite(button1_vpin, led1_state); 

      digitalWrite(led2_pin, led2_state);
      delay(200);
      Blynk.virtualWrite(button2_vpin, led2_state); 

    }

I’d suggest that you delete this line from your void loop…

You also need to stop doing this…

in BLYNK_WRITE(button1_vpin) and the corresponding command in BLYNK_WRITE(button2_vpin)

Pete.

Thank you, Pete!
that makes the code much nicer. Sorry, I just started playing with Blynk, not familiar with the syntax.
The main issue however still remains. I want to set pin 26 and pin 33. Instead first button doesn’t seem to be doing anything and second button sets internal LED, both fail to switch on respective intended pins. why is that? and how can I correct it?

and what does serial monitor message “: 2 is reserved” repeating with a blank line following it mean.

First of all, you should post your revised code, and your serial monitor output (post text rather than screenshots, and use triple backticks).

Your datastream screenshot doesn’t show what Max value you’ve defined. Is this “1” ? If not then it should be.

How are your LEDs wired? Are you using current limiting resistors, and if so what value, and is the anode or the cathode connected to the pin? What is the other side of the LED connected to?

Pete.