IoT Motion Detector With NodeMCU and BLYNK

1 Like

Hi,@mjrovai i have a few questions about your code. I am going to include it here:

/**************************************************************
 * IoT Motion Detector with Blynk
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 * 
 * Developed by Marcelo Rovai - 30 November 2016
 **************************************************************/
#include <ESP8266WiFi.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
char auth[] = "YOUR AUTH CODE HERE";
/* WiFi credentials */
char ssid[] = "YOUR SSID";
char pass[] = "YOUR PASSWORD";
/* HC-SR501 Motion Detector */
#define ledPin D7 
#define pirPin D1 // Input for HC-S501
int pirValue; // Place to store read PIR Value
void setup()
{
  Serial.begin(115200);
  delay(10);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
  digitalWrite(ledPin, LOW);
}
void loop()
{
  getPirValue();
  Blynk.run();
}
/***************************************************
 * Get PIR data
 **************************************************/
void getPirValue(void)
{
  pirValue = digitalRead(pirPin);
  if (pirValue) 
  { 
    Serial.println("==> Motion detected");
    Blynk.notify("T==> Motion detected");  
  }
  digitalWrite(ledPin, pirValue);
}

I understand you are running the getPirValue function from the loop, without any delays. So, wouldnt that cause flood problems, since the motion detector sends data continuously (I presume), and therefore blynk notifications are supposedly happening continuously? Please correct me if I am wrong. I am waiting for the motion detector to arrive, so I cant test the code. Thanks!

2 Likes

The code you are looking at will work as is, but mainly because a PIR only goes high when motion is actually detected…

However if constant motion is occurring then yes you are correct, it could flood… particularly on a ESP, as they tend to be more picky on communication disruption.

A more “Blynkified” code solution would be to move the getPirValue(); into a >SimpleTimer< loop, say every 500-1000ms, instead of the main void loop().

1 Like

Thats what i thought, thanks.
So i assume you are recommending to use the simpletimer loop as a rule of thumb. For instance if I want to use the motion detector to turn on the lights in a bathroom, the provided code wouldn’t be perfect?

With SimpleTimer it would. SimpleTimer has become more than a rule of thumb. It’s basically groundwork. The foundation of a solid Blynk project :slight_smile:

Yes, setting up timers for Blynk is pretty much a fundamental need for anything beyond the basic single task code.

Both to prevent flooding from too much data at once and prevent disconnects; Since Blynk itself is constantly running in the background… anything that blocks Blynk’s constant communication link with the server for too long (a second or so) can cause those disconnects.

Thanks again, @Gunner.
Additional question, just how often is it a good idea to call a function via simpleTimer? I mean would calling getPirValue every 200ms be bad?

That will be a subjective issue… it depends on the time a function would take, how often that function really needs to be called, as well as how many other timers are going off at the same time.

In the case of the PIR, I can’t see any reason for less than 1/4 second (250ms). But honestly if that is all you have running then the best way is to test it and see.

Thanks, this clarifies things.

You should really look at using an interrupt to detect the change.

Then it would be trigger when PIR motion is detected instead of a timed check.

So in this case, I would recommend against using SimpleTime and instead catch the RISING/FALLING or CHANGE state depending on what retriggering settings you’re using on your PIR sensor.

I remember reading sometime that attaching interrupts to pins on a esp8266 device was not a good thing, is this not the case anymore?

Either option of Interrupt or digitalRead() is a signal detection method, not a timing method. Regardless of how you detect the signal you still don’t want any rapid movement (at whatever rate these cheap PIRs can clock at) to cause a flood, thus the timer recommendation.

The advantage (albeit slim) of the interrupt (assuming it doesn’t cause other conflicts) is that it might catch quick movement that even a 1/4 second scan might not… or will it even matter on these little things?

Thanks a lot Gunner.
I only tested the project on my lab and it worked fine. For a simple home alarm system, the project would work as it, but using SimpleTimer is a much better solution for sure! BTW, I will update my posts on Instructables.com and Hackster.io to clarify it. On a more complex projects (for example my last one: https://www.hackster.io/mjrobot/automatic-gardening-system-with-nodemcu-and-blynk-47f040) would be impossible to run them without using SimpleTimer.

2 Likes

not working for me…pir cannot send motion data on apps n led…