What is wrong with this sketch?
-
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); } } -
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); } }@Cliff-Karlsson A few things:
- 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 ).
- 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.
- The operator <test>?<if true>: <if false> takes a boolean as first argument.
- 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?
-
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. -
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 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-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?
-
Ok, I know this is probably very basic. But exactly how would I modify the original sketch to have it behave like this?
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) -
@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)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 ?
-
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 ?
@Cliff-Karlsson how is your motion sensor powered? It needs a solid power supply
-
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 ?
@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.