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.
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.
#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
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.
Error installing /*************************************************************: Library '/*************************************************************' not found
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)….
(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
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?
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.