From arduino sketch to blynk

Hi there,
I’m sorry to bother you all with probably stupid questions, but I don’t find a solid starting point.
I have been using Arduino for a few months, and I am currently using it for some projects, but i’m trying tu use it combined with blynk… and here I got some problems, probably because I still don’t have a solid basis for writing codes.

The problem is not communicating with Blynk, I succesfully used esp8266 for the easiest projects like turning on a led … etc and it works fine, but I don’t know how to carry, for example, this sketch that I wrote (and it’s working on Arduino uno stand alone) on arduino combined with ESP8266 and blynk.
Given that I have to keep clean void loop () function, how could I make what I written in it working?

I wrote this sketch to monitor the event of water losses on my irrigation system, and it consist in a water flow connected to the water tap. If I would, for example, display what I see on my LCD on the LCD on Blynk… where do I have to wrote my code?

this is my sketch I use with arduino Uno, water flow and LCD

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int X;
int Y;
float TIME = 0;
float FREQUENCY = 0;
float WATER = 0;
float TOTAL = 0;
float LS = 0;
const int input = A0;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(input, INPUT);
}

void loop()
{
  X = pulseIn(input, HIGH);
  Y = pulseIn(input, LOW);
  TIME = X + Y;
  FREQUENCY = 1000000 / TIME;
  WATER = FREQUENCY / 10.4;
  LS = WATER / 60;
  if (FREQUENCY >= 0)
  {
    if (isinf(FREQUENCY))
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("VOL:0.00");
      lcd.setCursor(0, 1);
      lcd.print("LITRI:");
      lcd.print( TOTAL);
      Serial.println ("ZEBROLLA");
    }
    else
    {
      TOTAL = TOTAL + LS;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("L/M: ");
      lcd.print(WATER);
      lcd.setCursor(0, 1);
      lcd.print("TOTAL:");
      lcd.print( TOTAL);
      lcd.print(" L");
      Serial.print( "VOL: "); Serial.print( WATER);                  
      Serial.print( "FREQUENCY: "); Serial.print( FREQUENCY);       
      Serial.print( "TOTALE: "); Serial.println( TOTAL);             
    }
  }
  delay(500);
}

I kow i have to start with

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

char auth[] = "123";
char ssid[] = "456";                            
char pass[] = "789";

#define EspSerial Serial
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

void setup()
{
Serial.begin(9600);
 EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
}

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

then I tryed with creating functions like “void water_flow” n which paste my previuos loop funcion, but… isn’t that so easy…

Could anyone tell me the way to go or some basic text to consult? I don’t want none to write me the sketch, otherwise I won’t learn anymore! just to figure out how to get out of this deadlock!
Thank you all!

you also need a blynk timer to call your function every x seconds.

1 Like

You can check out this example of DHT sensor to see how we use timer to call functions every specified interval time and send data to server as well as hardware.

Check this link for more info on Blynk timer.

Hope this helps. :smile:

2 Likes

Thanks all for the reply! Just a question… my waterflow sensor needs to read the frequency continuously to work properly, so I think it won’t work if it reads every x sec.
That’s why I wrote my code into loop () function.
Is there a way to make arduino reading continuously as written in the code below and sending to Blynk the result (as it is an LCD screen I.e.) every x sec?

{
X = pulseIn(input, HIGH);
Y = pulseIn(input, LOW);
TIME = X + Y;
FREQUENCY = 1000000 / TIME;
WATER = FREQUENCY / 10.4;
LS = WATER / 60;
if (FREQUENCY >= 0)
{
if (isinf(FREQUENCY))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“VOL:0.00”);
lcd.setCursor(0, 1);
lcd.print(“LITRI:”);
lcd.print( TOTAL);
Serial.println (“ZEBROLLA”);
}
else
{
TOTAL = TOTAL + LS;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“L/M: “);
lcd.print(WATER);
lcd.setCursor(0, 1);
lcd.print(“TOTAL:”);
lcd.print( TOTAL);
lcd.print(” L”);
Serial.print( "VOL: "); Serial.print( WATER);
Serial.print( "FREQUENCY: "); Serial.print( FREQUENCY);
Serial.print( "TOTALE: "); Serial.println( TOTAL);
}
}
delay(500);
}

Yes have a look at the examples for BlynkTimer.

1 Like

If you can use a digital pin rather than analogue, you could attach an interrupt to count the pulses over time.
There is at least one fairly recent example of a guy who hacked a meter by adding a reed switch to monitor water flow.

Pete.

2 Likes

Have you tried using your code in void loop()?

You may get away with just the calcs in there. I’d put the lcd stuff in a timer that gets called every x seconds to update the display/Blynk.

That’s the point! I was assuming it’s better to keep void loop () clean, but in this case I’m not sending anything to Blynk server… so… I think it will works! Good advice! Thanks!

1 Like

Your current void loop has this:

which is no different from putting your code in a new function and calling it with a timer every 500ms (except you’re less likely to get Blynk disconnections and device WDT resets that way).

It would be useful if you shared details of the water flow monitoring hardware that you’re using, as it seems that you don’t fully understand how it works and how the data coming from it should be sampled and interpolated.

Pete.

1 Like

This is my hardware specifications:

DN20 G3/4 Copper Water Flow Sensor Pulse Output 1.75Mpa 2~45L/min Flowmeter
Specification:
Model:SEN-HZ43WB
Flow range:2~45(m³/h)
Fit media:water
Working pressure:1.75MPa
Voltage range:DC4.5~18V
instantaneous flow pulse characteristics:F=[8.1Q-5]±10%, F means instantaneous pulse value(Hz),Q means instant flow
Max. Working current:15mA
Insulativity:insulation resistance≥100MΩ
Electric strength:AC 500V, 50Hz
Outside nominal diameter:G3/4(mm)
Precision grade:±10%
Type:vane-wheel type flowmeter
Connect way:Φ35*60/45mm length, G3/4 outside diameter water inlet and outlet.
Use voltage:DC 5V

(Even if with 8.1Q seems not measuring flows correctly, so I changed to obtain a perfect match between waterflow monitor and real water volume)
Thanks for your help!

Okay, this device uses a Hall-Effect sensor, which is basically the same as a solid state reed switch.
Here’s an example of how to use the device via an interrupt on a GPIO pin, rather than an analogue pin:

You should also take a look at this topic, which is the one I referred to earlier:

I’m sure that this approach will work with your device, with a bit of tweaking and calibration.

Pete.

1 Like