Sending console information to blynk

I am working on a project where I need to send console information from Arduino to Blynk. The console prints information in centimeters below 15 and that needs to be sent to a gauge in Blynk.
My current code looks like this:

int Trig_pin =5;
int Echo_pin =18;
long duration;
float Speed_of_sound =0.034;
float dist_in_cm;

void setup() {
  Serial.begin(115200);
  pinMode(Trig_pin, OUTPUT);
  pinMode(Echo_pin,INPUT);

}

void loop() {
  digitalWrite(Trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig_pin,LOW);

  duration =pulseIn(Echo_pin,HIGH);

  dist_in_cm=duration*Speed_of_sound/2;
  if (dist_in_cm < 15.0){
    Serial.print("distance in cm :");
  Serial.println(dist_in_cm);
  }


  delay(1000); 
}

If someone could help me out that would be great.
Thanks.

@bob1 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:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Sorry for the confusion, it should be fixed now.

I assume that when you refer to “console” you mean the serial monitor output?

The first thing you should do is to create a Blynk template and device, so you have the three lines of firmware co figuration to add to the top of your sketch.

Then take a look at the Blynk stand-alone (not Edgent) examples for your board and add-in the Blynk libraries and the Blynk.begin() and Blynk.run() commands to your sketch, in the same way that they appear in the example.

Then read this…

and restructure your void loop so that it works with a BlynkTimer instead of a delay.

Finally, you’ll need to create your datastreams and dashboard widgets, then add-in a Blynk.virtualWrite() command to write your distance reading to Blynk.

Pete.

I currently have this code:

#define BLYNK_TEMPLATE_ID "xxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "esp32"
#define BLYNK_AUTH_TOKEN "xxxxxxxxx"

BlynkTimer timer;
void setup()
{
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}
void loop()
{
  Blynk.run();        // run Blynk magic
  timer.run();        // run timer every second
}

int Trig_pin =5;
int Echo_pin =18;
long duration;
float Speed_of_sound =0.034;
float dist_in_cm;

void setup() {
  Serial.begin(115200);
  pinMode(Trig_pin, OUTPUT);
  pinMode(Echo_pin,INPUT);

}

void sensorDataSend() {
  digitalWrite(Trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig_pin,LOW);

  duration =pulseIn(Echo_pin,HIGH);

  dist_in_cm=duration*Speed_of_sound/2;
  if (dist_in_cm < 15.0){
    Serial.print("distance in cm :");
  Serial.println(dist_in_cm);
  }

  Blynk.virtualWrite(V3, dist_in_cm)
}

But when I try to run it, I get this fail:

sketch.ino:6:1: error: 'BlynkTimer' does not name a type; did you mean 'ETSTimer'?
 BlynkTimer timer;
 ^~~~~~~~~~
 ETSTimer
sketch.ino: In function 'void setup()':
sketch.ino:9:3: error: 'timer' was not declared in this scope
   timer.setInterval(1000L, sensorDataSend); //timer will run every sec
   ^~~~~
sketch.ino:9:3: note: suggested alternative: 'time'
   timer.setInterval(1000L, sensorDataSend); //timer will run every sec
   ^~~~~
   time
sketch.ino: In function 'void loop()':
sketch.ino:13:3: error: 'Blynk' was not declared in this scope
   Blynk.run();        // run Blynk magic
   ^~~~~
sketch.ino:13:3: note: suggested alternative: 'link'
   Blynk.run();        // run Blynk magic
   ^~~~~
   link
sketch.ino:14:3: error: 'timer' was not declared in this scope
   timer.run();        // run timer every second
   ^~~~~
sketch.ino:14:3: note: suggested alternative: 'time'
   timer.run();        // run timer every second
   ^~~~~
   time
sketch.ino: At global scope:
sketch.ino:23:6: error: redefinition of 'void setup()'
 void setup() {
      ^~~~~
sketch.ino:7:6: note: 'void setup()' previously defined here
 void setup()
      ^~~~~
sketch.ino: In function 'void sensorDataSend()':
sketch.ino:45:3: error: 'Blynk' was not declared in this scope
   Blynk.virtualWrite(V3, dist_in_cm)
   ^~~~~
sketch.ino:45:3: note: suggested alternative: 'link'
   Blynk.virtualWrite(V3, dist_in_cm)
   ^~~~~
   link
sketch.ino:45:22: error: 'V3' was not declared in this scope
   Blynk.virtualWrite(V3, dist_in_cm)
                      ^~
sketch.ino:45:22: note: suggested alternative: 'T3'
   Blynk.virtualWrite(V3, dist_in_cm)
                      ^~
                      T3

Error during build: exit status 1

what should I do?

That’s because you haven’t added-in the necessary Blynk libraries

Pete.

What are the Blynk stand-alone examples and where do I find them?

They are installed when you install the library in your IDE.
If you’re using the Arduino IDE go to File > Examples > Blynk > Boards_WiFi and choose an example that coincides with your board type.

Pete.

Now I got this error:

Error installing /*************************************************************: Library '/*************************************************************' not found

You’ll need to provide more context about this.

Pete

Alright, ignore that last message.
I downloaded the ESP_32_SSL file under Examples > Boards_Wifi like you said. What should I do with it though?

Look at it and compare it to your sketch.
You’ll see that the example contains two WiFi libraries and one Blynk library that are needed for your particular board and your chosen connection method (SSL)….

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>

These will solve your compilation issue.

Pete.

I decided not to use SSL. I added these from the example:

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

And also these:

char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void loop()
{
  Blynk.run();
}

(with my network credentials, of course)
But when I run it i get this:

sketch.ino: In function 'void setup()':
sketch.ino:26:11: error: 'Trig_pin' was not declared in this scope
   pinMode(Trig_pin, OUTPUT);
           ^~~~~~~~
sketch.ino:27:11: error: 'Echo_pin' was not declared in this scope
   pinMode(Echo_pin,INPUT);
           ^~~~~~~~
sketch.ino:27:11: note: suggested alternative: 'lchown'
   pinMode(Echo_pin,INPUT);
           ^~~~~~~~
           lchown

Error during build: exit status 1

What should I do?

Re-insert the definitions of these variables from your original sketch…

Pete.

I got it to connect to Blynk and the device is online, but it doesn’t show the information on the gauge, this is my code:


#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "esp32"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxx"

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

char ssid[] = "xxxxx";
char pass[] = "xxxxx";

int Trig_pin =5;
int Echo_pin =18;
long duration;
float Speed_of_sound =0.034;
float dist_in_cm;

BlynkTimer timer;

void setup()
{
  timer.setInterval(1000L, sensorDataSend);

  
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  Serial.begin(115200);

  pinMode(Trig_pin, OUTPUT);
  pinMode(Echo_pin,INPUT);
}
void loop()
{
  Blynk.run();
  timer.run();
}

void sensorDataSend() {
  digitalWrite(Trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig_pin,LOW);

  duration =pulseIn(Echo_pin,HIGH);

  dist_in_cm=duration*Speed_of_sound/2;
  if (dist_in_cm < 15.0){
    Serial.print("distance in cm :");
  Serial.println(dist_in_cm);
  }

  Blynk.virtualWrite(V3, dist_in_cm);
}

What does your serial monitor show?

How is your V3 datastream configured?

Is your gauge widget attached to your v3 datastream?

BTW, you have Serial.begin(115200); twice in your void setup.

Pete.

First the monitor shows this:

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1156
load:0x40078000,len:11456
ho 0 tail 12 room 4
load:0x40080400,len:2972
entry 0x400805dc

Then it shows the distance as normal, like this:

distance in cm :10.98
distance in cm :11.02
distance in cm :1.99
distance in cm :1.99
distance in cm :4.00

The virtual pin datastream is set to the V3 pin and the data type is set to “Integer”

The gauge is attached to the v3 datastream

And I fixed the double Serial.begin(115200)

Okay, you should all #define BLYNK_PRINT Serial immediately after your first three lines of code. This will give you more info about the Blynk connection.

How have you defined the other datastream parameters, especially min/max values?

Pete.

I added that string.
The min value is 10 and the max is 98. I know that most of the gauge wouldnt be used in this situation since only values below 15 are sent, but it’s ok, if that needs to be changed it’s also fine.

And?

Pete.