Skip to content
  • 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. Combined motion sensor with battery monitor sketch
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

Combined motion sensor with battery monitor sketch

Scheduled Pinned Locked Moved Development
6 Posts 2 Posters 849 Views 2 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.
  • G Offline
    G Offline
    grumpazoid
    wrote on last edited by
    #1

    I am using Domoticz with serial gateway. I have a motion sensor with voltage detect circuit.

    I have combined the motion and battery sketches and I can get a reading in Domoticz.

    However it only updates the battery reading when the motion sensor triggers.

    Maybe this is a Domoticz similar to another issue I am having https://forum.mysensors.org/topic/9967/detect-missing-unresponsive-sensor

    I'd be grateful if someone could see if my sketch seems ok.

    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    
    uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    
    // uint32_t SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
    int oldBatteryPcnt = 0;
    
    void setup()
    {
    	pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    
      // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
      analogReference(INTERNAL1V1);
    #else
      analogReference(INTERNAL);
    #endif
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Battery Meter", "1.0");
    
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("Motion Sensor", "1.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID, S_MOTION);
    }
    
    void loop()
    {
    	// Read digital motion value
    	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
    	Serial.println(tripped);
    	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
    
    	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    
      // get the battery Voltage
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
    #ifdef MY_DEBUG
      Serial.println(sensorValue);
    #endif
    
      // 1M, 470K divider across battery and using internal ADC ref of 1.1V
      // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
      // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
      // 3.44/1023 = Volts per bit = 0.003363075
    
      int batteryPcnt = sensorValue / 10;
    
    #ifdef MY_DEBUG
      float batteryV  = sensorValue * 0.003363075;
      Serial.print("Battery Voltage: ");
      Serial.print(batteryV);
      Serial.println(" V");
    
      Serial.print("Battery percent: ");
      Serial.print(batteryPcnt);
      Serial.println(" %");
    #endif
    
      if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
    
      }
    }```

    Raspberry Pi 3B - Domoticz + Node Red
    Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
    Arduino Mega, RFLink 433Mhz
    Sonoff/ESP8266/Tasmota switches

    mfalkviddM 1 Reply Last reply
    0
    • G grumpazoid

      I am using Domoticz with serial gateway. I have a motion sensor with voltage detect circuit.

      I have combined the motion and battery sketches and I can get a reading in Domoticz.

      However it only updates the battery reading when the motion sensor triggers.

      Maybe this is a Domoticz similar to another issue I am having https://forum.mysensors.org/topic/9967/detect-missing-unresponsive-sensor

      I'd be grateful if someone could see if my sketch seems ok.

      
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_RF24
      //#define MY_RADIO_NRF5_ESB
      //#define MY_RADIO_RFM69
      //#define MY_RADIO_RFM95
      
      #include <MySensors.h>
      
      uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
      #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
      #define CHILD_ID 1   // Id of the sensor child
      
      // Initialize motion message
      MyMessage msg(CHILD_ID, V_TRIPPED);
      
      int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
      
      // uint32_t SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
      int oldBatteryPcnt = 0;
      
      void setup()
      {
      	pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      
        // use the 1.1 V internal reference
      #if defined(__AVR_ATmega2560__)
        analogReference(INTERNAL1V1);
      #else
        analogReference(INTERNAL);
      #endif
      }
      
      void presentation()
      {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Battery Meter", "1.0");
      
      	// Send the sketch version information to the gateway and Controller
      	sendSketchInfo("Motion Sensor", "1.0");
      
      	// Register all sensors to gw (they will be created as child devices)
      	present(CHILD_ID, S_MOTION);
      }
      
      void loop()
      {
      	// Read digital motion value
      	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      
      	Serial.println(tripped);
      	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
      
      	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
      	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
      
        // get the battery Voltage
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
      #ifdef MY_DEBUG
        Serial.println(sensorValue);
      #endif
      
        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
        // 3.44/1023 = Volts per bit = 0.003363075
      
        int batteryPcnt = sensorValue / 10;
      
      #ifdef MY_DEBUG
        float batteryV  = sensorValue * 0.003363075;
        Serial.print("Battery Voltage: ");
        Serial.print(batteryV);
        Serial.println(" V");
      
        Serial.print("Battery percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
      #endif
      
        if (oldBatteryPcnt != batteryPcnt) {
          // Power up radio after sleep
          sendBatteryLevel(batteryPcnt);
          oldBatteryPcnt = batteryPcnt;
      
        }
      }```
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @grumpazoid the debug output from the node will show what the node is sending. From that it should be easy to determine if it is a sketch problem or a Domoticz problem.

      G 1 Reply Last reply
      0
      • mfalkviddM mfalkvidd

        @grumpazoid the debug output from the node will show what the node is sending. From that it should be easy to determine if it is a sketch problem or a Domoticz problem.

        G Offline
        G Offline
        grumpazoid
        wrote on last edited by
        #3

        @mfalkvidd Good point. You are right I can see a value relating to the battery being sent in the serial monitor.

        It is domoticz not updating "last seen" until it sees a "1" from the sensor.

        Raspberry Pi 3B - Domoticz + Node Red
        Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
        Arduino Mega, RFLink 433Mhz
        Sonoff/ESP8266/Tasmota switches

        1 Reply Last reply
        1
        • G Offline
          G Offline
          grumpazoid
          wrote on last edited by
          #4

          Ok so what I have done is added in a child id to separately display the state of the battery.

          I followed the thread here https://forum.mysensors.org/topic/6048/reporting-battery-to-domoticz

          I now have a new item in the domoticz utility section that shows the current battery state and I can get a graph displayed.

          I have used :

          definitions: #define CHILD_ID_BATTERY 2
          MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
          in presentation: present(VOLTAGE_CHILD_ID, S_MULTIMETER, "Battery level" );
          sending: send(msgbatt.set(batteryPcnt ,2)); //send battery in Volt 2 decimal places

          i don't really understand all of the above. if anyone can point me to an explanation that would be good.

          next step is to perform the correct calculation for my 18650 cell, but for now my brain hurts!!!

          Raspberry Pi 3B - Domoticz + Node Red
          Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
          Arduino Mega, RFLink 433Mhz
          Sonoff/ESP8266/Tasmota switches

          mfalkviddM 1 Reply Last reply
          0
          • G grumpazoid

            Ok so what I have done is added in a child id to separately display the state of the battery.

            I followed the thread here https://forum.mysensors.org/topic/6048/reporting-battery-to-domoticz

            I now have a new item in the domoticz utility section that shows the current battery state and I can get a graph displayed.

            I have used :

            definitions: #define CHILD_ID_BATTERY 2
            MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
            in presentation: present(VOLTAGE_CHILD_ID, S_MULTIMETER, "Battery level" );
            sending: send(msgbatt.set(batteryPcnt ,2)); //send battery in Volt 2 decimal places

            i don't really understand all of the above. if anyone can point me to an explanation that would be good.

            next step is to perform the correct calculation for my 18650 cell, but for now my brain hurts!!!

            mfalkviddM Offline
            mfalkviddM Offline
            mfalkvidd
            Mod
            wrote on last edited by
            #5

            @grumpazoid if you haven't already, check the section on presentation and sending data in the api documentation.

            G 1 Reply Last reply
            0
            • mfalkviddM mfalkvidd

              @grumpazoid if you haven't already, check the section on presentation and sending data in the api documentation.

              G Offline
              G Offline
              grumpazoid
              wrote on last edited by
              #6

              @mfalkvidd That looks just what I'm after...Thanks

              Raspberry Pi 3B - Domoticz + Node Red
              Arduino Pro Mini 3.3V - W5100 Ethernet, Nrf24
              Arduino Mega, RFLink 433Mhz
              Sonoff/ESP8266/Tasmota switches

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


              13

              Online

              11.7k

              Users

              11.2k

              Topics

              113.0k

              Posts


              Copyright 2019 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
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular