Combined motion sensor with battery monitor sketch

  • 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

    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__)
    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;
    	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
      // 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.println(" V");
      Serial.print("Battery percent: ");
      Serial.println(" %");
      if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        oldBatteryPcnt = batteryPcnt;

  • Mod

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

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

  • 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

    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!!!

  • Mod

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

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

Log in to reply

Suggested Topics

  • 1
  • 3
  • 5
  • 1
  • 2
  • 5