How to use a TFLuna with Blynk and ESP32

Hello I try to merge two sketches to make the TFLuna LiDAR work with Blynk, but I can't get it to work.

I also use the ESP32.

I have tested both sketches individually and they work.

TFLuna Sketch, to get the data from TFLuna:

#include <HardwareSerial.h> // Reference the ESP32 built-in serial port library
HardwareSerial lidarSerial(2); // Using serial port 2
#define RXD2 16
#define TXD2 17

void setup() {
  Serial.begin(115200); // Initializing serial port
  lidarSerial.begin(115200, SERIAL_8N1, RXD2, TXD2); // Initializing serial port
}

void loop() {
  uint8_t buf[9] = {0}; // An array that holds data
  if (lidarSerial.available() > 0) {
    lidarSerial.readBytes(buf, 9); // Read 9 bytes of data
    if( buf[0] == 0x59 && buf[1] == 0x59)
    {
      uint16_t distance = buf[2] + buf[3] * 256;
      uint16_t strength = buf[4] + buf[5] * 256;
      int16_t temperature = buf[6] + buf[7] * 256;
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.print(" cm, strength: ");
      Serial.print(strength);
      Serial.print(", temperature: ");
      Serial.println(temperature / 8.0 - 256.0);
    }
  }
  delay(10); 
}

Blynk Sketch, is successfully connects and send data to blynk, I have already implemented the Hardware Serial for the ESP32

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL562CiRkrg"
#define BLYNK_TEMPLATE_NAME         "Silo niveu"
#define BLYNK_AUTH_TOKEN            "5f2rod1VjOP6I7BCdAWVimRUcxoY4nGz"


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

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <HardwareSerial.h> // Reference the ESP32 built-in serial port library
HardwareSerial lidarSerial(2); // Using serial port 2
#define RXD2 16
#define TXD2 17

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "RUM";
char pass[] = "!Xylon524";

BlynkTimer timer;


//What to do when successfully connected to Blynk
BLYNK_CONNECTED() {

  //Makes sure that V1 is in sync in case of lost connection.
  Blynk.syncVirtual(V1);

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Connected to Blynk 🙌");

  //A LED lights up when connected to server.
  digitalWrite(21, HIGH);
}

//What to do when not connected to Blynk
BLYNK_DISCONNECTED() {

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Blynk disconnected");
}



//This is for preventing dataoverflow so it only sends the updated value with certain intervals.
//The update interval is defined by timer.setInterval in the void setup section.
void myTimerEvent() {
  
  // You can send any value at any time.
  // Please don't send more that 10 values per second.

  //send among of seconds that has past to virtual pin V0
 // Blynk.virtualWrite(V0, millis() / 1000);

  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V2
  Blynk.virtualWrite(V1, 10);
  Serial.println(10);
}
void setup()
{

  // Debug console
  Serial.begin(115200);
  Serial.println();
  lidarSerial.begin(115200, SERIAL_8N1, RXD2, TXD2); // Initializing serial port
  // Give Serial Monitor some time to connect
  delay(3000);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

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



void loop()
{

  // runs BlynkTimer
  timer.run();

  // Runs all Blynk stuff
  Blynk.run();

}

Hope anyone can help. Thanks in advance.

I eventually figured it out a workaround.

This is my final sketch to make it work.

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL562CiRkrg"
#define BLYNK_TEMPLATE_NAME         "Silo niveu"
#define BLYNK_AUTH_TOKEN            "5f2rod1VjOP6I7BCdAWVimRUcxoY4nGz"


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

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <HardwareSerial.h> // Reference the ESP32 built-in serial port library
HardwareSerial lidarSerial(2); // Using serial port 2
#define RXD2 16
#define TXD2 17

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

//BlynkTimer timer;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;

const long interval = 25;
const long interval2 = 1000;

//What to do when successfully connected to Blynk
BLYNK_CONNECTED() {

  //Makes sure that V1 is in sync in case of lost connection.
  Blynk.syncVirtual(V1);

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Connected to Blynk 🙌");

  //A LED lights up when connected to server.
  digitalWrite(21, HIGH);
}

//What to do when not connected to Blynk
BLYNK_DISCONNECTED() {

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Blynk disconnected");
}



//This is for preventing dataoverflow so it only sends the updated value with certain intervals.
//The update interval is defined by timer.setInterval in the void setup section.
/*

void myTimerEvent() {
  
  // You can send any value at any time.
  // Please don't send more that 10 values per second.

  //send among of seconds that has past to virtual pin V0
 // Blynk.virtualWrite(V0, millis() / 1000);

  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V2
  Blynk.virtualWrite(V1, 10);
  Serial.println(10);
}
*/
void setup()
{

  // Debug console
  Serial.begin(115200);
  Serial.println();
  lidarSerial.begin(115200, SERIAL_8N1, RXD2, TXD2); // Initializing serial port
  // Give Serial Monitor some time to connect
  delay(3000);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

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



void loop()
{

  // runs BlynkTimer
 // timer.run();

  // Runs all Blynk stuff
  Blynk.run();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;

  uint8_t buf[9] = {0}; // An array that holds data
  if (lidarSerial.available() > 0) {
    lidarSerial.readBytes(buf, 9); // Read 9 bytes of data
    if( buf[0] == 0x59 && buf[1] == 0x59)
    {
      uint16_t distance = buf[2] + buf[3] * 256;
      uint16_t strength = buf[4] + buf[5] * 256;
      int16_t temperature = buf[6] + buf[7] * 256;
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.print(" cm, strength: ");
      Serial.print(strength);
      Serial.print(", temperature: ");
      Serial.println(temperature / 8.0 - 256.0);
      
if (currentMillis - previousMillis2 >= interval2) {

    previousMillis2 = currentMillis;

    Blynk.virtualWrite(V1, distance);
    Blynk.virtualWrite(V2, temperature / 8.0 - 256.0);
    Blynk.virtualWrite(V3, strength);
  }
    }
  }
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.