Magnetswitch + LUX sensor = false positives



  • Im trying to combine some sketches in hope to create something that should monitor my mailbox and read the light outside while powering the thing by battery!

    Right now i have basicly cut and pasted the 3 sketches for the switch/battery power and light sensor and have em hooked up on a uno with breadboard. Using USB connection to power it.

    Started with switch and sleep interupt and it worked fine. Removed the debounce since it couldnt be used while sleeping aprently (nuub here)

    Added battery sketch and it seem to be working..

    THEN i put in the damn lux meter and all hell broke lose.
    Everytime i trigger the switch now it starts sending status like crazy to the gw, im talking light speed.

    And while its sending and the switch is seperated it allso gets alot of false positives (or negatives the way you look at it)

    The Battery voltage can be ignored since it isnt even powered by battery or have anything from that hook up on the breadboard.

    And the other thing i noticed is that if i leave the switch closed and let it do its sleep time thing. It reports every 5 minutes! how can that be when i have put the sleep time to 1800000ms, that should if i counted correctly equal to a reading every 30 minutes?

    It seems to be reading the lux and the switch right in domoticz but as said the switch goes on and off.

    Below is a snipped from serial monitor when it goes haywire.

    3061 TSF:MSG:SEND,5-5-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    693
    Battery Voltage: 2.33 V
    Battery percent: 69 %
    3070 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=0,pMSG:SEND,5-5-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:71
    403
    2988 MCO:SLP:MS=1800000,SMS=0,I1=1,M1=1,I2=255,M2=255
    2993 MCO:SLP:TPD
    2995 MCO:SLP:WUP=1
    1
    2998 TSF:MSG:SEND,5-5-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    692
    Battery Voltage: 2.33 V
    Battery percent: 69 %
    3007 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:69
    404
    3017 TSF:MSG:SEND,5-5-0-0,s=2,c=1,t=23,pt=3,l=2,sg=0,ft=0,st=OK:404
    3023 MCO:SLP:MS=1800000,SMS=0,I1=1,M1=1,I2=255,M2=255
    3028 MCO:SLP:TPD
    3031 MCO:SLP:WUP=1
    1
    3034 TSF:MSG:SEND,5-5-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    701
    Battery Voltage: 2.36 V
    Battery percent: 70 %
    3043 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:70
    404
    3051 MCO:SLP:MS=1800000,SMS=0,I1=1,M1=1,I2=255,M2=255
    3056 MCO:SLP:TPD
    3058 MCO:SLP:WUP=1
    0

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     *
     * Arduino BH1750FVI Light sensor
     * communicate using I2C Protocol
     * this library enable 2 slave device addresses
     * Main address  0x23
     * secondary address 0x5C
     * connect the sensor as follows :
     *
     *   VCC  >>> 5V
     *   Gnd  >>> Gnd
     *   ADDR >>> NC or GND  
     *   SCL  >>> A5
     *   SDA  >>> A4
     * http://www.mysensors.org/build/light
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <BH1750.h>
    #include <Wire.h>
    
    #define CHILD_ID_LIGHT 2
    #define CHILD_ID_BUTTON 1
    #define BUTTON_PIN 3  // Arduino Digital I/O pin for button/reed switch
    unsigned long SLEEP_TIME = 1800000; // Sleep time between reports (in milliseconds)
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    int oldBatteryPcnt = 0;
    
    BH1750 lightSensor;
    
    MyMessage msg(CHILD_ID_BUTTON, V_TRIPPED);
    MyMessage msglux(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    uint16_t lastlux;
    
    void setup()
    { 
      lightSensor.begin();
    
    // 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("Switch_Light", "1.0");
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(CHILD_ID_BUTTON, S_DOOR);
      present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    
    }
    
    void loop()     
      {
        
      // Read digital motion value
      boolean tripped = digitalRead(BUTTON_PIN) == HIGH; 
    
      Serial.println(tripped);
      send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
    
    // 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;
    		}
    
           
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      Serial.println(lux);
      if (lux != lastlux) {
          send(msglux.set(lux));
          lastlux = lux;
          }
      
      // Sleep until interrupt comes in on motion sensor
      sleep(digitalPinToInterrupt(BUTTON_PIN), HIGH, SLEEP_TIME);
    }```


  • Well it seems to be the debounce thing that is Messing with me.

    How can u remove false positives when battery powered?


  • Mod

    @meanmrgreen easiest way is to do a wait for 10-30ms before going back to sleep. That will let the bouncing settle.



  • @mfalkvidd

    Thanks alot for all the help with My newbie arduino questions. Ill try the wait statement later



  • I think i solved it by replacing the tripped code with this modificed code from
    https://codebender.cc/example/MySensor/BinarySwitchSleepSensor#BinarySwitchSleepSensor.ino

    {
    uint8_t value;
    static uint8_t sentValue=2;
    // Short delay to allow buttons to properly settle
    sleep(5);
    value = digitalRead(BUTTON_PIN);

      if (value != sentValue) {
     // Value has changed from last transmission, send the updated value
     send(msg.set(value==HIGH ? 1 : 0));
     sentValue = value;
      }

Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts