NodeMCU ESP-12 PIR Sensor Publish to MQTT Broker on PI over Wifi
-
Ok, I've read and read and cannot find a suitable example that will dumb it down enough for me to comprehend so here I am.
I currently have OpenHab and my MQTT Broker running on a RPi-3. I also have multiple RPi-3s in closets around the house / land connected to SainSmart 16 Channel relays to turn on and off various lights. All this is working great. Now I wanted to add PIR sensors to some rooms so if a kid forgets to turn off their lights the PIR can via MQTT to the same relays. Also I have a tremendous wifi and switched network backend all done via Ubiquitys Unifi Switches and various Ubiquity wifi devices, enough to cover my whole 10 acres.
Because I do a lot with drones I was limited in my ability to utilize most all frequencies, even 433mhz range on any devices. Since I have a great wifi setup it made the most sense for me to use NodeMCUs with Wifi. I'm using the HC-SR501 PIR sensor connected VSS-3.3v, GND-GND, OUT-D0 but am clueless on how to configure it. I was able using the following sketch, from this site, to get the NodeMCU on the wifi-network but am clueless on how to setup the void loop. My current MQTT clients are RPi-3s running sensorReporter but this is greek to me. LOL Does anyone know of a good example I can use to make this work? I sure appreciate it.
// Enable debug prints to serial monitor #define MY_DEBUG // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h #define MY_BAUD_RATE 9600 // Enables and select radio type (if attached) //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #define MY_GATEWAY_MQTT_CLIENT #define MY_GATEWAY_ESP8266 // Set this node's subscribe and publish topic prefix #define MY_MQTT_PUBLISH_TOPIC_PREFIX "sensors/laundry/main" #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "sensors/landry/main" // Set MQTT client id #define MY_MQTT_CLIENT_ID "sensors_laundry" // Enable these if your MQTT broker requires usenrame/password #define MY_MQTT_USER "*********" #define MY_MQTT_PASSWORD "************" // Set WIFI SSID and password #define MY_ESP8266_SSID "Batcave" #define MY_ESP8266_PASSWORD "***********" // Set the hostname for the WiFi Client. This is the hostname // it will pass to the DHCP server if not static. // #define MY_ESP8266_HOSTNAME "sensors-laundry" // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP) #define MY_IP_ADDRESS 10,0,0,51 // If using static ip you need to define Gateway and Subnet address as well #define MY_IP_GATEWAY_ADDRESS 10,0,0,1 #define MY_IP_SUBNET_ADDRESS 255,255,255,0 // MQTT broker ip address. #define MY_CONTROLLER_IP_ADDRESS 10, 0, 0, 11 // The MQTT broker port to to open #define MY_PORT 1883 /* // Enable inclusion mode #define MY_INCLUSION_MODE_FEATURE // Enable Inclusion mode button on gateway #define MY_INCLUSION_BUTTON_FEATURE // Set inclusion mode duration (in seconds) #define MY_INCLUSION_MODE_DURATION 60 // Digital pin used for inclusion mode button #define MY_INCLUSION_MODE_BUTTON_PIN 3 // Set blinking period #define MY_DEFAULT_LED_BLINK_PERIOD 300 // Flash leds on rx/tx/err #define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin #define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin #define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED */ #include <ESP8266WiFi.h> #include <MySensors.h> void setup() { } void presentation() { // Present locally attached sensors here } void loop() { }```
-
@dwbro1 welcome to the MySensors community, great to have you here!
The sketch at https://www.mysensors.org/build/motion should have all the code you need. However, the hc-sr501 requires 5V to work properly (it does all sort of strange things if it does not get enough voltage) so you'll need a step-up or a 5V power supply.
-
If you search around there should be a mod for the pir sensor to make it run on 3,3v if you are up for the challenge
-
@gohan I had let that slip my mind. Getting 5v to the PIR sensor is not a big deal that part I can handle.
It's the sketch that scares the hell out of me. LOL.
I looked over the motion sketch linked and still not any clearer. Yes I see the basics of reading the pin but it is setup to publish to a gateway not directly to the broker as a client via my wifi.
What I need in the end which I know complicates the code greatly is:
- Sensor does not detect movement for x number of minutes then it sends "OFF" to that lights topic.
- Sensor detects movement and sends "ON" message to lights topic then starts to monitor for non-movement as above in number 1 (eventually, I'd like to add a light sensor and not have it come on when light levels too high).
-
Don't worry about it, it's one of the easiest sketches since it's a simple digital pin
-
@gohan "one of the easiest sketches" doesn't make me feel any better. hahahaha.. I sure appreciate any help. I have burned many brain cells getting this far with the Pi and sensorReporter the thought of plowing into another 6 month learning curve just turns me off right now and wife is starting to bitch at my half finished project. LOL
-
I looked over the motion sketch linked and still not any clearer. Yes I see the basics of reading the pin but it is setup to publish to a gateway not directly to the broker as a client via my wifi.
Yes it will publish to the gateway. As per your sketch above you have configured the gateway to publish to mqtt. That's it. No more work to be done