Every time gw.send from a sensor is invoked my, actuator (relay) powers on and off



  • Hi,
    I am coding a sketch for a MySensor-Sensorboard with a few sensors like temperature etc and two relays. I had Problems with the sketches I found, so I tried myself (unfinished).
    No problem with the relays, they did what they should. But after adding the first sensor the problem began. At the Point in the code where I want to send the value of the sensor to the Gateway with "gw.send..." my relay (mostly the second one on Pin5, seldom the first on Pin4) powers up and back down. It sounds like the powering after a reset.
    I have everything on a breadboard on a Arduino Nano powered over USB. I use Version 1.5 of MySensors.
    Is there someone, who knows this Problem?

    The message in the Monitor when the sensor send is:

    send: 111-111-0-0 s=8,c=1,t=0,pt=7,l=5,sg=0,st=ok:20.0
    

    The Code:

    #include <MySensor.h>
    #include <SPI.h>
    #include <DHT.h>
    
    
    #define Relay1 1    //Variables for all Sensors for presenting to GW
    #define Relay2 2
    #define LED 3
    #define Piezo 4
    #define Motion 5
    #define Button1 6
    #define Button2 7
    #define DHT_temp 8
    #define DHT_hum 9
    #define Brightness 10
    
    #define Relay1_PIN 4    //Pins for the sensors, actuator and triggers
    #define Relay2_PIN 5
    #define LED_PIN 9
    #define Piezo_PIN 10
    #define Motion_PIN 3
    #define Button1_PIN 7
    #define Button2_PIN 8
    #define DHT_PIN 6
    #define brightness_PIN A0
    
    MyMessage Meldung_temp(DHT_temp,V_TEMP);        //Testmessage for Troubleshooting
    MyMessage msg11(DHT_temp,V_TEMP);                     //Sensormessages
    MyMessage msg12(DHT_hum,V_HUM);
    MyMessage msg13(Brightness,V_LIGHT_LEVEL);
    
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    unsigned long previousMillis = 0; // last time update
    long interval = 15000;                     //Intervall for processing the sensors all x (default 60000) milliseconds
    
    
    MySensor gw;
    DHT dht;
    
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, 111, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay Eigener Entwurf", "0.4");
    
      
    //-----------Present Sensors, Triggers, Actuators -----------------  
    
      gw.present(Relay1, S_LIGHT);
        pinMode (Relay1_PIN, OUTPUT);                                       // Then set relay pins in output mode
        digitalWrite(Relay1_PIN, gw.loadState(Relay1)?RELAY_ON:RELAY_OFF);  // Set relay to last known state (using eeprom storage) 
      gw.present(Relay2, S_LIGHT);
        pinMode (Relay2_PIN, OUTPUT);                                       // Then set relay pins in output mode
        digitalWrite(Relay2_PIN, gw.loadState(Relay2)?RELAY_ON:RELAY_OFF);  // Set relay to last known state (using eeprom storage) 
      gw.present(DHT_temp, S_TEMP);
      gw.present(DHT_hum, S_HUM);
        pinMode(DHT_PIN, INPUT);
      gw.present(Brightness, S_LIGHT_LEVEL);
        pinMode(brightness_PIN, INPUT);  
    
      dht.setup(DHT_PIN);  
    }
    
    void loop() 
    {
      unsigned long currentMillis = millis();
      
      // Alway process incoming messages whenever possible
      gw.process();
      
      delay(dht.getMinimumSamplingPeriod());
      
      if(currentMillis - previousMillis > interval)
        {
          previousMillis = currentMillis;
          Serial.print("Es sind: ");Serial.print(currentMillis);Serial.println(" Millisekunden vergangen!");
    
          float DHT_temp_Read = dht.getTemperature();
          if (isnan (DHT_temp_Read))
            {
              Serial.println("Failed reading temperatur from DHT");
            }
          else
            {
              gw.send(Meldung_temp.set(DHT_temp_Read,1));
              Serial.print("T: "); Serial.println(DHT_temp_Read);  
            }
        }
      
      
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
       if (message.type==V_LIGHT) 
         {
           int actPin; 
           if (message.sensor == 1){ actPin = Relay1_PIN;}
           if (message.sensor == 2){ actPin = Relay2_PIN;}
           if (message.sensor == 3){ actPin = LED_PIN;}
           if (message.sensor == 4){ actPin = Piezo_PIN;}
           if (message.sensor == 8){ actPin = 55;}
         
           // Change relay state
           digitalWrite(actPin, message.getBool()?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.saveState(message.sensor, message.getBool());
           // Write some debug info
           Serial.print("Incoming change for sensor:");
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
       } 
    }
    

    Thanks in advance and regards,
    Thorsten


  • Mod

    Hi Thorsten. Welcome to MySensors!

    What capacitor(s) are you using and how is the sensor powered?

    I see that you have two buttons defined in the sketch, but they don't seem to be used anywhere. Have you posted a subset if your sketch or are the buttons truly unused?



  • Hi,
    No its no subset, i have just not finished because of the situation. Where do you expect capacitors (at the moment there aren't any). The Arduino Nano is powerd via ist USB Connection the sensor (DHT11) via the 5V PIN of the Arduino.
    In the meantime I did a bit try an error and used PIN2 instead PIN5 for my second relay. No everything seems ok, but I don't think that this the solution, just a workaround.
    Is there a known issue with triggering Special PINs when doing an gw.send ?


  • Mod

    Instructions for connecting a capacitor are available here.
    The sticky post at the top of the Troubleshooting category might be useful, especially this part:

    How do you power your sensor/gateway? The NRF-radios is very sensitive to power spikes and transients. 90% of communication problems can be resolved by replacing a bad transformer or adding a decoupling capacitor to your radio.

    What is at the other end of the USB cable? A computer or a USB charger?



  • @mfalkvidd at the moment a Computer.


  • Hardware Contributor

    I have experienced this and it helped me to drav 5v direct from the power source and no through the arduino (Yes, this is hard to do if you have connected usb to a nano). Maybe you can break out the power to a board and try?



  • @sundberg84 said:

    I have experienced this and it helped me to drav 5v direct from the power source and no through the arduino (Yes, this is hard to do if you have connected usb to a nano). Maybe you can break out the power to a board and try?

    I will try. When i am ready with coding and begin soldering, I will use an Pro Mini and so it is no Problem.


  • Hardware Contributor

    What kind of relay is it and how do you have it wired (a schematic is always helpful)? As people have said, it's most likely a power drop which can be fixed with bypass capacitor. I always use at least one cap on the radio and I use caps on the relay power lines as well. Relays (depending on the type) can also put a lot of noise on the line and do other weird things so if you have the space, it's not a bad idea to use an optocoupler (or buy a break out board with one) to isolate the relay coil (assuming it's not a solid state relay) from the arduino. You can read more here: https://arduino-info.wikispaces.com/RelayIsolation. I use a circuit similar to that one and also added 2 caps (0.01 and 10uf) on the relay power line as well and haven't had any problems so far.



  • @TD22057
    Hi I use a relay breakoutboard with optocoupler. I think it is the Radio where I have to add a capacitor. Ist only sometime now an when I can see that the strange behaviour occurs, when a sensor tries to send a message to the GW and received an error.
    I think this will be the solution so I close here.


Log in to reply
 

Suggested Topics

  • 3
  • 15
  • 2
  • 2
  • 2
  • 3

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts