Standard code with blynk.begin stop my code

Dear all,

The code attached is running fine for both Ardiono Uno and Mega.
However, when I add blynk commands, specially blynk begin, code stops to work. LCD stops, relay stops and so on.
I search many topics about lack of memory and thats why I changed to Mega, external source, change blynk.begin to blynk.config() and blynk.connect() (that I have no experience before or how to code them).

Could you guys help me on that?

• Hardware model: Arduino Uno or Mega, W5100 shield, LCD 16X2 with I2C, DHT11, relay 1 channel

• Smartphone IoS

Code that works:

#include <Wire.h> //INCLUSÃO DE BIBLIOTECA
#include <LiquidCrystal_I2C.h> //INCLUSÃO DE BIBLIOTECA
#include <dht.h> //INCLUSÃO DE BIBLIOTECA

const int pinoRele = 8; //PINO DIGITAL UTILIZADO PELO MÓDULO RELÉ

const int pinoDHT11 = A2; //PINO ANALÓGICO UTILIZADO PELO DHT11
dht DHT; //VARIÁVEL DO TIPO DHT

const int pinoSensorChuva = A1; //PINO ANALÓGICO UTILIZADO PELO SENSOR DE CHUVA
int leituraA1; //VARIÁVEL QUE ARMAZENA O VALOR LIDO NA PORTA ANALÓGICA

int moistpercent;

// Inicializa o display no endereco 0x27
LiquidCrystal_I2C lcd(0x3f,2,1,0,4,5,6,7,3, POSITIVE); //ENDEREÇO DO I2C E DEMAIS INFORMAÇÕES
 
void setup(){
Serial.begin(9600); //INICIALIZA A PORTA SERIAL

  pinMode(pinoRele, OUTPUT); //DEFINE A PORTA COMO SAÍDA
  digitalWrite(pinoRele, HIGH); //MÓDULO RELÉ INICIA DESLIGADO
  
 lcd.begin (16,2); //SETA A QUANTIDADE DE COLUNAS(16) E O NÚMERO DE LINHAS(2) DO DISPLAY. EM SUMA: UMA MATRIZ DE 16 COLUNAS E 2 LINHAS
 lcd.setBacklight(HIGH); //LIGA O BACKLIGHT (LUZ DE FUNDO)

  pinMode(pinoSensorChuva,INPUT); //DEFINE A PORTA COMO ENTRADA
   
  delay(1000); //INTERVALO DE 1 SEGUNDO ANTES DE INICIAR O CÓDIGO
}
 
void loop(){
  lcd.setCursor(0,0); 
  lcd.print("UA:"); 
  lcd.setCursor(3,0); 
  lcd.print(DHT.humidity); 
  lcd.setCursor(5,0); 
  lcd.print(" % "); 
  lcd.setCursor(8,0); 
  lcd.print("TA:"); 
  lcd.setCursor(11,0); 
  lcd.print(DHT.temperature); 
  lcd.setCursor(13,0); 
  lcd.print(" \337C"); 


  DHT.read11(pinoDHT11); //LÊ AS INFORMAÇÕES DO SENSOR
  Serial.print("Umidade = "); //ESCREVE O TEXTO NA JANELA SERIAL
  Serial.print(DHT.humidity); //ESCREVE NA JANELA SERIAL O VALOR DA UMIDADE
  Serial.print(" %  "); //ESCREVE O TEXTO EM SEGUIDA
  Serial.print("Temperatura = "); //ESCREVE O TEXTO NA JANELA SERIAL
  Serial.print(DHT.temperature); //ESCREVE NA JANELA SERIAL O VALOR DE TEMPERATURA
  Serial.println("* Celsius  "); //ESCREVE O TEXTO NA JANELA SERIAL
  delay(2000); //INTERVALO DE 2 SEGUNDOS * NÃO DIMINUIR ESSE VALOR

   leituraA1 = analogRead(pinoSensorChuva); //LÊ O VALOR NA PORTA ANALÓGICA (VALOR LIDO EM BITS QUE VAI DE 0 A 1023 BITS)
  moistpercent=(100-(leituraA1-500)/5.23);
 if(leituraA1 < 761){ //SE A LEITURA FOR MENOR QUE "511", FAZ / O VALOR 511 PODE SER AJUSTADO ENTRE 0 E 1023
    Serial.print(100-(leituraA1-500)/5.23); //ESCREVE O VALOR LIDO NA PORTA ANALÓGICA
    Serial.println(" / UMIDO"); //ESCREVE O TEXTO EM SEGUIDA
    
  
  if (moistpercent >=100) {
  lcd.setCursor(0,1); 
  lcd.print("US:");
  lcd.setCursor(3,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO  "); 
  
   }else{ //SENÃO
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(" ");
  lcd.setCursor(4,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO  ");

   } 
  }else{ //SENÃO
    Serial.print(100-(leituraA1-500)/5.23); //ESCREVE O VALOR LIDO NA PORTA ANALÓGICA
    Serial.println(" / IRRIGAR"); //ESCREVE O TEXTO EM SEGUIDA
    
   if (moistpercent >=100) {
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO   "); 
  
   }else{ //SENÃO
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(" ");
  lcd.setCursor(4,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
  lcd.setCursor(8,1); 
  lcd.print("IRRIGAR");
  }
  delay(100); //DELAY DE 100ms
}

 if (moistpercent<=50){ 
    digitalWrite(pinoRele, LOW); //ENERGIZA O PINO 8 E O RELÉ ATIVA
    Serial.println("RELE LIGADO"); //ESCREVE O TEXTO NA JANELA SERIAL
    }else{ //SENÃO, FAZ
      digitalWrite(pinoRele, HIGH); //ENERGIZA O PINO 8 E O RELÉ DESATIVA
      Serial.println("RELE DESLIGADO"); //ESCREVE O TEXTO NA JANELA SERIAL
    }
    }

Tks all

Edit your code so that it displays correctly:
Blynk - FTFC

Then we’ll tell you what you’re doing wrong and how to fix it.

Pete.

Hi,
Attached as requested.
If Blynk.begin is commented code works fine. if uncommented. LCD stops, relay stops and so on. The ideia is now provide control by Blynk.

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


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 
#include <dht.h> 

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XX";

#define W5100_CS  10
#define SDCARD_CS 4
const int pinoRele = 8; //DIGITAL PIN TO RELAY

const int pinoDHT11 = A2; //ANALOG PIN TO DHT11
dht DHT; 

const int pinoSensorChuva = A1; //ANALOG PIN TO HUMIDITY SENSOR
int leituraA1; 

int moistpercent;

// Inicializa o display no endereco 0x27
LiquidCrystal_I2C lcd(0x3f,2,1,0,4,5,6,7,3, POSITIVE); 

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}
BlynkTimer timer;


void setup(){
Serial.begin(9600); 

Blynk.config(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  pinMode(pinoRele, OUTPUT); 
  digitalWrite(pinoRele, HIGH); 
  
 lcd.begin (16,2); 
 lcd.setBacklight(HIGH); 

  pinMode(pinoSensorChuva,INPUT); 
   
  delay(1000); 

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


 
void loop(){

   {
    Blynk.run();
  timer.run(); // Initiates BlynkTimer
    }
    
  lcd.setCursor(0,0); 
  lcd.print("UA:"); 
  lcd.setCursor(3,0); 
  lcd.print(DHT.humidity); 
  lcd.setCursor(5,0); 
  lcd.print(" % "); 
  lcd.setCursor(8,0); 
  lcd.print("TA:"); 
  lcd.setCursor(11,0); 
  lcd.print(DHT.temperature); 
  lcd.setCursor(13,0); 
  lcd.print(" \337C"); 


  DHT.read11(pinoDHT11); 
  Serial.print("Umidade = "); 
  Serial.print(DHT.humidity); 
  Serial.print(" %  "); 
  Serial.print("Temperatura = "); 
  Serial.print(DHT.temperature); 
  Serial.println("* Celsius  "); 
  delay(2000); 

   leituraA1 = analogRead(pinoSensorChuva); 
  moistpercent=(100-(leituraA1-500)/5.23);
 if(leituraA1 < 761){ 
    Serial.print(100-(leituraA1-500)/5.23); 
    Serial.println(" / UMIDO"); 
    
  
  if (moistpercent >=100) {
  lcd.setCursor(0,1); 
  lcd.print("US:");
  lcd.setCursor(3,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO  "); 
  
   }else{ //SENÃO
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(" ");
  lcd.setCursor(4,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO  ");

   } 
  }else{ //SENÃO
    Serial.print(100-(leituraA1-500)/5.23); 
    Serial.println(" / IRRIGAR"); 
    
   if (moistpercent >=100) {
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
   lcd.setCursor(8,1); 
  lcd.print("UMIDO   "); 
  
   }else{ //SENÃO
   lcd.setCursor(0,1); 
  lcd.print("US:");  
  lcd.setCursor(3,1); 
  lcd.print(" ");
  lcd.setCursor(4,1); 
  lcd.print(moistpercent);
  lcd.setCursor(6,1); 
  lcd.print("%");
  lcd.setCursor(8,1); 
  lcd.print("IRRIGAR");
  }
  delay(100); //DELAY DE 100ms
}

 if (moistpercent<=50){ 
    digitalWrite(pinoRele, LOW); 
    Serial.println("RELE LIGADO"); 
    }else{ //SENÃO, FAZ
      digitalWrite(pinoRele, HIGH); 
      Serial.println("RELE DESLIGADO"); 
    }
   
}

Tks

Your problem is your void loop.
It should look like this:
void loop()
{

  Blynk.run();        // run Blynk magic
  timer.run();        // run timer

}
and all the stuff that’s in your void loop at the moment should be in a function that’s called with a timer.

Read this:

Pete.

Checking that and come back soon. Tks.

Pete,
What I understood is blynk timer creates a kind of loop for each funcion into void setup. (exactlly to clean void loop). And it works to send data or to interact with Blynk avoinding spamming. My question now is whether I can use it to run what initially was set at void loop locally and also send to Blynk the same info. Example: display temperature and humidity at LCD AND on mobile phone using blynk. Could you give me a simple sample to be replicated in my code?

Tks

Search the forum for DHT11 and LCD.

Pete.

@ReiLorenzato Code formatting is done with backticks not commas, etc… I fixed your posts above.

Blynk - FTFC

Tks @Gunner!
But I suppose just inserting backticks will not help my project.
As I am beginner I suppose I need to change my code to make it work with Blynk and physiscal LCD.
I am able to get all info on my mobile, but I would like to add a LCD to help local operators to see what is going on when they are not using mobile phones,
If you have any tip to help me please do not hesitate.
Tks again.

Hi All,
I solved it. Not sure whether you need to close this topic or not. Anyway you are informed.
Tks.

Depends… if you actually want others to look at your code or not :stuck_out_tongue_winking_eye:

We are here to help you learn about Blynk, not so much about programming in general… However, “printing” to an LCD is fairly easy and well documented in Google land.

Look up examples that work with your particular LCD, use the same library and commands along side your Blynk virtual write commands to send the same data to both App & LCD.

For example (NOT drop in code… needs preliminary setup with RTC and LCD)

//===== Time =====
void timeDisplay() {
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());

  // Send time to Display Widget
  Blynk.virtualWrite(V12, currentTime);

  // Send time to LCD Widget
  lcd.print(0, 0, currentTime);  

  // Send time to Physical LCD
  PLCD.setCursor(0, 0); 
  PLCD.print(currentTime);
}
1 Like