Will this work with Blynk 2.0 from esp8266 to esp32

I’m going to try and convert this to Blynk 2.0 with Edgent. Will my code work? I’m not good at coding. Any advise is greatly appreciated.

/* Liquid flow rate sensor - originally for Arduino as DIYhacking.com Arvind Sanjeev
   WFSensorV8.ino modifications by Paul Alcock aka Costas 5th October 2016.
   Licensed for the sole use of saurabh Gangamwar http://www.instructables.com/member/Saurabh%20Gangamwar47/instructables/
   Ebay:  https://m.ebay.in/itm/231904508264
   Instructables: http://www.instructables.com/id/How-to-Use-Water-Flow-Sensor-Arduino-Tutorial/step3/The-code/ 
   Measure the liquid/water flow rate using this code. 
   Connect Vcc and Gnd of sensor to ESP(Nodemcu), and the signal line to  Nodemcu D2.
   Connect Vcc and Gnd of Relay module to ESP(Nodemcu), and the control pin to  Nodemcu D3.
Widgets Pins
//#define BLYNK_DEBUG           // Comment this out to disable debug and save space
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
//#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Ticker.h>              //for LED status
Ticker ticker;

char auth[] = "d1a89c65c41c4565923b9e6ee0647ff1";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] ="TELUS4556";
char pass[] ="j78nfmuqi4";

SimpleTimer timer;
WidgetRTC rtc;         
WidgetTerminal terminal(V2);
WidgetLCD lcd(V7);
bool simulation = false;     

byte sensorInterrupt = 4;   // WeMos & NodeMCU D2
byte sensorPin       = 4;   // WeMos & NodeMCU D2
byte pumpInterrupt   = 5; //2;  //5;   // WeMos D1 ModeMCU D3
byte pumpPin         = 5; //2;  //5;   // WeMos D1 ModeMCU D3

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per litre/minute of flow.
float calibrationFactor = 4.5;
volatile byte pulseCount;  
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

#define averageperiod 5         // currently set to average the flow every 5 seconds
int countseconds = 0;           // count up to averageperiod
int averageflow = 0;            // used for averaging flow over averageperiod
bool notificationsent = false;  // to ensure just one message for each flow start
bool pumpState = false;         // pump is OFF on start up
bool masterState = false;       // 
bool flowoffprintonce = false;  // to ensure just one serial and terminal print for each flow stop
int rowIndex = 0;                //Saurabh
String currentDate  ;
String daybefore;
int rtctimer = 1; //check if RTC is OK and then disable / delete this #1 timer
int currentDatesi = 0;         //Saurabh simulation
int daybeforesi=currentDatesi;
int menu=0;
int s;

void setup()

 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);
  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, LOW);  // ACTIVE HIGH, pump relay set to OFF on restart
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;
  flowRate          = 0.0;
  if(simulation == true){
    pulseCount        = 47;
    pulseCount        = 0;

  // Configured to trigger on a FALLING state change (transition from HIGH state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);//FALLING

  // Configured to trigger on a CHANGE state change LOW to HIGH or HIGH to LOW
  attachInterrupt(pumpInterrupt, pumpToggle, CHANGE);
  Blynk.begin(auth, ssid, pass);
  terminal.println("Connected to Blynk");
  timer.setInterval(1000L, showFlow);
  timer.setInterval(100L, pumpControl);  // check if pump needs to be switched ON or OFF every 0.1s
  rtctimer = timer.setInterval(2000L, checkRTC);   // check every 2s if RTC is correct
void showFlow()  // average the flow over averageperiod
    detachInterrupt(sensorInterrupt);  // Disable the interrupt while calculating flow rate and sending the value to the host        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;
    // Add the ml passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print("\t");          // Print tab space
    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");        
    Serial.print("\t");       // Print tab space
    if(simulation != true){  
      pulseCount = 0; // Reset the pulse counter so we can start incrementing again
    if(countseconds > 0){    // used to skip the first rogue data flow reading
      averageflow = averageflow + flowRate;   // used to calculate the average flow over averageperiod cycles
    if(countseconds == averageperiod){
      Serial.print("Average water flow in litres / M is ");
      Serial.println(averageflow / averageperiod);
      Blynk.virtualWrite(V0, int(averageflow) / averageperiod);
      Blynk.virtualWrite(V1, totalMilliLitres/1000);
      countseconds = 0;  // reset the counter
      averageflow = 0;     // reset the average but only after chkFlow() function
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);  // Enable the interrupt again now that we've finished sending output

void chkFlow(){
  if((averageflow > 3) && (notificationsent == false)){  // guess of a decent water pressure
    Serial.println("Water IS flowing.");
    lcd.print(0,0,"Water is flowing");
    Blynk.email("Water Flow Sensor", "Water IS flowing.");
    Blynk.notify("Sensor: Water IS flowing.");
    notificationsent = true;         // stop getting messages until water stops flowing and starts again
    flowoffprintonce = false;        // when water stops flowing again we can restart serial and terminal print (once)
  if((averageflow <= 3)&& (flowoffprintonce == false)){
    Serial.println("Water is NOT flowing.");
    lcd.print(1,0,"Water is NOT");
    notificationsent = false;  // when water starts flowing again we can send another notification
    flowoffprintonce = true;      // stop serial and terminal prints after first pass of water stopping
 if(averageflow <= 3){
     digitalWrite(pumpPin, LOW);   // turn off pump //s*
if(averageflow>3 && menu == 1){
    digitalWrite(pumpPin, HIGH);   // turn on pump //s*
    Blynk.virtualWrite(V8, "ON");
    Blynk.virtualWrite(V4, 1);     // update app button on V4  COSTAS//s*

void pulseCounter()
  pulseCount++;  // Increment the pulse counter

void pumpToggle(){                // toggle just pumpState OFF and ON from pin interrupt
  pumpState = !pumpState;         // don't do anything else in this function or the system will crash
  //Serial.println(pumpState);      // for debugging only  TODO comment this out later     

void pumpControl()                // toggle pump OFF and ON
  detachInterrupt(pumpInterrupt);  // disable interrupt
  if(pumpState == masterState){
    // do nothing
    masterState = pumpState;
    if(pumpState == true){
    Blynk.virtualWrite(V4, 1);
    Blynk.virtualWrite(V8, "ON");
    // Blynk.setProperty(V8, "color","#48E06B");
    Serial.println("Pump turned ON"); 
      Blynk.virtualWrite(V4, 0);
      Blynk.virtualWrite(V8, "OFF");
   //   Blynk.setProperty(V8, "color","#04C0F8");
      Serial.println("Pump turned OFF"); 
  attachInterrupt(pumpInterrupt, pumpToggle, CHANGE);   // enable pump pin interrupt
void checkRTC(){
  if(year() != 1970){
    timer.disable(rtctimer);  // disable rtctimer now RTC is ok
    //rtcupdated = true; // can be commented out as checkRTC will stop when RTC is ok
    currentDate = String(day()) + "/" + month() + "/" + year();   // etc
    terminal.println("RTC started");
    timer.setInterval(60000L, table);   //start table() now RTC is OK
void table()                               //Saurabh
  currentDate = String(day()) + "/" + month() + "/" + year();  // etc
   if(currentDate != daybefore)
    //currentDate = String(day()) + "/" + month() + "/" + year();
    Blynk.virtualWrite(V10, "add", rowIndex,daybefore,totalMilliLitres/1000+String(" litre")); //Saurabh
    Blynk.virtualWrite(V1, 0);
    flowMilliLitres = 0;
    totalMilliLitres = 0;
BLYNK_WRITE(V3){   // reset with button in PUSH mode on virtual pin 3
  int resetdata = param.asInt();
  if(resetdata == 1){
    Serial.println("Clearing data");
    Blynk.virtualWrite(V0, 0);
    Blynk.virtualWrite(V1, 0);
    averageflow = 0;
    countseconds = 0;
    flowMilliLitres = 0;
    totalMilliLitres = 0;
BLYNK_WRITE(V20){   // reset with button in PUSH mode on virtual pin 20
  int resetdata = param.asInt();
  if(resetdata == 1){
    Serial.println("Clearing table data");
    Blynk.virtualWrite(V10, "clr");
BLYNK_WRITE(V4){   // Button in SWITCH mode on virtual pin 4 to control relay
  int controlRelay = param.asInt();
  if(controlRelay == 1){
    digitalWrite(pumpPin, HIGH);  // turn relay ON
   // Blynk.virtualWrite(V8, "ON"); 
    digitalWrite(pumpPin, LOW);  // turn relay OFF 
     //Blynk.virtualWrite(V8, "OFF"); 
  case 1:{
    lcd.print(1,0,"Automatic Mode");
  case 2:{
    lcd.print(3,0,"Manual Mode");
void loop(){

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


Thank You!

Do you fully understand what Edgent is?
Why do you want to turn this into an Edgent sketch rather than a Blynk IoT static provisioning sketch?

Blynk IoT has no Table widget, and RTC is done differently in IoT versus Bynk Legacy.
Notificatiins are also done very differently in Blynk IoT, so that part of the sketch will need re-working.

Sadly we’ve not seen @Costas on the forum for over 4 years, which is a pity.


I plan to use this project where the wifi credentials are unknown. Thinking edgent will be a lot easier
to switch networks without reprograming.


That’s correct.