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. Trouble combining sketches for temp and motion control

Trouble combining sketches for temp and motion control

Scheduled Pinned Locked Moved Troubleshooting
24 Posts 6 Posters 9.6k Views 2 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.
  • 8 Offline
    8 Offline
    8200
    wrote on last edited by
    #1

    Hi,

    So I am new to building sensors.

    I have managed to build a ethernet gateway and have this connect to my Vera2.

    Furthermore I have succeeded in building temperature and motion sensors.

    So far so good.

    Now I want to combine the sensors in a new sensor - a 230V powered motion and temp sensor (based on a Nano-clone)

    I have search the forum to see if anyone else has made this kind of sensor (for me to copy the sketch) but with no luck.

    I have the tried to combine the temp sketch and the motion sketch from the build-page, into the below sketch but the sketch wont compile. I have no experience in this so my combination is mostly based on gut-feeling :)

    Is there any one who can have a look at the sketch and explain where I go wrong or even better - are there anyone with a similar sensor and working sketch that I can use?

    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>

    #define CHILD_ID_MOT 2 // Id of the sensor child
    #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected

    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);

    #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)

    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true;
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);

    // Initialize motion message
    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);

    void setup()
    {
    sensors.begin();

    gw.begin();

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Motion Sensor", "1.0");

    numSensors = sensors.getDeviceCount();

    for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    gw.present(i, S_TEMP);

    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_MOT, S_MOTION);

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Temperature Sensor", "1.0");

    // Fetch the number of attached temperature sensors
    numSensors = sensors.getDeviceCount();

    // Present all sensors to controller
    for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    gw.present(i, S_TEMP);
    }
    }

    void loop()
    {

    // Read digital motion value
    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;

    Serial.println(tripped);
    gw.send(msg.set(tripped?"1":"0")); // Send tripped value to gw

    {
    // Process incoming messages (like config from server)
    gw.process();

    // Fetch temperatures from Dallas sensors
    sensors.requestTemperatures();

    // Read temperatures and send them to controller
    for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {

    // Fetch and round temperature to one decimal
    float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
    
    // Only send data if temperature has changed and no error
    if (lastTemperature[i] != temperature && temperature != -127.00) {
    
      // Send in the new temperature
      gw.send(msg.setSensor(i).set(temperature,1));
      lastTemperature[i]=temperature;
    }
    

    }

    // Sleep until interrupt comes in on motion sensor. Send update every two minute.
    gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    }
    }

    Thanks!

    1 Reply Last reply
    0
    • hekH Offline
      hekH Offline
      hek
      Admin
      wrote on last edited by
      #2

      Here is a sketch I use myself. A combination of hum/temp (DHT) and motion.

      It reports motion status on motion or every 30 sec.

      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      #define CHILD_ID_MOTION 3
      
      #define HUMIDITY_SENSOR_DIGITAL_PIN 6
      #define MOTION_SENSOR_PIN 3
      #define INTERRUPT MOTION_SENSOR_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
      
      unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
      
      MySensor gw;
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
      
      
      void setup()  
      { 
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("Multi Motion+Hum", "1.0");
      
        // Register all sensors to gw (they will be created as child devices)
        gw.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
      
        pinMode(MOTION_SENSOR_PIN, 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_MOTION, S_MOTION);
        
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      {  
        // Read digital motion value
        boolean tripped = digitalRead(MOTION_SENSOR_PIN) == HIGH; 
        
        Serial.print("Tripped: ");
        Serial.println(tripped);
        gw.send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw 
      
        delay(dht.getMinimumSamplingPeriod());
      
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
            Serial.println("Failed reading temperature from DHT");
        } else if (temperature != lastTemp) {
          lastTemp = temperature;
          if (!metric) {
            temperature = dht.toFahrenheit(temperature);
          }
          gw.send(msgTemp.set(temperature, 1));
          Serial.print("T: ");
          Serial.println(temperature);
        }
        
        float humidity = dht.getHumidity();
        if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
        
        gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      }
      
      
      
      8 1 Reply Last reply
      0
      • hekH hek

        Here is a sketch I use myself. A combination of hum/temp (DHT) and motion.

        It reports motion status on motion or every 30 sec.

        #include <SPI.h>
        #include <MySensor.h>  
        #include <DHT.h>  
        
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 1
        #define CHILD_ID_MOTION 3
        
        #define HUMIDITY_SENSOR_DIGITAL_PIN 6
        #define MOTION_SENSOR_PIN 3
        #define INTERRUPT MOTION_SENSOR_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
        
        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
        
        MySensor gw;
        DHT dht;
        float lastTemp;
        float lastHum;
        boolean metric = true; 
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
        
        
        void setup()  
        { 
          gw.begin();
          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        
          // Send the Sketch Version Information to the Gateway
          gw.sendSketchInfo("Multi Motion+Hum", "1.0");
        
          // Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID_HUM, S_HUM);
          gw.present(CHILD_ID_TEMP, S_TEMP);
        
          pinMode(MOTION_SENSOR_PIN, 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_MOTION, S_MOTION);
          
          metric = gw.getConfig().isMetric;
        }
        
        void loop()      
        {  
          // Read digital motion value
          boolean tripped = digitalRead(MOTION_SENSOR_PIN) == HIGH; 
          
          Serial.print("Tripped: ");
          Serial.println(tripped);
          gw.send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw 
        
          delay(dht.getMinimumSamplingPeriod());
        
          float temperature = dht.getTemperature();
          if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT");
          } else if (temperature != lastTemp) {
            lastTemp = temperature;
            if (!metric) {
              temperature = dht.toFahrenheit(temperature);
            }
            gw.send(msgTemp.set(temperature, 1));
            Serial.print("T: ");
            Serial.println(temperature);
          }
          
          float humidity = dht.getHumidity();
          if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
          } else if (humidity != lastHum) {
              lastHum = humidity;
              gw.send(msgHum.set(humidity, 1));
              Serial.print("H: ");
              Serial.println(humidity);
          }
          
          gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
        }
        
        
        
        8 Offline
        8 Offline
        8200
        wrote on last edited by
        #3

        @hek Wow - thanks for a fast reply

        I have used a some what similar sketch as a basis and I guess I go wrong when adapting the sketch to the Dallas sensor which I presume is needed?

        1 Reply Last reply
        0
        • hekH Offline
          hekH Offline
          hek
          Admin
          wrote on last edited by
          #4

          The DHT11/22 sensor gives both temperature and humidity.

          8 1 Reply Last reply
          0
          • hekH hek

            The DHT11/22 sensor gives both temperature and humidity.

            8 Offline
            8 Offline
            8200
            wrote on last edited by
            #5

            @hek But I was hoping to use the DS18b20?

            1 Reply Last reply
            0
            • hekH Offline
              hekH Offline
              hek
              Admin
              wrote on last edited by
              #6

              Yep, I know. This was just an example how I combined them to help you a bit while exploring the wonderful world of embedded programming ;)

              1 Reply Last reply
              0
              • 8 Offline
                8 Offline
                8200
                wrote on last edited by
                #7

                I am gratefull ;)

                I will try to compare the sketches.

                1 Reply Last reply
                1
                • D Offline
                  D Offline
                  davvvvvo
                  wrote on last edited by
                  #8

                  Hek, tried your sketch. Motion works but not getting a temp reading on my Vera. Using the dht 22,sensor onto pin 6. Am I missing something? I'm using the same power that the motion uses on the nano.

                  1 Reply Last reply
                  0
                  • sundberg84S Offline
                    sundberg84S Offline
                    sundberg84
                    Hardware Contributor
                    wrote on last edited by sundberg84
                    #9

                    Do you have a bare DHT that needs a resistor?
                    link text
                    What does the serial say?

                    Controller: Proxmox VM - Home Assistant
                    MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                    MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                    RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      davvvvvo
                      wrote on last edited by
                      #10

                      It doesnt give a reading for temperature or humidity. Sensor works. Yeah it's the solder free module dht 22 that has the three pronges

                      DwaltD 1 Reply Last reply
                      0
                      • D davvvvvo

                        It doesnt give a reading for temperature or humidity. Sensor works. Yeah it's the solder free module dht 22 that has the three pronges

                        DwaltD Offline
                        DwaltD Offline
                        Dwalt
                        wrote on last edited by
                        #11

                        @davvvvvo Did you re-include the node after changing the sketch? What controller are you using? Post your sketch using the </> tool.

                        Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                        D 1 Reply Last reply
                        0
                        • DwaltD Dwalt

                          @davvvvvo Did you re-include the node after changing the sketch? What controller are you using? Post your sketch using the </> tool.

                          D Offline
                          D Offline
                          davvvvvo
                          wrote on last edited by
                          #12

                          @Dwalt yes I removed the node off Vera U7 and re added it as instructions say. Using Nana serial controller. The motion sensor works perfectly. I'm using hek sketch, I just cut and paste the sketch into to program and compiled and uploaded. Do I need a resistor with the dht 22 temp sensor because I don't have one on it at the moment.

                          DwaltD 1 Reply Last reply
                          0
                          • D davvvvvo

                            @Dwalt yes I removed the node off Vera U7 and re added it as instructions say. Using Nana serial controller. The motion sensor works perfectly. I'm using hek sketch, I just cut and paste the sketch into to program and compiled and uploaded. Do I need a resistor with the dht 22 temp sensor because I don't have one on it at the moment.

                            DwaltD Offline
                            DwaltD Offline
                            Dwalt
                            wrote on last edited by
                            #13

                            @davvvvvo If you used Hek's sketch above and your DHT is connected to pin 6 and the PIR on pin 3, it should work. How are you powering the DHT and PIR? You should be using the nano's 5v. You do not need a separate resistor on the DHT module (3 pins), it has one onboard the module. Are you seeing the temp and hum child devices in Vera? If so, try powering the node off-on and reloading the Vera page.

                            Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                            D 1 Reply Last reply
                            0
                            • DwaltD Dwalt

                              @davvvvvo If you used Hek's sketch above and your DHT is connected to pin 6 and the PIR on pin 3, it should work. How are you powering the DHT and PIR? You should be using the nano's 5v. You do not need a separate resistor on the DHT module (3 pins), it has one onboard the module. Are you seeing the temp and hum child devices in Vera? If so, try powering the node off-on and reloading the Vera page.

                              D Offline
                              D Offline
                              davvvvvo
                              wrote on last edited by davvvvvo
                              #14

                              @Dwalt I'm powering both sensors off the 5v pin on the board so they are sharing the power. Temp is on pin 6. All three nodes added to Vera. I can arm or bypass the motion and this works, the temp and humidity node are on Vera but not showing any numbers. Temp is at 0 degrees and humidity is at 0%. I tried cycling the nodes off and on a few times. With no results.

                              DwaltD 1 Reply Last reply
                              0
                              • D davvvvvo

                                @Dwalt I'm powering both sensors off the 5v pin on the board so they are sharing the power. Temp is on pin 6. All three nodes added to Vera. I can arm or bypass the motion and this works, the temp and humidity node are on Vera but not showing any numbers. Temp is at 0 degrees and humidity is at 0%. I tried cycling the nodes off and on a few times. With no results.

                                DwaltD Offline
                                DwaltD Offline
                                Dwalt
                                wrote on last edited by
                                #15

                                @davvvvvo If you eliminate any wiring mistakes, I assume you must have a bad DHT sensor. You could try it without the PIR and load up the basic temp/hum sketch to see if it works or see if you can see anything through the serial log.

                                Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                                D 1 Reply Last reply
                                0
                                • DwaltD Dwalt

                                  @davvvvvo If you eliminate any wiring mistakes, I assume you must have a bad DHT sensor. You could try it without the PIR and load up the basic temp/hum sketch to see if it works or see if you can see anything through the serial log.

                                  D Offline
                                  D Offline
                                  davvvvvo
                                  wrote on last edited by
                                  #16

                                  @Dwalt Just used the DHT sensor on its own and loaded the humidity sketch and it works perfectly. did try and load the dallas temp sketch and that didnt work on its own. Question will i need to upload if there is one someting like a dallas.JSON or or another XML onto vera for hek's sketch to work possibly?

                                  DwaltD 1 Reply Last reply
                                  0
                                  • D davvvvvo

                                    @Dwalt Just used the DHT sensor on its own and loaded the humidity sketch and it works perfectly. did try and load the dallas temp sketch and that didnt work on its own. Question will i need to upload if there is one someting like a dallas.JSON or or another XML onto vera for hek's sketch to work possibly?

                                    DwaltD Offline
                                    DwaltD Offline
                                    Dwalt
                                    wrote on last edited by
                                    #17

                                    @davvvvvo The Dallas sensor is completely different from the DHT and does not factor into your setup. The DHT provides both temp and humidity. The Dallas temp sensor uses a separate library for pulling data from that particular sensor. Can you post your serial log from your sensor with both DHT and PIR connected using the sketch Hek provided above? That would tell us what is happening at the communication level between the sensor node and gateway.

                                    Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                                    D 3 Replies Last reply
                                    0
                                    • DwaltD Dwalt

                                      @davvvvvo The Dallas sensor is completely different from the DHT and does not factor into your setup. The DHT provides both temp and humidity. The Dallas temp sensor uses a separate library for pulling data from that particular sensor. Can you post your serial log from your sensor with both DHT and PIR connected using the sketch Hek provided above? That would tell us what is happening at the communication level between the sensor node and gateway.

                                      D Offline
                                      D Offline
                                      davvvvvo
                                      wrote on last edited by
                                      #18

                                      @Dwalt how do i go about gathering the serial log. Can i plug in the sensor and download the serial log with the arduino program

                                      1 Reply Last reply
                                      0
                                      • DwaltD Dwalt

                                        @davvvvvo The Dallas sensor is completely different from the DHT and does not factor into your setup. The DHT provides both temp and humidity. The Dallas temp sensor uses a separate library for pulling data from that particular sensor. Can you post your serial log from your sensor with both DHT and PIR connected using the sketch Hek provided above? That would tell us what is happening at the communication level between the sensor node and gateway.

                                        D Offline
                                        D Offline
                                        davvvvvo
                                        wrote on last edited by
                                        #19
                                        This post is deleted!
                                        1 Reply Last reply
                                        0
                                        • DwaltD Dwalt

                                          @davvvvvo The Dallas sensor is completely different from the DHT and does not factor into your setup. The DHT provides both temp and humidity. The Dallas temp sensor uses a separate library for pulling data from that particular sensor. Can you post your serial log from your sensor with both DHT and PIR connected using the sketch Hek provided above? That would tell us what is happening at the communication level between the sensor node and gateway.

                                          D Offline
                                          D Offline
                                          davvvvvo
                                          wrote on last edited by
                                          #20

                                          @Dwalt
                                          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=16,st=ok:Multi Motion+Hum
                                          send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                          send: 2-2-0-0 s=0,c=0,t=7,pt=0,l=0,st=ok:
                                          send: 2-2-0-0 s=1,c=0,t=6,pt=0,l=0,st=ok:
                                          send: 2-2-0-0 s=3,c=0,t=1,pt=0,l=0,st=ok:
                                          Tripped: 0
                                          send: 2-2-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
                                          Failed reading temperature from DHT
                                          Failed reading humidity from DHT
                                          Tripped: 0
                                          send: 2-2-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
                                          Failed reading temperature from DHT
                                          Failed reading humidity from DHT
                                          Tripped: 0
                                          send: 2-2-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
                                          Failed reading temperature from DHT
                                          Failed reading humidity from DHT

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


                                          21

                                          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