Door, Motion and Temperature Sensor



  • Hi guys,

    It's been a few months since I last made a sensor, and I'm experiencing some issues trying to create one now. I've made a sketch, from parts from other projects and made myself a 3in1 sensor, but the problem is that I have no parent node, and I can't understand why.

    Can anyone take a look over it and correct me ?

    Note that this is meant to be a power operated sensor. Thank you 🙂

    //This sketch is for Door & Motion & Temp Sensor
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <Bounce2.h>
    
    char sketch_name[] = "Multi Sensor";
    char sketch_ver[]  = "1.0";
    
    #define CLIENT_ID AUTO  // Sets MYSensors client id
    #define RADIO_CH 76  // Sets MYSensors to use Radio Channel
    #define TRIGGER	3	// used to connect motion sensor
    #define BUTTON_PIN  4 // used to connect door/window sensor
    
    //Temp Sensor bits
    #define ONE_WIRE_BUS 14 // Pin where dallas sensor is connected
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DallasSensors(&oneWire);
    float lastTemperature ;
    #define CHILD_ID_T1 3 //CHILD ID for temp
    
    
    //Door/Window bits
    
    #define CHILD_ID_D1 1 //CHILD ID for door/window
    
    //Trigger Sensor Bits
    #define CHILD_ID_S1 2 //CHILD ID for Reed/Motion sensor
    boolean lastTripped = 0;
    
    
    MySensor gw;
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID_D1,V_TRIPPED);
    MyMessage triggerMsg(CHILD_ID_S1,V_TRIPPED);
    MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
    
    void setup()
    {
    	// Startup OneWire Temp Sensors
    	DallasSensors.begin();
    	
    	// Initialize library and add callback for incoming messages
    	 gw.begin(NULL, AUTO, true);
    	
    	// Send the sketch version information to the gateway and Controller
    	gw.sendSketchInfo(sketch_name, sketch_ver);
    
    	// Register all sensors to gw (they will be created as child devices)
    	gw.present(CHILD_ID_D1, S_DOOR);
    	gw.present(CHILD_ID_T1, S_TEMP);
    	gw.present(CHILD_ID_S1, S_MOTION);
    
    	// Setup the button
            pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
      
            // After setting up the button, setup debouncer
            debouncer.attach(BUTTON_PIN);
            debouncer.interval(5);
      
            // Register binary input sensor to gw (they will be created as child devices)
            // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
            // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    	
    }
    
    
    void loop()
    {
    	// Alway process incoming messages whenever possible
    	gw.process();
    	
    	
    	// Check for motion change value
    		boolean tripped = digitalRead(TRIGGER) == HIGH; 
    
    		if (lastTripped != tripped ) {
    			gw.send(triggerMsg.set(tripped?"1":"0")); // Send new state and request ack back
    			Serial.println("Tripped");
    			lastTripped=tripped;
    		}
    		
    	//  Check if digital input has changed and send in new value
                    debouncer.update();
                  // Get the update value
                    int value = debouncer.read();
     
                    if (value != oldValue) {
                        // Send in the new value
                         gw.send(msg.set(value==HIGH ? 1 : 0));
                         oldValue = value;
                    }
    	
    	// Fetch temperatures from Dallas sensors
    		DallasSensors.requestTemperatures();
    		float tempC = DallasSensors.getTempCByIndex(1);
    		
    		// Only send data if temperature has changed and no error
    		if (lastTemperature != tempC && tempC != -127.00) {
    			
    			// Send in the new temperature
    			gw.send(tempMsg.set(tempC,1));
    			lastTemperature=tempC;
                            delay(30000);
    		}
    		
    }
    

  • Hero Member

    @CaptainZap
    What do you mean by "have no parent node"?

    Have a look at the serial monitor (connected to the sensor Arduino).
    And you are initializing this node as a repeater.. is that what you were planning to do?

    // Initialize library and add callback for incoming messages
         gw.begin(NULL, AUTO, true); 
    // change to gw.begin(NULL, <fixed_node>) 
    


  • @AWI Yes I want all my nodes to be repeaters, and that's exactly what I want to do 🙂 However I use a Vera as my controller, and there you are supposed to have a parent node for your arduino sensor, followed by child devices for sensors/relays etc. Right now I have 3 child devices for the My Sensors plugin which isn't right.


  • Hardware Contributor

    The fetching of Dallas temperature and also the delay(30000) are rather blocking code. Maybe not the cause of your problem, but not very good as a repeater node. Use a timing if-statement instead.



  • @m26872 No idea how do to that, could you please help me ? 🙂


  • Hardware Contributor

    Can't guide you in detail right now but this should give you an idea.



  • @CaptainZap said:

    @m26872 No idea how do to that, could you please help me ? 🙂

    Here is an excellent article; How and why to avoid Delay()


  • Hardware Contributor

    Regarding the Vera parental node issue. I see it often but don't know what's causing this. A lot of struggle usually help. I'd try things like:

    1. put a "delay(500);" before gw.begin and before gw.sendsketchinfo to make sure radio is fully powered and ready when initializing. Then redo the Vera inclusion. Try a few times.
    2. If still no success. Load and run the ClearEepromConfig sketch. Reload your sketch again and continue trying inclusion in vera.

    Perhaps you'll get a lot of ghost child nodes to delete. I use static node Ids so inclusion with cleared eeprom doesn't make the controller give me new node ids every time.



  • @mvdarend @m26872 Thanks so much for your feedback, I did apply the hints and I'll test the sketch later on today. I do have a question... does anyone know how do I upgrade my library to version 1.4.1 ? I've copied the one available from the main page, but it's version 1.4.

    Thanks.



  • DallasSensors.requestTemperatures(); delays code execution up to 750ms. MySensors DS18B20 library is bit outdated, you should update library (https://github.com/milesburton/Arduino-Temperature-Control-Library) and use following code:

    In setup:

    	DallasSensors.begin();
    	DallasSensors.setWaitForConversion(false);
    

    In loop:

    		DallasSensors.requestTemperatures(); // no delay here
    		gw.wait(750); // insert another value for non-12-bit resolution
    		float tempC = DallasSensors.getTempCByIndex(1);
    


  • @robosensor Thank you very much for your insight... do I just download the new library and overwrite the one in the Arduino IDE install folder ?

    I've also used your code and I'm getting errors compiling it, any hints ? I guess it could be because of the library ?

    Arduino: 1.6.4 (Windows 8.1), Board: "Arduino Mini, ATmega328"

    Door_Motion_Temp_rev2.ino: In function 'void getTemps()':
    Door_Motion_Temp_rev2:115: error: 'class MySensor' has no member named 'wait'
    Multiple libraries were found for "DallasTemperature.h"

    Used: C:\Users***\Documents\Arduino\libraries\DallasTemperature << this is where I've copied the downloaded library

    Not used: C:\Users***\Documents\Arduino\libraries\Arduino-Temperature-Control-Library-master

    'class MySensor' has no member named 'wait'

    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.

    'class MySensor' has no member named 'wait'



  • @CaptainZap yes, but update two libraries (OneWire and Dallas).

    gw.wait() function was added 1 feb 2015: https://github.com/mysensors/Arduino/commit/d45a48d8b786656968fb4b45b049e16700dd3fd0 so you should use latest stable 1.4.* code.



  • @robosensor I don't know where to find the complete library for version 1.4.x



  • @CaptainZap

    http://www.mysensors.org/download/ click on Download button under "1.4 - Latest Release" section, download zip archive, extract archive and check Arduino-master\libraries\MySensors\Version.h file.



  • @robosensor Thanks, I installed that but it's version 1.5 b1 and after using it it's giving me lots of errors 😐 I think there have been changes in how the code is used...

    LE: I finally found the 1.4.1 branch and downloaded it however it's still giving me the same error about wait:

    Door_Motion_Temp_rev2.ino: In function 'void getTemps()':
    Door_Motion_Temp_rev2:115: error: 'class MySensor' has no member named 'wait'
    'class MySensor' has no member named 'wait'

    LLE: Just checked the cpp and there is no wait, this is the library I used :
    https://github.com/mysensors/Arduino/tree/1.4.1

    Can you tell me if there will be any issue if I comment that part ?




  • Admin



  • That was the version I was using when I first encountered the issues... anyways I'll remove arduino IDE with everything and reinstall it. Thanks.

    @hek I also LOL-ed 😄 Gotta love the helping spirit in the community you guys built.



  • @CaptainZap also try to backup and clean C:\Users***\Documents\Arduino\ directory 🙂



  • @hek as I said before, Dallas library is bit outdated. Is it possible to update dallas and onewire libraries? I provided links for newer versions of this libraries in this thread. I can do PR in github myself, but I don't know how to do it 🙂 I need to read github manuals first 🙂

    requestTemperatures() is can be non-blocking in newer version of dallas library, so code can deep sleep (better for batteries) or process messages (better for repeaters and msg-receiving nodes) about 750 ms.


  • Admin

    @robosensor

    Using github is quite easy (when you get a hang of it). We can use this as a "training session" if you want?

    I did a quick getting-started guide here:
    http://forum.mysensors.org/topic/330/how-to-contribute-code-to-the-mysensors-project

    Basically you need to

    • Fork the mysensors project on github
    • Clone your fork from your computer.
    • Replace the libraries that is outdated.
    • Commit as push the changes back to your fork of github.
    • Create a pull-request (see above instructions) which I'll review and merge.

    Easy peasy.. 🙂



  • @hek thank you, I'll read and try on dallas&onewire 🙂


  • Admin

    @robosensor

    Great, just ask (or open a chat) if you run into trouble.



  • @hek I have forked MySensors project, added 3 commits into master branch with updated libraries and updated dallas sensors example (lower power usage).

    I'm using similar code in my own version of MySensors 1.5-dev in my home, but I havn't tested this committed 1.4.1 github code on real hardware, so I will make PR after test on real hardware.

    https://github.com/mysensors/Arduino/compare/master...roboprint:master?diff=split&name=master


  • Admin

    Sounds good!



  • @hek It seems that I did it. If this push request is correct, what I should patch? Development branch, master branch or both?


  • Admin

    Good!
    You can create pull request on master (and I'll merge it to development later).


  • Admin

    @robosensor

    Thanks and congrats! You've made your first contribution to the MySensors project. 🙂



  • Guys, I had some free time tonight and tried to apply this fix... I ended up re-installing the arduino IDE + downloading the latest version, which now is 1.4.1 as opposed to the 1.4 that I used to have ?

    Anyways I had to upgrade my ethernet gateway (ENC28J60) and it's no longer working now... any hints will be golden.
    LE: I was too tired to check the sketch... I forgot to include the correct library and went to all debugging first, reset router config to default, but in the end I did notice the issue 🙂 So I learned a valuable lesson, never work on this if it's late or you're tired.



  • Hello guys,

    I really need an expert's opinion on this because I'm experiencing some trouble... I've tried to include suggestions from all you guys, except the delay part, but so far it works partially... in my original code the temperature worked, and now it doesn't. The rest works ok, sensor and motion, so I would be grateful if anyone could take a look at my code and help me with it... I kind of need to get it in place soon, as I intend to leave my house for some time.

    Thank you.

    //This sketch is for Door & Motion & Temp Sensor
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <Bounce2.h>
    
    #define CLIENT_ID AUTO  // Sets MYSensors client id
    #define RADIO_CH 76  // Sets MYSensors to use Radio Channel
    #define TRIGGER	3	// used to connect motion sensor
    #define BUTTON_PIN  4 // used to connect door/window sensor
    
    //Temp Sensor bits
    #define ONE_WIRE_BUS 14 // Pin where dallas sensor is connected
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DallasSensors(&oneWire);
    float lastTemperature ;
    #define CHILD_ID_T1 3 //CHILD ID for temp
    
    
    //Door/Window bits
    
    #define CHILD_ID_D1 1 //CHILD ID for door/window
    
    //Trigger Sensor Bits
    #define CHILD_ID_S1 2 //CHILD ID for Motion sensor
    boolean lastTripped = 0;
    
    MySensor gw;
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID_D1,V_TRIPPED);
    MyMessage triggerMsg(CHILD_ID_S1,V_TRIPPED);
    MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
    
    void setup()
    {
    	// Startup OneWire Temp Sensors
        DallasSensors.begin();
        DallasSensors.setWaitForConversion(false);
    
    	// Initialize library and add callback for incoming messages
    	 gw.begin(NULL, AUTO, true);
    	
    	// Send the sketch version information to the gateway and Controller
    	gw.sendSketchInfo("Multi Sensor", "1.1");
    
    	// Register all sensors to gw (they will be created as child devices)
    	gw.present(CHILD_ID_D1, S_DOOR);
    	gw.present(CHILD_ID_T1, S_TEMP);
    	gw.present(CHILD_ID_S1, S_MOTION);
    
    	// Setup the button
            pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
      
            // After setting up the button, setup debouncer
            debouncer.attach(BUTTON_PIN);
            debouncer.interval(5);
      
            // Register binary input sensor to gw (they will be created as child devices)
            // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
            // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    	
    }
    
    
    void loop()
    {
    	// Alway process incoming messages whenever possible
    	gw.process();
    	
    	
    	// Check for motion change value
    		boolean tripped = digitalRead(TRIGGER) == HIGH; 
    
    		if (lastTripped != tripped ) {
    			gw.send(triggerMsg.set(tripped?"1":"0")); // Send new state and request ack back
    			Serial.println("Tripped");
    			lastTripped=tripped;
    		}
    		
    	//  Check if digital input has changed and send in new value
                    debouncer.update();
                  // Get the update value
                    int value = debouncer.read();
     
                    if (value != oldValue) {
                        // Send in the new value
                         gw.send(msg.set(value==HIGH ? 1 : 0));
                         oldValue = value;
                    }
                    
        DallasSensors.requestTemperatures(); // no delay here
        gw.wait(750); // insert another value for non-12-bit resolution
        float tempC = DallasSensors.getTempCByIndex(1);
        
        // Only send data if temperature has changed and no error
        if (lastTemperature != tempC && tempC != -127.00) {
          
          // Send in the new temperature
          gw.send(tempMsg.set(tempC,1));
          lastTemperature=tempC;
                       }    		
    
    }
    

  • Contest Winner

    @CaptainZap

    what does the Serial output show during execution of the sketch?



  • @BulldogLowell Unfortunately I don't have a way to test that yet... I'm uploading the sketch using an arduino uno, and I have no idea how to do that with it. I do have an FTDI serial adapter but is not picked up by the IDE, it was at some point but no longer... probably fake 😐


  • Hardware Contributor

    I don't know what's the issue, but I'd still prefer a normal timing code instead of just the gw.wait(). The fetching of temperatures in every loop makes no sense to me.



  • @m26872 If you have time, could you please tell me how to do that, I know you've posted a link a few post back but I didn't do much with it 😞



  • @CaptainZap You should not query dallas sensor temperature every 750 ms. This heats up sensor itself and quite useless.

    Use something like this:

    unsigned long SLEEP_TIME = 30000; // Sleep time between reports (in milliseconds)
    unsigned long lastRefreshTime = 0;
    
    void loop()
    {
    	gw.wait(100);
      
    	boolean bNeedRefresh = (millis() - lastRefreshTime) > SLEEP_TIME;
    	if (bNeedRefresh)
    	{
    		lastRefreshTime = millis();
    
    		DallasSensors.requestTemperatures();
    		gw.wait(750);
    		float tempC = DallasSensors.getTempCByIndex(1);
    		float difference = lastTemperature - tempC;
    		if (tempC != -127.00 && abs(difference) > 0.5)
    		{
    			// Send in the new temperature
    			gw.send(tempMsg.set(tempC, 1));
    			lastTemperature = tempC;
    		}
    	}
    }
    


  • @robosensor Thanks so much, I highly appreciate your comments. Will test this tomorrow, and hopefully, soon share my hardware designs 😄



  • Just uploaded the new sketch to my nodes, one built on a pro mini, and one built on a nano (got one to view serial output), however both nodes have the same behavior when added to the Vera unit... the repeater node doesn't show up (is this somehow related to UI7 ?), and the weird thing is that temperature value is never populated.

    Regarding the repeater, looking into the user_data output I can see that a repeater node is created, but it's not used as a parent device for the sensors... as all sensors have as parent the gateway. I'm using this version of the plugin :
    https://github.com/mysensors/Vera/tree/UI7

    This is the serial monitor output from the nano :

    repeater started, id 13
    send: 13-13-1-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
    send: 13-13-1-0 s=255,c=3,t=6,pt=1,l=1,st=ok:1
    send: 13-13-1-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Multi Sensor
    send: 13-13-1-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.1
    send: 13-13-1-0 s=1,c=0,t=0,pt=0,l=0,st=ok:
    send: 13-13-1-0 s=3,c=0,t=6,pt=0,l=0,st=ok:
    send: 13-13-1-0 s=2,c=0,t=1,pt=0,l=0,st=ok:
    send: 13-13-1-0 s=1,c=1,t=16,pt=2,l=2,st=ok:0
    

    This is the updated sketch:

    //This sketch is for Door & Motion & Temp Sensor
    //rev3 - changed temp pin to 5 & temperature logic re-worked
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <Bounce2.h>
    
    #define CLIENT_ID AUTO  // Sets MYSensors client id
    #define RADIO_CH 76  // Sets MYSensors to use Radio Channel
    #define TRIGGER	3	// used to connect motion sensor
    #define BUTTON_PIN  4 // used to connect door/window sensor
    
    //Temp Sensor bits
    #define ONE_WIRE_BUS 5 // Pin where dallas sensor is connected - on Rboard this is A0(D14)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DallasSensors(&oneWire);
    float lastTemperature ;
    #define CHILD_ID_T1 3 //CHILD ID for temp
    
    
    //Door/Window bits
    
    #define CHILD_ID_D1 1 //CHILD ID for door/window
    
    //Trigger Sensor Bits
    #define CHILD_ID_S1 2 //CHILD ID for Motion sensor
    boolean lastTripped = 0;
    
    unsigned long SLEEP_TIME = 30000; // Sleep time between reports (in milliseconds)
    unsigned long lastRefreshTime = 0;
    
    MySensor gw;
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID_D1,V_TRIPPED);
    MyMessage triggerMsg(CHILD_ID_S1,V_TRIPPED);
    MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
    
    void setup()
    {
    	// Startup OneWire Temp Sensors
        DallasSensors.begin();
        DallasSensors.setWaitForConversion(false);
    
    	// Initialize library and add callback for incoming messages
    	 gw.begin(NULL, AUTO, true);
    	
    	// Send the sketch version information to the gateway and Controller
    	gw.sendSketchInfo("Multi Sensor", "1.1");
    
    	// Register all sensors to gw (they will be created as child devices)
    	gw.present(CHILD_ID_D1, S_DOOR);
    	gw.present(CHILD_ID_T1, S_TEMP);
    	gw.present(CHILD_ID_S1, S_MOTION);
    
    	// Setup the button
            pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
      
            // After setting up the button, setup debouncer
            debouncer.attach(BUTTON_PIN);
            debouncer.interval(5);
      
            // Register binary input sensor to gw (they will be created as child devices)
            // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
            // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    	
    }
    
    
    void loop()
    {
    	// Alway process incoming messages whenever possible
    	gw.process();
    	
    	
    	// Check for motion change value
    		boolean tripped = digitalRead(TRIGGER) == HIGH; 
    
    		if (lastTripped != tripped ) {
    			gw.send(triggerMsg.set(tripped?"1":"0")); // Send new state and request ack back
    			Serial.println("Tripped");
    			lastTripped=tripped;
    		}
    		
    	//  Check if digital input has changed and send in new value
                    debouncer.update();
                  // Get the update value
                    int value = debouncer.read();
     
                    if (value != oldValue) {
                        // Send in the new value
                         gw.send(msg.set(value==HIGH ? 1 : 0));
                         oldValue = value;
                    }
                    
        gw.wait(100);
      
        boolean bNeedRefresh = (millis() - lastRefreshTime) > SLEEP_TIME;
        if (bNeedRefresh)
        {
            lastRefreshTime = millis();
    
            DallasSensors.requestTemperatures();
            gw.wait(750);
            float tempC = DallasSensors.getTempCByIndex(1);
            float difference = lastTemperature - tempC;
            if (tempC != -127.00 && abs(difference) > 0.5)
            {
                // Send in the new temperature
                gw.send(tempMsg.set(tempC, 1));
                lastTemperature = tempC;
            }
        }  		
    
    }
    

    Any feedback will be appreciated. Thanks.


  • Admin

    All devices has the gateway as parent (Vera does not support a multi-level device hierarchy).



  • Can anyone share their thoughts ? I've been running two identical nodes, with no temperature output for a few days now 😞



  • @CaptainZap Did you delete the original devices from Vera after you changed the code? I had something similar happen when I rewrote a sketch to modify battery monitoring and Vera would not show the new data. I deleted the node and child devices and reincluded them from scratch and then it worked.


  • Hardware Contributor

    I think you have to redo inclusion of the sensors if you want them to use the repater in their routing. At least it looks like this when I setup my Vera and sensors.



  • @Dwalt @m26872 Thanks for the feedback, I did do that on the Vera unit. I've even removed the plugin files and re-did everything. I even, and this is a bit extreme, cleared the eprom on the gateway and reflashed it. None of it worked, I really have no clue what to do next. I'm open to every suggestion 😄



  • @CaptainZap It your sketch you have this line:
    #define ONE_WIRE_BUS 14

    What pin is your Dallas sensor connected to?


  • Hardware Contributor

    First make sure a pure repeater node is working. With only the gw.process() to run, it should be fast to enough catch any new inlusion before the gateway. With a proper range of course. Clear the sensors eeprom and Vera-device before reinclusion, but you should not need to do anything with the gateway.



  • @Dwalt said:

    @CaptainZap It your sketch you have this line:
    #define ONE_WIRE_BUS 14

    What pin is your Dallas sensor connected to?

    One sensor is connected to A0(14) the other is connected to D5 as it can be seen in my last sketch. Temperature reporting worked on, in my original sketch, however the sketch wasn't perfect so I improved it based on feedback.

    @m26872 The sensor is on a breadboard 5cm from the gateway, and I'm not sure about the repeater part...



  • Well since nobody knew what the problem is I thought I would re-flash my first sketch which was working properly... however after doing it it no longer works... this is blowing my mind away. I don't understand why that would be, as the only changes I've done are listed in this thread, major one was that I upgraded lib to 1.4.1 from 1.4. Anyone has any ideas cause this is driving me crazy now 😞


  • Hero Member

    @CaptainZap would suggest to clear the eeprom


  • Hardware Contributor

    Did you ever get a repeater-only node to work in your mys network ?



  • @Moshe-Livne @m26872 : I did clear the eprom, I've tried everything I could think of... I'm not quite sure if the repeater works, so far everything is at the breadboard state, so I was pretty much prototyping.


  • Hardware Contributor

    @CaptainZap said:

    @AWI Yes I want all my nodes to be repeaters,

    Why?
    Unless you plan expand your network in the near future, think the increased complexity just will give you trouble.


  • Hero Member

    @CaptainZap did you clear the eeprom before loading the sketch that used to work?



  • Hello,

    I've read this thread with interest because I want to make my Motion/Temp sensor using Dallas.

    Adapting the given code was quite easy (well I guess as I have not tested it for now 😄 ), I only have changed gw.wait with gw.sleep, because if I understand it correctly, gw.sleep put the sensor in sleep mode which is a bit better for battery :

    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #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_S1 1   // Id of the sensor child
    
    
    #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected 
    #define CHILD_ID_T1 2 //CHILD ID for temp
    
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DallasSensors(&oneWire);
    float lastTemperature ;
    unsigned long SLEEP_TIME = 30000; // Sleep time between reports (in milliseconds)
    unsigned long lastRefreshTime = 0;
    boolean lastTripped = 0;
    
    MySensor gw;
    // Initialize motion message
    MyMessage motionMsg(CHILD_ID_S1, V_TRIPPED);
    MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
    
    void setup()  
    {  
       // Startup OneWire Temp Sensors
        DallasSensors.begin();
        DallasSensors.setWaitForConversion(false);
    
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion and Temperature Sensor", "1.0");
    
      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_S1, S_MOTION);
      gw.present(CHILD_ID_T1, S_TEMP);
      
    }
    
    void loop()     
    {
    	// Process incoming messages (like config from server)
    	gw.process();
    	
    	// Read digital motion value
    	boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
    
    	if (lastTripped != tripped ) 
    	{
    		Serial.println("Tripped");
    		gw.send(motionMsg.set(tripped?"1":"0")); // Send tripped value to gw 
    		lastTripped=tripped;
    	}
    
    	boolean bNeedRefresh = (millis() - lastRefreshTime) > SLEEP_TIME;
    	if (bNeedRefresh)
    	{
    		lastRefreshTime = millis();
    		DallasSensors.requestTemperatures();
    		
    		gw.sleep(750);
    		float tempC = DallasSensors.getTempCByIndex(1);
    		float difference = lastTemperature - tempC;
    		if (tempC != -127.00 && abs(difference) > 0.1)
    		{
    			// Send in the new temperature
    			gw.send(tempMsg.set(tempC, 1));
    			lastTemperature = tempC;
    		}
    	}
    	else
    	{
    		gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    	}
    }
    
    
    

    But I'm concerned about thoses particular lines :

    lastRefreshTime = millis();
    		DallasSensors.requestTemperatures();
    		
    		gw.sleep(750);
    		float tempC = DallasSensors.getTempCByIndex(1);
    

    Why do we need gw.sleep(750) ? My concern is, when the sensor tries to pull temperature, it could not trigger new motion state for at least 750ms ? This is a problem for me, as I want to use these sensor to trigger light when I come in the room, so I need it to be fast.

    Thanks !


  • Contest Winner

    @petoulachi said:

    this will sleep the device until the interrupt triggers it to awaken...

    gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    

    you are correct that it seems that the sleep(750) function will block your device, I don't think you want that.



  • @petoulachi @BulldogLowell Glad to hear there is some progress, in regards to my issue and hopefully this is step in the right direction. I'm positive that we're not alone in our need to create multisensors, both powered and battery operated, as arduinos offer too many possibilities to limit yourself to just a single type of device 🙂


  • Contest Winner

    @CaptainZap said:

    hopefully this is step in the right direction

    you don't have a working sketch?



  • @BulldogLowell Temperature isn't working, and is never updated on the Vera interface 😐


  • Contest Winner

    try something like this, which is untested.

    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    //myPins
    const int PIR_PIN = 3;
    const int INTERRUPT = 1;
    const int DALLAS_PIN = 4;
    
    //myDevices
    const int PIR_SENSOR = 1;
    const int DALLAS_SENSOR = 2;
    
    OneWire oneWire(DALLAS_SENSOR);
    DallasTemperature DallasSensors(&oneWire);
    
    int lastTemperature = -99;
    unsigned long readTempInterval = 60 * 60 * 1000UL;  // One Hour Sleep time between reports (in milliseconds)
    unsigned long sleepTime = readTempInterval; 
    boolean lastTripped = 0;
    boolean sendTemp = true;
    
    MySensor gw;
    
    MyMessage motionMsg(PIR_SENSOR, V_TRIPPED);
    MyMessage tempMsg(DALLAS_SENSOR, V_TEMP);
    
    void setup()
    {
      DallasSensors.begin();
      //DallasSensors.setWaitForConversion(false);  /I could not get this to compile
      gw.begin();
      gw.sendSketchInfo("Multi-Sensor", "1.0alpha");
      pinMode(PIR_PIN, INPUT);      // sets the motion sensor digital pin as input
      gw.present(PIR_SENSOR, S_MOTION);
      gw.present(DALLAS_SENSOR, S_TEMP);
    }
    
    void loop()
    {
      //gw.process(); don't need this, it is a one-way device.
      boolean tripped = digitalRead(PIR_PIN);
      if (lastTripped != tripped )
      {
        Serial.println(tripped? "Tripped" : "Not Tripped");
        gw.send(motionMsg.set(tripped)); // Send tripped value to gw
        lastTripped = tripped;
      }
      if (sendTemp)
      {
        DallasSensors.requestTemperatures();
        int tempC = (int) DallasSensors.getTempCByIndex(1);
        gw.send(tempMsg.set(tempC));
        lastTemperature = tempC;
        sendTemp = false;
      }
      unsigned long startSleepTime = millis();
      gw.sleep(INTERRUPT, CHANGE, sleepTime);
      unsigned long endSleepTime = millis();
      if (endSleepTime - startSleepTime >= sleepTime)
      {
        sleepTime = readTempInterval;
        sendTemp = true;
      }
      else
      {
        sleepTime = max(sleepTime -= (endSleepTime - startSleepTime), 1000UL);
      }
    }
    

    It will send temp to controller once an hour... you can change that for testing;

    also will detect motion...

    it may need debouncing.



  • @BulldogLowell Thanks, I'll try it tonight once I get home.



  • @BulldogLowell I just managed to test this and I have some questions :

    1. Motion doesn't seem to work that great - meaning that it takes at least one minute after being untripped to be tripped again
    2. Door/window doesn't seem to work - I think I have to add this part, but I'm a bit overwhelmed
    3. Temperature starts with temperature of -127C and doesn't update at all (I held my finger on it for 2 minutes) plus I think I need something to avoid -127 temps, like :
    if (lastTemperature != tempC && tempC != -127.00)
    

    For your info I used these pins to connect my sensors :

    const int PIR_PIN = 3; - PIR sensor
    const int INTERRUPT = 4; - Door/Window
    const int DALLAS_PIN = 5; - Temperature sensor
    [...]
    unsigned long readTempInterval = 1 * 60 * 1000UL; - modified read interval to 1 minute for testing purposes
    

    Also I get this output of the serial monitor once I compile and upload the sketch to my nano after 2 minutes and multiple tries to trip PIR sensor :

    sensor started, id 2
    send: 2-2-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
    send: 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
    send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Multi-Sensor
    send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=8,st=ok:1.0alpha
    send: 2-2-0-0 s=1,c=0,t=1,pt=0,l=0,st=ok:
    send: 2-2-0-0 s=2,c=0,t=6,pt=0,l=0,st=ok:
    send: 2-2-0-0 s=2,c=1,t=0,pt=2,l=2,st=ok:-127
    Tripped
    send: 2-2-0-0 s=1,c=1,t=16,pt=2,l=2,st=ok:1
    Not Tripped
    send: 2-2-0-0 s=1,c=1,t=16,pt=2,l=2,st=ok:0
    Tripped
    send: 2-2-0-0 s=1,c=1,t=16,pt=2,l=2,st=ok:1
    
    

    I would appreciate any help I can get to get this started... this project has been on hold for at least 1 year and I would like to implement it now since I have all the parts I need, including 5 cases for the motion detectors (I will need to have at least two variations for them). My focus right now is getting a working sketch, with repeating functions or not, I can place repeaters if needed that is not a problem.


  • Hero Member

    @CaptainZap The motion detector (if you are using one like in the store) has two little dials on it. One is sensitivity and the other is time delay (see http://www.mpja.com/download/31227sc.pdf). Try to play with these until you get the effect you are looking for



  • @Moshe-Livne That's not the problem... The previous code had the same pir sensor working, it has a couple of seconds timeout.
    At this time I'll take another look at my very first sketch, and remove the repeating part, maybe I can get somewhere...
    Looking back 1 year ago this was a nice idea, however I've put more time than I would have hoped for into it and I still don't have something that I can call reliable. I know this is a labor of love, and not perfect in any way, I just expected it to be a bit easier to do...



  • Did you manage to get this working?
    if yes, can you share your script?
    I am also having trouble with the dallas temp sensor, if your code works i think it can help me a lot!
    (i am trying to make a 'motion - temperature - 3 pwm dimmer' sensor



  • @ThomasDecock No I wasn't, I was able to get the standard dallas temperature working(because I thought the sensors no longer work), but unfortunately my time on this is pretty limited. I hoped I got it working until I go on vacation, but probably will look into it after.
    I would appreciate if someone could help with it, I think everyone would.


Log in to reply
 

Suggested Topics

  • 8
  • 1
  • 1
  • 5
  • 90
  • 7

8
Online

11.4k
Users

11.1k
Topics

112.7k
Posts