ESP8266 NodeMCU Multiple PIR Sensors?

Hi All,

I’m using a Fibaro motion sensor and a Aeotec Multisensor 6 in my config at the moment and I’ve decided to purchase a NodeMCU ESP8266 for use with a HC-SR501 PIR as a cheaper alternative to cover every room with a PIR (while the z-wave motion sensors mentioned above are VERY good, I simply can’t afford to cover the whole house in them)

Does anyone have any experience of adding more than one PIR to an ESP8266?

There’s a location in one room that would really suite 3 PIR’s (Bedroom with a joining walk-in-wardrobe and an Ensuite). While the boards are very cheap an I accept I COULD just build 3… powering 3 board separately would be more difficult than from one…

Does anyone know if I’d be able able power 3 PIR’s and get singnal info from x3 PIR’s with one ESP8266?

Was planning on adapting this type of thing or this one

If anyone’s interest, I’m starting to build a github of my config (work in progress, need to tidy my automations before sharing them)

Would really appreciate your help :slight_smile:

Thank you!

It depends on how you plan to power the sensors.

If you can power the sensors through the mains or a usb adaptor, you can keep the esp8266 running and you can poll the inputs too see what has changed.

If you try and power the sensors from a battery there are a couple of problems I have come across.

  • the time it takes for the esp8266 to wake up and establish a wifi connection was about 3s, which is too long for a PIR which was turning on the lights
  • the esp8266 still uses significant power even when sleeping, so battery life is not great.
  • for your application, you are limited by the number of inputs that can interrupt the sleep mode, and wake the processor.

you can use nodemcu or arduino with mysensors for this.
on a arduino you have only 2 interrupts possible but i got mine just in the main loop and it is working fine.
i dont know about the interrupts from the nodemcu

i think 3 PIR would go just as good as 1 PIR, 1 temperature and 2 switches (like i have)

You could also use a sonoff and add the PIR to the sonoff GPIO14, I did this for my bathroom light but you could adapt it to just send back a MQTT status, it depends on exactly what you want to do.

Thanks all.

I’d almost completely given up on battery powering the PIR sensors to be honest but I’ve just been looking at mysensors.

Has anyone got experience of using battery powered PIR mysensors? Seems to be mixed messages on battery life…

Also it needs to be FAST, I’m using the PIR to then trigger an automation to switch on a light in the room. Im guessing that going down the mysensors route the latency will be higher because there’s an extra hop (the gateway)…

If I’m going to mains power the sensors though, I don’t see the benefit of mysensors setup when I can just use an ESP8266 directly with the PIR and report to HASS via MQTT.

if you already use MQTT and have wifi all around the house and outside, then there are probably little advantages.

i dont use MQTT at all. mainly because i dont want my data to get outside and i dont want another server on my network.

i have a large place and not everywhere i have good wifi (even with 4 APs)
with mysensors i can reach out to every corner, because i can repeat the sensor values from one to the other.

mysensors is pretty fast. you think there is an extra hop, but thats not true.

sensor ==>> mysensor gateway ==>> hass
sensor ==>> network ==>> MQTT broker ==> network(if the broker isnt on the same server) ==>> hass

i have not used batteries with mysensors yet. i think it also depends on what kind of battery type you use and how you program it. if you use the interrupts the right way, you can put the sensor to sleep for most of the time, but if you use 3 PIRs on 1 device that probably wont work so then you are gonna use a lot more battery power.
a bigger batterypack could then solve the problem, but yeah it would be a bigger device.

Thanks @ReneTode , you’re right with the hops. the MQTT runs on same server as HASS so same amount of hops in theory.

I might give mysensors a go anyway and compare the speed with a MQTT ESP8266 sensor. There’s a few spots outside the house that a weak on wifi.

If I were to have a x3 PIR’s on one mysensor node. Would I be able to make each PIR report a unique state back to HASS? As I’d like to trigger 3 different lights to turn on (PIR Bathroom Light, PIR Wardrobe Light, PIR Bedroom Light)

Are you using the Arduino Mini Pro?

Cheers. Appreciate your help!

i use arduino nano. (there are about 20 running in my house right now)

for each PIR you would create a sensor in hass.
you could also use a lot of other sensors at the same time. to measure lightlevel, temperature, humidity, sound, or actually anything you could think of :wink:

Excellent. Sounds good to me.

I like the idea of having one node for multiple sensors. Although not sure if this would slow things down too much? for temp, light level, humidity it’s no big deal. Just PIR has to be fast.

I might end up building my PIR’s individually (I need 10 in total for the house).

And then putting the other non-PIR sensors on one node.

If I were to put x3 PIR’s on one mysensor, would I just apply the ino 3 times with different child id’s and pin input id’s or just add the 3 child ID’s and input i’d to one INO?

Cheers!

do you really think that you would notice it if your lights go on half a second or even a second after you come into a room?
we are talking about microseconds difference if you use other sensors on the same node as the PIR node.

if you put 3 PIRs on 1 node, you just write 1 sketch.
in that sketch you define 3 childIDs and connect that to 3 pinIDs.

a sketch for motion with 1 PIR and 1 temperaturesensor would look something like this:

#define MY_DEBUG 
#define MY_RADIO_NRF24
//#define MY_NODE_ID 10
//#define MY_RF24_CHANNEL 76 

#include <SPI.h>
#include <MySensors.h>  
#include <DallasTemperature.h>
#include <OneWire.h>


//##########################################
//  motion
//##########################################
#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define CHILD_ID 100   // Id of the motion sensor child
bool tripped = false;
bool lasttripped = false;
MyMessage msg_motion(CHILD_ID, V_TRIPPED);

//##########################################
// temperature
//##########################################
#define ONE_WIRE_BUS 6 // Pin where dallase sensor is connected 
#define TEMP_CHILD_ID 0
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature = 0;
float temperature = 0;
bool receivedConfig = false;
bool metric = true;
MyMessage msg_temp(0,V_TEMP);

//##########################################
// general (my personal touch)
//##########################################
#define CONTROLESWITCH 200
MyMessage msgcontroleswitch(CONTROLESWITCH, V_STATUS);
bool gemeld=false;
long time=0;

void before()
{
//##########################################
// temperature
//##########################################
  sensors.begin();
}

void setup()  
{ 
//##########################################
// temperature
//##########################################
  sensors.setWaitForConversion(false);
  
//##########################################
// motion
//##########################################
  pinMode(DIGITAL_INPUT_SENSOR, INPUT);      
}

void presentation() {
//##########################################
// general
//##########################################
  sendSketchInfo("kitchen", "1.1");
  present(CONTROLESWITCH,S_BINARY);

//##########################################
// temperature
//##########################################
 present(TEMP_CHILD_ID, S_TEMP);
  
//##########################################
// motion
//##########################################
 present(CHILD_ID, S_MOTION);
}

void loop()     
{     
//##########################################
// temperature
//##########################################
  sensors.requestTemperatures();
  wait(1000);
  temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(TEMP_CHILD_ID):sensors.getTempFByIndex(TEMP_CHILD_ID)) * 10.)) / 10.;
  if (lastTemperature != temperature && temperature != -127.00 && temperature != 85.00) {
    //send(msg_temp.setSensor(TEMP_CHILD_ID).set(temperature,1));
    lastTemperature=temperature;
  }

//##########################################
// motion
//##########################################
  if(digitalRead(DIGITAL_INPUT_SENSOR) == HIGH){
    tripped = true;
  } else {
    tripped = false;
  }
  if (lasttripped != tripped){
     send(msg_motion.set(tripped?"1":"0"));   
     lasttripped = tripped;
  }
  
//##########################################
// general
//##########################################  
  if ( not gemeld or millis()-time>300000){ //tell hass that its alive every 5 mins.
    melden();
    gemeld=true;
    time=millis();
  }
}


void receive(const MyMessage &message) {
  if (message.type==V_STATUS) {    
     if (message.sensor==CONTROLESWITCH){
        melden();
     }
  } 
}


void melden(){
  send(msgcontroleswitch.set(0));  
  send(msg_motion.set(tripped?"1":"0"));  
  send(msg_temp.setSensor(TEMP_CHILD_ID).set(temperature,1));
}

I agree. My usb powered nodemcu PIR works very well just like this.

I also have a mysensors PIR running with a 9V battery, but that battery has gone from 90% to 65% in less than a week, so I don’t think its going to work. The latest suggestion on the mySensors forum is to use 2 AA batteries and remove the power regulators from the chips, which will be my next experiment.

edit: I would share the code, but it is the one thing I lost when my hard disk crashed :cry:

Rene is right. We’re talking about microseconds timeout, not even half a second.
I have about 5 sensors based on NodeMCU in my network. All of them have PIR, light, temperature and humidity measurements.
For single NodeMCU you can connect up to 5 PIRs with ease and another 5 with some digging around. All of them can be powered directly from NodeMCU. Please note that PIRs you mentioned will have many false positives in case the distance between PIR and MCU is less than 0.5 meter. The issue here is the near WiFi signal source.
One more thing. There is no sense in using interrupts in this case: changed pin level lasts about 1 second minimum on PIR so you won’t miss it. And to react fast enough, I use 100ms timeout for PIR (don’t use sleep() - it will block your code). That means MCU checks PIR 10 times per second.
And the last thing - I strongly suggest to use MQTT. It can be installed on the same machine with HASS. You won’t notice any delay with sensors, but it will be so much convenience in programming and configuration that you won’t regret about this decision.

PS
WiFi and battery sucks. On 2 AA batteries PIR sensor won’t work more than 2 weeks.

Hi @Vladimir Thanks for your input.

I have Mosquiotto running on same server as HASS. Good information on the interrupts, thank you.

Regarding the cable length between the PIR and the NodeMCU are you saying I will get lots of false positives if the PIR is over 0.5m? If I need to keep the length below 0.5m then I think you have answered my questions, I build a NodeMCU/PIR for each location.

Again, many thanks :slight_smile:

Hi,

this is a litte off-topic, but maybe one of you guys has some experience with this…
I recently got a bunch of (my first) ESP8266 NodeMCU boards and some PIR sensors to experiment with.
So far this works pretty well and it integrates perfectly into HASS using MQTT.
Running on battery power would be nice, though.
So I found this article that shows some tricks to run an esp8266 on battery power for months: https://openhomeautomation.net/esp8266-battery/

The basic idea is to put the ESP into DeepSleep mode to reduce power consumption.
I haven’t had time yet to play with this.
I’m not sure how feasible this would be regarding the expected latency of a PIR sensor.
I also have no idea how much current the PIR sensor attached to the NodeMCU board draws.

As I understand it, the esp will need to reconnect to the wifi network every time it wakes up.
Also, it would be ideal it there was a way to wake the esp when the PIR registers a motion - instead of a periodically scheduled wakup-interval.

Has anyone tried something along those lines?

Sebastian

Hi Sebastian, I made one node based on pure ESP 8266 with deep sleep and 2 AA batteries. Node did send temperature every 10mins. All other time it was in deep sleep. This config lasts about 2 months.
In case of PIR you’ll face with some additional issues:

  • There is no easy way to attach PIR to wake up interrupt
  • PIR has some pre-heating time to become reliable. That’s about 1.5 sec
  • ESP takes about 1 sec to wake up, init sketch and connect to WiFi and MQTT

Assuming node wakes up every second and spends another 2.5 seconds to init and check PIR means about 3-4 sec of idle time. As for me that’s not acceptable for reliable security system.

Better options are:

  • Outlet powered NodeMCU
  • Battery powered Arduino mini 3.3V or even Attiny with 433 or RF transmitter

Yes, I went through exactly the same thought process, and set up the default esp environment to program in C and get into deepsleep mode (at the time nodemcu didn’t allow this).

The trouble was, it took 3-4 seconds for the esp8266 to come out of deep-sleep mode and connect to the wifi. I decided that was too long for a motion sensor, as I wanted it to turn on my lights when I walked in and wouldn’t want to wait that long in the dark.

I didn’t go any further and check out battery operation.

My attempt is at GitHub - gpbenton/esp8266-motion-sensor: An attempt to create a motion sensor using an esp8266 esp-01 module if you want too look.

you have to use interrupts if you want your esp to be sleeping to save power.
i tried things like that but then you cant have other sensors on the same node.
i avoid battery powered use.

Thanks guys! I’ll go with an usb-powered setup then :slight_smile:

Sebastian

Not based on ESP8266, but on MySensors…I have three of this at home, powered by 2xAA, and they are still at 100%, for at least 2 months.
They use the ATMega328 chip alone, with some capacitors and the NRF24 to communicate with my gateway.
If you don’t bother to DIY, they’re are so easy to hide.

2 Likes

Also just tried MySensors platform - that’s really great platform. For me the most advantages are possibility of using external interrupts to wake the sensor up. That means with binary sensors (up to 2) your device can sleep all the time, with minimal power consumption.
The only disadvantage is to have 1 extra radio module for communication. Unfortunately I didn’t find Arduinos with NRF24/RFM69 modules builtin on AliExpress. I’m aware of openhadware.io and Monteinos, but they’re a bit pricey