Hi, I have a code running ok, running on an Arduino Nano, an LCD 0802a and an Ultrasonic sensor Hc-sr04.
I want to see on my phone with blynk what I currently see on LCD 0802a.
What widget should I use?
Value Display?
Labeled Value?
LCD?
I have tried with Blynk.virtualWrite but I have not managed to use my variables already with loaded data to be able to show them
Hi ldb, thank you for answering me, I forgot to clarify that I am connected to blynk by wifi and everything works ok, I did simple tests that have nothing to do with my code and it works very well, for example, turning on and off led and other tests.
But I can not make the variables that I already have with data send them that I see in my physical LCD, show them in one of the widgets.
// Coneccion wifi + BLYNK
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
char auth[] = "xxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxx";
// formateo la coneccion AT con el ESP8266
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 57600
ESP8266 wifi(&EspSerial);
// DISPLAY 0802a
#include <LiquidCrystal.h>
const int rs = 4, en = 5, db4 = 6, db5 = 7, db6 = 8, db7 = 9;
LiquidCrystal lcd(rs, en, db4, db5, db6, db7);
#define COLS 8
#define ROWS 2
// SENSOR DE AGUA
const int PinTrig = 10;
const int PinEcho = 11;
const float VelSon = 34000.0;
const int numLecturas = 10; //lo baje a 10 para pruebas ponerlo a 100.
const float distancia100 = 10;
const float distanciaVacio = 148;
float lecturas[numLecturas]; // Array para almacenar lecturas
int lecturaActual = 0; // Lectura por la que vamos
float total = 0; // Total de las que llevamos
float media = 0; // Media de las medidas
bool primeraMedia = false; // Para saber que ya hemos calculado por lo menos una
void setup()
{
// Iniciamos el monitor serie para mostrar el resultado
Serial.begin(57600);
delay(10);
// Set ESP8266 baud rate para la WIFI
EspSerial.begin(ESP8266_BAUD);
delay(10);
// Ponemos el pin Trig en modo salida
pinMode(PinTrig, OUTPUT);
// Ponemos el pin Echo en modo entrada
pinMode(PinEcho, INPUT);
// Inicializamos el array
for (int i = 0; i < numLecturas; i++)
{
lecturas[i] = 0;
}
// Configuramos las filas y las columnas del LCD en este caso 8 columnas y 2 filas
lcd.begin(COLS, ROWS);
// Mandamos al Blynk
Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
// Eliminamos la última medida
total = total - lecturas[lecturaActual];
iniciarTrigger();
// La función pulseIn obtiene el tiempo que tarda en cambiar entre estados, en este caso a HIGH
unsigned long tiempo = pulseIn(PinEcho, HIGH);
// Obtenemos la distancia en cm, hay que convertir el tiempo en segudos ya que está en microsegundos
// por eso se multiplica por 0.000001
float distancia = tiempo * 0.000001 * VelSon / 2.0;
// Almacenamos la distancia en el array
lecturas[lecturaActual] = distancia;
// Añadimos la lectura al total
total = total + lecturas[lecturaActual];
// Avanzamos a la siguiente posición del array
lecturaActual = lecturaActual + 1;
// Comprobamos si hemos llegado al final del array
if (lecturaActual >= numLecturas)
{
primeraMedia = true;
lecturaActual = 0;
}
// Calculamos la media
media = total / numLecturas;
// Solo mostramos si hemos calculado por lo menos una media
if (primeraMedia)
{
float distanciaLleno = distanciaVacio - media;
float cantidadLiquido = distanciaLleno * 100 / distancia100;
int porcentaje = (int) (distanciaLleno * 100 / distanciaVacio);
// Mostramos en la pantalla LCD
lcd.clear();
// Cantidada de líquido
lcd.setCursor(0, 0);
// Formatie de ml a Litros
//lcd.print(String (cantidadLiquido) + " ml");
lcd.print(String (int (cantidadLiquido)) + " Lit");
// Porcentaje
lcd.setCursor(0, 1);
lcd.print(String(porcentaje) + " % Car");
// Mostramos en el Monitor
Serial.print(media);
Serial.println(" cm");
Serial.print(cantidadLiquido);
Serial.println(" ml");
}
else
{
lcd.setCursor(0, 0);
lcd.print("Calc: " + String(lecturaActual));
}
delay(500);
Blynk.run();
}
//////////////////////////////////////////////////////////////////////
// Método que inicia la secuencia del Trigger para comenzar a medir //
void iniciarTrigger()
{
// Ponemos el Triiger en estado bajo y esperamos 2 ms
digitalWrite(PinTrig, LOW);
delayMicroseconds(2);
// Ponemos el pin Trigger a estado alto y esperamos 10 ms
digitalWrite(PinTrig, HIGH);
delayMicroseconds(10);
// Comenzamos poniendo el pin Trigger en estado bajo
digitalWrite(PinTrig, LOW);
}
I would suggest to create a timer and send data to Blynk, this example has it all: Push Data Example
An you send it as you print to LCD, for example: Blynk.virtualWrite(VX, String(lecturaActual)); or Blynk.virtualWrite(VX, String(porcentaje) + " % Car");
But in my old code, in the void () loop I still have a delay (500); and I do not know how to change it because I will also be penalized with high delay.
My code into void loop ir big ( is part of view my lcd fisic).
ok there send all the code out of the void loop();
remove the delay and place everything inside:
BlynkTimer timer;
void myTimerEvent ()
It works correctly and the void loop was clean.