Data from serial monitor to Blynk

Dear forum
With no experince in coding, i have taken the challenge to learn this.....and this is indeed a challenge.
I am working to create a project, which can meassure the water level in my garden tank.
The board needs to operate with a external power supply, and i need to read the data via the Blynk app.
The board is a Wemos D1 mini, and it works in the Arduino IDE, and when connected via the USB cabel.
I'm able to read the data in the serial monitor, and this data is correct.
But when i connect this to Blynk, i don't get the same data in the Blynk app...?
Eg. the serial monitor reads 0% and the Blynk app shows 12%... ?
I have found a way to convert the reading to a reading in %, and i can tell the ultrasound sensor the size of the tank, and this will read in between 0-100 %.
But in the Blynk app...this is not the same. clearly not using the conversion to %.
And I can't figur out way.....about to throw all my Arduino out the window...grrrr.

#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_TEMPLATE_NAME "wemos"
#define BLYNK_AUTH_TOKEN "xxxxxx"
#define trigPin D5
#define echoPin D6

int pingTravelTime;
float pingTravelDistance;
float distancetoTarget;

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266_SSL.h>

char auth[] = BLYNK_AUTH_TOKEN;

// WiFi setup
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  // int value = param.asInt();

  // Update state
  //Blynk.virtualWrite(V1, distancetoTarget);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{

}

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

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

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  //Reset the trigger pin og vent halvt sekund
  digitalWrite(trigPin, LOW);
  delayMicroseconds(500);

  Blynk.begin(auth, ssid, pass);

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

void loop() {

  int procent;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pingTravelTime = pulseIn(echoPin, HIGH);
  delay(500);
  pingTravelDistance = (pingTravelTime * 1230.*1000.*100) / (3600.*1000000);
  distancetoTarget = pingTravelDistance / 2;

  // 12 is the dist. to the bottom = 0%
  // 3 is the dist. from sensor to top of the tank = 100%
  procent = map(distancetoTarget, 12, 3, 0, 100);
  if (procent < 0)
  {
    procent = 0;
  }
  else if (procent > 100)
  {
    procent = 100;
  }

  Serial.print("Water level in the tank is: ");
  Serial.print(procent);
  Serial.println("% ");


  Blynk.run();
  timer.run();

}

i see the number i'm getting in the Blynk app are the distance in cm, and not the data from the map command.. ?
So in Blynk i get 12 = 12 centimeters from the sensor, but not in %.
Could this be a blynk thing ?

I have read that the void loop should be kept clean, but if i move all a part from timer run and Blynk run to the void setup, it dosen't work.
I can get the distance to target to the virtual pin V0, but this only shows the distance in cm and not the map function which are in % ?
Changeing V0 to procent or to map (procent is = to map) then i get no data in Blynk....help some one ?

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