What is wrong with this sketch?



  • Why does not the motion-sensor trigger in this sketch?

    #define MY_DEBUG    // Enables debug messages in the serial log
    #define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
    
    
    #define PRESSURE_ID 1
    
    
    unsigned long 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 MOTION_ID 2   // Id of the sensor child
    int FSR_Pin = A0; //analog pin 0
    int value = 0;
    int oldvalue = 0;
    int OPEN = 0;
    int CLOSE = 1;
    boolean lastTripped = 0;
    MyMessage msgPressure(PRESSURE_ID, V_TRIPPED);
    MyMessage msgMotion(MOTION_ID, V_TRIPPED);
    
    void setup()  
    {  
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
     sendSketchInfo("Pressure/Motion Sensor", "1.0");
      present(PRESSURE_ID, S_DOOR);
      present(MOTION_ID, S_MOTION);
    }
    
    void loop()
    {
    int FSRReading = analogRead(FSR_Pin); 
    if(FSRReading > 150)
      {
        value=CLOSE;
      } 
    else
      {
        value=OPEN;
      }  
    // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
       
       if(lastTripped != tripped && value!= oldvalue)
       {
          send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw 
          lastTripped=tripped;
          send(msgPressure.set(value?"1":"0")); 
          oldvalue = value;   
          sleep(1000);    
        }
       else if (lastTripped != tripped) 
          {      
          //Serial.println(tripped);
            send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw 
            lastTripped=tripped;
            sleep(1000);
          }
       else if(value!= oldvalue)
          {
            send(msgPressure.set(value?"1":"0")); 
           oldvalue = value;    
           sleep(1000);
           }
        else
          {
            sleep(1000);
          }
    
    }
    
    


  • I am trying to debug it. I will let you know in a second.



  • #define MY_DEBUG    // Enables debug messages in the serial log
    #define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
    
    
    #define PRESSURE_ID 1
    #define MOTION_ID 2   // Id of the sensor child
    
    
    unsigned long 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 FSR_Pin = A0; //analog pin 0
    
    int value = 0; // change this to a signed  or unsigned thingy
    int oldvalue = 0; // change this to a signed  or unsigned thingy
    
    #define OPEN 0
    #define CLOSE 1
    
    boolean lastTripped = 0;
    MyMessage msgPressure(PRESSURE_ID, V_TRIPPED);
    MyMessage msgMotion(MOTION_ID, V_TRIPPED);
    
    void setup()
    {
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as inputa
    }
    
    void presentation()
    {
      sendSketchInfo("Pressure/Motion Sensor", "1.0");
      present(PRESSURE_ID, S_DOOR);
      present(MOTION_ID, S_MOTION);
    }
    
    void loop()
    {
      int FSRReading = analogRead(FSR_Pin);
      if (FSRReading > 150)
      {
        value = CLOSE;
      }
      else
      {
        value = OPEN;
      }
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR);
    
      if (lastTripped != tripped) // if only the tripped has changed
      {
        //Serial.println(tripped);
        send(msgMotion.set(tripped ? "1" : "0")); // Send tripped value to gw
        lastTripped = tripped;
      }
      if (value != oldvalue)
      {
        send(msgPressure.set(value ? "1" : "0"));
        oldvalue = value;
      }
      wait(SLEEP_TIME);
    }
    

    I changed the loop part. I had a lot of double code which did not do anything. I changed some ints to defines because they did not change ever. I changed sleep to wait since it is a repeater (they cannot sleep). Also. After every round you want to sleep, so why not just write it once instead of in every if statement.

    I hope this helps you.



  • @nielsokker

    I see it does not compile. You have to change the define.

    
    #define FSR_Pin = A0; //analog pin 0 needs to be
    

    needs to be

    
    #define FSR_Pin  A0 //analog pin 0 needs to be
    


  • @nielsokker
    That wait at the end of the loop won't wake up on interrupt, for the motion sensor.

    @Cliff-Karlsson
    Is this node intended to be a repeater as well?



  • @corbin said:

    Yes it will be permanently powered so I figured I could use it for a repeater as well.

    As it won't run on batteries I don't need sleep (?) I just want it to update on pressure (when it goes from 1 to 0 or from 0 to 1) and when detecting movement and not spamming my controller as the original sketch that I made did.



  • @Cliff-Karlsson

    This is the sketch I'm using for a repeater and motion node. If you are using a HC-SR501 PIR with high and low triggers it should work ok without spamming your gateway.

    Note that there are no sleeps or waits, it will just keep running the loop. I don't know if that is ideal - definitely no good for batteries.

    /**
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to create a node thay repeates messages
     * from nodes far from gateway back to gateway. 
     * It is important that nodes that has enabled repeater mode calls  
     * process() frequently. Repeaters should never sleep. 
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enabled repeater feature for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    
    unsigned long 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
    
    boolean previousTripped = LOW;
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup() {
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input  
    }
    
    void presentation()  
    {  
      //Send the sensor node sketch version information to the gateway
      sendSketchInfo("Motion and Repeater Node", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_MOTION);
    }
    
    void loop() 
    {
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      //Serial.println(tripped);
      // Sensor must be HIGH and not yet sent the status of the sensor
      if ((tripped == HIGH) && (tripped != previousTripped) ) {     
        send(msg.set(tripped ? "1" : "0")); // Send tripped value to gw
        previousTripped = tripped;   
      }  
      
      // Is sensor low and not sent? Then send LOW to gateway
      if ((tripped == LOW) && (tripped != previousTripped)) {    
          send(msg.set(tripped ? "1" : "0")); // Send tripped value to gw
          previousTripped = tripped;
      }
    }
    


  • This is my last attempt and I think it works pretty well, but the gateway still get spammed. When I trigger the motion-sensor the domotizc log looks like this:

    2016-07-19 11:38:53.250 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:54.354 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:54.411 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:55.515 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:55.574 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:56.678 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:56.736 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:57.842 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:57.899 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:59.003 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:59.061 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:39:00.166 (RFM) Lighting 2 (Säng Motion)
    
    #define MY_DEBUG    // Enables debug messages in the serial log
    #define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
    
    
    #define PRESSURE_ID 1
    
    
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define MOTION_ID 2   // Id of the sensor child
    int FSR_Pin = A0; //analog pin 0
    int value = 0;
    int oldvalue = 0;
    int OPEN = 0;
    int CLOSE = 1;
    
    boolean previousTripped = LOW;
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    
    MyMessage msgPressure(PRESSURE_ID, V_TRIPPED);
    MyMessage msgMotion(MOTION_ID, V_TRIPPED);
    
    void setup()  
    {  
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
     sendSketchInfo("Pressure/Motion Sensor", "1.0");
      present(PRESSURE_ID, S_DOOR);
      present(MOTION_ID, S_MOTION);
    }
    
    void loop()
    {
    int FSRReading = analogRead(FSR_Pin); 
    if(FSRReading > 150)
      {
        value=CLOSE;
      } 
    else
      {
        value=OPEN;
      }  
    // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
       // Sensor must be HIGH and not yet sent the status of the sensor
        if ((tripped == HIGH) && (tripped != previousTripped) ) {     
        send(msgMotion.set(tripped ? "1" : "0")); // Send tripped value to gw
        previousTripped = tripped;   
      }  
      
      // Is sensor low and not sent? Then send LOW to gateway
      else if ((tripped == LOW) && (tripped != previousTripped)) {    
          send(msgMotion.set(tripped ? "1" : "0")); // Send tripped value to gw
          previousTripped = tripped;
      }
      
       else if(value!= oldvalue)
          {
            send(msgPressure.set(value?"1":"0")); 
           oldvalue = value;    
           }
       else{
            sleep(1000);
       }
    
    }
    

  • Hero Member

    @Cliff-Karlsson A few things:

    1. You are mixing all kinds of variable types which makes it rather confusing. a boolean can only have a "true" or "false" value (not HIGH/LOW or OPEN/CLOSE ).
    2. The pressure sensor "value" is fact a boolean. I would be easier to read if you make it a boolean and name it "pressureHigh" or so.
    3. The operator <test>?<if true>: <if false> takes a boolean as first argument.
    4. There are two independent sensors (motion and "pressure") in your node. so you should treat them as such. so something like this instead of stacking "else if"
    if (<sensor 1>)    // sensor 1 actions
    else (<sensor 1>)
    
    if (<sensor 2>)    // sensor 2 actions
    else (<sensor 2>)
    
    sleep(  <interrupt>, <time>) ;
    

    the spamming of the gateway is probably caused by somethin else 😉



  • Ok, I am going to try to make some use of that info. But can I still use "sleep( <interrupt>, <time>) ;" when I also need to monitor the A0 (Pressure)? Will the Pressure still trigger when the sensor sleeps and there are no motion?



  • @Cliff-Karlsson
    You cannot use sleep of any kind, because it will power down the radio, and you need that running 100% of the time for the repeater function.


  • Hero Member

    @Cliff-Karlsson It depends what kind of pressure sensor you have, i will probably be enough if you poll it each second or so. And as @corbin mentioned don't use repeater function when you sleep.



  • Ok, I know this is probably very basic. But exactly how would I modify the original sketch to have it behave like this?



  • @Cliff-Karlsson

    I dont really understand what you mean. You want it to send its value every second? In that case my attempt still works. You want to use interrupts?



  • @nielsokker said:

    I dont really understand what you mean. You want it to send its value every second? In that case my attempt still works. You want to use interrupts?

    I just want a sketch that updates the status of the pressure sensor when it goes from HIGH to LOW or from LOW to HIGH. And when Movement is detected, but does only report back to the controller when a actual change has happened.

    In my last sketch all seemed to work except it "spammed" the controller with the motion-sensor:

    2016-07-19 11:38:53.250 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:54.354 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:54.411 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:55.515 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:55.574 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:56.678 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:56.736 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:57.842 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:57.899 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:59.003 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:38:59.061 (RFM) Lighting 2 (Säng Motion)
    2016-07-19 11:39:00.166 (RFM) Lighting 2 (Säng Motion)
    


  • @Cliff-Karlsson

    I see why. But once again, i think i made your sketch work.

    As i see it. This is the current desired logic.

    once per second, you want to check the following things:

    • is there motion?
    • has there been a change in pressure (HIGH to LOW or LOW to HIGH)?

    But actually you want to check both ALWAYS. The thing you are obviously looking for, are interrupts. I do not know how these work if you do not want to sleep. Maybe there is a wait(interrupt_pin_1_functor, interrupt_pin_2_functor, waittime=forever) function (I know the sleep() version exists).

    Maybe you already know what interrupts are but, in short: When a given interrupt pin notices a difference in value (HIGH to LOW, for example) it gives a signal. The current code is being stopped and the function that is bound to the pin is executed. This function could be sending the new value to the gateway.

    Can someone confirm this?



  • I tried again with your sketch @nielsokker , I think I only added some sleep. But the motion-sensor just turns on/off super-quick every time the motion triggers. What am I doing wrong ?


  • Hero Member

    @Cliff-Karlsson how is your motion sensor powered? It needs a solid power supply



  • @Cliff-Karlsson said:

    I tried again with your sketch @nielsokker , I think I only added some sleep. But the motion-sensor just turns on/off super-quick every time the motion triggers. What am I doing wrong ?

    Yea, my example just checks both sensors every second. So it sends both values every second. I know now that is not what you want. At least the code looks better in my opinion (ofc its my code 🙂 ).

    I have to agree with @AWI that it seems like your sensor does not work properly.



  • ahh!!!, you where right. I powered the node using a mobile charger but had not connected the PIR to the same GND as the charger. Now it works.


Log in to reply
 

Suggested Topics

  • 3
  • 15
  • 24
  • 4
  • 1
  • 3

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts