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 5.8k 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.
  • thermistor1T Offline
    thermistor1T Offline
    thermistor1
    wrote on last edited by thermistor1
    #1

    First, I'm using a Mac 10.6.8 with Indigo 6 and the MySensors plugin (newest Arduino software, two Diecimillias with 328 chips). I have two arduinos that communicate with each other, and I have caps on the radios for both so they are reliably communicating.

    One of my arduinos has multiple devices attached from a previous project and I'm trying to code them over to MySensors. Specifically, there is a Temp/Humidity sensor, a PIR motion sensor, an RGB LED to indicate what the arduino is doing, and 4 relays. I can get each device/sensor to behave properly on its own, but when I try using them together I get strange behavior, such as:

    1. A build-up of messages or something in a buffer somewhere, so that it takes the Indigo plugin a couple minutes to get through the messages (it processes about one every 10 seconds, and I'm sending 1 to 3 messages every ten seconds. In the code below, after all the devices are registered, the temperature and humidity update once and then the only updates I get are from the PIR sensor.

    2. Extra relays - the plugin mistakes two of the sensors for relays, despite manually registering only 4. This is not a problem in the code below but has happened with previous attempts, I THINK I fixed it when I turned the gw.begin node to false but I could be wrong.

    3. Erratic relay behavior/unreliability. The wrong relay or state is sometimes triggered. With the code below, the relays are not even responding.

    Below is my code, I'm hoping someone can give me insight on why I may be getting such behavior.

    #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(NULL, 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);
      pinMode(RELAY_1, OUTPUT);   
      digitalWrite(RELAY_1,RELAY_OFF);
      gw.present(RELAY_2, S_LIGHT);
      pinMode(RELAY_2, OUTPUT);   
      digitalWrite(RELAY_2,RELAY_OFF);
      gw.present(RELAY_3, S_LIGHT);
      pinMode(RELAY_3, OUTPUT);   
      digitalWrite(RELAY_3,RELAY_OFF);
      gw.present(RELAY_4, S_LIGHT);
      pinMode(RELAY_4, OUTPUT);   
      digitalWrite(RELAY_4,RELAY_OFF);
    
    
      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;
      }
      if (currentMillis - previousMillis2 > interval2) {
        float state = digitalRead(DIGITAL_INPUT_SENSOR);
        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-1+RELAY_1, 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());
       }
    }
    
    thermistor1T 1 Reply Last reply
    0
    • thermistor1T thermistor1

      First, I'm using a Mac 10.6.8 with Indigo 6 and the MySensors plugin (newest Arduino software, two Diecimillias with 328 chips). I have two arduinos that communicate with each other, and I have caps on the radios for both so they are reliably communicating.

      One of my arduinos has multiple devices attached from a previous project and I'm trying to code them over to MySensors. Specifically, there is a Temp/Humidity sensor, a PIR motion sensor, an RGB LED to indicate what the arduino is doing, and 4 relays. I can get each device/sensor to behave properly on its own, but when I try using them together I get strange behavior, such as:

      1. A build-up of messages or something in a buffer somewhere, so that it takes the Indigo plugin a couple minutes to get through the messages (it processes about one every 10 seconds, and I'm sending 1 to 3 messages every ten seconds. In the code below, after all the devices are registered, the temperature and humidity update once and then the only updates I get are from the PIR sensor.

      2. Extra relays - the plugin mistakes two of the sensors for relays, despite manually registering only 4. This is not a problem in the code below but has happened with previous attempts, I THINK I fixed it when I turned the gw.begin node to false but I could be wrong.

      3. Erratic relay behavior/unreliability. The wrong relay or state is sometimes triggered. With the code below, the relays are not even responding.

      Below is my code, I'm hoping someone can give me insight on why I may be getting such behavior.

      #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(NULL, 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);
        pinMode(RELAY_1, OUTPUT);   
        digitalWrite(RELAY_1,RELAY_OFF);
        gw.present(RELAY_2, S_LIGHT);
        pinMode(RELAY_2, OUTPUT);   
        digitalWrite(RELAY_2,RELAY_OFF);
        gw.present(RELAY_3, S_LIGHT);
        pinMode(RELAY_3, OUTPUT);   
        digitalWrite(RELAY_3,RELAY_OFF);
        gw.present(RELAY_4, S_LIGHT);
        pinMode(RELAY_4, OUTPUT);   
        digitalWrite(RELAY_4,RELAY_OFF);
      
      
        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;
        }
        if (currentMillis - previousMillis2 > interval2) {
          float state = digitalRead(DIGITAL_INPUT_SENSOR);
          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-1+RELAY_1, 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());
         }
      }
      
      thermistor1T Offline
      thermistor1T Offline
      thermistor1
      wrote on last edited by
      #2

      @thermistor1 I should add that the I want the relays to start with pins HIGH which makes the relays OFF every time I start the arduino. There are appliances that I want to remain off unless the pin goes low (e.g. the home router is ON when the pin is HIGH and I don't want it to reset every time the arduino is energized).

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

        You don't write how you power the relays.

        thermistor1T 1 Reply Last reply
        0
        • hekH hek

          You don't write how you power the relays.

          thermistor1T Offline
          thermistor1T 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
          • hekH Offline
            hekH 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.

            thermistor1T 2 Replies Last reply
            0
            • hekH 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.

              thermistor1T Offline
              thermistor1T 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
              • hekH 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.

                thermistor1T Offline
                thermistor1T 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());
                   }
                }
                
                RJ_MakeR 1 Reply Last reply
                0
                • thermistor1T 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());
                     }
                  }
                  
                  RJ_MakeR Offline
                  RJ_MakeR 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
                  • BulldogLowellB Offline
                    BulldogLowellB 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);
                    
                    thermistor1T 1 Reply Last reply
                    0
                    • BulldogLowellB 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);
                      
                      thermistor1T Offline
                      thermistor1T 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.

                      BulldogLowellB 1 Reply Last reply
                      0
                      • thermistor1T Offline
                        thermistor1T 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
                        • thermistor1T 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.

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

                          @thermistor1

                          So... all working OK now?

                          thermistor1T 1 Reply Last reply
                          0
                          • BulldogLowellB BulldogLowell

                            @thermistor1

                            So... all working OK now?

                            thermistor1T Offline
                            thermistor1T 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
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            14

                            Online

                            11.7k

                            Users

                            11.2k

                            Topics

                            113.0k

                            Posts


                            Copyright 2019 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