2 channel relais with 2 buttons sending strange values for on:off



  • Hello,

    I have an 2 channel relais device with two buttons attached to controll them locally (to be integrated in wall light switch). Here is the sketch:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_1  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    #define BUTTON1_ID 4
    #define BUTTON_PIN1  4  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON2_ID 5
    #define BUTTON_PIN2  5  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    
    // Instantiate a Bounce object
    Bounce debouncer1 = Bounce();
    
    // Instantiate another Bounce object
    Bounce debouncer2 = Bounce();
    
    int oldValueB1 = -1;
    int oldValueB2 = -1;
    
    MyMessage msgB1(BUTTON1_ID, V_LIGHT);
    MyMessage msgB2(BUTTON2_ID, V_LIGHT);
    MyMessage msgR1(1, V_LIGHT);
    MyMessage msgR2(2, V_LIGHT);
    
    void setup() {
    
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, 16, false);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay with Button", "1.0");
    
    
      // Setup the first button with an internal pull-up :
      pinMode(BUTTON1_ID, INPUT_PULLUP);
      // After setting up the button, setup the Bounce instance :
      debouncer1.attach(BUTTON1_ID);
      debouncer1.interval(5); // interval in ms
    
      // Setup the second button with an internal pull-up :
      pinMode(BUTTON2_ID, INPUT_PULLUP);
      // After setting up the button, setup the Bounce instance :
      debouncer2.attach(BUTTON2_ID);
      debouncer2.interval(5); // interval in ms
    
      // Fetch relay status
      for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, 0);
      }
    
      gw.present(BUTTON1_ID, S_LIGHT);
      gw.present(BUTTON2_ID, S_LIGHT);
    }
    
    void loop() {
    
    
      // Update the Bounce instances :
      debouncer1.update();
      debouncer2.update();
    
      int value1 = digitalRead(4);//debouncer1.read();
      int value2 = debouncer2.read();
    
      if (value1 != oldValueB1) {
        Serial.print("Setze Button1 auf");
        Serial.println(value1);
        gw.send(msgB1.set(value1 == HIGH ? 1 : 0));
        oldValueB1 = value1;
        digitalWrite(6,value1);
        Serial.println("Schalte Relais");
        gw.send(msgR1.set(value1 == HIGH ? 1 : 0), true);
      }
      if (value2 != oldValueB2) {
        Serial.print("Setze Button2 auf");
        Serial.println(value2);
        gw.send(msgB2.set(value2 == HIGH ? 1 : 0));
        oldValueB2 = value2;
        digitalWrite(7,value1);
        Serial.println("Schalte Relais");
        gw.send(msgR2.set(value2 == HIGH ? 1 : 0), true);
      }
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
    
      if (message.isAck())
      {
        Serial.println("This is an ack from gateway");
      }
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_LIGHT) {
        // Change relay state
        digitalWrite(message.sensor - 1 + RELAY_1, 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());
      }
    
    }
    

    At first everything is working fine. When using the pins 4 and 5 as local switch relais switches and state is send to gw. In MYSController I see the message with payload 1 or 0 as expected. After some time (few minutes) the payload changes to 1107296256 (instead of 0) and 1107296257 (instead of 1). Of course my controller (pimatic) can not interpret this and does not know what to do and does not set status of relais accordingly.
    But sending 0 or 1 from controller (both pimatic and MYSController) works all the time. Even manually contructing a message with payload 1107296256 or 1107296257 switches the relais to on and off.
    Does anyone see a problem in my sketch or knows that strange number?

    Best regards,
    Anduril


  • Mod

    Are you using an ESP8266? If so, this thread seems to discuss a similar problem.



  • @mfalkvidd thank you very much. I use a ESP as gateway and was following this thread, but must have missed that problem in the 200+ posts. I will try to use newest development branch and see if problem still exists.



  • ok tested that again, installed a fresh mysensors lib (stable master, downloaded 11.01.16) and first test ran fine. Relay gives 1 or 0 as status, worked even for hours.
    But after powering another node it starts again. Sad that I have no third one here at the moment to test if its a general problem with more than one node or specific to this one. I also reflashed the problematic node (with dht and switch), but nothing changed. Is it possible that a message from the DHT-node brings the ESP to a special mode/error state that makes it misinterprete 0 and 1 readings to 1107296257?



  • Did some further tests. As it seems to be related with the ESP gateway maybe @Yveaux has a hint for me.
    When starting the relay node it works fine, until the first message of the DHT node is received. After that it starts to get strange. I already tried to recompile all sketches with newest MS 1.5.1 and cleared all eeproms (except the gw, is that neccesary?)

    MYSController.png

    just for completeness the sketch of my DHT node. If someone finds something strange in there please let me know.

    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <DHT.h>  
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID_HUM 1
    #define CHILD_ID_TEMP 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 5
    unsigned long SLEEP_TIME = 1000; // Sleep time between reads of switch (in milliseconds)
    
    unsigned int debounce_nr = 10; // No of cycles doing button test
    unsigned int cycle_min = 10; // No of cycles min for DHT read
    unsigned int cycle_max = 60; // No of cycles max for DHT read (must be cycle_min*n)
    float Temp_dif =0.3; // change in Temp to trigger send
    float Hum_dif =3; // change in Hum to trigger send
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean sendNow = true;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    unsigned int debouncecycle=0;
    unsigned int cycle=0;
    unsigned int cycle_multiplier = 1;
    float temperature;
    float humidity;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("DHT with Switch", "1.0");
      
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(CHILD_ID, S_DOOR);
      delay(10);  
      gw.present(CHILD_ID_HUM, S_HUM);
      delay(10);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      
    
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
    
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
         }
      if (cycle >= cycle_min * cycle_multiplier) {
      cycle_multiplier=cycle_multiplier+1;
        delay(dht.getMinimumSamplingPeriod());
    
      temperature = dht.getTemperature();
      //Serial.print("Temp: ");
      //Serial.print(temperature);
      //Serial.print("LastTemp: ");
      //Serial.println(lastTemp);
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if ((temperature < lastTemp - Temp_dif) or (temperature > lastTemp + Temp_dif)) {
        sendNow=true;
        Serial.println("SendNow set by Temp");
        
      }
      
      humidity = dht.getHumidity();
      //Serial.print("Hum: ");
      //Serial.print(humidity);
      //Serial.print("LastHum: ");
      //Serial.println(lastHum);
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if ((humidity < lastHum - Hum_dif) or (humidity > lastHum + Hum_dif)) {
          sendNow=true;
          Serial.println("SendNow set by Hum");
          
      }
      if (cycle >= cycle_max){
        sendNow=true;
        Serial.println("SendNow set by cycle_max");
      }
      }
      if (debouncecycle>debounce_nr){
      gw.sleep(SLEEP_TIME); //sleep a bit
      debouncecycle=0;
      cycle=cycle+1;
      //Serial.print("Anzahl Zyklen: ");
      Serial.println(cycle);
      }
      debouncecycle=debouncecycle+1;
      Serial.print(debouncecycle);
      if (sendNow){
        sendNow = false;
        cycle=0;
        cycle_multiplier=1;
        Serial.println("Zyklen zurueckgesetzt, Daten gesendet.");
        Serial.println(cycle);
        gw.send(msgTemp.set(temperature, 1));
        lastTemp = temperature;
        gw.send(msgHum.set(humidity, 1));
        lastHum = humidity;
        
      }
      
    } 
    
    

  • Mod

    @Anduril is the second node running the same MySensors version as the other node and gateway?
    What Arduino core version are you using for ESP support?



  • Used: C:\Users\vampircik\Documents\Arduino\libraries\Bounce2
    Not used: C:\arduino-1.6.3\libraries\Bounce2

    I tried to upload a sketch, an error says no such library



  • @Yveaux yeah I compiled all 2 nodes and the gw just yesterday with the same MySensors version. With Arduino core version you mean the version of Arduino IDE? 1.6.6
    Is it necessary/usefull/possible to do a eeprom reset on the gw?
    Btw sometimes MYSController does not recognice all presentation messages. Is that somehow a problem? I don't know why this happens as all my nrf are equiped with 4.7µF (node in question also tried with 2 caps).


  • Mod



  • ESP.PNG



  • these relays your sketch works well, but is not displayed correctly in the controller
    http://ru.aliexpress.com/item/2-channel-New-2-channel-relay-module-relay-expansion-board-5V-low-level-triggered-2-way/1617727658.html
    and with these relay failures. why is that? although "RelayWithButtonActuator" in truth it works with one group
    http://ru.aliexpress.com/item/10PCS-LOT-New-original-solid-state-relay-G3MB-202P-DC-AC-PCB-SSR-In-5VDC-Out/32339505849.html



  • @vampircik I don't realy understand what you tried to say. At the moment I have no real relays connected but simply LEDs for testing purposes.
    @Yveaux I updated the ESP8266 definitions to ver 2.0.0. But now I can't upload a sketch. I get this message:

    warning: espcomm_sync failed
    error: espcomm_open failed
    

    Do you know what that means?


  • Mod

    @Anduril said:

    warning: espcomm_sync failed
    error: espcomm_open failed

    Did you select the right target board & settings? They did make some changes there with 2.0.0



  • I just left them on standard as before...
    just found one option named " reset mothode" which was ck and now I set it to nodemcu (only option that seems reasonable to me by it's name). Just worked to upload the sketch. I will do further tests and report back.



  • Still getting those strange numbers for relay status on or off. I will build another node to test this new constellation.



  • Now I tested with a third node the check. Also after node 3 sending data my 0 and 1 from the relay node become large numbers. One thing that seems strange to me is the behavior during reboot of the temp/hum node. First this node is active and sending temp and hum data. Readings from relay are strange as usual. When rebooting the relay signals turn to be 0 and 1 after the temp/hum node has presented itself to the gateway. But after sending the first data for temp/hum it is again receiving those large numbers. Does this make any sense for you @Yveaux?



  • I updated my gateway to newest dev version and now my nodes are communicating correctly (not updated, still 1.5.3). So the problem seems to be gone in 2.0, but I think it would still be nice to know what caused it for those not willing to upgrade to dev branch.
    For @netram and @kr0815 (and maybe others) who had similiar problems: dev branch fixes this issue.



  • Sweet! Thanks for the feedback @Anduril .


Log in to reply
 

Suggested Topics

  • 8
  • 90
  • 7
  • 5
  • 1
  • 1

8
Online

11.4k
Users

11.1k
Topics

112.7k
Posts