Skip to content
  • MySensors
  • 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. Troubleshooting
  3. What is wrong with this sketch?

What is wrong with this sketch?

Scheduled Pinned Locked Moved Troubleshooting
20 Posts 4 Posters 3.7k Views 4 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.
  • Cliff KarlssonC Offline
    Cliff KarlssonC Offline
    Cliff Karlsson
    wrote on last edited by
    #8

    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);
       }
    
    }
    
    AWIA 1 Reply Last reply
    0
    • Cliff KarlssonC Cliff Karlsson

      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);
         }
      
      }
      
      AWIA Offline
      AWIA Offline
      AWI
      Hero Member
      wrote on last edited by AWI
      #9

      @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 ;-)

      1 Reply Last reply
      0
      • Cliff KarlssonC Offline
        Cliff KarlssonC Offline
        Cliff Karlsson
        wrote on last edited by
        #10

        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?

        corbinC AWIA 2 Replies Last reply
        0
        • Cliff KarlssonC Cliff Karlsson

          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?

          corbinC Offline
          corbinC Offline
          corbin
          wrote on last edited by
          #11

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

          1 Reply Last reply
          0
          • Cliff KarlssonC Cliff Karlsson

            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?

            AWIA Offline
            AWIA Offline
            AWI
            Hero Member
            wrote on last edited by
            #12

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

            Cliff KarlssonC 1 Reply Last reply
            0
            • AWIA AWI

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

              Cliff KarlssonC Offline
              Cliff KarlssonC Offline
              Cliff Karlsson
              wrote on last edited by
              #13

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

              nielsokkerN 1 Reply Last reply
              0
              • Cliff KarlssonC Cliff Karlsson

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

                nielsokkerN Offline
                nielsokkerN Offline
                nielsokker
                wrote on last edited by
                #14

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

                1 Reply Last reply
                0
                • Cliff KarlssonC Offline
                  Cliff KarlssonC Offline
                  Cliff Karlsson
                  wrote on last edited by
                  #15

                  @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)
                  
                  nielsokkerN 1 Reply Last reply
                  0
                  • Cliff KarlssonC Cliff Karlsson

                    @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)
                    
                    nielsokkerN Offline
                    nielsokkerN Offline
                    nielsokker
                    wrote on last edited by
                    #16

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

                    1 Reply Last reply
                    0
                    • Cliff KarlssonC Offline
                      Cliff KarlssonC Offline
                      Cliff Karlsson
                      wrote on last edited by
                      #17

                      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 ?

                      AWIA nielsokkerN 2 Replies Last reply
                      0
                      • Cliff KarlssonC Cliff Karlsson

                        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 ?

                        AWIA Offline
                        AWIA Offline
                        AWI
                        Hero Member
                        wrote on last edited by
                        #18

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

                        1 Reply Last reply
                        0
                        • Cliff KarlssonC Cliff Karlsson

                          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 ?

                          nielsokkerN Offline
                          nielsokkerN Offline
                          nielsokker
                          wrote on last edited by nielsokker
                          #19

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

                          1 Reply Last reply
                          0
                          • Cliff KarlssonC Offline
                            Cliff KarlssonC Offline
                            Cliff Karlsson
                            wrote on last edited by
                            #20

                            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.

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


                            19

                            Online

                            11.7k

                            Users

                            11.2k

                            Topics

                            113.1k

                            Posts


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