Arduino Raspberry HC SR04 laying cilinder tank waterlevel

Hi, i have already have learned a lot from this great community, but this is my first post.

My program skills really suck but i managed to get a Raspberry measuring a waterlevel due a HC SR04, and temperature with a DHT11 after a lot trail and error on the old legacy platform.
I could not figure out how to migrate it to the Blynk 2.0, so i gave it a try with an Arduino Nano 33 IOT. The Nano is online and measuring already the distance but i have problems getting the distance translated to volume (m3). I hope someone can help me out here with the coding. Or maybe it is easier to migrate the Raspberry?

Here is the code i already got for the NaNo:


> // Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           ""
#define BLYNK_DEVICE_NAME           ""
#define BLYNK_AUTH_TOKEN            ""


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


#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <HCSR04.h>
 

char auth[] = BLYNK_AUTH_TOKEN;

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

HCSR04 hc(5, 6); //initialisation class HCSR04 (trig pin , echo pin)

BlynkTimer timer;
int L= 935;
int R= 150;
int D= 300;
int h= V9;
int volume;

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

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  // Setup a function to be called every second
}

void loop()
{
  Blynk.run();
  timer.run();
   
  {
    Serial.println(hc.dist()); // return curent distance in serial
    delay(1000);                 // we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
    Blynk.virtualWrite(V9, hc.dist());
    delay(1000);                 // we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
  Serial.println(hc.dist()); // return curent distance in serial
    delay(1000);                 // we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
    Blynk.virtualWrite(V8, L * hc.dist());
    delay(1000);                 // we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
  }
}
Raspberry code
> code raspberry fagerhøy
niels wolting
var BLYNK_TEMPLATE_ID "F"
var BLYNK_DEVICE_NAME ""
var BLYNK_AUTH_TOKEN ""'

var blynkLib = require('blynklib');
var sensorLib = require('node-dht-sensor');

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// Setup sensor, exit if failed
var sensorType = 11; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin  = 4;  // The GPIO pin number for sensor signal
if (!sensorLib.initialize(sensorType, sensorPin)) {
    console.warn('Failed to initialize sensor');
    process.exit(1);
}

// Automatically update sensor value every 2 seconds
setInterval(function() {
    var readout = sensorLib.read();
    blynk.virtualWrite(3, readout.temperature.toFixed(1));
    blynk.virtualWrite(4, readout.humidity.toFixed(1));
   
    console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
    console.log('Humidity:   ', readout.humidity.toFixed(1)    + '%');
}, 600000);
const Gpio = require('pigpio').Gpio;

// The number of microseconds it takes sound to travel 1cm at 20 degrees celcius
const MICROSECDONDS_PER_CM = 1e6/34321;

const trigger = new Gpio(23, {mode: Gpio.OUTPUT});
const echo = new Gpio(24, {mode: Gpio.INPUT, alert: true});

trigger.digitalWrite(0); // Make sure trigger is low

const watchHCSR04 = () => {
  let startTick;
  let R = 3 / 2;
  let L = 9.35
 
  echo.on('alert', (level, tick) => {
    if (level == 1) {
      startTick = tick;
    } else {
      const endTick = tick;
      const diff = (endTick >> 0) - (startTick >> 0); // Unsigned 32 bit arithmetic
      console.log(diff / 2 / MICROSECDONDS_PER_CM);
     var readout = sensorLib.read();
blynk.virtualWrite(5, 3.53-diff / 200 / MICROSECDONDS_PER_CM.toFixed(2));
blynk.virtualWrite(6, (L) *(Math.pow(R, 2) * Math.acos((R - (3.53-(diff/200/MICROSECDONDS_PER_CM))) / R) - (R - (3.53-(diff/200/MICROSECDONDS_PER_CM))) * (Math.pow((2 * R * (3.53-(diff/200/MICROSECDONDS_PER_CM)) - Math.pow((3.53-(diff/200/MICROSECDONDS_PER_CM)), 2)), .5))));

Thanks a lot i advance.

Niels Wolting

@Niels 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.

@PeteKnight done.

I’d suggest you start by reading this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

I’d also suggest that you structure your code so that at each timed interval (let’s say that this is every 5 seconds) you do the following…

Take ONE distance reading and store the result in a variable with a meaningful name (let’s say distance), something like this…

distance = hc.dist();

then use that saved distance value to perform your calculations and write the results to Blynk, like this…

Blynk.virtualWrite(V9, distance);
volume = distance * ((3.14*(r*r))*distance);
Blynk.virtualWrite(V8, volume);

Your calculation could obviously be simpler by pre-calculation Pi x radius squared and storing this as a variable.

Pete.