Query related to my IoT project

I have a sketch that detect the obstacle using IR sensor and it works. The problem is the code to configure with the app.

Post your code, and assuming that you have the coding skills to understand what you’ve posted then we’ll help steer you in the right direction with Blynk.

Pete.

const int irPin = 5;
const int buzzerPin = 16;

void setup() {
Serial.begin(9600);
pinMode(irPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

int irValue;

void loop() {
  irValue = digitalRead(irPin);
Serial.println("Irvalues are here");
Serial.println(irValue);

if(irValue == 1) {
  digitalWrite(buzzerPin, HIGH);
}
else  {
  digitalWrite(buzzerPin, LOW);

}

delay(1000);
}

This is just a code for obstacle detection without wifi. Just the detection of any object using IR module

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

Pete.

done

Okay, first step is to make that void loop Blynk friendly by using a SimpleTimer to clean the loop up and remove the need for the delay() command.

Take a read of this…

and…

  1. add the SimpleTimer library
  2. create a SimpleTimer object
  3. move the code minus teg delay, which isn’t needed now) into a function
  4. add a setInterval timer in your void setup, which will call your function every 1000ms
  5. add timer.run(); to your void loop
  6. test the code to make sure it works
  7. post your updated sketch here.

Pete.

1 Like

ok sir, I will let you know.

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>


#include <SimpleTimer.h>

#define BLYNK_TEMPLATE_ID "TMPL3zFXMu5S"

#define BLYNK_DEVICE_NAME "Contactless Doorbell"

#define BLYNK_AUTH_TOKEN "iiChfcvEhTGQym4Yna7XyoeI-UdrRJl8"

#define BLYNK_PRINT Serial

char auth[] ="iiChfcvEhTGQym4Yna7XyoeI-UdrRJl8";

char ssid[] = "*****";

char pass[] = "*****";

const int irPin = 5;

const int buzzerPin = 16;

SimpleTimer timer;

void setup() {

Serial.begin(115200);

Blynk.begin(auth, ssid, pass);

pinMode(irPin, INPUT_PULLUP);

pinMode(buzzerPin, OUTPUT);

pinMode(V0, INPUT);

timer.setInterval(1000L, Sensor);

  timer.setInterval(1000L, FromApp);

}

void Sensor()

{

while( digitalRead(irPin) == LOW)

{

  digitalWrite(buzzerPin, HIGH);

  delay(1000);

  Blynk.begin(auth, ssid, pass);

}

 

}

void FromApp()

{

 if(digitalRead(V0) == HIGH)

 {

digitalWrite(buzzerPin, HIGH);

 }

 if(digitalRead(V0) == LOW)

 {

digitalWrite(buzzerPin, LOW);

 }

   

}

void loop() {

 

timer.run();

Blynk.run();

}

This is the code that I wrote but got an unknown error when verified.

Should be at the top of your sketch (the first lines).

Change it to

char auth[] = BLYNK_AUTH_TOKEN;

I’d suggest using BlynkTimer instead of SimpleTimer.

You didn’t declare a variable called V0. If I understand correctly, V0 is attached to a button widget, and you would like to use it to control the buzzer. If so, then you should use BLYNK_WRITE(vPIN), which is a function called every time device gets an update of Virtual Pin value from the server or app. For example,

BLYNK_WRITE(V0) // this command is listening when something is written to V0
{
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  
  if (pinValue == 1){
   // do something when button is pressed;
  } else if (pinValue == 0) {
   // do something when button is released;
  }
  
  Serial.print("V0 button value is: "); // printing value to serial monitor
  Serial.println(pinValue);
}

And delete this

void FromApp()

{

 if(digitalRead(V0) == HIGH)

 {

digitalWrite(buzzerPin, HIGH);

 }

 if(digitalRead(V0) == LOW)

 {

digitalWrite(buzzerPin, LOW);

 }

   

}

And this

timer.setInterval(1000L, FromApp);

You shouldn’t use delay because it’s a blocking function.

Blynk.begin() belongs to the void setup. You have one already, so you can remove it from the void sensor function.

2 Likes

Edited sketch:

#define BLYNK_TEMPLATE_ID "TMPL3zFXMu5S"
#define BLYNK_DEVICE_NAME "Contactless Doorbell"
#define BLYNK_AUTH_TOKEN "iiChfcvEhTGQym4Yna7XyoeI-UdrRJl8"
#define BLYNK_PRINT Serial


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





char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "*******";
char pass[] = "******";


const int irPin = 5;
const int buzzerPin = 16;

BlynkTimer timer;


void setup() {
Serial.begin(115200);

Blynk.begin(auth, ssid, pass);

pinMode(irPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);


timer.setInterval(1000L, Sensor);
 

}

BLYNK_WRITE(V0) 
{
  int pinValue = param.asInt(); 

  if (pinValue == 1){
  
  } else if (pinValue == 0) {
   
  }
  
  Serial.print("V0 button value is: ");
  Serial.println(pinValue);
}


void Sensor()
{

while( digitalRead(irPin) == LOW) 
{
  digitalWrite(buzzerPin, HIGH);
  
  
}



  
}



void loop() {
  

timer.run();
Blynk.run();
}




If you re-read the steps that I gave here…

You’ll see that I didn’t make any mention of adding any Blynk code into your sketch. I just wanted you ro restructure your exiting code into a format that was more appropriate for use with Blynk.
Instead, you’ve jumped ahead several steps without really understanding what you are doing. You’ve also kept the delay() command and in addition added a while() command - neither of which are compatible with Blynk.

If you want me to guide you through the process then go back to your original sketch and di exactly what I told you to do in post #10

Pete.

#include <SimpleTimer.h>


SimpleTimer timer;



const int irPin = 5;
const int buzzerPin = 16;

void setup() {
Serial.begin(9600);
pinMode(irPin, INPUT);
pinMode(buzzerPin, OUTPUT);
timer.setInterval(1000L, Sensor);
}

int irValue;

void Sensor() {

irValue = digitalRead(irPin);
Serial.println("Irvalues are here");
Serial.println(irValue);

if(irValue == 1) {
  digitalWrite(buzzerPin, HIGH);
}
else  {
  digitalWrite(buzzerPin, LOW);

}


void loop() {

timer.run();
  
}

and ?

Pete.

displays error

exit status 1

Compilation error: no matching function for call to 'SimpleTimer::setInterval(long int, void (&)())'


I don’t think that the code you posted in post #16 and the code you are compiling to produce that error message are the same.

Pete.

I don’t know but my arduino IDE is not compiling any code. It shows a huge list of something when compile and at the end it shows like this:

exit status 1

Compilation error: exit status 1

But which code are you compiling?
The code in post #16 is missing a closing curly bracket at the end of your void Sensor() function, but that doesn’t produce the error message you posted in post #18

Issues like missing curly brackets are much easier to spot if you use indents sensibly, and if/else statements are much easier to read…

void Sensor()
{
  irValue = digitalRead(irPin);
  Serial.println("Irvalues are here");
  Serial.println(irValue);

  if(irValue == 1)
  {
    digitalWrite(buzzerPin, HIGH);
  }
  else
  {
    digitalWrite(buzzerPin, LOW);
  }
<<< no closing curly bracket here!!

Pete.

Yes, I noted the missing curly braces before compiling and corrected it. Still got the error in post #17

Post the EXACT code that you are compiling, and details of what board type you are using and what board type you have selected in the IDE.

Also, post the full compiler output, and use triple backticks in the same way as when you post code.

Pete.

The code is:

#include <SimpleTimer.h>


SimpleTimer timer;



const int irPin = 5;
const int buzzerPin = 16;

void setup() {
Serial.begin(9600);
pinMode(irPin, INPUT);
pinMode(buzzerPin, OUTPUT);
timer.setInterval(1000L, Sensor);
}

int irValue;

void Sensor() {

irValue = digitalRead(irPin);
Serial.println("Irvalues are here");
Serial.println(irValue);

if(irValue == 1) {
  digitalWrite(buzzerPin, HIGH);
}
else  {
  digitalWrite(buzzerPin, LOW);

}

}


void loop() {

timer.run();
  
}


The board type that I am using is Node MCU ESP8266 WIFI .
The selected board in the IDE is Generic ESP8266 Module.