Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. NodeMCU ESP-12 PIR Sensor Publish to MQTT Broker on PI over Wifi

NodeMCU ESP-12 PIR Sensor Publish to MQTT Broker on PI over Wifi

Scheduled Pinned Locked Moved Development
7 Posts 3 Posters 3.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dwbro1D Offline
    dwbro1D Offline
    dwbro1
    wrote on last edited by
    #1

    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()
    {
    
    }```
    mfalkviddM 1 Reply Last reply
    0
    • dwbro1D dwbro1

      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()
      {
      
      }```
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0
      • gohanG Offline
        gohanG Offline
        gohan
        Mod
        wrote on last edited by
        #3

        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 😀

        dwbro1D 1 Reply Last reply
        0
        • gohanG gohan

          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 😀

          dwbro1D Offline
          dwbro1D Offline
          dwbro1
          wrote on last edited by dwbro1
          #4

          @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:

          1. Sensor does not detect movement for x number of minutes then it sends "OFF" to that lights topic.
          2. 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).
          mfalkviddM 1 Reply Last reply
          0
          • gohanG Offline
            gohanG Offline
            gohan
            Mod
            wrote on last edited by
            #5

            Don't worry about it, it's one of the easiest sketches since it's a simple digital pin 😁

            dwbro1D 1 Reply Last reply
            0
            • gohanG gohan

              Don't worry about it, it's one of the easiest sketches since it's a simple digital pin 😁

              dwbro1D Offline
              dwbro1D Offline
              dwbro1
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • dwbro1D dwbro1

                @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:

                1. Sensor does not detect movement for x number of minutes then it sends "OFF" to that lights topic.
                2. 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).
                mfalkviddM Offline
                mfalkviddM Offline
                mfalkvidd
                Mod
                wrote on last edited by mfalkvidd
                #7

                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 :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                15

                Online

                11.7k

                Users

                11.2k

                Topics

                113.1k

                Posts


                Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • MySensors
                • OpenHardware.io
                • Categories
                • Recent
                • Tags
                • Popular