Lcd not working with node mcu

Hello. I am trying to make an automatic plugpoint using nodemcu. It works properly but the only problem I encountered was that the local Ip of my wifi changed everyday. So I decided to use an i2c lcd with it. the lcd doensnt print anything with this code.

#include <ESP8266WiFi.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

const char* ssid = "TP-Link_3540";
const char* password = "saraf123";

int Switch1 = 13;
int Switch2 = 12;
int Switch3 = 15;
//int led = ;
int i;
WiFiServer server(80);
 
void setup() 
{
  Serial.begin(115200);
  lcd.init();   
  lcd.init();
    lcd.backlight();
  
  
  pinMode(Switch1, OUTPUT);
  digitalWrite(Switch1, HIGH);
   pinMode(Switch2, OUTPUT);
  digitalWrite(Switch2, HIGH);
   pinMode(Switch3, OUTPUT);
  digitalWrite(Switch3, HIGH);
   //  pinMode(led, OUTPUT);
 // digitalWrite(led, HIGH);
       pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
        pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  Serial.println();   // Connect to WiFi network
  Serial.print("Connecting to ");
  lcd.print("connecting to");
  Serial.println(ssid); 
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected"); 
  server.begin(); // Start the server
  Serial.println("Server started"); 
  Serial.print("Use this URL to connect: ");   // Print the IP address
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/"); 

}
 
void loop() 
{
  if ((WiFi.status() == WL_CONNECTED) ) {
    Serial.println("Connected");
   // digitalWrite(led,HIGH);
    digitalWrite(2,LOW);
    }
     if ((WiFi.status() != WL_CONNECTED) ) {
   Serial.println("Reconnecting to WiFi...");
   digitalWrite(2,HIGH);
//   digitalWrite(led,LOW);
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    }
 
  
  WiFiClient client = server.available();   // Check if a client has connected
  if (!client) 
  {
    return;
  } 
  
  while(!client.available())
  {
    delay(1);
  }


  
  String request = client.readStringUntil('\r');   // Read the first line of the request
  Serial.println(request);
  client.flush();

  if (request.indexOf("/gpio/1") != -1)  
  {
    digitalWrite(Switch1, LOW);
  }
  if (request.indexOf("/gpio/0") != -1)  
  {
    digitalWrite(Switch1, HIGH);
  }
 if (request.indexOf("/gpio/2") != -1)  
  {
    digitalWrite(Switch2, LOW);
  }
  if (request.indexOf("/gpio/3") != -1)  
  {
    digitalWrite(Switch2, HIGH);
  }
  if (request.indexOf("/gpio/4") != -1)  
  {
    digitalWrite(Switch3, LOW);
  }
  if (request.indexOf("/gpio/5") != -1)  
  {
    digitalWrite(Switch3, HIGH);
  }
    if (request.indexOf("/gpio/6") != -1)  
  {
      digitalWrite(4,HIGH);
     //   digitalWrite(led,LOW);
     for(i=0;i<=60;i++){
  Serial.println(i);
  delay(1000);
 if (i==60){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/7") != -1)  
  {
      digitalWrite(4,HIGH);
//        digitalWrite(led,LOW);
     for(i=0;i<=3600;i++){
  Serial.println(i);
  delay(1000);
 if (i==3600){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/8") != -1)  
  {
      digitalWrite(4,HIGH);
      //  digitalWrite(led,LOW);
     for(i=0;i<=5400;i++){
  Serial.println(i);
  delay(1000);
 if (i==5400){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/9") != -1)  
  {
      digitalWrite(4,HIGH);
      //  digitalWrite(led,LOW);
     for(i=0;i<=7200;i++){
  Serial.println(i);
  delay(1000);
 if (i==7200){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  // Return the response
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); //  do not forget this one
}

but if i use this code then it seems to display just fine

#include <ESP8266WiFi.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

const char* ssid = "TP-Link_3540";
const char* password = "saraf123";

int Switch1 = 13;
int Switch2 = 12;
int Switch3 = 15;
//int led = ;
int i;
WiFiServer server(80);
 
void setup() 
{
  Serial.begin(115200);
  lcd.init();   
  lcd.init();
    lcd.backlight();
  
lcd.print("connecting to");
  
  pinMode(Switch1, OUTPUT);
  digitalWrite(Switch1, HIGH);
   pinMode(Switch2, OUTPUT);
  digitalWrite(Switch2, HIGH);
   pinMode(Switch3, OUTPUT);
  digitalWrite(Switch3, HIGH);
   //  pinMode(led, OUTPUT);
 // digitalWrite(led, HIGH);
       pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
        pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  Serial.println();   // Connect to WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid); 
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected"); 
  server.begin(); // Start the server
  Serial.println("Server started"); 
  Serial.print("Use this URL to connect: ");   // Print the IP address
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/"); 

}
 
void loop() 
{
  if ((WiFi.status() == WL_CONNECTED) ) {
    Serial.println("Connected");
   // digitalWrite(led,HIGH);
    digitalWrite(2,LOW);
    }
     if ((WiFi.status() != WL_CONNECTED) ) {
   Serial.println("Reconnecting to WiFi...");
   digitalWrite(2,HIGH);
//   digitalWrite(led,LOW);
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    }
 
  
  WiFiClient client = server.available();   // Check if a client has connected
  if (!client) 
  {
    return;
  } 
  
  while(!client.available())
  {
    delay(1);
  }


  
  String request = client.readStringUntil('\r');   // Read the first line of the request
  Serial.println(request);
  client.flush();

  if (request.indexOf("/gpio/1") != -1)  
  {
    digitalWrite(Switch1, LOW);
  }
  if (request.indexOf("/gpio/0") != -1)  
  {
    digitalWrite(Switch1, HIGH);
  }
 if (request.indexOf("/gpio/2") != -1)  
  {
    digitalWrite(Switch2, LOW);
  }
  if (request.indexOf("/gpio/3") != -1)  
  {
    digitalWrite(Switch2, HIGH);
  }
  if (request.indexOf("/gpio/4") != -1)  
  {
    digitalWrite(Switch3, LOW);
  }
  if (request.indexOf("/gpio/5") != -1)  
  {
    digitalWrite(Switch3, HIGH);
  }
    if (request.indexOf("/gpio/6") != -1)  
  {
      digitalWrite(4,HIGH);
     //   digitalWrite(led,LOW);
     for(i=0;i<=60;i++){
  Serial.println(i);
  delay(1000);
 if (i==60){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/7") != -1)  
  {
      digitalWrite(4,HIGH);
//        digitalWrite(led,LOW);
     for(i=0;i<=3600;i++){
  Serial.println(i);
  delay(1000);
 if (i==3600){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/8") != -1)  
  {
      digitalWrite(4,HIGH);
      //  digitalWrite(led,LOW);
     for(i=0;i<=5400;i++){
  Serial.println(i);
  delay(1000);
 if (i==5400){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  if (request.indexOf("/gpio/9") != -1)  
  {
      digitalWrite(4,HIGH);
      //  digitalWrite(led,LOW);
     for(i=0;i<=7200;i++){
  Serial.println(i);
  delay(1000);
 if (i==7200){
     digitalWrite(Switch3, HIGH);
      digitalWrite(Switch1, HIGH);
       digitalWrite(Switch2, HIGH);
       digitalWrite(4,LOW);
    }
  }

  }
  // Return the response
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); //  do not forget this one
}

pls help me to identify what the problem is

why two lcd.init()?

What GPIOs did you use to connect the LCD I2C?

the gpios i used to connect the lcd are :- scl-d1, sda-d2

in the example code from the library, there was two lcd.init given. I first tried with only one but it didnt work so i tried with two it still didnt work.
In the example code, the lcd setup is for a 20/4 lcd so i changed that to 16/2 as i have a 16/2 lcd

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  lcd.print("Ywrobot Arduino!");
   lcd.setCursor(0,2);
  lcd.print("Arduino LCM IIC 2004");
   lcd.setCursor(2,3);
  lcd.print("Power By Ec-yuan!");
}


void loop()
{
}

Depending on the type of board you selected and which library is included in your sketch,
try replacing
lcd.init(); // initialize the lcd
lcd.init();
by
lcd.begin(4, 5); //SDA = GPIO4, SCL = GPIO5

the library im using doesnt have lcd.begin

try this way:

Wire.begin(4,5);

ok...btw what is karma?

Look here:

https://forum.arduino.cc/badges

The use of lcd.init() vs lcd.begin or multiple calls to lcd.init() has nothing to do with this issue.

The issue is the code is attempting to use and control the pin that the Wire library is using for SDA.
The majority of the ESP8266 boards use GPIO 4 for SDA and GPIO 5 for SCL
The nodemcu variant uses these pins.

While neither code example really works,
the code that "works" doesn't mess with the GPIO 4 pin until after "connecting to" has been printed on the LCD.

Keep in mind that on nearly all the esp8266 boards arduino pin numbers and GPIO numbers are not the same.
To use Arduino pin numbers you use Dn like D0, D1, D4 etc...
A naked constant is a GPIO number.
(This pin mapping, use of Dn numbers is unique to the esp platform)
The pins_arduino.h variant file maps the Dn numbers to the GPIO numbers.
The Adafruit boards being one that does not use or create the Dn symbols.
IMO, this was a dumb thing for Adafruit to do to as they could have simply created a 1 to 1 mapping just so the symbols exist to be consistent with all the other esp8266 boards.
I.e. create symbols such that Dn is the same as N example: Dn is 0, D2 is 2 etc...

For the nodemcu variant
SDA uses GPIO 4 which is D2
SCL uses GPIO 5 which is D1

My recommendation would be switch to the hd44780 library and use the hd44780_I2Cexp i/o class.
There are many different "LiquidCrystal_I2C" libraries out there and none of the are actively maintained, which is particularly an issue for the one in the IDE library manager as it currently has some known issues.
The hd44780 library is actively maintained, has some additional capabilities, and is also much faster.
The hd44780_I2Cexp i/o class comes with a diagnostic tool that will test your setup to make sure that everything is working and will print out diagnostic information including which pins are used for the i2c signals.
If you do install the hd44780 library, the first thing you should do is first review the documentation. It is in the Documentation sketch.
Lots of links to information that can be clicked on directly from the IDE including to the hd44780 library Wiki which is here:

Then run the included I2CexpDiag sketch to test that everything is working.

That said, I would encourage you to take note of the 3v vs 5v interfacing issues when using this device.

In your setup I can't tell what voltage you are using for the LCD.
The LCD wants 5v but the nodemcu uses 3v signals on its pins.

Most LCD backpack have pullups on the backpack which pullup the signals to the provided voltage on the i2c header pins.
If you provide 5v (which is what the LCD will need for adequate contrast) that means than the LCD backpack will pull up SDA and SCL to 5v.
This is over the maximum operating voltage for the nodemcu pins.
In this type of situation, you should use a level shifter to interface 3v ic2 to 5v i2c signals.
Not doing so runs the risk of damaging the 3v nodemcu part.

--- bill

hi...thank you all for trying to help me out but idk how the lcd was displaying yesterday and is now working

though i still have one problem...when i turn of my relay, the led on the relay turns off but the relay itself is still turned on. Then if i shake it or knock it, it switches off

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.