Blynk device won't go online after adding LCD I2C codes in my sketch

Hello, just an amateur programmer here, my code seems to be working fine until I added these line of codes →

lcd.begin();
lcd.backlight();
lcd.print("Hello world!");

The LCD still prints the string but Blynk won’t let me device go online.

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "censored"
#define BLYNK_DEVICE_NAME "LED"
#define BLYNK_AUTH_TOKEN "censored"

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial
#define IR 12
#define SERVO 6

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>


// Your WiFi credentials.
char auth[] = BLYNK_AUTH_TOKEN;
// Set password to "" for open networks.
char ssid[] = "PLDTHOMEFIBR_c6kdB";
char pass[] = "PLDTWIFIkmnM4";

ESP8266 wifi(&EspSerial);
BlynkTimer timer;
WidgetRTC rtc;
Servo servo;
LiquidCrystal_I2C lcd(0x27,20,4);

int interval = 0;
int cHour;
int cMins;
int alarmMins;
int alarmHour;
int statusIR;
String alarmTime;
String alarmHourD;
String alarmMinsD;
String currentHour;
String currentMins;
boolean alarmOn = false;
boolean shouldSend = false;
boolean tookMed = false;
boolean takeMed = false;
boolean buzzer = false;
boolean openServo = false;

//clock-a-roonie, call hour(), minute()
void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  //currentHour = String(hour(), BIN);
  //currentMins = String(minute(), BIN);
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  
  cHour = hour();
  cMins = minute();
  
  if (takeMed == true) {
    Serial.println("WANG WANG WANG!");
    Blynk.logEvent("alarm", "TAKE PILL");
    alarmOn = false;
    takeMed = false;
  }
  
  Serial.println(String(cHour));
  Serial.println(String(cMins));
  // Send time to the App
  Blynk.virtualWrite(V2, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V3, currentDate);
}

void buzzerCheck() {
  if (buzzer == true) {
  noTone(2);
  buzzer = false;
  }
}

void setServo() {
  if (openServo == true) {
  servo.write(0);
  delay(5000);
  noTone(2);
  openServo = false;
  }
}

//sync stuff
BLYNK_CONNECTED() {
  rtc.begin();
}

void print() {
    alarmHour = cHour + interval;
    alarmMins = cMins;
    Serial.println("Alarm:");
    Serial.print(alarmHour);
    Serial.print(alarmMins);
    delay(1000);
}

//this for interval datastream button reader (how many hours is the interval) lowkey main code
BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); //gets the value of the button
  boolean isPressed;
  
  //startButton = true;
  if (pinValue == 1) {
    isPressed = true;
    shouldSend = true;
    if (interval <= 24 && isPressed == true) {
      interval++;
      isPressed = false;
      Serial.println("ADDED TO INTERVAL 1");
    } else if (interval > 22 && isPressed == true) {
      interval = 0;
      isPressed = false;
      Serial.println("INTERVAL 1");
    }
     Serial.println("ABCD" + String(interval));
  } 
   else if (pinValue == 0) {
     Serial.println("ABCD" + String(interval));
     isPressed = false;
  }
  else {
    shouldSend = false;
  }
  
  if (shouldSend = true) { //alarm setting logic
    alarmOn = true;
    String intervalBin = String(interval, BIN);
    int getHour = hour();
    int getMinute = minute();
    int preAlarmHour = getHour;
    if (preAlarmHour >= 24) {
      int x = preAlarmHour;
      preAlarmHour = x - 24;
    } else {
      //do nothing
    }
    int preAlarmMinute = getMinute + interval;
    
    Serial.println(preAlarmMinute);
  
    alarmHourD = String(hour(), BIN);
    alarmMinsD = String(minute(), BIN);
    alarmHour = preAlarmHour;
    alarmMins = preAlarmMinute;
    Serial.println("Alarm: ");
    Serial.print(String(alarmHour));
    Serial.print(",");
    Serial.print(String(alarmMins));
    shouldSend = false;
  }
    else {
      //do nothing
    }
}

void setup()
{
  pinMode(2, OUTPUT);
  
  // Debug console
  Serial.begin(115200);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  pinMode(IR, INPUT); 
  Blynk.begin(auth, wifi, ssid, pass);
  
  servo.attach(SERVO);
  servo.write(0);
  
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
  timer.setInterval(500L, setServo);
  //timer.setInterval(500L, isSensor);
  timer.setInterval(10000L, clockDisplay);
  
  lcd.begin();
  lcd.backlight();
  lcd.print("Hello World!");
}

void loop()
{
  Blynk.run();
  timer.run();
  if (cHour == alarmHour && cMins == alarmMins) {
    if (alarmOn) {
      takeMed = true;
      //buzzer = true;
      openServo = true;
      servo.write(105);
      tone(2, 1000);
    }
  } else {
    //do nothing
  }
  
  statusIR = digitalRead(IR);
  
  if (statusIR == 1) {
    tookMed = false;
  }
  else
  {
    Serial.println("No");
    tookMed = true;
    print();
  }
}

Your void loop must contain only 2 lines

Blynk.run();
timer.run();

You have to use timer to call your if statement 😜

Your use of serial ports is totally messed-up.
If you’re using an Uno then you need to use a software serial port for you ESP device.
If you’re using a Mega then you need to be connecting your ESP device to Serial1 or Serial2, not Serial.

You should read this…

You also need to eliminate the blocking delays from your code. You should use a timeout timer instead.

Also the RTC methods have changed in Blynk IoT, you should read the documentation.

Pete.

1 Like

Hello, an update, thanks for the people who responded, I fixed the issue by using an Arduino MEGA instead, for some reason the code run smoothly and by taking note what you guys have suggested (cleaning my code in general esp void loop()) I have successfully run the program that way I intended it to do so. Thanks.

1 Like