Need help making pin D1 available for 8 relay module



  • Is there a way to utilize pin D1 for my 8 relay module with this or a modified sketch? I understand from research that the serial commands are getting in the way but I am not sure how to work around this and need pins 8-13 for my radio. (Powering via vin)

    // Override Setting for Manual Node ID to 2
    #define MY_NODE_ID 51
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define RELAY_1  1          // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 8 // Total number of attached relays: 4
    
    // Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
    #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
    
    bool initialValueSent = false;
    
    //Init MyMessage for Each Child ID
    MyMessage msg1(1, V_LIGHT);
    MyMessage msg2(2, V_LIGHT);
    MyMessage msg3(3, V_LIGHT);
    MyMessage msg4(4, V_LIGHT);
    MyMessage msg5(5, V_LIGHT);
    MyMessage msg6(6, V_LIGHT);
    MyMessage msg7(7, V_LIGHT);
    MyMessage msg8(8, V_LIGHT);
    
    void before() { 
      for (int sensor=1, pin=RELAY_1; 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() {
      
    }
    
    void presentation()  
    {   
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Multiple Relay", "1c.0");
    
      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)
        present(sensor, S_LIGHT);
      }
    }
    
    
    void loop() 
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg3.set(loadState(3)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg4.set(loadState(4)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
         send(msg5.set(loadState(5)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg6.set(loadState(6)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg7.set(loadState(7)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg8.set(loadState(8)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        Serial.println("Sending initial value: Completed");
        wait(5000);
      }
    }
    
    void receive(const MyMessage &message) {
      Serial.println("=============== Receive Start =======================");
      if (message.isAck()) {
         Serial.println(">>>>> ACK <<<<<");
         Serial.println("This is an ack from gateway");
         Serial.println("<<<<<< ACK >>>>>>");
      }
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
        Serial.println(">>>>> V_LIGHT <<<<<");
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
         // Update relay state to HA
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         switch (message.sensor) {
            case 1:
              Serial.print("Incoming change for sensor 1");
              send(msg1.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 2:
              Serial.print("Incoming change for sensor 2");
              send(msg2.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 3:
              Serial.print("Incoming change for sensor 3");
              send(msg3.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 4:
              Serial.print("Incoming change for sensor 4");
              send(msg4.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 5:
              Serial.print("Incoming change for sensor 5");
              send(msg5.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 6:
              Serial.print("Incoming change for sensor 6");
              send(msg6.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 7:
              Serial.print("Incoming change for sensor 7");
              send(msg7.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 8:
              Serial.print("Incoming change for sensor 8");
              send(msg8.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;                               
            default: 
              Serial.println("Default Case: Receiving Other Sensor Child ID");
            break;
         }
         // Store state in Arduino eeprom
         saveState(message.sensor, message.getBool());
         Serial.print("Saved State for sensor: ");
         Serial.print( message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
         Serial.println("<<<<<< V_LIGHT >>>>>>");
       } 
       Serial.println("=============== Receive END =======================");
    }
    

  • Mod

    @mrhutchinsonmn which Arduino are you using?



  • Ardunio Nano


  • Mod



  • Will this mess with data sent to my home assistant controller, or only disable serial monitor and usb?



  • @mfalkvidd Got it! Had to put on top of sketch to get it to work.. Thank you!


Log in to reply
 

Suggested Topics

  • 4
  • 274
  • 2
  • 9
  • 1
  • 9

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts