relay with button...relay not physically changing state



  • Hi all, please help i'm having issues with relay button sketch...relay doesn't change state when changing physical switch, i can see it changing in oh2 and arduino serial monitor i'm pretty new to this and don't know why?? other than that relay works perfect with oh2 here is my sketch

    // MySensor Debug
    #define MY_DEBUG
    
    // Enables repeater functionality (relays messages from other nodes)
    //#define MY_REPEATER_FEATURE
    
    
    #define MY_NODE_ID 50
    
    #define MY_RF24_CE_PIN 40
    #define MY_RF24_CS_PIN 53
    #define MY_RF24_MOSI_PIN 51
    #define MY_RF24_MISO_PIN 50
    #define MY_RF24_SCK_PIN 52
    
    #define MY_RADIO_NRF24
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <DHT.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define RELAY_PIN    22  // Arduino Digital I/O pin number for relay 
    #define RELAY_PIN_2  23
    #define RELAY_PIN_3  24
    #define RELAY_PIN_4  25
    #define RELAY_PIN_5  26
    #define RELAY_PIN_6  27
    #define BUTTON_PIN   9  // Arduino Digital I/O pin number for button 
    #define BUTTON_PIN_2 10
    #define BUTTON_PIN_3 11
    #define BUTTON_PIN_4 12
    #define BUTTON_PIN_5 17
    #define BUTTON_PIN_6 18
    #define CHILD_ID 11 // Id of the sensor child for 1st relay
    #define CHILD_ID_2 12 // Id of the sensor child for 2nd relay
    #define CHILD_ID_3 13
    #define CHILD_ID_4 14
    #define CHILD_ID_5 15
    #define CHILD_ID_6 16
    #define CHILD_ID_HUM1 7
    #define CHILD_ID_TEMP1 8
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 1
    #define MOTION_DATA_PIN 5
    #define CHILD_ID_MOTION 5
    // Relay status
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    // Source of state change (used when printing debug information)
    #define CHANGE_STATE_SOURCE_RADIO 0
    #define CHANGE_STATE_SOURCE_SWITCH 1
    // Set this to the pin you connected the DHT's data pin to
    #define DHT1_DATA_PIN 2
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #define SENSOR_TEMP_OFFSET 0
    
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 5000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    float lastTemp1;
    float lastHum1;
    bool lastMotion = false;
    
    uint8_t nNoUpdatesTemp1;
    uint8_t nNoUpdatesHum1;
    uint8_t nNoUpdatesMotion;
    bool metric = true;
    
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    
    
    Bounce debouncer = Bounce();
    int oldValue;
    bool state;
    Bounce debouncer2 = Bounce();
    int oldValue2;
    bool state2;
    Bounce debouncer3 = Bounce();
    int oldValue3;
    bool state3;
    Bounce debouncer4 = Bounce();
    int oldValue4;
    bool state4;
    Bounce debouncer5 = Bounce();
    int oldValue5;
    bool state5;
    Bounce debouncer6 = Bounce();
    int oldValue6;
    bool state6;
    
    MyMessage msgDallas(0,V_TEMP);
    MyMessage msg(CHILD_ID, V_LIGHT);
    MyMessage msg2(CHILD_ID_2, V_LIGHT);
    MyMessage msg3(CHILD_ID_3, V_LIGHT);
    MyMessage msg4(CHILD_ID_4, V_LIGHT);
    MyMessage msg5(CHILD_ID_5, V_LIGHT);
    MyMessage msg6(CHILD_ID_6, V_LIGHT);
    MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
    MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
    MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
    DHT dht;
    
    void before()
    {
      // Startup up the OneWire library
      sensors.begin();
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Double Relay & Button", "0.2");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_LIGHT);
      present(CHILD_ID_2, S_LIGHT);
      present(CHILD_ID_3, S_LIGHT);
      present(CHILD_ID_4, S_LIGHT);
      present(CHILD_ID_5, S_LIGHT);
      present(CHILD_ID_6, S_LIGHT);
      present(CHILD_ID_MOTION, S_MOTION);
      present(CHILD_ID_HUM1, S_HUM);
      present(CHILD_ID_TEMP1, S_TEMP);
      
      metric = getControllerConfig().isMetric;
    }
    
    void setup()
    {
      pinMode(MOTION_DATA_PIN, INPUT); 
       
       // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    //numSensors = 1;
      // Present all sensors to controller
      for (int i =0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
    }
      
      // Setup the button
      pinMode(BUTTON_PIN, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN, HIGH);
    
      // Setup the button
      pinMode(BUTTON_PIN_2, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_2, HIGH);
    
       // Setup the button
      pinMode(BUTTON_PIN_3, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_3, HIGH);
    
       // Setup the button
      pinMode(BUTTON_PIN_4, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_4, HIGH);
    
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      debouncer2.attach(BUTTON_PIN_2);
      debouncer2.interval(5);
    
      debouncer3.attach(BUTTON_PIN_3);
      debouncer3.interval(5);
    
      debouncer4.attach(BUTTON_PIN_4);
      debouncer4.interval(5);
    
      debouncer5.attach(BUTTON_PIN_5);
      debouncer5.interval(5);
    
      debouncer6.attach(BUTTON_PIN_6);
      debouncer6.interval(5);
    
      // Set the initial values of oldValue/oldValue2 variables from status of physical switches
      //  if this is not done the loop() will detect status change and switch the relays on or off
      debouncer.update();
      debouncer2.update();
      debouncer3.update();
      debouncer4.update();
      debouncer5.update();
      debouncer6.update();
      oldValue = debouncer.read();
      oldValue2 = debouncer2.read();
      oldValue3 = debouncer3.read();
      oldValue4 = debouncer4.read();
      oldValue5 = debouncer5.read();
      oldValue6 = debouncer6.read();
    
      // Make sure relays are off when starting up
      setRelayState(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);
    
      digitalWrite(RELAY_PIN_2, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_2, OUTPUT);
      
       digitalWrite(RELAY_PIN_3, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_3, OUTPUT);
    
       digitalWrite(RELAY_PIN_4, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_4, OUTPUT);
    
      digitalWrite(RELAY_PIN_5, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_5, OUTPUT);
    
      digitalWrite(RELAY_PIN_6, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_6, OUTPUT);
    
      // Set relay to last known state (using eeprom storage)
      state = loadState(CHILD_ID);
      setRelayState(RELAY_PIN, state);
      state2 = loadState(CHILD_ID_2);
      setRelayState(RELAY_PIN_2, state2);
      state3 = loadState(CHILD_ID_3);
      setRelayState(RELAY_PIN_3, state3);
      state4 = loadState(CHILD_ID_4);
      setRelayState(RELAY_PIN_4, state4);
      state5 = loadState(CHILD_ID_5);
      setRelayState(RELAY_PIN_5, state5);
      state6 = loadState(CHILD_ID_6);
      setRelayState(RELAY_PIN_6, state6);
      
    }
    
    void loop()
    {
      bool tripped = digitalRead(MOTION_DATA_PIN) == HIGH;
    if (tripped || nNoUpdatesMotion == FORCE_UPDATE_N_READS) {
        lastMotion = tripped;
        Serial.println(tripped);
        send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw
        nNoUpdatesMotion = 0;
      }
      else if (lastMotion) {
        Serial.println(tripped);
        send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw
        nNoUpdatesMotion = 0;
      }
      else {
        nNoUpdatesMotion++;
      }
      
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
      // query conversion time and sleep until conversion completed
      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      wait(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i = 0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
         //Only send data if temperature has changed and no error
        //#if COMPARE_TEMP == 1
       // if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
       // #else
       // if (temperature != -127.00 && temperature != 85.00) {
        //#endif
     
          // Send in the new temperature
          send(msgDallas.setSensor(i).set(temperature,1));
          Serial.print("Dallas");
          Serial.println(i);
          Serial.println(" :");
        Serial.println(temperature);
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        //}
      }
    
    // Sleep for a while to save energy
      wait(UPDATE_INTERVAL);
    
       
      dht.setup(DHT1_DATA_PIN); // set data pin of DHT1 sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      wait(dht.getMinimumSamplingPeriod());
      
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
      
      // Get temperature from DHT library
      float temperature1 = dht.getTemperature();
      if (isnan(temperature1)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature1 != lastTemp1 || nNoUpdatesTemp1 == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp1 = temperature1;
        if (!metric) {
          temperature1 = dht.toFahrenheit(temperature1);
        }
        // Reset no updates counter
        nNoUpdatesTemp1 = 0;
        temperature1 += SENSOR_TEMP_OFFSET;
        send(msgTemp1.set(temperature1, 1));
    
        //#ifdef MY_DEBUG
        //Serial.print("T1: ");
        //Serial.println(temperature1);
        //#endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp1++;
      }
    
      // Get humidity from DHT library
      float humidity1 = dht.getHumidity();
      if (isnan(humidity1)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity1 != lastHum1 || nNoUpdatesHum1 == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum1 = humidity1;
        // Reset no updates counter
        nNoUpdatesHum1 = 0;
        send(msgHum1.set(humidity1, 1));
        
        #ifdef MY_DEBUG
        Serial.print("H1: ");
        Serial.println(humidity1);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum1++;
      }
    
      // Sleep for a while to save energy
      wait(UPDATE_INTERVAL); 
    
    
      debouncer.update();
      debouncer2.update();
      debouncer3.update();
      debouncer4.update();
      debouncer4.update();
      debouncer4.update();
      // Get the update value
      int value = debouncer.read();
      int value2 = debouncer2.read();
      int value3 = debouncer3.read();
      int value4 = debouncer4.read();
      int value5 = debouncer5.read(); 
      int value6 = debouncer6.read(); 
    
    
      if (value != oldValue) {
        send(msg.set(state ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID, value);
      }
      oldValue = value;
    
      if (value2 != oldValue2) {
        send(msg2.set(state2 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_2, value2);
      }
      oldValue2 = value2;
     if (value3 != oldValue3) {
        send(msg3.set(state3 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_3, value3);
      }
      oldValue3 = value3;
    
      if (value4 != oldValue4) {
        send(msg4.set(state4 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_4, value4);
      }
      oldValue4 = value4;
    
      if (value5 != oldValue5) {
        send(msg5.set(state5 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_5, value5);
      }
      oldValue5 = value5;
    
      if (value6 != oldValue6) {
        send(msg6.set(state6 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_6, value6);
      }
      oldValue6 = value6;
    
     }
    
    void receive(const MyMessage &message) 
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
    #ifdef MY_DEBUG
        Serial.println(F("This is an ack from gateway"));
    #endif
      }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID) {
        // Change relay state
        state = message.getBool();
        setRelayState(RELAY_PIN, state);
        // Store state in eeprom
        saveState(CHILD_ID, state);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID, state);
      }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID_2) {
        state2 = message.getBool();
        setRelayState(RELAY_PIN_2, state2);
        // Store state in eeprom
        saveState(CHILD_ID_2, state2);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_2, state2);
      }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID_3) {
       state3 = message.getBool();
        setRelayState(RELAY_PIN_3, state3);
        // Store state in eeprom
        saveState(CHILD_ID_3, state3);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_3, state3);
      }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID_4) {
        state4 = message.getBool();
        setRelayState(RELAY_PIN_4, state4);
        // Store state in eeprom
        saveState(CHILD_ID_4, state4);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_4, state4);
        }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID_5) {
        state5 = message.getBool();
        setRelayState(RELAY_PIN_5, state5);
        // Store state in eeprom
        saveState(CHILD_ID_5, state5);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_5, state5);
        }
      else if (message.type == V_LIGHT && message.sensor == CHILD_ID_6) {
        state6 = message.getBool();
        setRelayState(RELAY_PIN_6, state6);
        // Store state in eeprom
        saveState(CHILD_ID_6, state6);
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_6, state6);
        
      }
    }
    
    // Set status of a relay pin
    void setRelayState(byte relayPin, bool value) {
      digitalWrite(relayPin, value ? RELAY_ON : RELAY_OFF);
    }
    
    // Print debug info, centralized in one place to minimize memory usage and have only one #ifdef MY_DEBUG for all state change messages
    void printStateChangedDebug(int source, int sensorID, bool value) {
    #ifdef MY_DEBUG
      Serial.print(F("Sensor value changed, source="));
      Serial.print(source == CHANGE_STATE_SOURCE_RADIO ? F("Radio") : F("Physical switch"));
      Serial.print(F(", Sensor="));
      Serial.print(sensorID);
      Serial.print(F(", New status: "));
      Serial.println(value);
    #endif
    }```
    
    any idea?  i'm stuck thanks in advance!

  • Hardware Contributor

    @Ticupolu
    Do you have a wiring diagram?
    Which arduino do you use'

    Do you really need waits to save batery?



  • thank you for response ..i'm using mega 2560 no i dont have diagram and do i need waits?? im not using batteries....trying to show you what is going on in serial monitor but dont know how to post it



  • @FotoFieber here is my serial monitor...i first close relay with oh2(it works my relay closes physically) then with physical switch (it shows relay is changing state in oh2 and serial monitor but not physically on relay)

    1
    70185 TSF:MSG:SEND,1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    71721 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.6
    Dallas0
    :
    18.60
    0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.2.0-beta
    4 MCO:BGN:BFR
    64 TSM:INIT
    65 TSF:WUR:MS=0
    72 TSM:INIT:TSP OK
    74 TSM:INIT:STATID=50
    76 TSF:SID:OK,ID=50
    77 TSM:FPAR
    114 TSF:MSG:SEND,50-50-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    586 TSF:MSG:READ,0-0-50,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    591 TSF:MSG:FPAR OK,ID=0,D=1
    2121 TSM:FPAR:OK
    2122 TSM:ID
    2123 TSM:ID:OK
    2125 TSM:UPL
    2128 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2139 TSF:MSG:READ,0-0-50,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2144 TSF:MSG:PONG RECV,HP=1
    2147 TSM:UPL:OK
    2148 TSM:READY:ID=50,PAR=0,DIS=1
    2153 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2161 TSF:MSG:READ,0-0-50,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2168 TSF:MSG:SEND,50-50-0-0,s=255,c=0,t=17,pt=0,l=10,sg=0,ft=0,st=OK:2.2.0-beta
    2178 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2197 TSF:MSG:READ,0-0-50,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2204 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=11,pt=0,l=21,sg=0,ft=0,st=OK:Double Relay & Button
    2214 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:0.2
    2223 TSF:MSG:SEND,50-50-0-0,s=11,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2231 TSF:MSG:SEND,50-50-0-0,s=12,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2239 TSF:MSG:SEND,50-50-0-0,s=13,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2247 TSF:MSG:SEND,50-50-0-0,s=14,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2254 TSF:MSG:SEND,50-50-0-0,s=15,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2263 TSF:MSG:SEND,50-50-0-0,s=16,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2271 TSF:MSG:SEND,50-50-0-0,s=5,c=0,t=1,pt=0,l=0,sg=0,ft=0,st=OK:
    2279 TSF:MSG:SEND,50-50-0-0,s=7,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2286 TSF:MSG:SEND,50-50-0-0,s=8,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
    2292 MCO:REG:REQ
    2296 TSF:MSG:SEND,50-50-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2302 TSF:MSG:READ,0-0-50,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2308 MCO:PIM:NODE REG=1
    2310 MCO:BGN:STP
    2314 TSF:MSG:SEND,50-50-0-0,s=0,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
    2320 MCO:BGN:INIT OK,TSP=1
    1
    2324 TSF:MSG:SEND,50-50-0-0,s=5,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    3862 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.6
    Dallas0
    :
    18.60
    10467 TSF:MSG:READ,0-0-50,s=12,c=1,t=2,pt=0,l=1,sg=0:0
    Sensor value changed, source=Radio, Sensor=12, New status: 0
    10880 TSF:MSG:SEND,50-50-0-0,s=8,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.6
    10889 TSF:MSG:SEND,50-50-0-0,s=7,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:41.1
    H1: 41.10
    1
    15897 TSF:MSG:SEND,50-50-0-0,s=5,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    17434 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.6
    Dallas0
    :
    18.60
    1
    29453 TSF:MSG:SEND,50-50-0-0,s=5,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    30990 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.6
    Dallas0
    :
    18.60
    38007 TSF:MSG:SEND,50-50-0-0,s=8,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.7
    38017 TSF:MSG:SEND,50-50-0-0,s=7,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:41.0
    H1: 41.00
    43024 TSF:MSG:SEND,50-50-0-0,s=12,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    Sensor value changed, source=Physical switch, Sensor=12, New status: 1
    1
    43033 TSF:MSG:SEND,50-50-0-0,s=5,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    43795 TSF:MSG:READ,0-0-50,s=12,c=1,t=2,pt=1,l=1,sg=0:1
    43800 TSF:MSG:ACK
    This is an ack from gateway
    44573 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.7
    Dallas0
    :
    18.70
    1
    56590 TSF:MSG:SEND,50-50-0-0,s=5,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    58127 TSF:MSG:SEND,50-50-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:18.7
    Dallas0
    :
    18.70


  • Hardware Contributor

    @Ticupolu
    Are thes buttons switches or push buttons?

    You call setRelayState only from the recieve functions. (Do I miss something?) This means, that relay changes must be initiated from outside and don't occur only from a button press.



  • limit switch



  • @FotoFieber from openhab2 controller on rpi3 it is for chicken coop door and use myopenhab app to open and close remotly



  • 43024 TSF:MSG:SEND,50-50-0-0,s=12,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    Sensor value changed, source=Physical switch, Sensor=12, New status: 1

    is there anyway i can make it read and send instead of only send


  • Hero Member

    @Ticupolu The problem with your relay not switching from the local switch may be in your void recieve code.

    When you switch the local switch it sends a message to the controller and asks for an ack back. It is this returning ack that is used to switch the relay.

    At the top of your void receive you have code that i think is trapping the ack and stopping it from going any further. Basically your code says if this is an ack print some text else if it's not do the other things. So your ack is just doing a serial print.

    I am not a big fan of multiple else if statements if they can be avoided and you really don't need to know if the incoming message is an ack or not anyway.

    I have not tested it but you could try something like

    void receive(const MyMessage &message) {
      if (message.type == V_STATUS) {  
        switch (message.sensor) {
            case CHILD_ID:                                                    
              state = message.getBool();    
              setRelayState(RELAY_PIN, state); // Change relay state
              saveState(CHILD_ID, state);      // Store state in eeprom
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID, state);
             break;
            case CHILD_ID_2:                                                     
              state2 = message.getBool();
              setRelayState(RELAY_PIN_2, state2);
              saveState(CHILD_ID_2, state2);
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_2, state2);
             break;
            case CHILD_ID_3:                                                     
              state3 = message.getBool();
              setRelayState(RELAY_PIN_3, state3);
              saveState(CHILD_ID_3, state3);
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_3, state3);
    	     break;
            case CHILD_ID_4:                                                    
              state4 = message.getBool();
              setRelayState(RELAY_PIN_4, state4);
              saveState(CHILD_ID_4, state4);
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_4, state4);
             break;
            case CHILD_ID_5:                                           
              state5 = message.getBool();
              setRelayState(RELAY_PIN_5, state5);
              saveState(CHILD_ID_5, state5);
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_5, state5); 
             break;
    	    case CHILD_ID_6:                                                    
              state6 = message.getBool();
              setRelayState(RELAY_PIN_6, state6);
              saveState(CHILD_ID_6, state6);
           // Write some debug info
              printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_6, state6);  
             break;
    	   	  
        }
      }   
    }
    

    Your project seems to have a lot going on so as @FotoFieber has said some sort of wiring diagram might also be useful.



  • @Boots33 thank you for your explanation and time on this ...i have tried your sketch and it works the relay are now changing state....the only thing that i forgot to mention is that my door open or closes right back after it leaves the limit switch. The relay needs to change state only when the limit switch gets hit not when it leaves lol i dont know if you understand, english is not my first language but i made some sort of wiring diagram like you mention i'm also opening a my project subject for Mysensors Smart chicken coop....new ideas are welcome

    0_1488324476029_Poulailler_bb.jpg

    [0_1488324612760_Poulailler.fzz](Envoi en cours 100%)


  • Hero Member

    @Ticupolu If you only want the relays to toggle when the limit switch is activated then you could add back the original code from the relay example sketch and see if that has the desired effect.

    if (value != oldValue && value == 0) {
        send(msg.set(state ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID, value);
      }
      oldValue = value;
    
      if (value2 != oldValue2 && value2 == 0) {
        send(msg2.set(state2 ? false : true), true); // Send new state and request ack back
        // Write some debug info
        printStateChangedDebug(CHANGE_STATE_SOURCE_SWITCH, CHILD_ID_2, value2);
      }
      oldValue2 = value2;
    


  • @Boots33 Yess! we got it this time the door relays and limit switch are acting just like i wanted...i will be testing the other 4 relays in the after noon... i wish i could be better at programming without your help i would be stuck thank you!


Log in to reply
 

Suggested Topics

63
Online

11.4k
Users

11.1k
Topics

112.7k
Posts