BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk not working help

I’m having a problem with my code, When I do it without blynk it works but when I add blynk it doesn’t work

Here are my codes with and without blynk

WITHOUT BLYNK

int sensor =13;
int buzzer = 0;
int sensorstate= 0;
//button
int buttonPin = 2;
int buttonState=0;
// input 1
int input1 =12;  //  
int input1state=0;
// input 2
int input2 =14;  //  
int input2state=0;
//input 3
int input3 =4;
int input3state=0;
int pin=16;
int pins=5;


void setup() {
 pinMode(buzzer,OUTPUT);
 pinMode(sensor,INPUT);
 pinMode(input1,INPUT);
 pinMode(input2,INPUT);
pinMode(input3,INPUT);
pinMode(pin,OUTPUT);
pinMode(pins,OUTPUT);
}
void sensorers(){
  
  sensorstate=digitalRead(sensor);
 if (sensorstate==HIGH)
 {
 digitalWrite(buzzer,HIGH);
 digitalWrite(pin,LOW);
 digitalWrite(pins,HIGH);
 }
 else{
 
  digitalWrite(buzzer,LOW);
   digitalWrite(pin,HIGH);
    digitalWrite(pins,LOW);
  }
  }
void callpanicbutton(){
  //own loop
 buttonState = digitalRead(buttonPin);
/////////
}
void callinput1(){
  //own loop
input1state = digitalRead(input1);
} 

void callinput2(){
  //own loop
input1state = digitalRead(input2);
} 
void callinput3(){
  //own loop
input1state = digitalRead(input3);
} 
void loop() {
 sensorers();
 callpanicbutton();
 callinput1();
 callinput2();
 callinput3();
}

WITH BLYNK

//sensors
int sensor =13;
int buzzer = 0;
int sensorstate = 0;

//button
int buttonPin = 2;
int buttonState=0;
#define BLYNK_PRINT Serial

//input 1
int input1 =12;  //  
int input1state=0;

//input 2
int input2 =14;
int input2state=0;

//input 3
int input3 =4;
int input3state=0;


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " uEv7YIPD4ytrrx9zFABHqS89vaG9gjro";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MBEVA";
char pass[] = "mashimbye";

BlynkTimer timer;

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

void callpanicbutton(){
 //own loop
 buttonState = digitalRead(buttonPin);

}
void setup()
{
  pinMode(input1,INPUT);
  pinMode(input2,INPUT);
  pinMode(input3,INPUT);
  pinMode(buzzer,OUTPUT);
 pinMode(sensor,INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  //Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}
void sensorers(){
  
  sensorstate=digitalRead(sensor);
 if (sensorstate==HIGH)
 {
 digitalWrite(buzzer,HIGH);
 Blynk.notify("MOVEMENT IN THE STORE");
 }
 else{
 
  digitalWrite(buzzer,LOW);
  }
  }
void callinput1(){
  //own loop
input1state = digitalRead(input1);
} 

void callinput2(){

 input2state = digitalRead(input2);
}

void callinput3(){
  //own loop
 input3state = digitalRead(input3);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  sensorers();
  callpanicbutton();
  callinput1();
  callinput2();
  callinput3();
}

Read this:

Pete.

Should I replace all functions(macros) in the loop by timers, will using multiple timers instead of macros fix the problem?

Having 3 Blynk.begin statements won’t work. If you’re using the Blynk cloud server then the last two should be commented-out or deleted.

Pete.

2 Likes

I read the article and I understand it as far as we are using one timer, but when we use two or more,I get confused, I din’t know how to put it in the code, and do we always use V1 to send our data to blynk?

Just add more timers like this, up which call different functions. Don’t have them all calling their functions at the same frequency though (once every second in this case) as they’ll clash with each other.

If you have multiple functions that need to be called at the same time then consider combining them onto one single function. If the function will take a while to execute then add some Blynk.run commands at various stages within the function, so that the Blynk library gets some processor time diving the execution of the function.

In your example, V1 has a labeled display widget attached to it, to show the uptime in seconds.
Attaching other widgets to the same virtual pin would be like attaching multiple devices to the same physical pin - it wouldn’t work!

Pete.

1 Like

I replaced all my macros/functions and used timers to call them but I’m getting errors about not declaring my functions. If you are not using blynk you dont have to declare them now I’m confused on why I should declare them. Did I do something wrong?

Here is my current code

//sensors
int sensor =13;
int buzzer = 0;
int sensorstate = 0;

//button
int buttonPin = 2;
int buttonState=0;
#define BLYNK_PRINT Serial

//input 1
int input1 =12;  //  
int input1state=0;

//input 2
int input2 =14;
int input2state=0;

//input 3
int input3 =4;
int input3state=0;

//timers call functions



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " uEv7YIPD4ytrrx9zFABHqS89vaG9gjro";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MBEVA";
char pass[] = "mashimbye";

BlynkTimer timer;
BlynkTimer timer1;
BlynkTimer timer2;
BlynkTimer timer3;
BlynkTimer timer4;
BlynkTimer timer5;
BlynkTimer timer6;

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}



void setup()
{
  pinMode(input1,INPUT);
  pinMode(input2,INPUT);
  pinMode(input3,INPUT);
  pinMode(buzzer,OUTPUT);
 pinMode(sensor,INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  //Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer2.setInterval(1300L, callpanicbutton);
  timer3.setInterval(1400L, sensors);
  timer4.setInterval(1700L, callinput1);
  timer5.setInterval(1900L, callinput2);
  timer6.setInterval(1800L, callinput3);
  
}

void callpanicbutton(){
 //own loop
 buttonState = digitalRead(buttonPin);

void sensors(){
  
  sensorstate=digitalRead(sensor);
 if (sensorstate==HIGH)
 {
 digitalWrite(buzzer,HIGH);
 Blynk.notify("MOVEMENT IN THE STORE");
 }
 else{
 
  digitalWrite(buzzer,LOW);
 }
}
void callinput1(){
  //own loop
input1state = digitalRead(input1);
} 

void callinput2(){

 input2state = digitalRead(input2);
}

void callinput3(){
  //own loop
 input3state = digitalRead(input3);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  timer1.run();
  timer2.run();
  timer3.run();
  timer4.run();
  timer5.run();
  timer6.run();
}

First of all, I didn’t say that you should create more BlynkTimer objects and assign just one timer to each object - and add run commands for each of those objects in the void loop. Which is why I said:

Each BlynkTimer object can support up to 16 different timers, so it just needs to look like this:

  //Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1300L, callpanicbutton);
  timer.setInterval(1400L, sensors);
  timer.setInterval(1700L, callinput1);
  timer.setInterval(1900L, callinput2);
  timer.setInterval(1800L, callinput3);

Secondly, when you’re posting about issues with code not compiling, you should include the EXACT compiler message. Otherwise, the only way for us to work-out what message you’re getting is to copy your code and compile it ourselves - not that much of an issue if we have all the correct libraries installed, but probably something that most people wont bother to do if it means installing other stuff.

In your case. the first error (which is often the most important one) was this:

In function 'void setup()':

sketch_dec05a:63:28: error: 'sensors' was not declared in this scope

   timer.setInterval(1400L, sensors);

                            ^

When you look at the lines of code immediately before your sensors function you see that it looks like this:

void callpanicbutton(){
 //own loop
 buttonState = digitalRead(buttonPin);

void sensors(){

It’s not immediately obvious, because of the way that you’ve arranged your curly brackets and not used indents effectively, but there is a closing curly bracket missing from the end of the callpanicbutton function.
If you’d arranged the code like this, it would be more obvious:

void callpanicbutton()
{
  buttonState = digitalRead(buttonPin);

void sensors()

This means that the compiler never sees the end of the callpanicbutton fnction, so all of the subsequent functions don’t exist as functions as far as it is concerned - hence the error messages.

Putting each opening and closing curly bracket on its own line, and using indents correctly make the code much more readable, especially when you’re using nested if/else/elseif statements and can save you hours of debugging.
On the subject of formatting, t’s also good/common practice to put all of your library include statements at the beginning of your code.
The final code looks like this:

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

//sensors
int sensor =13;
int buzzer = 0;
int sensorstate = 0;

//button
int buttonPin = 2;
int buttonState=0;

//input 1
int input1 =12;  //  
int input1state=0;

//input 2
int input2 =14;
int input2state=0;

//input 3
int input3 =4;
int input3state=0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "NEVER POST YOUR BLYNK AUTH CODE";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MBEVA";
char pass[] = "mashimbye";

BlynkTimer timer;

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

void setup()
{
  pinMode(input1,INPUT);
  pinMode(input2,INPUT);
  pinMode(input3,INPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(sensor,INPUT);
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  //Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1300L, callpanicbutton);
  timer.setInterval(1400L, sensors);
  timer.setInterval(1700L, callinput1);
  timer.setInterval(1900L, callinput2);
  timer.setInterval(1800L, callinput3);
}

void callpanicbutton()
{
  buttonState = digitalRead(buttonPin);
} // <<<<<<<<<< THIS CLOSING BRACKET WAS MISSING!

void sensors()
{
  sensorstate=digitalRead(sensor);
  if (sensorstate==HIGH)
  {
    digitalWrite(buzzer,HIGH);
    Blynk.notify("MOVEMENT IN THE STORE");
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
}

void callinput1()
{
  input1state = digitalRead(input1);
} 

void callinput2()
{
  input2state = digitalRead(input2);
}

void callinput3()
{
  input3state = digitalRead(input3);
}

void loop()
{
  Blynk.run();
  timer.run(); // Feeds the BlynkTimer
}

Pete.