Motion Sensor stopped working on combined sketch ( Hum, Temp, Motion + Bat Level )
-
Hello:
**Please see edit BELOW:
It appears that after a while the motion sensor actually works.... It's something to do with the sleep time....
What I am trying to do is have the sensor sleep as much as possible to save battery, BUT should always trigger on motion movement. I think I have an error with sleep values and that is affecting the motion sensor from triggering.
If anyone could recommend a way to sleep the unit as much as possible ( 3 in 1 Temp, Hum, Motion, + Bat reporting ), but always trigger when motion is around that would be greatly appreciated.**
Sorry if this is a bit of a newbie question, but I can't figure out why my motion sensor stopped working in my sketch? It works fine if I upload the basic mysensors motion sensor sketch, but as it is combined in this code, it no longer reports any motion movement back ( physically waving my hand in front of it has no reaction ). I have tried to combine the following code, and also trying to make it as ultra lower power as possible, as it will be a small battery operated ( 3 x AA Batts ) pro mini, 8Mhz, 3.3v . Can anyone please suggest what I have done wrong? Below is the code:
#define MY_NODE_ID 31 // Enable debug prints #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <DHT.h> #include <Vcc.h> #define CHILD_ID_TEMP 1 // Child id for temperatur #define CHILD_ID_HUM 0 // Child id for humidity #define CHILD_ID_VOLT 3 // Child id for battery reading #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define CHILD_ID 2 // Id of the sensor child int node_id=31; // What is my node id unsigned long SLEEP_TIME =30000UL; // Sleep time between reads (in milliseconds) int sleepcycle=1; // Counter to count the amout of time not sending data int humoffset=2; // only data is send if humidity is changed for this amout int tempoffset=0.5; // only data is if temperature is changed for this amout int gwsendtimout=20; // each 20*sleep_time (10 minutes) data will be send const float VccMin = 1.5; // Minimum expected Vcc level, in Volts. const float VccMax = 3.2; // Maximum expected Vcc level, in Volts. const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc Vcc vcc(VccCorrection); MySensor gw; DHT dht; //Store last values float lastTemp = 0 ; float lastHum = 0 ; float batteryV=0; int oldBatteryPcnt = 0; boolean lastTripped = false ; boolean metric = true; boolean gwsend = true; // to determin if data has to be send MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgVolt(CHILD_ID_VOLT, V_VOLTAGE); MyMessage msg(CHILD_ID, V_TRIPPED); void setup() { gw.begin(NULL,31,false); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Kitchen", "Sensor",true); pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM,"Kitchen Humidity"); gw.present(CHILD_ID_TEMP, S_TEMP,"Kitchen Temperatuur"); gw.present(CHILD_ID_VOLT, S_MULTIMETER,"Kitchen Battery voltage"); gw.present(CHILD_ID, S_MOTION,"Kitchen Motion Sensor"); metric = gw.getConfig().isMetric; } void loop() { // get the battery Voltage batteryV = vcc.Read_Volts(); int batteryPcnt = vcc.Read_Perc(VccMin, VccMax); if (oldBatteryPcnt != batteryPcnt) { // Power up radio after sleep gwsend=true; oldBatteryPcnt = batteryPcnt; } delay(dht.getMinimumSamplingPeriod()); float temp1 = dht.getTemperature(); float humidity = dht.getHumidity(); if (isnan(temp1)) { // Serial.println("Failed reading temperature from DHT"); } else if ((temp1 <= lastTemp-tempoffset)||(temp1 >= lastTemp+tempoffset)) { lastTemp = temp1; if (!metric) { temp1 = dht.toFahrenheit(temp1); } gwsend=true; } if (isnan(humidity)) { // Serial.println("Failed reading humidity from DHT"); } else if ((humidity <= lastHum-humoffset)||(humidity >= lastHum+humoffset)) { lastHum = humidity; gwsend=true; } // Read digital motion value boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; Serial.println(tripped); gwsend=true; if (sleepcycle>gwsendtimout){ gwsend=true; } if (gwsend){ gw.sendBatteryLevel(oldBatteryPcnt); gw.send(msgVolt.set(batteryV, 1)); gw.send(msgTemp.set(lastTemp, 1)); gw.send(msgHum.set(lastHum, 1)); gw.send(msg.set(tripped?"1":"0")); // Send tripped value to gw gwsend=false; sleepcycle=1; } sleepcycle++; gw.sleep(SLEEP_TIME); }
Debug Below:
send: 31-31-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0 send: 31-31-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4 send: 31-31-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0 sensor started, id=31, parent=0, distance=1 send: 31-31-0-0 s=255,c=3,t=11,pt=0,l=7,sg=0,st=ok:Kitchen send: 31-31-0-0 s=255,c=3,t=12,pt=0,l=6,sg=0,st=ok:Sensor send: 31-31-0-0 s=0,c=0,t=7,pt=0,l=16,sg=0,st=ok:Kitchen Humidity send: 31-31-0-0 s=1,c=0,t=6,pt=0,l=19,sg=0,st=ok:Kitchen Temperatuur send: 31-31-0-0 s=3,c=0,t=30,pt=0,l=23,sg=0,st=ok:Kitchen Battery voltage send: 31-31-0-0 s=2,c=0,t=1,pt=0,l=21,sg=0,st=ok:Kitchen Motion Sensor 0 send: 31-31-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:100 send: 31-31-0-0 s=3,c=1,t=38,pt=7,l=5,sg=0,st=ok:3.4 send: 31-31-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:19.0 send: 31-31-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:24.0 send: 31-31-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
If I upload an older sketch, or even the basic motion sensor sketch, no issues, so it's not a hardware issue, just the combination of sketches broke the motion sensor readings ( 0 or 1 )
Thanks very much
-
You don't activate the interrupt while sleeping. Look at the basic motion example, where the sleep method is called.
-
Yeah, @TimO Is right.
Check the code example here http://www.mysensors.org/build/motion and look for the gw.sleep sentence.
I have a similar sensor and this is what I have in my scketch:
gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
The problem with your sentenc is that the sleep command is not overridden by the motion detection. You need to include the interruption as part of the sleep command.
Suggested Topics
-
Day 1 - Status report
Announcements • 23 Mar 2014, 22:45 • hek 24 Mar 2014, 20:12 -
Echo request is not set
Troubleshooting • 12 Aug 2024, 15:36 • kamilb85 3 Sept 2024, 08:58 -
DHT22 wrong sensor type in Home Assistant
Troubleshooting • 16 days ago • Commodoreuno 11 days ago -
Ghost Child
Troubleshooting • 15 Mar 2025, 07:44 • FcNanoLed 18 Mar 2025, 18:36 -
JSN SR04T - Temperature Influencing Readings
Troubleshooting • 6 Sept 2019, 07:51 • Timbergetter 29 days ago -
JSN-SR04T-V3.0 Coax cable extended
Troubleshooting • 19 Mar 2025, 21:00 • bocalexandru 21 Mar 2025, 12:05