NodeMCU8266 + Blynk issue

Hi everybody, I’m a new member ^^. I’m trying to do “Smart Gardening” project with NodeMCU ESP8266 UART CH340 and Blynk app. I have a problem when I use VirtualPin instead of DigitalPin to control 4 relays. I can connect to NodeMCU and control with Blynk app after uploading code, but when I unplug a usb cable and plug 5V power or plug again usb cable to laptop, relay4 ON and I can’t use Blynk to control, it shows that NodeMCU is disconnected. I connected 4 relay to GPIO2, GPIO12-14. How can I fix this issue ? Here’s my code :

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>               
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial

#define relay1 D6
#define relay2 D7
#define relay3 D5
#define relay4 D4

char auth[] = "jWkG2JD7zD0bjWG_fcRK4jstZVmdAPTW";
char ssid[] = "PhanLong";
char pass[] = "Long123456";
LiquidCrystal_I2C lcd(0x27,20,4);

BLYNK_CONNECTED(){
  Blynk.syncAll();
}

BLYNK_WRITE(V10) {
  int pumpstat = param.asInt();
  digitalWrite(relay1,pumpstat);
  if(pumpstat == 0){
    lcd.setCursor(8,0);
    lcd.print("OFF"); 
  }
  else{
    lcd.setCursor(8,0);
    lcd.print("ON ");       
  }
}

BLYNK_WRITE(V11) {
  int relay1stat = param.asInt();
  digitalWrite(relay2,relay1stat);
  if(relay1stat == 0){
    lcd.setCursor(8,1);
    lcd.print("OFF"); 
  }
  else{
    lcd.setCursor(8,1);
    lcd.print("ON ");       
  }
}

BLYNK_WRITE(V12) {
  int relay2stat = param.asInt();
  digitalWrite(relay3,relay2stat);
  if(relay2stat == 0){
    lcd.setCursor(8,2);
    lcd.print("OFF"); 
  }
  else{
    lcd.setCursor(8,2);
    lcd.print("ON ");       
  }
}

BLYNK_WRITE(V13) {
  int relay3stat = param.asInt();
  digitalWrite(relay4,relay3stat);
  if(relay3stat == 0){
    lcd.setCursor(8,3);
    lcd.print("OFF"); 
  }
  else{
    lcd.setCursor(8,3);
    lcd.print("ON ");       
  }
}

void setup()
{

  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(relay3,OUTPUT);
  pinMode(relay4,OUTPUT);
  digitalWrite(relay1,LOW);
  digitalWrite(relay2,LOW);
  digitalWrite(relay3,LOW);
  digitalWrite(relay4,LOW);
  lcd.begin();
  lcd.backlight();         
  lcd.setCursor(0,0);             
  lcd.print("BOM :"); 
  lcd.setCursor(0,1);
  lcd.print("MODE 1 : ");           
  lcd.setCursor(0,2);    
  lcd.print("MODE 2 : ");          
  lcd.setCursor(0,3);
  lcd.print("MODE 3 : ");
 
}

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

@LongPhan please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

Thank you ^^ , I edited.

What does your serial monitor and physical LCD display show?

Pete.

@PeteKnight My LCD use I2C module and I connected SCL to GPIO5, SDA to GPIO4 but it don’t show anything, I’ll review 5V and GND pin later. I’ll use serial monitor too and reply you later ^^. Because when I use DigitalPin, it’s run normally, so I want to add LCD to make the code longer. And I replace Digital to Virtual but I just test the relay state.

I think you’re probably missing the wire.begin() and lcd.init() commands.

If that doesn’t fix it then try running an I2c scanner sketch to ensure that the LCD is actually using the 0x27 address.

Pete.

In my other projects before, I used code like that and it worked so I think that the problem in the power pin, maybe I have some mistake. But am I missing some code in Virtual ?

The first thing to do is to see what the serial monitor says when the device boots.

Pete.

Ok I’ll test this and mess to you soon. Thank you :smiley:

@PeteKnight Thanks for your help ^^. I’ve fixed this issue. Just change pin GPIO2 to GPIO16 and everything works perfectly :smiley: