gydota
March 12, 2017, 6:05pm
1
Hi everyone, I want to when the turn on widget button, message received from the PIR sensor.
char auth[] = "12844f188a8446c126c2c6c8cedd6915";
/* HC-SR501 Motion Detector */
#define ledPin 13
#define pirPin 5
int pirState;
int val = 0;
int x = 0;
SimpleTimer timer;
void setup()
{
Serial.begin(115200);
delay(10);
Blynk.begin(auth, ssid, pass);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
}
BLYNK_WRITE(V0)
{
int x = param.asInt();
}
void loop()
{
Blynk.run();
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
pirState = HIGH;
}
if (x == 1){
if (digitalRead(pirPin) == HIGH){
Blynk.notify("MOTION detect!!!");
}
}
} else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH){
pirState = LOW;
}
}
}
What am I doing wrong?
Gunner
March 12, 2017, 6:33pm
2
Please properly format sketch above. Otherwise it is difficult to read.
wanek
March 12, 2017, 6:54pm
3
@gydota , i would like to help, but can you straighten up the language a little bit? i do not understand what you want
@Gunner , can i steal this picture?
1 Like
gydota
March 12, 2017, 7:02pm
5
I tried, but bad know English
wanek
March 12, 2017, 7:03pm
6
hm, then maybe @Dmitriy , could you help please?
Eugene
March 12, 2017, 7:05pm
7
Code formatting is a mess to be honest. But there is at least one obvious bug: your global x variable is never gets assigned.
Change this:
BLYNK_WRITE(V0)
{
int x = param.asInt();
}
to this:
BLYNK_WRITE(V0)
{
x = param.asInt();
}
Gunner
March 12, 2017, 7:06pm
8
I believe he wants to enable or disable the PIR sensing with a button (in switch mode). But too much happening in the main loop.
wanek
March 12, 2017, 7:11pm
9
actually, it doesn’t seems to happen too much in main loop. there are some condition checks, that is hardly needs to much cpu time.
contrary to what a lots of people think, it is not necessary causing errors if you have lots of code in main loop. it depends what it does . for example in my current project i have around 500 lines of code in my main loop, and it works flawlessly with blynk! i think we should speak about these theories in some dedicated topics…
can we call that picture FTFC?
i guess you can figure out the meaning…
1 Like
gydota
March 12, 2017, 7:23pm
10
I thank everyone for the advice.
Made by this method and it worked
BLYNK_WRITE(V0)
{
x = param.asInt();
}
void loop()
{
Blynk.run();
pir();
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH){
pirState = LOW;
}
}
}
void pir(){
if (x == 1)
{
if (digitalRead(pirPin) == HIGH){
Blynk.notify("MOTION detect!!!");
}
}
}
3 Likes
hi mr …what is the board type? nodemcu?
why is not working for me…pir cannot response.can you share wiring schema?
thx
gydota
December 30, 2017, 1:02pm
14
try to use this code here
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "**************************";
char ssid[] = "*********";
char pass[] = "********";
char server[] = "192.168.1.100";// your local server, otherwise it should be commented
#define ledPin 16
#define pirPin 14
int pirState;
int val = 0;
int x = 0;
SimpleTimer timer;
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
}
BLYNK_WRITE(V0){
x = param.asInt();
}
void PIRval(){
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW){
pirState = HIGH;
}
else {
digitalWrite(ledPin, LOW);
}
if (pirState == HIGH){
pirState = LOW;
}
}
}
void pir(){
if (x == 1){
if (digitalRead(pirPin) == HIGH){
Blynk.notify("ALARM!!!");
}
}
}
void setup(){
Blynk.begin (auth, ssid, pass, server);//local server
// You can change server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
timer.setInterval(1000L, PIRval);
}
void loop(){
Blynk.run();
timer.run();
}
16= D0
14= D5
im edit
char server[ ]= "192.168.1.100";
to
//char server[ ] = "192.168.1.100";
and
Blynk.begin (auth, ssid, pass, server);
edit to (because im not use local server)
Blynk.begin (auth, ssid, pass);
but im move pin to D0 for led and D5 for pir input…R3 is blank (not use)
Why is not working for too me with nodemcu lolin?
something wrong?
thx for advice
gydota
January 2, 2018, 11:29am
16
I do not use resistors with PIR sensor
i will try again with your suggestion…can provide the wiring advice that works normally?
gydota
January 2, 2018, 11:49am
18
I will come to the house in the evening and again check the scheme for work capacity.
gydota
January 2, 2018, 7:26pm
19
made corrections and checked, now everything is working
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "**************************";
char ssid[] = "*********";
char pass[] = "********";
char server[] = "192.168.1.100";// your local server, otherwise it should be commented
#define ledPin 12
#define pirPin 14
int pirState;
int val;
int x;
SimpleTimer timer;
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
}
BLYNK_WRITE(V0){
x = param.asInt();
}
void PIRval(){
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
void pir(){
if (x == 1){
if (digitalRead(pirPin) == HIGH){
Blynk.notify("ALARM!!!");
}
}
}
void setup(){
Blynk.begin (auth, ssid, pass, server);//local server
// You can change server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
timer.setInterval(1000L, PIRval);
timer.setInterval(1000L, pir);
}
void loop(){
Blynk.run();
timer.run();
}
https://youtu.be/uVXJXGBoCHY
1 Like