I have been working on to my project i.e. contactless doorbell. I used node mcu esp8266 generic wifi module along with IR sensor Module to detect the movement, and buzzer to alert. Furthermore, my plan is to connect my esp8266 through wifi so that I could control my project online by just clicking a button and alert the user through buzzer and by notification. For this, I am using Blynk IoT application. Now the real trouble is the code for node mcu and blynk. I am going to use aurdino IDE to write and upload the code. Please help me with the code for the project. It should work on both object detection as well as through push nofication button on the blynk app. I have attached the circuit and connections for my project.
Do you have a working arduino sketch and you don’t know how to convert it into a blynk sketch, or don’t have a sketch at all?
Driving the buzzer directly from a GPIO pin by on the NodeMCU doesn’t sound like a great idea, although it does depend on the buzzer.
Do you have the coding skills to write a sketch which activate buzzer when the PIR detects movement?
If yes then post that sketch.
If no, then I’d suggest you do some learning about C++ coding.
Pete.
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…
- add the SimpleTimer library
- create a SimpleTimer object
- move the code minus teg delay, which isn’t needed now) into a function
- add a setInterval timer in your void setup, which will call your function every 1000ms
- add
timer.run();
to your void loop - test the code to make sure it works
- post your updated sketch here.
Pete.
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.
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.