Converting to 2.0 Slim Node as a Mini 2AA Battery PIR Motion Sensor code



  • Hello,

    I'm trying to convert @m26872 Slim Node as a Mini 2AA Battery PIR Motion Sensor code, from 1.5 to 2.0 MySensors library, but becose I'm very begginer at programming I facing with some unknow issues for me.

    Converted code from 1.5 to 2.0 MySensors library:

    /**
     * EgSlimReed2
     * Sketch for Slim Node and HC-SR505 based motion sensor. 
     * Inspired by:
     * - MySensors motion sensor example: http://www.mysensors.org/build/motion
     * - AWI's CR123A based Slim Node motion sensor: http://forum.mysensors.org/topic/2478/slim-cr123a-2aa-battery-node
     *
     * Created by m26872
     * Documentation: 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Test to if node can operate in some way dealing with the Pir extreme sensitivity to noise on Vcc.
     * Version 2.0 - First "production node". "Inactivity day counter" introduced.
     * 
     * DESCRIPTION
     * This sketch will only send trips as "1" to the controller. It's up to the controller to deal with the info. 
     * The motion node will not notify controller when resets back to low state and is ready for a new trip. In reality 
     * this is ~10s. And EVEN IF motion is detected continuously, there will be no new trip until motion has stopped for ~10s.  
     * The HC-SR505 is very noise sensitive and will trigger spuriously for almost any activity 
     * on Vcc (test thoroughly). To run the HC-505 at low voltages (tested flawlessly down to 1.6V), 
     * the 7133-reg and diode are removed (and possibly increase the sensitivity even further). 
     * Solution is to deal with it by interrupt type, check which source, block by sleep time etc.
     * 
     * HC-505 output connects to MOTION_INPUT_PIN (here D3) without any supporting component. Input pull-up disabled.
     * Every 24 hrs without trip, increments a counter and after "BATTERY_REPORT_DAY" counts a battery report will be sent as a heartbeat signal.
     * Else a battery report will be sent after every "BATTERY_REPORT_BY_IRT_CYCLE" motion trips.
     *
     */
     
    #include <SPI.h>
    #include <MySensors.h>
    #include <Vcc.h>
    
    //#define DEBUG
    
    #define MY_BAUD_RATE 9600
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 3  // Use static Node_ID  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  //14 var senaste "slim-PIR"-id 
    #define SKETCH_NAME "EgSlimPIR2"
    #define SKETCH_VERSION "2.0 2016-01-01"
    #define CHILD_ID 5
    #define MOTION_INPUT_PIN 3
    #define BATTERY_REPORT_DAY 2   // Desired heartbeat(battery report) interval when inactive. 
    #define BATTERY_REPORT_BY_IRT_CYCLE 10  // Make a battery report after this many trips. Maximum report interval will also be equal to this number of days.
    #define ONE_DAY_SLEEP_TIME 86400000
    #define VCC_MIN 1.9
    #define VCC_MAX 3.3
    
    #ifdef DEBUG
    #define DEBUG_SERIAL(x) Serial.begin(x)
    #define DEBUG_PRINT(x) Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
    #else
    #define DEBUG_SERIAL(x)
    #define DEBUG_PRINT(x) 
    #define DEBUG_PRINTLN(x) 
    #endif
    
    int dayCounter = BATTERY_REPORT_DAY;
    int irtCounter = 0;
    
    
    bool interruptReturn = false; // "false" will make the first loop disregard high output from HV-505 (from start-up) and make a battery report instead.
     
    Vcc vcc;
    
    MyMessage msg(CHILD_ID, S_MOTION);
    
    void presentation()  
    { 
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      present(CHILD_ID, S_MOTION);
    }
    
    void setup()  
    {  
      DEBUG_SERIAL(9600);
      DEBUG_PRINTLN(("Serial started"));
      delay(100); // to settle power for radio
      pinMode(MOTION_INPUT_PIN, INPUT);
      digitalWrite(MOTION_INPUT_PIN, LOW);    // Disable internal pull-ups
      DEBUG_PRINTLN("Warming and blocking PIR trip for 20s.");
      sleep(20000); // Wait until HC-505 warmed-up and output returned low.
    }
    
    void loop() 
    {
      if (interruptReturn) {    // Woke up by rising pin
        send(msg.set("1"));  // Just send trip (set) commands to controller. (Let controller reset and decide what to do with it.)
        irtCounter++;
        if (irtCounter>=BATTERY_REPORT_BY_IRT_CYCLE) {
            irtCounter=0;
            sendBatteryReport();
        }
      }
      else { // Woke up by timer  (or it's the first run)
        dayCounter++; 
        if (dayCounter >= BATTERY_REPORT_DAY) {
              dayCounter = 0;
              sendBatteryReport();
        }
      }
      
      sleep(3000);  // Make sure everything is stable before start to sleep with interrupts. (don't use "gw.wait()" here). Tests shows false trip ~2s after battery report otherwise.
    
      // Sleep until interrupt comes in on motion sensor or sleep time passed.
      interruptReturn = sleep(MOTION_INPUT_PIN-2,RISING, ONE_DAY_SLEEP_TIME);
      // DEBUG_PRINT("interruptReturn: ");DEBUG_PRINTLN(interruptReturn);
    
    } 
    
    void sendBatteryReport() {
              float p = vcc.Read_Perc(VCC_MIN, VCC_MAX, true);
              int batteryPcnt = static_cast<int>(p);
              sendBatteryLevel(batteryPcnt);
    }
    

    When I compiling code on arduino I get:

    Warning: Board arduino:avr:apm96 doesn't define a 'build.board' preference. Auto-set to: AVR_APM96
    In file included from C:\Users\Moni\Documents\Arduino\sketch_aug11c\sketch_aug11c.ino:37:0:
    
    C:\Users\Moni\Documents\Arduino\libraries\MySensors/MySensors.h:287:4: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
    
       #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
    
        ^
    
    exit status 1
    Error compiling for board APM Optiboot internal 1MHz noBOD 9600baud.
    

    I was trying to reinstall MySensors library, but it didn't help.

    Maybe someone could advise, where I made a mistake in code when converting it?

    Thank You!


  • Hardware Contributor

    @jacikaas I'm not very up-to-date on anything in these days 😞 , but it looks like a preference is missing from the "boards.txt" in Arduino IDE. Which IDE version do ou use? Can you post the "boards.txt" please?



  • @m26872
    Actually that one error with preference:

    Warning: Board arduino:avr:apm96 doesn't define a 'build.board' preference. Auto-set to: AVR_APM96
    

    ...was also on 1.5 library and sketches was succsessfully uploaded.

    Which IDE version do ou use?

    I'm using 1.6.10 Arduino IDE version.

    Can you post the "boards.txt" please?

    Yes, of course. In boards.txt file I paste same configuration description as in this link, at the end of file:

    ##############################################################
    # Add the new board to boards.txt (normally located at "C:\Program Files\Arduino\hardware\arduino\avr"
    # The *.bootloader.* etries only matters if you want to program bootloader (and fuses) from Arduino IDE. 
    # See http://www.engbedded.com/fusecalc (select Atmega328p) for interpretation of fuse values and how 
    # extended fuses are written in different applications (07h in Arduino IDE = FFh in Atmel studio).
    ##############################################################
    
    apm96.name=APM Optiboot internal 1MHz noBOD 9600baud
    
    apm96.upload.tool=avrdude
    apm96.upload.protocol=arduino
    apm96.upload.maximum_size=32256
    apm96.upload.speed=9600
    apm96.bootloader.tool=avrdude
    apm96.bootloader.low_fuses=0x62
    apm96.bootloader.high_fuses=0xde
    apm96.bootloader.extended_fuses=0x07
    apm96.bootloader.path=optiboot_v50
    apm96.bootloader.file=atmega328_1a.hex
    apm96.bootloader.unlock_bits=0x3F
    apm96.bootloader.lock_bits=0x2F
    apm96.build.mcu=atmega328p
    apm96.build.f_cpu=1000000L
    apm96.build.core=arduino
    apm96.build.variant=standard
    
    ##############################################################
    

    Do I need to post whole boards.txt file?


  • Hardware Contributor

    @jacikaas Ah! Forget about the warning. You error comes from that you haven't put your MySensors-related defines before the "include MySensors.h". Look here link.



  • @m26872

    A golden rule - RTFM. Next time I will use it :)))

    Thank You, that was it! Now code is compiling. But I think my HC-SR505 is broken, becose it don't see any movement.

    I need to order new one 🙂

    A lot of thanks!


  • Hardware Contributor

    @jacikaas said:

    But I think my HC-SR505 is broken, becose it don't see any movement.

    Did you test it with just a simple LED+resistor at the output ? Another option is to just measure the supply current (47uA when idle, 64uA for 10s after trigger).



  • @m26872

    Yes, now I test it with LED+resistor. It's definately broken, becose if I hide PIR sensor, data output always get high. LED light up for a while, then goes down for 2-3s and goes up again. And it repeating all the time, when it is covered (no movement in background).

    Thanks for Your help @m26872 !



  • Tried this setup and its not working very well for me, can be a busted PIR but I don't know.
    It seems like it sometimes work, but if I leave the room and then enter it can take long time until the PIR triggers.

    If i put a LED and a resistor I do that between GND and signal or where do I place it?





  • Thanks I'll try that


Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 24
  • 1
  • 10
  • 2

26
Online

11.2k
Users

11.1k
Topics

112.5k
Posts