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. [SOLVED] Multiple devices demonstrating erratic behavior

[SOLVED] Multiple devices demonstrating erratic behavior

Scheduled Pinned Locked Moved Troubleshooting
temperaturemotionindigoarduinorelay
13 Posts 4 Posters 7.2k 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.
  • H Offline
    H Offline
    hek
    Admin
    wrote on last edited by
    #3

    You don't write how you power the relays.

    T 1 Reply Last reply
    0
    • H hek

      You don't write how you power the relays.

      T Offline
      T Offline
      thermistor1
      wrote on last edited by
      #4

      @hek I have an 8 relay board and I'm using only 4. The board is powered off the arduino 5V pin.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hek
        Admin
        wrote on last edited by
        #5

        This is probably the cause of your problems. The Arduino 5V pin cannot provide the necessary power needed.

        You have to power them separately.

        T 2 Replies Last reply
        0
        • H hek

          This is probably the cause of your problems. The Arduino 5V pin cannot provide the necessary power needed.

          You have to power them separately.

          T Offline
          T Offline
          thermistor1
          wrote on last edited by
          #6

          @hek Thank you - it worked without the addition of the NRF24L01+ so I'm guessing that pushed it over the edge. I'll try without it and let you know, thanks!

          1 Reply Last reply
          0
          • H hek

            This is probably the cause of your problems. The Arduino 5V pin cannot provide the necessary power needed.

            You have to power them separately.

            T Offline
            T Offline
            thermistor1
            wrote on last edited by
            #7

            @hek ok, I think I solved some of this. The extra sensors were coming from previous trials and errors. I removed the device and added it again, and bingo, no extra relays.

            As for the relays not responding, I found an error in the code AND I think the motion sensor is getting stuck in a loop. When I activate all sensors there is still enough power for the relays to go. The problem is when I uncomment the code monitoring the motion sensor, everything becomes stuck again. There's a problem with the code to continuously monitor the motion sensor:

             float state = digitalRead(DIGITAL_INPUT_SENSOR);
              if (state == HIGH) {
                 if (state != lastMo) {
                  gw.send(msgPIR.set(state?"1":"0"));
                  lastMo = state;
                }
                previousMillis2 = currentMillis;
              }
            

            and here is the current version of the code:

            #include <SPI.h>
            #include <MySensor.h>  
            #include <DHT.h>  
            
            //Temp
            #define CHILD_ID_HUM 10
            #define CHILD_ID_TEMP 20
            #define HUMIDITY_SENSOR_DIGITAL_PIN 7
            
            //Motion
            #define DIGITAL_INPUT_SENSOR A0
            #define CHILD_ID_PIR 30
            float lastMo;
            
            
            // Relay
            #define RELAY_1  3
            #define RELAY_2  4
            #define RELAY_3  5
            #define RELAY_4  6
            #define RELAY_ON 0
            #define RELAY_OFF 1
            
            // LED
            int red = A5;
            int blue = A4;
            int green = A3;
            
            //
            MySensor gw;
            
            DHT dht;
            float lastTemp;
            float lastHum;
            
            //Timer
            long previousMillis1 = 0;
            long interval1 = 60000;
            long previousMillis2 = 0;
            long interval2 = 5000;
            
            MyMessage msgHum(CHILD_ID_HUM, V_HUM);
            MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
            MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
            
            void setup()
            {
              //Still unsure on conditions for this:
              gw.begin(incomingMessage, AUTO, false);
            
              //start temp sensor
              dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
            
              // Send the Sketch Version Information to the Gateway
              gw.sendSketchInfo("Arduino 2", "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);
            //  gw.present(CHILD_ID_PIR, S_MOTION);
            
              //LED
              pinMode(red, OUTPUT);
              pinMode(green, OUTPUT);
              pinMode(blue, OUTPUT);
            
              // Register Relays
            
              gw.present(RELAY_1, S_LIGHT);
              digitalWrite(RELAY_1,RELAY_OFF);
              pinMode(RELAY_1, OUTPUT);   
              gw.present(RELAY_2, S_LIGHT);
              digitalWrite(RELAY_2,RELAY_OFF);
              pinMode(RELAY_2, OUTPUT);   
              gw.present(RELAY_3, S_LIGHT);
              digitalWrite(RELAY_3,RELAY_OFF);
              pinMode(RELAY_3, OUTPUT);   
              gw.present(RELAY_4, S_LIGHT);
              digitalWrite(RELAY_4,RELAY_OFF);
              pinMode(RELAY_4, OUTPUT);   
            
            
              digitalWrite(red, HIGH);
              digitalWrite(green, LOW);
              digitalWrite(blue, LOW);
              
            }
            
            void loop()      
            {
              unsigned long currentMillis = millis();
              if(currentMillis - previousMillis1 > interval1) {
                sendTemp();
                delay(500);
                sendHumid();
                previousMillis1 = currentMillis;
              }
            /* float state = digitalRead(DIGITAL_INPUT_SENSOR);
              if (state == HIGH) {
                 if (state != lastMo) {
                  gw.send(msgPIR.set(state?"1":"0"));
                  lastMo = state;
                }
                previousMillis2 = currentMillis;
              }*/
              gw.process();
            }
            
            
            void sendTemp(){
              float temperature = dht.getTemperature();
              if (isnan(temperature)) {
                  Serial.println("Failed reading temperature from DHT");
              } else if (temperature != lastTemp) {
                lastTemp = temperature;
                temperature = dht.toFahrenheit(temperature);
                gw.send(msgTemp.set(temperature, 1));
                Serial.print("T: ");
                Serial.println(temperature);
              }
            }
            
            void sendHumid(){
              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);
              }
            }
            
            void incomingMessage(const MyMessage &message) {
              if (message.type==V_LIGHT) {
                 digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
                 gw.saveState(message.sensor, message.getBool());
                 Serial.print("Incoming change for sensor:");
                 Serial.print(message.sensor);
                 Serial.print(", New status: ");
                 Serial.println(message.getBool());
               }
            }
            
            R 1 Reply Last reply
            0
            • T thermistor1

              @hek ok, I think I solved some of this. The extra sensors were coming from previous trials and errors. I removed the device and added it again, and bingo, no extra relays.

              As for the relays not responding, I found an error in the code AND I think the motion sensor is getting stuck in a loop. When I activate all sensors there is still enough power for the relays to go. The problem is when I uncomment the code monitoring the motion sensor, everything becomes stuck again. There's a problem with the code to continuously monitor the motion sensor:

               float state = digitalRead(DIGITAL_INPUT_SENSOR);
                if (state == HIGH) {
                   if (state != lastMo) {
                    gw.send(msgPIR.set(state?"1":"0"));
                    lastMo = state;
                  }
                  previousMillis2 = currentMillis;
                }
              

              and here is the current version of the code:

              #include <SPI.h>
              #include <MySensor.h>  
              #include <DHT.h>  
              
              //Temp
              #define CHILD_ID_HUM 10
              #define CHILD_ID_TEMP 20
              #define HUMIDITY_SENSOR_DIGITAL_PIN 7
              
              //Motion
              #define DIGITAL_INPUT_SENSOR A0
              #define CHILD_ID_PIR 30
              float lastMo;
              
              
              // Relay
              #define RELAY_1  3
              #define RELAY_2  4
              #define RELAY_3  5
              #define RELAY_4  6
              #define RELAY_ON 0
              #define RELAY_OFF 1
              
              // LED
              int red = A5;
              int blue = A4;
              int green = A3;
              
              //
              MySensor gw;
              
              DHT dht;
              float lastTemp;
              float lastHum;
              
              //Timer
              long previousMillis1 = 0;
              long interval1 = 60000;
              long previousMillis2 = 0;
              long interval2 = 5000;
              
              MyMessage msgHum(CHILD_ID_HUM, V_HUM);
              MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
              MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
              
              void setup()
              {
                //Still unsure on conditions for this:
                gw.begin(incomingMessage, AUTO, false);
              
                //start temp sensor
                dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
              
                // Send the Sketch Version Information to the Gateway
                gw.sendSketchInfo("Arduino 2", "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);
              //  gw.present(CHILD_ID_PIR, S_MOTION);
              
                //LED
                pinMode(red, OUTPUT);
                pinMode(green, OUTPUT);
                pinMode(blue, OUTPUT);
              
                // Register Relays
              
                gw.present(RELAY_1, S_LIGHT);
                digitalWrite(RELAY_1,RELAY_OFF);
                pinMode(RELAY_1, OUTPUT);   
                gw.present(RELAY_2, S_LIGHT);
                digitalWrite(RELAY_2,RELAY_OFF);
                pinMode(RELAY_2, OUTPUT);   
                gw.present(RELAY_3, S_LIGHT);
                digitalWrite(RELAY_3,RELAY_OFF);
                pinMode(RELAY_3, OUTPUT);   
                gw.present(RELAY_4, S_LIGHT);
                digitalWrite(RELAY_4,RELAY_OFF);
                pinMode(RELAY_4, OUTPUT);   
              
              
                digitalWrite(red, HIGH);
                digitalWrite(green, LOW);
                digitalWrite(blue, LOW);
                
              }
              
              void loop()      
              {
                unsigned long currentMillis = millis();
                if(currentMillis - previousMillis1 > interval1) {
                  sendTemp();
                  delay(500);
                  sendHumid();
                  previousMillis1 = currentMillis;
                }
              /* float state = digitalRead(DIGITAL_INPUT_SENSOR);
                if (state == HIGH) {
                   if (state != lastMo) {
                    gw.send(msgPIR.set(state?"1":"0"));
                    lastMo = state;
                  }
                  previousMillis2 = currentMillis;
                }*/
                gw.process();
              }
              
              
              void sendTemp(){
                float temperature = dht.getTemperature();
                if (isnan(temperature)) {
                    Serial.println("Failed reading temperature from DHT");
                } else if (temperature != lastTemp) {
                  lastTemp = temperature;
                  temperature = dht.toFahrenheit(temperature);
                  gw.send(msgTemp.set(temperature, 1));
                  Serial.print("T: ");
                  Serial.println(temperature);
                }
              }
              
              void sendHumid(){
                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);
                }
              }
              
              void incomingMessage(const MyMessage &message) {
                if (message.type==V_LIGHT) {
                   digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
                   gw.saveState(message.sensor, message.getBool());
                   Serial.print("Incoming change for sensor:");
                   Serial.print(message.sensor);
                   Serial.print(", New status: ");
                   Serial.println(message.getBool());
                 }
              }
              
              R Offline
              R Offline
              RJ_Make
              Hero Member
              wrote on last edited by
              #8

              @thermistor1 What is the Serial Monitor outputting?

              RJ_Make

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by BulldogLowell
                #9

                @thermistor1 said:

                float lastMo;

                /* float state = digitalRead(DIGITAL_INPUT_SENSOR);
                  if (state == HIGH) {
                     if (state != lastMo) {
                      gw.send(msgPIR.set(state?"1":"0"));
                      lastMo = state;
                    }
                    previousMillis2 = currentMillis;
                  }*/
                

                if anything is a binary device it is a motion sensor... total possible states = 2

                You then introduce all of the problems of arduino's lousy float precision to your sketch?

                try instead to declare lastMo as a byte or int.

                byte lastMo;
                

                oh... and state too....

                byte state = digitalRead(DIGITAL_INPUT_SENSOR);
                
                T 1 Reply Last reply
                0
                • B BulldogLowell

                  @thermistor1 said:

                  float lastMo;

                  /* float state = digitalRead(DIGITAL_INPUT_SENSOR);
                    if (state == HIGH) {
                       if (state != lastMo) {
                        gw.send(msgPIR.set(state?"1":"0"));
                        lastMo = state;
                      }
                      previousMillis2 = currentMillis;
                    }*/
                  

                  if anything is a binary device it is a motion sensor... total possible states = 2

                  You then introduce all of the problems of arduino's lousy float precision to your sketch?

                  try instead to declare lastMo as a byte or int.

                  byte lastMo;
                  

                  oh... and state too....

                  byte state = digitalRead(DIGITAL_INPUT_SENSOR);
                  
                  T Offline
                  T Offline
                  thermistor1
                  wrote on last edited by
                  #10

                  @BulldogLowell Thank you! I'm not a programmer, can you tell?
                  I'll implement that. Got the rest of it working and will share the code as well.

                  B 1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    thermistor1
                    wrote on last edited by thermistor1
                    #11

                    So for those who may be wondering, here are a couple things I learned working with multiple sensors + relays:

                    • All radios need capacitors because the 3.3V on the arduino is not sufficient
                    • Extra devices and arduinos that appear in your device list can be cleared out - remove the device and re-scan, and it should sort those problems out. I had mysterious sensors appear and refuse to leave until I did this.
                    • Keep track of and double check your pin numbers.
                    • (Clearly) don't use delay(), instead use millis()
                    • I did some of this piece by piece instead of repeating a function because I was trying to diagnose problems. It could probably be cleaned up but it successfully has seven different devices being controlled and functioning properly.
                    #include <SPI.h>
                    #include <MySensor.h>  
                    #include <DHT.h>  
                    
                    //Temp
                    #define CHILD_ID_HUM 10
                    #define CHILD_ID_TEMP 20
                    #define HUMIDITY_SENSOR_DIGITAL_PIN 7
                    
                    //Motion
                    #define DIGITAL_INPUT_SENSOR A0
                    #define CHILD_ID_PIR 30
                    byte lastMo;
                    
                    
                    // Relay
                    #define RELAY_1  3
                    #define RELAY_2  4
                    #define RELAY_3  5
                    #define RELAY_4  6
                    #define RELAY_ON 0
                    #define RELAY_OFF 1
                    
                    // LED
                    int red = A5;
                    int blue = A4;
                    int green = A3;
                    
                    //
                    MySensor gw;
                    
                    DHT dht;
                    float lastTemp;
                    float lastHum;
                    
                    //Timer
                    long previousMillis1 = 0;
                    long interval1 = 60000;
                    long previousMillis2 = 0;
                    long interval2 = 5000;
                    
                    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                    MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
                    
                    void setup()
                    {
                      //Still unsure on conditions for this:
                      gw.begin(incomingMessage, AUTO, false);
                    
                      //start temp sensor
                      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
                    
                      // Send the Sketch Version Information to the Gateway
                      gw.sendSketchInfo("Arduino 2", "0.9");
                    
                      // 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);
                      gw.present(CHILD_ID_PIR, S_MOTION);
                    
                      //LED
                      pinMode(red, OUTPUT);
                      pinMode(green, OUTPUT);
                      pinMode(blue, OUTPUT);
                    
                      // Register Relays
                    
                      gw.present(RELAY_1, S_LIGHT);
                      digitalWrite(RELAY_1,RELAY_OFF);
                      pinMode(RELAY_1, OUTPUT);   
                      gw.present(RELAY_2, S_LIGHT);
                      digitalWrite(RELAY_2,RELAY_OFF);
                      pinMode(RELAY_2, OUTPUT);   
                      gw.present(RELAY_3, S_LIGHT);
                      digitalWrite(RELAY_3,RELAY_OFF);
                      pinMode(RELAY_3, OUTPUT);   
                      gw.present(RELAY_4, S_LIGHT);
                      digitalWrite(RELAY_4,RELAY_OFF);
                      pinMode(RELAY_4, OUTPUT);   
                    
                    
                      digitalWrite(red, HIGH);
                      digitalWrite(green, LOW);
                      digitalWrite(blue, LOW);
                      
                    }
                    
                    void loop()      
                    {
                      unsigned long currentMillis = millis();
                      if(currentMillis - previousMillis1 > interval1) {
                        sendTemp();
                        delay(500);
                        sendHumid();
                        previousMillis1 = currentMillis;
                      }
                      byte state = digitalRead(DIGITAL_INPUT_SENSOR);
                      if (state == HIGH) {
                         if (state != lastMo) {
                          gw.send(msgPIR.set(state?"1":"0"));
                          lastMo = state;
                         }
                      }
                      if (state == LOW) {
                         if (state != lastMo) {
                          gw.send(msgPIR.set(state?"1":"0"));
                          lastMo = state;
                         }
                      }
                      previousMillis2 = currentMillis;
                      gw.process();
                    }
                    
                    
                    void sendTemp(){
                      float temperature = dht.getTemperature();
                      if (isnan(temperature)) {
                          Serial.println("Failed reading temperature from DHT");
                      } else {
                        lastTemp = temperature;
                        temperature = dht.toFahrenheit(temperature);
                        gw.send(msgTemp.set(temperature, 1));
                        Serial.print("T: ");
                        Serial.println(temperature);
                      }
                    }
                    
                    void sendHumid(){
                      float humidity = dht.getHumidity();
                      if (isnan(humidity)) {
                          Serial.println("Failed reading humidity from DHT");
                      } else {
                          lastHum = humidity;
                          gw.send(msgHum.set(humidity, 1));
                          Serial.print("H: ");
                          Serial.println(humidity);
                      }
                    }
                    
                    void incomingMessage(const MyMessage &message) {
                      if (message.type==V_LIGHT) {
                        if (message.sensor==3){
                          digitalWrite(RELAY_1,RELAY_ON);
                          digitalWrite(red, HIGH);
                          digitalWrite(blue, HIGH);
                          digitalWrite(green, LOW);
                        } else if (message.sensor==4){
                          digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
                          gw.saveState(message.sensor, message.getBool());
                        } else if (message.sensor==5){
                          digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
                          gw.saveState(message.sensor, message.getBool());
                        } else if (message.sensor==6){
                          digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
                          gw.saveState(message.sensor, message.getBool());
                          if((digitalRead(RELAY_4))==LOW){  //If relay 4 is on:
                           digitalWrite(red, LOW);
                           digitalWrite(blue, HIGH);
                           digitalWrite(green, HIGH);
                         }
                         if((digitalRead(RELAY_4))==HIGH){  //If relay 4 is off and the thing is on:
                           digitalWrite(red, HIGH);
                           digitalWrite(blue, LOW);
                           digitalWrite(green, LOW);
                         }
                    
                        }
                      }
                    }
                    
                    1 Reply Last reply
                    0
                    • T thermistor1

                      @BulldogLowell Thank you! I'm not a programmer, can you tell?
                      I'll implement that. Got the rest of it working and will share the code as well.

                      B Offline
                      B Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #12

                      @thermistor1

                      So... all working OK now?

                      T 1 Reply Last reply
                      0
                      • B BulldogLowell

                        @thermistor1

                        So... all working OK now?

                        T Offline
                        T Offline
                        thermistor1
                        wrote on last edited by
                        #13

                        @BulldogLowell Yes, all is working, thanks. If you want to use this code for reference it is fully functional.

                        1 Reply Last reply
                        0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        12

                        Online

                        12.0k

                        Users

                        11.2k

                        Topics

                        113.4k

                        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