Blynk WIth I2c 1602LCD Display and Blynk App

I have create a project that works with LCD display 1602 via i2c and monitor with Blynk Apps Also, so we have two display run at same time on blynk.

#define BLYNK_PRINT Serial    
#include SPI.h>
#include Ethernet.h>
#include BlynkSimpleEthernet.h>
#include SimpleTimer.h>
#include Wire.h> 
#include LiquidCrystal_I2C.h>
#include DHT.h>

char auth[] = "";

#define DHTPIN 2          
LiquidCrystal_I2C lcd(0x27,16,2);

#define DHTTYPE DHT11     
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{  
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  Serial.begin(9600);
  Blynk.begin(auth);
  
  timer.setInterval(1000L, sendSensor);
}

  void loop() {

  delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
}
4 Likes

You’re a life saver! I was making something simmilar and i couldnt figure it out, thanks!

I want to know if the lcd is updating even if the system is not connected to BLYNK.
Please let me know about this.
thanks

Blynk.begin is a blocking function. The code execution wont progress beyond this point if a connection to either the Wi-Fi or Blynk isn’t possible.

Changing the code to use Blynk.config and Blynk.connect would overcome this issue.
https://docs.blynk.cc/#blynk-firmware-configuration

Pete.

Why the delay(500) in the loop? Everytime the loops goes round it will stop for half a second?

1 Like

Not a good idea !!
Move all into void sendSensor() and delete delay(500);

 void loop() {

  delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
{

I have used the following code and it works.

/*
 * Program: Water_Palnt_ESP_R4.ino
 * Plant watering system based on Espresso Lite V2.0 and
 * Blynk app.
 * Auto Mode: ------------------------------------
 * V1 - Slider to set the moisture threshhold
 * V5 - Moisture % derived from analog data A0
 * Soil moisture sensor detects water in the soil and
 * triggers a pump when dry.
 * 
 * Manual Mode:-----------------------------------
 * V5 - Moisture % derived from analog data A0
 * V3 - Pump ON/OFF switch
 * V2 - Auto/Manual select switch
 * Two buttons are defined in Blynk to control
 * manual pump operation.
 * 
 */
 
#define BLYNK_PRINT Serial      // Blynk debug enabled
#include <Wire.h>               // I2C library
#include <LiquidCrystal_I2C.h>  // I2C LCD library
#include <ESP8266WiFi.h>        // ESP8266 library for Espresso
#include <BlynkSimpleEsp8266.h> // Blynk library

#define relayPin 0              // Pump relay output

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

// WiFi credentials....
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxx";

int sensorData;            // Moisture sensor ADC value
int moistPercent;          // Moisture % reading
int setValue;              // Moisture % set point
int modSelect = 0;         // Auto/Manual select, Auto
int pumpStat;              // Pump ON/OFF status

BlynkTimer timer;                 // Blynk Timer object
LiquidCrystal_I2C lcd(0x27,20,4); // LCD object

// In the app, Widget's reading frequency should be set to PUSH. 
// Here data is sent to Blynk App every second
void myTimerEvent(){
  sensorData = analogRead(A0);       // Read moisture data
  moistPercent = map(sensorData,450,1024,100,0); // Map min/max values to 100-0%(inverted)
  if (modSelect == 0){              // Auto mode selected
    if (moistPercent < setValue) {  // If Moist % below set point
      lcd.setCursor(8,2);           // Display data on LCD
      lcd.print(moistPercent);     
      lcd.print("  ");
      lcd.setCursor(8,3);
      lcd.print("ON ");
      digitalWrite(relayPin, HIGH); // Turn ON pump
      Serial.println(sensorData);
    } else {                        // If Moist % above set point
      lcd.setCursor(8,2);           // Display data on LCD
      lcd.print(moistPercent);
      lcd.print("  ");
      lcd.setCursor(8,3);
      lcd.print("OFF");
      digitalWrite(relayPin, LOW); // Turn OFF pump
      Serial.println(sensorData); 
    }
  } else {                         // Man mode selected    
    if (pumpStat == 1){            // Pump ON selected
      digitalWrite(relayPin, HIGH);
    }else{                         // Pump OFF selected
      digitalWrite(relayPin, LOW);  
    }
  }
  
  Blynk.virtualWrite(V5, moistPercent);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
}
// Every time we connect to the cloud...

BLYNK_CONNECTED()
{
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
  //Blynk.syncVirtual(V2);
}
// Set the moisture % threshhold
BLYNK_WRITE(V1) {
  setValue = param.asInt();
  lcd.setCursor(8,1);
  lcd.print(setValue);
  lcd.print("  ");
}

// Pump ON/OFF select
BLYNK_WRITE(V3){
  pumpStat = param.asInt();
  if(pumpStat == 0){
    lcd.setCursor(8,3);
    lcd.print("OFF"); 
  }else{
    lcd.setCursor(8,3);
    lcd.print("ON ");       
  }
}

// Auto/Manual Mode select
BLYNK_WRITE(V2) {
  modSelect = param.asInt();
  if(modSelect == 0){
    lcd.setCursor(8,0);
    lcd.print("Auto"); 
  }else{
    lcd.setCursor(8,0);
    lcd.print("Man ");       
  }
}

void setup(){
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);

  lcd.init();
  lcd.backlight();
  lcd.print("Plant Water Sys R4"); // Prog version
  delay(2000);                     // Wait 2 sec
  lcd.clear();                     // Clear display
  
  lcd.print("Mode  : ");           // Mode disp line 1
  lcd.setCursor(8,0);              // Default Auto mode
  lcd.print("Auto"); 
  lcd.setCursor(0,1);
  lcd.print("Set % : ");           // Set Point disp line 2
  lcd.setCursor(0,2);    
  lcd.print("Moist%: ");           // Moist % disp line 3
  lcd.setCursor(0,3);
  lcd.print("Pump  : ");           // Pump disp line 4



  // Setup timer function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop(){
  Blynk.run(); // Update Blynk
  timer.run(); // Update BlynkTimer
}
3 Likes

\ HI I USE USB TO LAPTOP INTERNETCONNECTION AND IT WORKS WELL ALSO WITH MY CODE HERE. ENJOY

#define BLYNK_PRINT SwSerial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX

#include <BlynkSimpleStream.h>
#include <DHT.h>

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

char auth[] = “xxx”;

#define DHTPIN 7 // What digital pin we’re connected to

LiquidCrystal_I2C lcd_I2C_27(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

if (isnan(h) || isnan(t)) {
SwSerial.println(“Failed to read from DHT sensor!”);
return;
}

Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}

void setup()
{

SwSerial.begin(9600);

lcd_I2C_27.begin (16, 2);
lcd_I2C_27.setBacklight(HIGH);

Serial.begin(9600);
Blynk.begin(Serial, auth);

dht.begin();

timer.setInterval(1000L, sendSensor);
}

void loop()
{

delay(500);

float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
lcd_I2C_27.print(“ERROR”);
return;
}

lcd_I2C_27.clear();
lcd_I2C_27.setCursor( (1) - 1, (1) - 1 );
lcd_I2C_27.print( “SUHU =” );
lcd_I2C_27.print(t);
lcd_I2C_27.setCursor( (14) - 1, (1) - 1 );
lcd_I2C_27.print( “C” );
delay(0);
lcd_I2C_27.setCursor( (1) - 1, (2) - 1 );
lcd_I2C_27.print( “HUMIDITY =” );
lcd_I2C_27.print(h);
lcd_I2C_27.setCursor( (16) - 1, (2) - 1 );
lcd_I2C_27.print( “%” );
delay(0);

{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}
}

@teguh 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.

I have read your code, but I do not fully understand it, but I find it works, thank you very much. good luck to you ! (i am Vietnamese, vansynghi@cdnpy.edu.vn)

Dear Pete Knight, Could you please help me, the movements to recharge Blynk
Thanks in advance (vansynghi@gmail.com).

I don’t understand your question.

Pete.

To recharge your account you can buy credit in the app. Stop the app go to the add widgets page. See photo.

If that don’t work please contact @Dmitriy . @PeteKnight can do almost everything but I don’t think he can do that :wink:

2 Likes

Thank you for share. I am new for blynk and your code help me…from Thailand. Thank you

1 Like