Can we use Nodemcu to execute a sketch HC Sr 04

Hi guys just a question, I have a nodemcu and using it with my ultrasonic sensor HC SR 04 to maintain water level in my tank automatically at a preset level. So my question is, is it possible to use Nodemcu to start a relay automatically at a preset value. I’ve seen some YouTube video where they said not to put programs in the void loop as it can damage the hardware???

1 Like

Yes, use Blynk Timer function (nothing to do with Blynk Timer widgets). Covered on Arduino site as “Simple Timer”.

I want the relay to start when the level in the tank is low… Not based on time

The “Blynk Timer” aka “Simple Timer” is a looping system preferable to loop(). It calls your HC SR04 sensor readings at chosen intervals, say every 3 seconds. Within this new loop you code up the relay trigger based on the water level. See Blynk’s PUSH_DATA example for the syntax etc.

But you’ll need to read the level of the water every so often (every 5 seconds maybe) which is where the timer function comes in.

Seems like every thread I’ve ever read on this forum about these ultrasonic sensors is from someone who wants to control/measure the level of water in a tank. A bit of searching might just deliver you some readymade code.
A word of warning though, don’t re-open long forgotten threads with a question about ‘how do I do xxx’ or the wrath of @Gunner will be upon you :wink:
Much better to open a new thread and reference back to the original one.

Pete.

2 Likes

very easy to code :wink:

Thanks I did check on it… But I couldn’t find exactly what I wanted. So just to be sure the same code I use for my arduino that runs in loop can I put in nodemcu?

The “Arduino” code will run on your ESP but you must make some minor mods for it to work with iOT and Blynk. See PUSH_DATA.

Thamks

Most Arduino “maker” sketches that you will see have everything banged into the main loop(). Some makers use millis() to structure events a little better than using a continuous loop() that wastes resources. Simple Timer is a well structured use of millis() but you don’t see it being used in many maker sketches.

For Arduino’s that are doing basic tasks then the poor use of loop() works. ESP’s are way more powerful than Arduino’s but with the WiFi stack and accessing the Blynk server they are working much, much harder. Using an “Arduino style” loop() with an ESP is a sure fire way of crashing your Blynk connected iOT project.

Oh no… So is there a solution to it? Without using another arduino board

Yes Blynk Timer aka Simple Timer.

There is always a solution… however they are rarely LEGO pieces to be simply snapped into place and offered to someone upon request :stuck_out_tongue_winking_eye: Had you searched this forum you would have found a few different topics referring to the Ultrasonic Sensor.

I use this (taken from some Googled site and modified to work better with Blynk’s timing) in my Testbench sketch, but I have to use a Blynk Button in Switch mode to turn it on and off, else it eventually causes timing issues with other processes, due the precise timing requirements of an Ultrasonic sensor.

You could try modifying this to work with your BlynkTimer and some logic to activate your relays when required.

You will have to figure out how to mod this code on your own though, it is more a programming 101 thing, not really a Blynk thing.

// ===== Timer Setup ( in void setup()  )=====
  PingTimer = timer.setInterval(500L, PingSensor);  // Ping distance routine - switched
  timer.disable(PingTimer);  // Disable timer until needed

// ===== Ultrasonic (Ping) Distance Sensor =====
BLYNK_WRITE(V29) {  // PING Activation Button
  pingSwitch = param.asInt();
  if (pingSwitch == 1) {
    timer.enable(PingTimer);
  } else {
    timer.disable(PingTimer);
  }
}  // END Blynk Function

void PingSensor() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration) / 58.2;
  if (distance >= 50 || distance <= 0) {
    Blynk.virtualWrite(V35, "N/A");
  } else {
    Blynk.virtualWrite(V35, distance); // in CM
  }  // END else
}  // End PingSensor Loop
1 Like

Thanks alot much appreciated… I’ll try using Millis to ease the the load on the Nodemcu and see how it goes.

I would avoid that unless you are very experienced with it. Simple Timer aka Blynk Timer was built to make working with millis() much easier. See Arduino Playground - HomePage

1 Like

Just learn to use BlynkTimer as recommended, repeatedly :wink: … then if you run into questions we might answer… otherwise if you try to reinvent the wheel… well… remember us when you are rich :stuck_out_tongue_winking_eye:

My example scans every 1/2 second… and if that is all you are running for timers, then you should be fine to leave it running… in fact even 1-5 second scans should be more than sufficient unless you have some serious water flow.

Thanks alot I really appreciate the time taken. I will keep yall updated

Just to clarify in this sketch V35 the virtual pin I can assign to any widget on the blynk app… Say for example the gauge widget. I just open the widget and assign it v35

Any display Widget, set to PUSH. I my case I think I used a Labeled Display.

Note: You can change all those vPins to fit your availability and needs in your project… This snippet came from a much bigger code, thus the higher vPin numbers.

Oks thanks