[SOLVED] Local LCD not working when using Blynk

I am trying to integrate Blynk into a smart thermostat Im building using my Arduino UNO. I have a local LCD with local buttons to display the temperature and desired temperature, and output to the AC/Heating accordingly.
I am beginning to try to integrate some of the functions on Blynk. Right now just the power on and off with a virtual Blynk button and a virtual Blynk LED.

However, now my local physical LCD is not functioning. It just displays a series of lines. The LCD display is done with the “displayReadings” function. Without the Blynk code, the LCD works fine. Is this something to do with the Blynk timer or other function calls?

Thanks:

Here is my code:

#define BLYNK_PRINT Serial
#include <LiquidCrystal.h>
#include <dht.h>
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

dht DHT;
RTC_DS1307 rtc;


#define DHT11_PIN 8
#define W5100_CS  10
#define SDCARD_CS 4

char auth[] = "***********************************"; //blynk token
BlynkTimer timer;

//timing related variables
unsigned long currentMillis;
unsigned long timeReadDHT = 0;
unsigned long timechangeAC = 0;
unsigned long timeLCD = 0;
unsigned long timeBounce = 0;
unsigned long timeTemp = 0;
unsigned long holdDisplay = 0;
unsigned long timesystemPower = 0;
unsigned long timeRTCcall = 0;
unsigned long timeMotion = millis();
int timerChange = 500;
#define BOUNCE_DURATION 400
#define AntiCyclingHold 200000 //30000=30seconds 60000=1min 180000=3min 300000=5min
bool cycling_timer_flag = LOW;
bool isDaytime = LOW;

volatile int desiredTemp = 24; //desired temperature
boolean AC_on = true; //AC or Heating On
int temp = 0; //temperature variabke
int humi = 0; //humidity variable
int pir = LOW; //PIR variable
bool systemPower = HIGH;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool activityFlag = LOW;
bool daytimeOFF = LOW;


bool SystemOn = LOW; // check if the system should be sent a high signal to turn it on

// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 6, 7, 9);


void setup() {
  //Set up pins
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(13, OUTPUT);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // set up the Serial Monitor
  Serial.begin(57600);

  // Print test message to the LCD.
  lcd.print("DHT test");
  Serial.println("DHT TEST Type,\tstatus");

  testDHT(); // testing DHT sensor

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Blynk.begin(auth);
  // Setup a function to be called every second
  timer.setInterval(500L, myTimerEvent);
}


void loop() {
Blynk.run();
  timer.run(); // Initiates BlynkTimer

  bool hold_flag = LOW;
  currentMillis = millis();
  pir = digitalRead(A0);

  if (pir == HIGH) {
    timeMotion = millis();
  }

  if ((currentMillis - timeMotion) > 1800000) {
    activityFlag = LOW;
  }
  else {
    activityFlag = HIGH;
  }

  RTCfunction();

  //Serial.println((currentMillis - timeMotion));


  if (digitalRead(A2) == LOW && ((currentMillis - timesystemPower) >= 250)) {
    systemPower = !systemPower;
    timesystemPower = millis();
  }

  if (isDaytime == HIGH && activityFlag == LOW) {
    daytimeOFF = HIGH;
  }
  else {
    daytimeOFF = LOW;
  }



  // read temp and humidity
  if ((currentMillis - timeReadDHT) > 2100)
  {
    DHT.read11(DHT11_PIN);
    temp = DHT.temperature;
    humi = DHT.humidity;
    timeReadDHT = millis();
  }




  int changeAC_send = digitalRead(A1);//read button for change between heating and AC

  changeAC_on (changeAC_send); //change between heating and AC

  if ((currentMillis - holdDisplay) >= timerChange) { //delay to display change from AC or Heating longer
    displayReadings(humi, temp, pir, hold_flag); //display on LCD - humidity, temperature, lux, desired temperature
    holdDisplay = millis();
    timerChange = 0;
  }

  if (digitalRead(2) == LOW) {
    upButtonOn();
  }

  if (digitalRead(3) == LOW) {
    downButtonOn();
  }


  if (cycling_timer_flag == HIGH) { //state machine for creating a delay to avoid quick cycling on and off of system
    timeTemp = currentMillis;
    cycling_timer_flag = LOW;
  }

  if ((currentMillis - timeTemp) >= AntiCyclingHold) { //state machine for creating a delay to avoid quick cycling on and off of system
    hold_flag = LOW; // for reading out holding of the temp to the LCD
    if ((temp > desiredTemp) && (AC_on == true)) { //Power On to the SystemOn flag in AC structure
      SystemOn = HIGH;
      cycling_timer_flag = HIGH;
      Serial.println("Turned ON");
    }
    else if ((temp < desiredTemp) && (AC_on == false)) { //Power On to the SystemOn flag in Heat Structure
      SystemOn = HIGH;
      cycling_timer_flag = HIGH;
      Serial.println("Turned ON");
    }
    else {
      SystemOn = LOW; //Power Off
      cycling_timer_flag = HIGH;
      Serial.println("Turned OFF");
    }
  }
  else {
    hold_flag = HIGH;
  }

  if (systemPower == HIGH && daytimeOFF == LOW) {

    //Structure to keep system on or off depending on the SystemOn flag
    if (SystemOn == HIGH) {
      digitalWrite(13, HIGH);
    }
    else if (SystemOn == LOW) {
      digitalWrite(13, LOW);
    }
  }
  else {
    digitalWrite(13, LOW);
  }

  
}

//Function to display readings to LCD
void displayReadings (int h, int t, int pirRec, bool hold_flag_received) {

  if (((currentMillis - timeLCD) > 300) && (systemPower == HIGH || daytimeOFF == HIGH)) { //refresh every 1/3 second

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("T:");
    lcd.print(t);
    lcd.print((char)223);
    lcd.print("C ");
    lcd.print("DesT:");
    lcd.print(desiredTemp);
    lcd.print((char)223);
    lcd.print("C");

    lcd.setCursor(0, 1);
    lcd.print("H:");
    lcd.print(h);
    lcd.print("% ");
    lcd.print("FND: ");
    lcd.print(pirRec);
    lcd.print("  ");
    if ((temp == desiredTemp) && SystemOn == HIGH) {
      lcd.print("-H");
    }

    Serial.print(h, 1);
    Serial.print(",\t");
    Serial.print(t, 1);
    Serial.print(",\t");
    Serial.print(pirRec);
    Serial.println(",\t");

    timeLCD = millis();
  }
  else if (((currentMillis - timeLCD) > 300) && (systemPower == LOW || daytimeOFF == HIGH)) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("POWER OFF");
    timeLCD = millis();
  }
}

//Function to test if temp/humidity sensor is working - only called in setup()
void testDHT () {
  // READ DATA
  lcd.clear();
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      Serial.println("OK,\t");
      break;

    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Can't get reading");
      lcd.setCursor(0, 1);
      lcd.print("from DHT");
      break;

    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Can't get reading");
      lcd.setCursor(0, 1);
      lcd.print("from DHT");
      break;

    default:
      Serial.print("Unknown error,\t");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Can't get reading");
      lcd.setCursor(0, 1);
      lcd.print("from DHT");
      break;
  }
}

//Function to increase desired temperature on button press
void upButtonOn () {
  if ((currentMillis - timeBounce) > BOUNCE_DURATION) {
    Serial.println("Up Pressed");
    desiredTemp++;
    timeBounce = millis();

    timeTemp = (currentMillis - (AntiCyclingHold + 300));
    cycling_timer_flag = LOW;
  }
}

//Function to decrease desired temperature on button press
void downButtonOn () {
  if ((currentMillis - timeBounce) > BOUNCE_DURATION) {
    Serial.println("Down Pressed");
    desiredTemp--;
    timeBounce = millis();

    timeTemp = (currentMillis - (AntiCyclingHold + 300));
    cycling_timer_flag = LOW;
  }

}

//Function to switch from AC mode to Heating mode
void changeAC_on (int changeAC) {

  if (currentMillis - timechangeAC >= 300) {

    if ((changeAC == LOW) && (AC_on == true)) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("HEATING MODE");
      Serial.println("Heat On");
      AC_on = false;
      timechangeAC = millis();
      holdDisplay = currentMillis;
      timerChange = 1000;
    }

    else if ((changeAC == LOW) && (AC_on == false)) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("A/C MODE");
      Serial.println("A/C On");
      AC_on = true;
      timechangeAC = millis();
      holdDisplay = currentMillis;
      timerChange = 1000;

    }

    else {
      timechangeAC = millis();

    }
  }
}

void RTCfunction() {
  if ((currentMillis - timeRTCcall) > 3000) {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    if ((now.hour() >= 9) && (now.hour() <= 22)) {
      isDaytime = HIGH;
    }
    else {
      isDaytime = LOW;
    }
    /*
      Serial.print(" since midnight 1/1/1970 = ");
      Serial.print(now.unixtime());
      Serial.print("s = ");
      Serial.print(now.unixtime() / 86400L);
      Serial.println("d");
    */
    /* calculate a date which is 7 days and 30 seconds into the future
      DateTime future (now + TimeSpan(7,12,30,6));

      Serial.print(" now + 7d + 30s: ");
      Serial.print(future.year(), DEC);
      Serial.print('/');
      Serial.print(future.month(), DEC);
      Serial.print('/');
      Serial.print(future.day(), DEC);
      Serial.print(' ');
      Serial.print(future.hour(), DEC);
      Serial.print(':');
      Serial.print(future.minute(), DEC);
      Serial.print(':');
      Serial.print(future.second(), DEC);
      Serial.println();

    */
    timeRTCcall = millis();
  }
}

BLYNK_WRITE(V0)
{
  //int height = param.asInt();
  //analogWrite(9, height);
int tempPower = param.asInt();
systemPower = bool(tempPower);
Serial.println(tempPower);  
}

void myTimerEvent()
{
bool onNow;
if ((systemPower == HIGH || daytimeOFF == LOW)){
  onNow = HIGH;
}
else{
  onNow = LOW;
}
  //Serial.println(onNow);
  Blynk.virtualWrite(V1, onNow);
  
}

how are the flash / rams are feeling on the uno?
(if you click on compile button in the arduino ide, you can see them values).

i can not compile your sketch, because i do not have that dht lib installed, but i’ll be bound that they are packed like sardines :slight_smile:

Hey, sorry what do you mean how are the flash / rams feeling?

I am getting a low memory warning at compiling. I am at 79% of program storage space and 81% of dynamic memory. Think that is whats causing the LCD issues?

yup, this is what i mean
i’m 99.9% sure this is the problem.
but maybe others should have a look…

the biggest disadvantage to blynk with uno + ethernet module is that right after installing the libs for ethernet and without adding anything else, you’re nearly out of ram / flash :frowning:

try replacing the uno with an esp8266. (wemos d1 mini pro, or i have seen there are esp8266 boards with an integrated oled display… you can experiment with them. you can find more info on @scargill blog about those oled boards…

Okay thanks. Ill have a look at that wifi module or Ill just go and get a Arduino Mega.

1 Like

yes, the mega is also a solution, good point!
than may we mark this topic as solved?

1 Like

also, if you are a kind of curious person, i recommend this excellent article:

1 Like

Instead of trying to determine this with your large sketch… make up a very simple one and test it with Blynk and the LCD.

I can confirm that Blynk and physical LCD’s work just fine together… yes, I am using a MEGA here, but my sketch is also very big and lots of Blynk action constantly going on.

1 Like

but what kind of internet communication are you using? i couldn’t identify any…

WiFi Serial Transceiver (ESP-01) - The lit up red and blue LEDs hiding behind all the wires on the breadboard.

The USB is just for Serial Monitor… as I slowly migrate any required status messages over to the LCD instead (for true battery powered portability when desired).