Can we use Nodemcu to execute a sketch HC Sr 04

Hi gunner so this is my sketch. It’s basically the Nodemcu connected to a ultrasonic sensor which reads the level in a bank and projects it onto an Lcd screen as well as the blynk app. I also made it to be able to start automatically when the level goes to a preset value and stop when it’s filled up.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define TRIGGERPIN D1
#define ECHOPIN    D2
#define Relay    D5

SimpleTimer timer;

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

char auth[] = "f48c84ae790766cdb20eb0e";
char ssid[] = "RES";
char pass[] = "69";

#define PinV5;

void runEveryMinute() {

}

void setup() {
// Debug console
Serial.begin(9600);
pinMode(TRIGGERPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(D5, OUTPUT);
Wire.begin(2,0);
Blynk.begin(auth, ssid, pass);
// You can also specify server: //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
 lcd.init();   // initializing the LCD
 lcd.backlight(); // Enable or Turn On the backlight
}

void loop(){
  
Blynk.run();
timer.run(); 
long duration, distance, Level;  

digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(3);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(12);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = ((duration/2) / 29.1) - 16;
Level = (73-distance)*1.37;

Serial.print(distance);
Serial.println("Cm");

lcd.init();   // initializing the LCD
lcd.backlight(); // Enable or Turn On the backlight

lcd.print("Ullage : "); // Start Printing
lcd.print(distance);

lcd.setCursor(0,1);
lcd.print("Level  : ");
lcd.print(Level);
lcd.print(" %    ");
Blynk.virtualWrite(V5, Level);

if ( distance <= 0 ){
  digitalWrite(D5, HIGH);
 }
 else if ( distance >= 69) {
  digitalWrite (D5,LOW);
 }


delay(2000);
}

I tried to use timer as u suggested but it didn’t work. Could u have a look at my code. Thanks

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define TRIGGERPIN D1
#define ECHOPIN    D2
#define Relay    D5

SimpleTimer timer;

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

char auth[] = "f48cdbe5af84e790766cdb22e0e";
char ssid[] = "RTS";
char pass[] = "91";

#define PinV5;

void runEveryMinute() {

long duration, distance, Level;  

digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(3);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(12);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = ((duration/2) / 29.1) - 16;
Level = (73-distance)*1.37;

Serial.print(distance);
Serial.println("Cm");

lcd.init();   // initializing the LCD
lcd.backlight(); // Enable or Turn On the backlight

lcd.print("Ullage : "); // Start Printing
lcd.print(distance);

lcd.setCursor(0,1);
lcd.print("Level  : ");
lcd.print(Level);
lcd.print(" %    ");
Blynk.virtualWrite(V5, Level);

if ( distance <= 0 ){
  digitalWrite(D5, HIGH);
 }
 else if ( distance >= 69) {
  digitalWrite (D5,LOW);
 }

}

void setup() {
// Debug console
Serial.begin(9600);
pinMode(TRIGGERPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(D5, OUTPUT);
Wire.begin(2,0);
Blynk.begin(auth, ssid, pass);
// You can also specify server: //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
 lcd.init();   // initializing the LCD
 lcd.backlight(); // Enable or Turn On the backlight
}

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

}

In what way? No compile. Runs, but no display. Display, but incorrect result. Etc…

Break it down step by step… look at what does work, use comments and serial prints to monitor code progress, find the parts that don’t work and slowly work you way through them looking for syntax or logic errors, etc.

I try to help people understand how Blynk works, not troubleshoot their code. Honestly, I have a hard enough time troubleshooting my own code… :blush:

Thanks… Yea it does compile but doesn’t run… I’ll have a look at it

Of course it doesn’t work, you forgot to run every minute your “runEveryMinute” function…
Just add this line inside of setup function:

timer.setInterval(60000L, runEveryMinute);

where 60000L is 60 seconds

Simple timer is very simple, just define a function (runEveryMinute) set it run every minute (timer.setInterval) and call it on loop (timer.run) that’s all!

3 Likes

Yep… totally missed that :blush:

Sometimes there is no time… :wink:

1 Like

Thanks darkmoon that was it… I missed that… Should’ve checked the code more before posting… One question the readings stop displaying when the wifi signal is off… Shouldn’t it continue to read and display on the lcd??

Not the way you have it set up… search this forum for the many topics about keeping the sketch running when no internet, no WiFi, disconnected from Blynk etc. and look at eh Connection management options.

1 Like

Will do