[SOLVED] Multiple devices demonstrating erratic behavior



  • 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());
       }
    }
    


  • @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).


  • Admin

    You don't write how you power the relays.



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


  • Admin

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

    You have to power them separately.



  • @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!



  • @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());
       }
    }
    

  • Hero Member

    @thermistor1 What is the Serial Monitor outputting?


  • Contest Winner

    @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);


  • @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.



  • 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);
         }
    
        }
      }
    }
    

  • Contest Winner

    @thermistor1

    So... all working OK now?



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



Suggested Topics

39
Online

11.4k
Users

11.1k
Topics

112.6k
Posts