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

PIR sensors motion with Neopixels and widget timer

esp8266
#1

Hello Blynkers I’m beginner for coding and my project is use the PIR sensor to detect the motion of human and send to NeoPixel 8 bit to activated the LED and while the object pass the sensor NeoPixel will be close (I use blue LED before NeoPixel to show if object come in range of PIR sensor , it will activate before NeoPixels) and I want to use blynk to create interface to set time to start/stop the NeoPixels and LED with the wifi.


The interface of blynk is :
LCD is pin V2 (It can use when motion is detected)
LedBlynk is pin V1(It can use when motion is detected)
Notification is worked
Timer is pin V0 (It can’t use I use the code in github http://docs.blynk.cc/#widgets-controllers-timer ) but I confuse about the code it’s working if I use only the Neopixel without PIR sensor but when connect the PIR to Neopixel it can’t work and I don’t understand in my confuse
My Project details :
• My Hardware is NodeMCU v2 with wifi
• I use Smartphone OS (iOS ) + version12.1.4
• Blynk server
• Blynk Library version is 0.6.0

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            D3

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char auth[] = "******";
char ssid[] = "******";
char pass[] = "******";
BlynkTimer timer;
int ledPin = D4; // choose the pin for the LED 
int ledPin1 = D2;
int inputPin = D0; // choose the input pin (for PIR sensor) 
int pirState = LOW; // we start, assuming no motion detected 
int val = 0; // variable for reading the pin status 
int delayval = 500; 
boolean stateled=0; 
boolean prevStateled=0;
WidgetLCD lcd(V2);
WidgetLED ledBlynk(V1);

void checkledstate()
{
  stateled=digitalRead(D4);
  if (stateled!=prevStateled)
  {
    if (stateled==0) Blynk.virtualWrite(D4,0); 
    if (stateled==1) Blynk.virtualWrite(D4,255); 
  }
  prevStateled=stateled;
}

void setup() {
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  timer.setInterval(300L, checkledstate);

  pinMode(inputPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  Serial.begin(115200);
  #if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code
  pixels.begin();
  pixels.show();
}

BLYNK_WRITE(V0)
{
  ***this I don't know where to put it and it will work****
}
void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
    getval();
  }
   timer.run();  
}
 
void getval(void){
  if (Blynk.connected()){
   //getval();
  val = digitalRead(inputPin); // read input value 
        if (val == HIGH) { // check if the input is HIGH 
          digitalWrite(ledPin, HIGH);
          delay(1000);
          digitalWrite(ledPin, LOW);
          delay(1000);
          ledBlynk.on();
          Blynk.virtualWrite(5,1023);
          lcd.clear();
          lcd.print(0,0,"Motion detected");
        for(int i=0;i<NUMPIXELS;i++){   // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
          pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
          pixels.show(); // This sends the updated pixel color to the hardware.
          delay(delayval);
          Blynk.virtualWrite(V13,255);
   }// Delay for a period of time (in milliseconds). turn LED ON 
          if (digitalRead(inputPin) == HIGH)  // we have just turned on 
            Serial.println("Motion detected!");
            Blynk.notify("Motion detected");   // We only want to print on the output change, not state
        }
    
    else {
        digitalWrite(ledPin, LOW); 
        ledBlynk.off();
        Blynk.virtualWrite(5,0);
        lcd.clear();
        lcd.print(0,0,"Motion end"); 
          for(int i=0;i<NUMPIXELS;i++){    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
            pixels.setPixelColor(i, pixels.Color(0,0,0)); // Moderately bright green color.
            pixels.show(); // This sends the updated pixel color to the hardware.
            delay(delayval);
            Blynk.virtualWrite(V13,0);  // turn LED OFF 
          // we have just turned of
            Serial.println("Motion ended!");// We only want to print on the output change, not state
                  
          }
        }
        }
}
BLYNK_CONNECTED() {
  Blynk.syncAll();
}

Thank you of all Solved answer :pray::pray::pray:

0 Likes

#2

16 people have viewed this topic but you’ve not had any replies yet, and I think the reason is that people don’t know where to begin!

Your code has a number of issues:

  1. you’re calling a function every time your void loop executes. This is no different to having that code within your void loop. That code contains delays and Blynk.virtualWrites, none of which are acceptable in the void loop.
  2. you have some very strange stuff happening with your timed function. It is checking the status of a GPIO pin to see if an LED is on, and mirroring the state of that pin to a virtual LED in your app. There no need to do this - simply update the physical LED and the LED widget at the same time.
  3. your code contains severe checks to see if it is running on AVR hardware. These aren’t needed when running on an ESP/NodeMCU device.
  4. you’re specifying port 8442 with the Blynk cloud server and library BlynkSimpleEsp8266.h I’m surprised that this works.

My assumption is that this code is cracked together from examples you’ve found on the internet, or written by someone else who is unfamiliar with Blynk.

I’d recommend that you follow some of the sketch builder examples to start from the basics. Look at using an interrupt on your PIR sensor, and use a simple LED rather than the neopixels. Once you have this working, and you’ve learned about C++ programming and Blynk then progress to building your project.

Pete.

2 Likes

#3

Thank you for your recommend @PeteKnight I’ll do it again and keep learning.

0 Likes

#4

OK , I’ve learned the code and NOW I can set timer by the timer widget at the first time BUT after that Blynk App is lost connection but in PIR sensor and NeoPixel are running the loop constantly everything should be done but I fault something I can’t find it . Please you give some suggestion , Thank you :pray:
I’m start from new example code and edit something from primary code, I set code NeoPixel to rainbow run before the loop of PIR sensor and set V0 is widget timer.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#define PIN            D3
#define NUMPIXELS      8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN ,NEO_GRB + NEO_KHZ800);

int delayval = 500;
char auth[] = "**********";
char ssid[] = "*******";
char pass[] = "************";

int motionPin = D0;       
int motionPin2 = D1;   
int senseMotion = 0;    
int senseMotion2 = 0;   


BLYNK_WRITE(V0) 
{
  Serial.print("Got a value: ");
  Serial.println(param.asInt());  
  if(param.asInt() == 0)
  {
    for(int i=1;i>param.asInt();i++){ 
      getval();   
    }
  }
  
  if(param.asInt() == 1)
  {
    getvar();    
 }
}

void  getval(){ 
    senseMotion = digitalRead (motionPin);     
    senseMotion2 = digitalRead (motionPin2);
    
      if (senseMotion == HIGH || senseMotion2 == HIGH) {              
          if (senseMotion2 == HIGH) {                       
          Serial.println("Motion Detect from sensor 2!!");
          colorWipe (strip.Color (30, 50, 255), 5);         
          strip.show();                                     
          delay (10000);                                    
          colorChase (strip.Color(255, 25, 25), 5);       
          colorChase2(strip.Color(25, 255, 25), 5);
          colorChase (strip.Color(25, 25, 255), 5);
          scanner(200, 0, 255, 1);
          colorWipe (strip.Color (0, 0, 0), 0);             
          strip.show();
                                          }                        
        
      if (senseMotion == HIGH) {                            
          Serial.println("Motion Detect !!");
          colorWipe (strip.Color(150, 250, 255), 5);
          strip.show();
          delay(10000);
          colorChase (strip.Color(255, 25, 25), 5);
          colorChase2 (strip.Color(25, 255, 25), 5);
          colorChase (strip.Color(25, 25, 255), 5);
          scanner(0, 200, 255, 150);
          colorWipe2 (strip.Color(0, 0, 0), 0);
          strip.show();
                                           }  
                                                      }
      else {                                      
          digitalWrite (motionPin, LOW);          
          digitalWrite (motionPin2, LOW);
          Serial.println("Motion End from sensor !!");
          colorWipe (strip.Color(0, 0, 0), 30);
          strip.show();
          delay(200);
          }
}
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
                                              }
                                          }                                                                                                  
void colorWipe2(uint32_t c, uint8_t wait) {
  for(uint16_t i=149; i<strip.numPixels(); i--) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
                                              }
                                          }                 
void rainbow(uint8_t wait) {
  uint16_t i, j;
  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
                                        }
    strip.show();
    delay(wait);
                        }
                           }
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
  for(j=0; j<256*5; j++) { 
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
                                        }
    strip.show();
    delay(wait);
                          }
                                }
   void colorChase(uint32_t c, uint8_t wait) {
   int i;                                                                                                  
    for(i=0; i<strip.numPixels(); i++)
      strip.setPixelColor(i, 0);                                                                                                 
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);                                                                  
      strip.show();                                                                              
      strip.setPixelColor(i, 0);                                                                  
      delay(wait);
                                        }
  strip.show();                                                                                  
                                            }                                           
   void colorChase2(uint32_t c, uint8_t wait) {
   int i;
                                                                                                 
    for(i=0; i<strip.numPixels(); i++)
      strip.setPixelColor(i, 0);                                                                                               
    for(i=59; i<strip.numPixels(); i--) {
      strip.setPixelColor(i, c);                                                                 
      strip.show();                                                                              
      strip.setPixelColor(i, 0);                                                                
      delay(wait);
                                        }
  strip.show();                                                                                 
                                            }                                            
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
   delay(100);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
   delay(100);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
   delay(100);
  }
}

void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
  int i, j, pos, dir;
  pos = 0;
  dir = 1;
  for(i=0; i<((strip.numPixels()-1) * 4); i++) {    
    strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
    strip.setPixelColor(pos, strip.Color(r, g, b));
    strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
    strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));
    delay(100);
    strip.show();
    delay(wait);
    for(j=-2; j<= 2; j++) 
      strip.setPixelColor(pos+j, strip.Color(0,0,0));
      delay(100);
    pos += dir;
    if(pos < 0) {
      pos = 1;
      dir = -dir;
    } 
    else if(pos >= strip.numPixels()) {
      pos = strip.numPixels() - 2;
      dir = -dir;
    }
  }
  
}

void getvar() 
{
   for(int i=0;i<NUMPIXELS;i++){
      strip.setPixelColor(i, strip.Color(0,0,0)); 
      strip.show(); 
      delay(delayval);
  }
} 

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(motionPin, INPUT);     
  pinMode(motionPin2, INPUT); 
  strip.begin();
  strip.show(); 
  rainbow (10);
  rainbowCycle (10);
  strip.show();
}

void loop()
{
  if(Blynk.connected()){
  Blynk.run();
  }
}

0 Likes

#5

Can I use the timer.run(); in void loop(); and create BlynkTimer timer;??

0 Likes

#6

You need to get rid of all those delay()’s. Yes look at timers but also search for blink without delay and look for an example that uses millis().

1 Like

#7

Thanks :smiley: @JustBertC

1 Like

#8

Now , I remove delay and change all code to millis() function connected to blynk and it’s can be work with widget timer, I set start time and stop time after that when function begin param.asInt() == 1 It’s connect constantly in but it can’t get out of timer.setInterval(1000L, NeoxPir); What should I do? Please Give me a suggestion How I can do it to success :pray::pray::pray:

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

#define NUM_PIXELS 8

const byte motionPin = D0;
const byte motionPin2 = D1;
const long eventTime_1_PIRxNeo1 = 1000;
const long eventTime_2_PIRxNeo2 = 1000;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
int senseMotion = 0;                                                                         
int senseMotion2 = 0;
char auth[] = "******";
char ssid[] = "******";
char pass[] = "******";
SimpleTimer timer;
SimpleTimer timer2;
bool Connected2Blynk = false;
int timerID;
int timerid;
 

uint32_t currentColor;
uint32_t currentColor2;
uint32_t currentColor3;
uint32_t currentColor4;
uint16_t currentPixel = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, D3, NEO_GRB + NEO_KHZ800);

void setup() {
  NeoxPir();
  currentColor = strip.Color(255,255,0);
  currentColor2 = strip.Color(0,0,0);
  currentColor3 = strip.Color(0,255,255);
  currentColor4 = strip.Color(0,0,0);
  currentPixel = 0;
  strip.begin();
  strip.show();
  pinMode(motionPin, INPUT);                                                                     
  pinMode(motionPin2, INPUT);
  Blynk.begin(auth, ssid, pass);
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
    
  Blynk.config(auth); 
  Blynk.connect(3333); 
  Serial.println("Connected to Blynk server");
  timer.setInterval(11000L, CheckConnection); 
}
  
  
void CheckConnection(){
  Connected2Blynk = Blynk.connected();
    if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server");
    Blynk.connect(3333); 
    }
    else{
    Serial.println("Connected to Blynk server");
    }
}

BLYNK_WRITE(V0)
{
   Serial.print("Got a value: ");
  Serial.println(param.asInt());
  
  if(param.asInt() == 1)
  {
    timer.setInterval(1000L, NeoxPir);
     
    
  }
  if(param.asInt() == 0)
  {
    timer.setInterval(1000L, NeoEndPir);
           
  }
}

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

void NeoxPir(){
   
  unsigned long currentTime = millis();
  senseMotion = digitalRead (motionPin);                                                     
  senseMotion2 = digitalRead (motionPin2);
  if ( currentTime - previousTime_1 >= eventTime_1_PIRxNeo1) {
    if (senseMotion == HIGH){
    Serial.println("PIR1: motion detected!!! ");
    colorWipe();
    }else{
    Serial.println("PIR1: motion end detected!!! ");
    colorWipe2();
    }
    previousTime_1 = currentTime;  
   
  }
  if ( currentTime - previousTime_2 >= eventTime_2_PIRxNeo2) {
    if (senseMotion2 == HIGH){
    Serial.println("PIR2: motion 2 detected!!! ");
    colorWipe();
    }else{
    Serial.println("PIR1: motion 2 end detected!!! ");
    colorWipe2();
    
    }     
   
    previousTime_2 = currentTime;
   }
}  


void NeoEndPir(){
  
  unsigned long currentTime = millis();
  senseMotion  = digitalRead (motionPin);                                                       
  senseMotion2 = digitalRead (motionPin2);
  if ( currentTime - previousTime_1 >= eventTime_1_PIRxNeo1) {
    if (senseMotion == LOW){
    Serial.println("PIR1: motion End detected!!! ");
    colorWipe2();
    }
    previousTime_1 = currentTime; 
  }
     
   
  
  if ( currentTime - previousTime_2 >= eventTime_2_PIRxNeo2) {
    if (senseMotion2 == LOW){
    Serial.println("PIR1: motion 2 end detected!!! ");
    colorWipe2();
    }     
   
    previousTime_2 = currentTime;
   }
  
}  

  

void colorWipe(){
    strip.setPixelColor(currentPixel,currentColor);
    strip.show();
    currentPixel++;
    Serial.println("Neo begin11");
    if(currentPixel == NUM_PIXELS){
      currentPixel = 0;
    }
  
}

void colorWipe2(){
    strip.setPixelColor(currentPixel,currentColor2);
    strip.show();
    currentPixel++;
    Serial.println("Neo Enddd");
    if(currentPixel == NUM_PIXELS){
      currentPixel = 0;
    }
  
  }

0 Likes

#9

Or it’s involve to my nodeMCU ?? If I change it to Raspberry pi , it will be complete?? , what code to use for loop NeoxPir and NeoEndPir in widget timer V0 :pray:t2::pray:t2:

0 Likes

#10

Search on the forum for “Timers” or “timers enable disable” etc - mainly how to enable and disable them - I think that is what you need - the nodemcu should be fine :wink:

Edit:

Check this out:

1 Like

#11

Thanks for suggestion @JustBertC :pray:t2: I hope it successful!!

0 Likes

#12

Yes , It’s work!!! Problem solved!!! Thanks for every recommendation :pray:

1 Like