Can someone help me if the code is proper

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

char auth[] = "authentication token";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
const int waterSens = A0
;int SensorValue;
BlynkTimer timer; // Announcing the timer
void myTimerEvent()
{
  SensorValue = analogRead(A0);
  Blynk.virtualWrite(V5, SensorValue);
}
  void setup() 
  {
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "Password");
  pinMode(waterSens,INPUT);
  timer.setInterval(1000L,myTimerEvent);
  int sensorValue =        analogRead(waterSens);//read the sensor value
Serial.println(sensorValue);
if (sensorValue >= 500) 
  Serial.println("be careful");
  Blynk.notify("be careful");//send notification
  delay(1000);
}


void loop()
{ 
  Blynk.run();
timer.run(); // running timer every second
 }

This above is the code I am using to measure the water level sensor and push to the blynk app sadly I don’t have the code mcu (esp8266) and I need to make sure that this code works before I buy can someone help me …
When I complile it is perfect but don’t know if it work it would make my day if u can help

1 Like

This is not my question I did not put it in the void loop I put it in void setup

It will (or you’ll make some adjustments), but you’ll have to put all the code that is now in the loop into a function and call that with a timer. Aldo, you should grt rid of the delays… read what Pavel said…

1 Like

I noobie at this stuff I didn’t completely understand what u said but are trying to say to me to put all the timer to void setup?

No, not in the setup… you already setup a timer in the setup, add anither, create a new function and call that…

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

Everything else in functions and call that with timers.

But I already did that in the void loop I am so sorry if i u feel irritated so will this code work with node mcu

Read the document that @Pavel linked in his post, and keep re-Reading it until you understand if fully.

Then take a look at your code again and you’ll see that the void loop in your code has quite a lot of things that shouldn’t be there.

Pete.

Hey Pete I edited the void setup now isn’t it correct ?

Dear Pete so the blynk notify should be in void setup or the void loop

What is the purpose of this code at the end of your sketch?

Pete.

For serial print of the water sensor and for the blynk to notify me when the water sensor is over 500

Should I change it ?

As it stands, it will never execute.

Pete.

What why? Please help me or edit the code

It won’t execute because it’s not part of any function.

This isn’t a Blynk related thing, it’s basic C++, which is something you need to learn rather than asking for people to edit yourcode for you.

Your latest version (keep going back and editing your first post isn’t making this any easier to follow) will check your sensor once, at startup, then never again.

Pete.

Oh so will my other function exucute for pushing the value of the sensor to the blynk app ? Through V5

Yes, because it’s a proper function and is being called by a timer once every second.

By the way, there’s no point in defining the variable waterSens and assigning the value A0 to it if you don’t then use that variable when you’re doing the analogRead.
The same applied to your SSID and Wi-Fi password. You define variables to hold these, but use string literals in your Blynk.begin command instead.

Pete.

Pete.