Stray values from Blynk.virtualWrite()

Just started experimenting with Blynk. I have a basic Arduino app using an ultrasonic sensor to measure inches from the sensor. My script works in the Arduino.
I am trying to debug a situation with Blynk, a discrepancy between the data reported by the following two sequential commands in the script

Serial.println(inches);
Blynk.virtualWrite(V2, inches);

The above code produces the following “correct” output in the Arduino IDE serial monitor, 19 inches:

However when I view the input received from the Blynk.virtualWrite() on my phone, I get correct value, 19 in this case, and then a stray value of 144 in between each correct value. 144 is the max value that I set for the chart I am presenting on my phone. So I end up with choppy oscillating chart between the actual value reported and the max value of the chart. I tried to get a screen print where it shows the 144 value below the chart in a value widget, but it happens too fast. Here is an image of the Arduino IDE serial monitor and a screen capture from my phone side-by-side. Any suggestions for weeding out the stray data? Seems like a Blynk bug to me.

How do you expect community members to be able to provide meaningful comments without seeing your sketch?

Pete.

@garlydog the reason why you’re having problems posting your sketch to the forum is that you didn’t read the instructions when you created this topic.

When you post code to the forum it needs to have triple backticks at the beginning and end, so that the code displays correctly.
Triple backticks look like this:
```

Pete.

Thank you for your replies. In the process of simplifying the code to post something precise, I figured out the solution to my problem. I was writing the wrong virtual port. Which is odd that it even worked at all. Regardless, it is working now.

Not sure why you’d say that.
You had two pieces of data being written to V2, and I assume that you’d defined the datastream to have a maximum value of around 144, so when you wrote uptimes greater than 144 seconds to the virtual pin they were being capped at the maximum.

However, you really need to read this, and sort out your void loop and get rid of all those delays…

Pete.

Of course. Thank you for the clarification. Also, thank you for the code critique. I made the following adjustments since you brought it up… Seems to be working.

//River Level indicator

#define BLYNK_TEMPLATE_ID "******"
#define BLYNK_DEVICE_NAME "Creek Meter"
#define BLYNK_AUTH_TOKEN "******"

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

// Define Physical connection to sensor

#define TRIGPIN 2   // Board pin D4
#define ECHOPIN 0   // Board pin D3

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "DTW";
char pass[] = "*******";

BlynkTimer timer;

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void sensorDataSend()
{  
    //Initialize measurement variables
  float duration = 0;   //time between ping and receive on sensor
  float distance = 0;   //distance calculated in millimeters
  int feet = 0;
  int inches = 0;
  int remainder_inches = 0;

  // Set the trigger pin LOW for 2uS to stablized sensor
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);

  // Set the trigger pin HIGH for 20us to send pulse
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(20);

  // Return the trigger pin to LOW
  digitalWrite(TRIGPIN, LOW);

  // Capture an incoming pulse
  duration = pulseIn(ECHOPIN, HIGH);

  // Determine distance in meters from duration
  // Use 343 metres per second as speed of sound
  // Divide by speed of sound by 1000 (.0343) as we result want millimeters

  distance = (duration / 2) * 0.343;
  
  //convert to inches
  inches = distance/25.4;
  //convert to feet
  feet = inches/12;
  //determine remainder in inches
  remainder_inches = inches-(feet*12);
    
  Serial.println(inches);
  Blynk.virtualWrite(V3, inches);
 
}

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

  Blynk.begin(auth, ssid, pass);
 
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, sensorDataSend);

  // intialize physical pins on Arduino
  
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
}

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

1 Like