Arduino Mega2560 and multifunction gateway issues/questions



  • (I hope I got the right subforum, as this is a troubleshooting issue)
    So, I have built a Arduino Mega with a Ethernet Shield coupled to a 16 channel relay board as a gateway/node with MyController as the main control point. I'm using the Mega in this configuration for future expansion(eventually going to run house heat circulator and zone valves thru this gateway as well).
    Currently, Im going to be utilizing it for home generator system control(choke control(eventually automated based on temp, but not there yet),ignition/start, low oil alarm, Gen AC OK, shelter door open, local generator inhibit), A/C power fail indication, and boiler summer/winter control, but I'm having a slight issue(s) in my coding i think in the sketch.
    So, I got the relays to autopopulate in MyC, but I only get one input to autopopulate(pin 39).
    When I trigger a input pin as a state change, there is an excessive delay before MyC sees it, but now adding in other inputs I no longer receive state changes in these sensors. I also dont see, like the relays output on command, a serial line for debug purposes, so I can even see if the trigger is even being seen, but the relays still trigger.
    Im going to be using contact closures for 80-90% of all my inputs.
    So, what did I goof, or miss?

    The code is below. Thanks in advance everyone!

    #define MY_DEBUG
    #define MY_GATEWAY_W5100
    // Enable UDP communication
    //#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS below
    #define MY_IP_ADDRESS 192,168,1,8
    //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
    //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
    // Renewal period DHCP
    //#define MY_IP_RENEWAL_INTERVAL 60000
    #define MY_PORT 5003
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    // #define MY_CONTROLLER_IP_ADDRESS 192,168,2,5
    //#define MY_CONTROLLER_URL_ADDRESS "my.controller.org"
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    #if defined(MY_USE_UDP)
    #include <EthernetUdp.h>
    #endif
    #include <Ethernet.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN 22
    #define NUMBER_OF_RELAYS 16 
    #define RELAY_ON 0 
    #define RELAY_OFF 1 
    /*#define LOCK_1  6 
    #define NOF_LOCKS 1   
    #define LOCK_LOCK 1   
    #define LOCK_UNLOCK 0 */
    
    #define CHILD_ID 20
    #define BUTTON_PIN  38  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 21
    #define BUTTON_PIN  39  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 22
    #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 23
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 24
    #define BUTTON_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 25
    #define BUTTON_PIN  5  // Arduino Digital I/O pin for button/reed switch
    /*#define CHILD_ID 8
    #define BUTTON_PIN  8  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 9
    #define BUTTON_PIN  9  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 10
    #define BUTTON_PIN  10  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID 11
    #define BUTTON_PIN  11  // Arduino Digital I/O pin for button/reed switch
    */
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    void before()
    {
      for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    void setup()  
    {
      pinMode(BUTTON_PIN,INPUT_PULLUP);
    /*  digitalWrite(BUTTON_PIN,HIGH);*/
      
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(3);
    
      /*for (int lock=1, pin=LOCK_1; lock<=NOF_LOCKS; lock++, pin++) {
        pinMode(pin, OUTPUT);
        digitalWrite(pin, loadState(lock)?LOCK_LOCK:LOCK_UNLOCK);*/
      }
    
    
    void presentation()
    
    {
      // 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.
      sendSketchInfo("Generator / Utility Room", "2.0");
      present(CHILD_ID, S_LIGHT); 
    
      for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_BINARY);
     
      // Fetch lock status
     /* for (int lock=1, pin=LOCK_1; lock<=NOF_LOCKS; lock++, pin++) {
        present(lock, S_LOCK, "SecureActuator", false);
      }*/
      }
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.getType()==V_STATUS) {
        // Change relay state
        digitalWrite(message.getSensor()-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        saveState(message.getSensor(), message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.getSensor());
        Serial.print(", New status: ");
        Serial.println(message.getBool());
        
    /*    if (message.getType()==V_LOCK_STATUS /*&& message.getSensor()<=NOF_LOCKS && !message.isEcho()) {
        // Change relay state
        digitalWrite(message.getSensor()-1+LOCK_1, message.getBool()?LOCK_LOCK:LOCK_UNLOCK);
        // Store state in eeprom
        saveState(message.getSensor(), message.getBool());
        // Write some debug info
        Serial.print("Incoming change for lock:");
        Serial.print(message.getSensor());
        Serial.print(", New status: ");
        Serial.println(message.getBool()); 
        }*/
    }
    }
    void loop()
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
    
      if (value != oldValue) {
         send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
     }
    }
    


  • @fdlou147 According to my personal experience, I'd recommend to split up Gateway and Node funktionality and use two seperate mcu's.

    Wrt. to the code itself: you only set up a debouncer on one of the pins (finally: Pin 5), as you always redefine the same "flags" over and over again. Perhaps have a look at this imo excellent multi relay/button sketch by korttoma here. This one should be not to hard to adopt to your needs (still, I'd recommend two mcu's).


Log in to reply
 

Suggested Topics

48
Online

11.4k
Users

11.1k
Topics

112.6k
Posts