Help with coding as learning more and more of Blynk, its great!

Im using an ultrasonic sensor to measure the depth of a tank it works really well. I have the depth value of the water PUSHING to V1. With this information i want to be able to trigger for instance, when V1 reaches a height of 112cm to turn digital output D4 on a nodeMcu On and when the reading goes down to say 20cm be able to turn off D4. This will drive a relay.

Any help with this idea would be greatly appreciated as slowly getting my head around Virtual Pins. It sounds easy and probably is but getting confused.

Thanks in advance for help.
Regards Richard

It would help if you shared the code (correctly formatted of course) that you’re running on on your NodeMCU.

Pete.

Thanks Pete new to this community ill post it and see what you think . Its a program that someone has developed but i suppose its open source.
Thanks Richard

Here is there the code I’m working but as am new to coding with blynk just getting used to Virtual Pins (a great concept) I’m looking to get V1 for example when it reads 112 cm it will trigger Output D4 on a Nodemcu High or (On) and when it reaches 20cm to trigger Output D4 Low (Off) which will inturn control a relay.
The code was developed and is working real well by and Open source designer… It is as follows

/*
 * Water_Level.ino
 *
 * Created: 19/09/2017 10:18:19 AM
 * Author: On Request
 * 
 * using  NodeMCU-12E v3 ESP8266 WiFi board and 2 x Waterproof HC-SR04 type ultrasonic sounders to measure the volume of water in 2 water tanks.
 * The sonar pings for the top of the water, gets the distance in cm, 
 * calculates depth of water in cm and then calculates volume of water in liters.
 * 
 * All the info is passed by WiFi on the WLAN to the Blynk server
 * Info is available on a phone using the Blynk App logged into the Blynk server.
 * 
*  pin mapping is different on these boards - CAUTION. Pin numbers are generally GPIO numbers
*  
 */

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NewPing.h>
#include <ArduinoOTA.h>                                                             //added for OTA
 
    #define SONAR_NUM 1                                         // Number of sensors. Change to suit your requirements.
    #define PI 3.1415926535897932384626433832795
    
    //** CHANGE TO SUIT TANK DIMENSIONS
    const int MAX_DISTANCE = 200;                              //max distance to measure
    const int Diameter1 = 10;		                               //internal Diameter of tank 1 in cm		    		   
    const int Depth1 = 170;			                               //total depth of tank 1 in cm	, from sensor to base	inside  

    const unsigned int Period = 2000;    //period between pings, in milliseconds. i.e  1 munute = 60,000. max 65535. Want longer? use unsigned long
    
    //** SENSOR PINS
  
    const int PingPin1 = 5;                                    //     GPIO5,  D1 
    const int EchoPin1 = 4;                                    //     GPIO4,  D2
    
    const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));	        //area of base of tank 1
    // Global variables
    int Litres1, Distance1, WaterDepth1;
    
    // Set password to "" for open networks.
    char ssid[] = "Networkname";		                                            //local wifi network SSID
    char pass[] = "Password";			                                            //local network password
    char auth[] = "AuthToken";                         // You should get Authority Token in your email.  




    BlynkTimer timer;                                                         //config timer

    NewPing sonar[SONAR_NUM] = {		                                          // Sensor object array.
	    NewPing(PingPin1, EchoPin1, MAX_DISTANCE),	                            // Each sensor's trigger pin, echo pin, and max distance to ping.
	     };

void sendSensorReadings()
{
//***********Readings Tank 1
    Distance1 = sonar[0].ping_cm();                                       //get distance to the top of the water tank 1
    if (Distance1 >= Depth1 || Distance1 == 0 )  Distance1 = Depth1;      //check it does not go negative
	WaterDepth1 = Depth1 - Distance1;							                          //calculate the depth of the water
	Litres1 = (Area1 * WaterDepth1) / 1000;	                                //calculate the volume of the water in litres
   


//***********SEND INFO TO BLYNK
    Blynk.virtualWrite(V1, WaterDepth1);                                    //send depth to Blynk server
    Blynk.virtualWrite(V2, Litres1);                                        //send litres to Blynk server
    Blynk.virtualWrite(V3, Litres1);                                        //send litres to Blynk server. for vertical level widget & chart, 
                                                                            //scaled to 1/10 as Blynk only goes up to 9999 and we need up to 16000
                                                                            //this project is scaled 1/1 for septic application
    delay(50);
   

    digitalWrite(13, HIGH);                                                //flash the LED on D7, just to let us know it's running
    delay(50);
    digitalWrite(13, LOW);

//************************* can be commented out, test use only
    Serial.println();
    Serial.println();
    Serial.println("Tank 1 water distance: " + String(Distance1));         //print depth
    Serial.println("Tank 1 water depth: " + String(WaterDepth1));          //print depth
    Serial.println("Tank 1 Litres: " + String(Litres1));                  //print litres

    
//***********************************************   
 
}

void setup() {
    ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });                           //added for OTA
    ArduinoOTA.begin();                                                                     //added for OTA
    
    pinMode(13, OUTPUT);                                                                   //LED D7
    
	timer.setInterval(Period, sendSensorReadings);		       // Setup a function to be called every n seconds
	delay(10);
  
//**    can be commented out, test only
    //Serial.begin(115200);										               // Open serial console. 
    //Serial.println();
    //Serial.println("Connecting to " + String(ssid));      // Connected to WiFi network
//**	
    Blynk.begin(auth, ssid, pass);
	delay(20);
    
//**    can be commented out, test only
	//Serial.println(WiFi.localIP());								          //this is local IP for this board
	//Serial.println("WiFi connected");

}

void loop() {
   
	Blynk.run();
	timer.run();
  ArduinoOTA.handle();                                     //added for OTA
}

Any help would be greatly appreciated

I merged your 2nd topic back to this initial topic… and properly formatted your code as you were requested, but didn’t…

Why not ask your previous designer to fix it for you?

@rich007 : from your code I think you are using the old newping library. Should you update to go with the latest one as below

#include <NewPingESP8266.h>

I just take an example from the concerned library fyi:

// ---------------------------------------------------------------------------
// Example NewPingESP8266 library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPingESP8266.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPingESP8266 setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

From your code as well as your expectation, you should not use the delay() when coding with Blynk based. In order to trigger the pump to be ON / OFF you need to add in the following code for those 2 situations:

This section of your code

    delay(50);
   

    digitalWrite(13, HIGH);                                                //flash the LED on D7, just to let us know it's running
    delay(50);
    digitalWrite(13, LOW);

should be revised as follows:

if (WaterDepth1 < 20 && digitalRead(13) == 1)
{
   digitalWrite(13, LOW);
}

if (WaterDepth1 > 112 && digitalRead(13) == 0)
{
   digitalWrite(13, HIGH);
}

If I am not wrong your pump is LOW triggered one? Then the above is ok for it. In case of HIGH triggered one, you should reverse the above bool values.

My apologize on not formatting the code correctly ill have a look at how that is done as its the first time Ive done it and thought cut and paste would have done it. Thanks again for Merging the topics and reformatting it.
Regards Richard

image

Sorry it’s taken sometime to get back on the community and thankyou for that modified code. It appears it will do just the job perfectly and I understand the code… Just getting my head around it all and was great… Thanks for your help.
Rich