How do i sent "virtual switches" from node to Controller?



  • I need some help with a project that i'm building. I'm calling it the Shed Controller. This is a Mysensors 2.0 node that controls all the electronics around my shed. I features relays, PIR, i plan to add temperature sensor, and it has a keypad.

    The keypad will not be used to enter numbers. I will place a sticker over it converting the text on the switches to functions as "turn party lights on", "switch heater on", and "turn on garden lights for 5 min". These buttons will be coupled to certain actions by the controller. I'm using Domoticz on a Rpi.

    I want to use a keypad (3 row, 4 column) because i have very limited I/O pins available. Also its a very clean and cheap way to place buttons on the housing, as it just sticks on there.

    I'm a bit of a noob when it comes to Arduino and Mysensors. Its not my first build, but i had a lot of trouble realizing all the relay code. That works fine now, but as you may understand i want other functionality than the relay + button example.

    I'm using a modified version of a standard keypad library example. When a button is pressed its decoded and passed tough a "switch/case" function where i can couple a action to each key. How do i make this key available in the controller?

    I would guess i have to create a boolean that can be changed by the pressing of the key. If i understand right, the status of any device is always hold on the device itself. The information in the controller is just a copy of that status. The boolean can be changed by either the keypad button(internally in the arduino code), or the controller (a script or a manual button press in the GUI). Am i right so far?

    • Now i need to know how i can present the boolean to the controler as a "switch"' so its added to the devices.
    • I also need to know how to sent changes to changes of the boolean to the controller (pressing the button)
    • I'm guessing that receiving the messages from the controller works the same as for the relays? Or is there a easier way?

    I would really need some simplified example code, as my understanding of the mysensors code is very basic! (I have read most of all the api documentation, but u still dont understand how to write the code)

    Your help would be very appreciated!



  • Here is most of my code;

    Relay code (fully working):

    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Schuur controller"
    #define ProjectVersion "0,1"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    #define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #include <Keypad.h>
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1 
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3 
    //#define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    // ##### DEFINE CHILD ID'S #####
    #define CHILD_ID1 1    // ID for child 1 (I/O)
    #define CHILD_ID2 2    // ID for child 2 (I/O)
    //#define CHILD_ID3 3  // ID for child 3 (I/O)
    //#define CHILD_ID4 4  // ID for child 4 (I/O)
    #define CHILD_ID5 5    // ID for child 5 (I/O)
    #define CHILD_ID6 6    // ID for child 6 (I/O)
    #define CHILD_ID7 7    // ID for child 7 (I/O)
    #define CHILD_ID10 10  // ID for child 10 (virtual switch)
    #define CHILD_ID11 11  // ID for child 11 (virtual switch)
    #define CHILD_ID12 12  // ID for child 12 (virtual switch)
    #define CHILD_ID13 13  // ID for child 13 (virtual switch)
    #define CHILD_ID14 14  // ID for child 14 (virtual switch)
    
    // ##### RELAY SETTING #####
    #define RELAY_ON 0          // Invert for some relay modules (currently inverted)
    #define RELAY_OFF 1         // Invert for some relay modules (currently inverted)
    
    // ##### OTHER VARIABLES #####
    bool state; // not sure what this does
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    
    void setup()  
    {  
      // ##### I/O SETUP FOR PHYSICAL I/O ON ARDUINO #####
      // Setup I/O 2
      pinMode(IoPin2, OUTPUT);                        // use I/O 2 as output for relay module
      digitalWrite(IoPin2, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID2);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 4                                     Not in use  
      // pinMode(IoPin4, OUTPUT);                        // use I/O 4 as output for relay module
      // digitalWrite(IoPin4, RELAY_OFF);                // and set switch relay output off
      // state = loadState(CHILD_ID4);                   // Load last known state (using eeprom storage) 
      // digitalWrite(IoPin4, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 5
      pinMode(IoPin5, OUTPUT);                        // use I/O 5 as output for relay module
      digitalWrite(IoPin5, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID5);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 6
      pinMode(IoPin6, OUTPUT);                        // use I/O 6 as output for relay module
      digitalWrite(IoPin6, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID6);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 7
      pinMode(IoPin7, OUTPUT);                        // use I/O 7 as output for relay module
      digitalWrite(IoPin7, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID7);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF); // write last know state
      // 
    }
    
    // ###### PRESENT ALL ATTACHED SENSORS TO CONTROLLER ######
    void presentation(){
      sendSketchInfo(ProjectName, ProjectVersion); // Send the sketch version information to the gateway and Controller
    
      // ----- Actual I/O child IDs -----
      //present(CHILD_ID1, XXXXXXX); //Register child ID 1 as NONE
      present(CHILD_ID2, S_LIGHT);   //Register child ID 2 as S_LIGHT
      //present(CHILD_ID3, XXXXXXX); //Register child ID 3 as NONE
      //present(CHILD_ID4, XXXXXXX); //Register child ID 4 as NONE
      present(CHILD_ID5, S_LIGHT);   //Register child ID 5 as S_LIGHT
      present(CHILD_ID6, S_LIGHT);   //Register child ID 6 as S_LIGHT
      present(CHILD_ID7, S_LIGHT);   //Register child ID 7 as S_LIGHT
    
      // ----- virtual switch child ID (keypad)
      present(CHILD_ID10, S_LIGHT);  //Register child ID 10 as S_LIGHT
      present(CHILD_ID11, S_LIGHT);  //Register child ID 11 as S_LIGHT
      present(CHILD_ID12, S_LIGHT);  //Register child ID 12 as S_LIGHT
      present(CHILD_ID13, S_LIGHT);  //Register child ID 13 as S_LIGHT
      present(CHILD_ID14, S_LIGHT);  //Register child ID 14 as S_LIGHT
    } 
    
    void loop() 
    {
    }
    
    // ##### CODE FOR RECEIVING MYSENSORS MESSAGES FROM CONTROLLER #####
    void receive(const MyMessage &message) {                         //start mysensor receiving code
      if (message.isAck()) {                                         //Check for gateway acknowledgment
         Serial.println("This is an ack from gateway");              //Print debug code (serial print) to confirm received ack
      }
      // ----- Action taken for child ID 1: Currently set as V_LIGHT to switch relay on IoPin1 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID2) {  //if message is V_LIGHT and the child ID is 2 
         state = message.getBool();                                  //guess this write the incomming boolean to state?
         digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF);             //change the the arduino pin from "on" to "off" or other way around
         saveState(CHILD_ID2, state);                                //guess this saves the state if child ID 2 to EEPPROM
         Serial.print("Incoming change for sensor:");                //debug info text
         Serial.print(message.sensor);                               //write received child ID
         Serial.print(", New status: ");                             //debug info text
         Serial.println(message.getBool());                          //write received boolean
       } 
      // ----- Action taken for child ID 5: Currently set as V_LIGHT to switch relay on IoPin5 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID5) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID5, state);
       } 
      // ----- Action taken for child ID 6: Currently set as V_LIGHT to switch relay on IoPin6 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID6) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID6, state);
       } 
      // ----- Action taken for child ID 7: Currently set as V_LIGHT to switch relay on IoPin7 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID7) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID7, state);
       } 
       
    }
    

    **Keypad code (concept works): **

    
    // -------- USED LIBARIES -----------
    #include <Keypad.h>
    
    // ##### KEYPAD SETUP #####
    const byte ROWS = 4; // Four rows
    const byte COLS = 3; // Three columns
    // Define the Keymap
    char keys[ROWS][COLS] = {
      {'1','2','3'},
      {'4','5','6'},
      {'7','8','9'},
      {'A','B','C'}
    };
    // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
    byte rowPins[ROWS] = { A0, A1, A2, A3 };
    // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
    byte colPins[COLS] = { A4, 1, 0 }; 
    // Create the Keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1 
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3 
    #define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    
    void setup()
    
    
    { // ##### PIN SETUP (FOR TESTING ONLY #####
    
      pinMode(IoPin4, OUTPUT);   //use I/O 4 as output for relay module
      digitalWrite(IoPin4, HIGH);//   and set is HIGH so relay stays off
    
      
      // ###### VARIOUS SETTINGS ######
      Serial.begin(115200); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SERIAL PRINT MUST BE DEACTIVATED TO MAKE THE WHOLE KEYPAD WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    
    } 
    
    void loop()
    {
      char key = kpd.getKey();     // Get key from keypad (if pressed)
      if(key)  {                   // compare key (defined in keypad setup)
        switch (key) {             // with multiple multiple options below 
          
          case '1':                // If the pressed key compares with "1"
            VirtalSwitch1 = true;  // Change te state of boolean VirtalSwitch1 to true
            Serial.println("1");   // Debug code. Print "1" to show that button 1 was pressed
            break;                 // End of code for button 1
          
          case '4':                // If the pressed key compares with "4"          
            VirtalSwitch1 = false; // Change te state of boolean VirtalSwitch1 to false
            Serial.println("2");   // Debug code. Print "4" to show that button 4 was pressed
            break;                 // End of code for button 4
    
            
          default:                 // If the pressed key does not match the cases above
            Serial.println(key);   // Print the key that was pressed 
            break;                 // End of function 
        }
      }
    
    // ##### WRITE BOOLEAN TO PIN (FOR TESTING PURPOSES ONLY) #####
      digitalWrite(IoPin4, VirtalSwitch1);
    
      
    }
    
    

    I need to join this code and get the boolean to communicate toe the controller as described in my first post.


  • Hero Member

    @SuperKris If I understand what you are trying to do is use the keypad instead of individual push buttons to change the relay state. Is that correct?

    from a quick look at your switch sketch you appear to be using switch 1 to turn on and switch 4 to turn off, You could also just use switch 1 to toggle the relay on and off, would that also be acceptable or do you prefer separate switches for on and off?

    just trying to get a clear idea of your thinking. I like the idea of using a keypad for the control.



  • @Boots33 said:

    @SuperKris If I understand what you are trying to do is use the keypad instead of individual push buttons to change the relay state. Is that correct?

    Yes and no... or mostly.... no. Instead of changing the relay state, i want the push button to be a separate switch which is technically not related to any relays or other outputs. Its supposed to behave like a walls witch that is available in the controller.

    In the end i may couple a push button to one of the relay outputs, but this will be done in the controller. For instance, the controller wil only allow to switch on the light with the pushbutton if its dark outside.

    In the keypad code i do use 2 keys to control the boolean. This has 2 reasons. 1) that part is just proof of concept. 2) for some of the buttons i want it to work like this. I'm aware that i can use 1 button to change the boolean and change it back. I will add this later for some buttons.

    I hope this clarifies my intentions. This makes it different from the relay+button example sketch. Otherwise i would probably find it easier. I'm currently hoping to learn how i can make these "virtual wall switches" that are controlled from the keypad.

    I just dont know how to sent the booleans to the controller. I'm also not sure how to receive them, though i'm thinking it must be similar to the actual relays.


  • Hero Member

    @SuperKris So the keypad will just control the state of a "virtual" switch on the controller ? and then you will have that virtual switch control other nodes to do the actual hardware switching?



  • @SuperKris : you should consider adding the "sending" routines for MySensors. Maybe dissecting the Binary Switch Example would be a better startingpoint than the RelaySketch.

    I tried to integrate these routines into your template. Unfortunately I can´t test this sketch due to the lack of the Keypad. At least the sketch compiles without error.

    
    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Schuur controller"
    #define ProjectVersion "0,2"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    #define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #include <Keypad.h>
    
    // ##### Child-IDs for "Virtual"Switches #####
    #define CHILD_ID_V1 1
    #define CHILD_ID_V2 2
    #define CHILD_ID_V3 3
    #define CHILD_ID_V4 4
    #define CHILD_ID_V5 5
    
    // ##### KEYPAD SETUP #####
    const byte ROWS = 4; // Four rows
    const byte COLS = 3; // Three columns
    // Define the Keymap
    char keys[ROWS][COLS] = {
      {'1', '2', '3'},
      {'4', '5', '6'},
      {'7', '8', '9'},
      {'A', 'B', 'C'}
    };
    // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
    byte rowPins[ROWS] = { A0, A1, A2, A3 };
    // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
    byte colPins[COLS] = { A4, 1, 0 };                                        //if you got any spare pins - change 0 and 1 accordingly  e.g. { A4, 8, 9 }
                                                                              // or see below
    // Create the Keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3    / Pin 6 seems to be unused
    #define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    
    
    // define Message"Containers" to communicate with Gateway
    MyMessage msgV1(CHILD_ID_V1,S_LIGHT);
    MyMessage msgV2(CHILD_ID_V2,S_LIGHT);
    MyMessage msgV3(CHILD_ID_V3,S_LIGHT);
    MyMessage msgV4(CHILD_ID_V4,S_LIGHT);
    MyMessage msgV5(CHILD_ID_V5,S_LIGHT);
    
    
    void setup()
    
    
    { // ##### PIN SETUP (FOR TESTING ONLY #####
    
      pinMode(IoPin4, OUTPUT);    // use I/O 4 as output for relay module
      digitalWrite(IoPin4, HIGH); // and set is HIGH so relay stays off
    
    
      // ###### VARIOUS SETTINGS ######
      Serial.begin(115200); // SERIAL PRINT MUST BE DEACTIVATED TO MAKE THE WHOLE KEYPAD WORK if Pin 0 and Pin1 are used for ColAdressing,
                            // maybe pin 8,9,10 would be preferable with respect to compact wiring (e.g. byte colPins[COLS] = { 8,9,10 }; )
                            // without pin 0 and one used, you can do some serial debugging, which is very helpful.
    }
    
    void loop()
    {
      char key = kpd.getKey();     // Get key from keypad (if pressed)
      if (key)  {                  // compare key (defined in keypad setup)
        switch (key) {             // with multiple multiple options below
    
          case '1':                // If the pressed key compares with "1"
            VirtalSwitch1 = true;  // Change te state of boolean VirtalSwitch1 to true
            send(msgV1.set(1));    // sending virtual switch state to Gateway
            Serial.println("1");   // Debug code. Print "1" to show that button 1 was pressed
            break;                 // End of code for button 1
    
          case '4':                // If the pressed key compares with "4"
            VirtalSwitch1 = false; // Change te state of boolean VirtalSwitch1 to false
            send(msgV1.set(0));    // sending virtual switch state to Gateway
            Serial.println("4");   // Debug code. Print "4" to show that button 4 was pressed
            break;                 // End of code for button 4
    
          case '2':                // If the pressed key compares with "2"
            VirtalSwitch1 = true;  // Change te state of boolean VirtalSwitch1 to true
            send(msgV2.set(1));    // sending virtual switch state to Gateway
            Serial.println("2");   // Debug code. Print "2" to show that button 2 was pressed
            break;                 // End of code for button 2
    
          case '5':                // If the pressed key compares with "5"
            VirtalSwitch1 = false; // Change te state of boolean VirtalSwitch1 to false
            send(msgV2.set(0));    // sending virtual switch state to Gateway
            Serial.println("5");   // Debug code. Print "4" to show that button 5 was pressed
            break;                 // End of code for button 5
    
          case '3':                // If the pressed key compares with "3"
            VirtalSwitch1 = true;  // Change te state of boolean VirtalSwitch1 to true
            send(msgV3.set(1));    // sending virtual switch state to Gateway
            Serial.println("3");   // Debug code. Print "2" to show that button 2 was pressed
            break;                 // End of code for button 2
    
          case '6':                // If the pressed key compares with "6"
            VirtalSwitch1 = false; // This doesn´t really make sense here. Key "3" triggers Garden light for 5 Minutes
            send(msgV3.set(0));    // Reset this with a switch doesn´t meet the purpose of this switch (3), it (6) may be used for something else.
            Serial.println("6");   // 
            break;                 // End of code for button 6
    
                                 // Other keys to be added here according to the same template
            
          default:                 // If the pressed key does not match the cases above
            Serial.println(key);   // Print the key that was pressed
            break;                 // End of function
        }
      }
    
      // ##### WRITE BOOLEAN TO PIN (FOR TESTING PURPOSES ONLY) #####
      digitalWrite(IoPin4, VirtalSwitch1);
    
    }
    
    void
    
     presentation() {
     
      present(CHILD_ID_V1, S_LIGHT, "Partylichtjes");  
      present(CHILD_ID_V2, S_LIGHT, "Verwarmingstoestel");
      present(CHILD_ID_V3, S_LIGHT, "Tuinverlichting gedurende 5 min");
      present(CHILD_ID_V4, S_LIGHT, "Bewegingsdetector");
      present(CHILD_ID_V5, S_LIGHT, "Muziek");
      
      
    }```

  • Hero Member

    Hi @SuperKris

    Have you checked if Domoticz support the scene controller type?

    Here is an example project by @petewill



  • @Boots33 said:

    @SuperKris So the keypad will just control the state of a "virtual" switch on the controller ? and then you will have that virtual switch control other nodes to do the actual hardware switching?

    That is exactly what i want!

    @tboha said:

    @SuperKris : you should consider adding the "sending" routines for MySensors. Maybe dissecting the Binary Switch Example would be a better startingpoint than the RelaySketch.

    I tried to integrate these routines into your template. Unfortunately I can´t test this sketch due to the lack of the Keypad. At least the sketch compiles without error.

    Thanks! this should really help! I will test this tonight but its already giving me some understanding of how the sending of the messages work. I already have a couple of questions from just looking at you code.

    1. I see you did not really use the manual created booleans ("VirtalSwitch1" for example). Do i need these manual created booleans for the function i want, of are these booleans automatically created by the mysensors library?

    2. The code you added to sent the "virtual" switches to the controller seems to be the following:

    • "MyMessage msgV1(CHILD_ID_V1,S_LIGHT)" presents the existence of the switch to the gateway
    • and "send(msgV1.set(1))" sends the status change to the gateway.
      Where is the status (boolean?) actually stored? Is this a boolean provided by the mysensors code?
    1. Again, if i understand right, the state of a (virtual) switch is always stored on the node. Not on the controller. Can you confirm?
      If so, how do i change the state of the (virtual) switch from the controller? For example. 1 keypad button will control the the outside lighting. Inside of the house i will place a 433mhz wallswitch that does the same thing. For this i need the 433mhz wallswitch to be able to change the state of the (virtual) button. The controller (Domoticz) will turn on the lights depending on the state of the virtual switch.
      In other words, it seems to me the vitual switches should be controlled by the controller, but i dont see any receiving code.

    2. I see you wrote some comments about not using pin 0 and 1. I would rather not used them too, but i do not have any pins left. I need:
      6 digital pins for the NRF24L01 radio
      5 digital pins for the keypad
      4 digital pins for relays
      1 digital pin for PIR
      1 digital pin for TEMP/HUM
      1 digital pin for future expansion
      This leaves me with no available pins. A5 and A6 are analog only so i have no use for these. Unfortunatly i really need 0 and 1. I can still use then for debugging, but when serial debug is activated i loose 2 collumns of the keypad. For debugging this is not a huge problem.



  • @korttoma said:

    Hi @SuperKris

    Have you checked if Domoticz support the scene controller type?

    Here is an example project by @petewill

    Hi Korttoma,

    I have no idea if, and how, the scene controller sketch is supported in Domotics. Looking at the sketch the function is a bit different from what i'm looking to do. The sketch seems to sent a number to the controller instead of the the on or off status of a "virtual" switch.

    I guess i could write code in Domotics that can convert this input to certain events, but i'm trying to keep it simple at the controllers side. Otherwise i would have to ceate virtual switches in domotics, and lots of scripts.



  • @SuperKris said:

    The keypad will not be used to enter numbers. I will place a sticker over it converting the text on the switches to functions as "turn party lights on", "switch heater on", and "turn on garden lights for 5 min". These buttons will be coupled to certain actions by the controller. I'm using Domoticz on a Rpi.

    Nice idea. When you do it could you provide a photo? Like to know how clean it looks.



  • @Qu3Uk said:

    @SuperKris said:

    The keypad will not be used to enter numbers. I will place a sticker over it converting the text on the switches to functions as "turn party lights on", "switch heater on", and "turn on garden lights for 5 min". These buttons will be coupled to certain actions by the controller. I'm using Domoticz on a Rpi.

    Nice idea. When you do it could you provide a photo? Like to know how clean it looks.

    Once the project is finished i'll make a new topic with the pictures, electrical connections, and the code so anyone can recreate it.

    The trick with the foil keyboards is one i did not do for some time now. I picked it up at a company i worked for a while. We took register scales for store. They have big foil keyboards with a lot PLU's (products) printed on each button. We would redraw the keyboard, put net tekst on the buttons, and print it on paper of plastic sheets and cut it to size. Next we would laminate it with double sticky plastic sheets with sticky back in a hot laminating machine. Next simply paste the sheets over the original keyboard, and it would look great. Pretty durable too. The effect might be less on a smaller keypad, bu i think it will probably look pretty decent.



  • @SuperKris

    I see you did not really use the manual created booleans ("VirtalSwitch1" for example). Do i need these manual created booleans for the function i want,

    1.) Yes, you need these variables just the way you created them. At the moment they are used for logging (as you used them until now), so they reflect the current state as changed by the switches. More features have to be implemented.

    of are these booleans automatically created by the mysensors library?

    No, these booleans are not implemented by MySensors, at least not in the way you plan to use them.

    "MyMessage msgV1(CHILD_ID_V1,S_LIGHT)" presents the existence of the switch to the gateway

    Exact.

    and "send(msgV1.set(1))" sends the status change to the gateway.
    Where is the status (boolean?) actually stored? Is this a boolean provided by the mysensors code?

    at last yes, but ... this statement performs two jobs:

    • msgV1.set(1) sets the payload of the Message"Container" to "1"
    • send(msgV1) performs the actual sending.
    • send(msgV1.set(VirtalSwitch1) would probably do the trick - but I was too lazy typing. The current status is stored in the variables you created.

    Again, if i understand right, the state of a (virtual) switch is always stored on the node. Not on the controller. Can you confirm?

    Yes.

    If so, how do i change the state of the (virtual) switch from the controller? For example. 1 keypad button will control the the outside lighting. Inside of the house i will place a 433mhz wallswitch that does the same thing. For this i need the 433mhz wallswitch to be able to change the state of the (virtual) button. The controller (Domoticz) will turn on the lights depending on the state of the virtual switch.
    In other words, it seems to me the vitual switches should be controlled by the controller, but i dont see any receiving code.

    Yes, there is much work left.

    I am sorry, these answers are not very helpful at the moment. I´m in a hurry and I will try to explain later in the evening.

    Until then I would propose you try to get the first fragment of the above sketch running. It will give you an impression of some possibilities - and also of some restrictions.

    Until later.



  • Thank you very much thoba! With that info is was able to build a fully working proof of concept!

    I attached a test relay to IoPin4 which is linked to VirtalSwitch1. I can see VirtalSwitch1 in the controller, and add it as switch. When i use the keypad it changes the boolean, and the status of the switch is updated in the controller.

    I can not change the same boolean trough the controller, as there is no receiving code. This however was easy to write because i figured it would not be much different than the relay pins. I added some code, and it seems to work fine. I can change the boolean in the node with the controller now.

    There is still 1 issue with this code. I can not store the state of the boolean to the EEPROM. If i uncomment the last line, the sketch will not compile and i get the following error:

    Arduino: 1.6.13 (Windows Store 1.6.13.0) (Windows 10), Board:"Arduino Nano, ATmega328"
    
    C:\Users\krist\Desktop\arduino testing\IO_test_0.2\IO_test_0.2.ino: In function 'void receive(const MyMessage&)':
    
    IO_test_0.2:207: error: too few arguments to function 'void saveState(uint8_t, uint8_t)'
    
          saveState(CHILD_ID_V1), state);                               //guess this saves the state if child ID 2 to EEPPROM
    
                               ^
    
    In file included from C:\Users\krist\Documents\Arduino\libraries\MySensors/MySensors.h:293:0,
    
                     from C:\Users\krist\Desktop\arduino testing\IO_test_0.2\IO_test_0.2.ino:13:
    
    C:\Users\krist\Documents\Arduino\libraries\MySensors/core/MySensorsCore.cpp:408:6: note: declared here
    
     void saveState(uint8_t pos, uint8_t value) {
    
          ^
    
    exit status 1
    too few arguments to function 'void saveState(uint8_t, uint8_t)'
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    

    The working code:

    
    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Schuur controller"
    #define ProjectVersion "0,2"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    #define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #include <Keypad.h>
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1 
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3 
    #define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    // ##### DEFINE CHILD ID'S #####
    #define CHILD_ID1 1    // ID for child 1 (I/O)
    #define CHILD_ID2 2    // ID for child 2 (I/O)
    //#define CHILD_ID3 3  // ID for child 3 (I/O)
    //#define CHILD_ID4 4  // ID for child 4 (I/O)
    #define CHILD_ID5 5    // ID for child 5 (I/O)
    #define CHILD_ID6 6    // ID for child 6 (I/O)
    #define CHILD_ID7 7    // ID for child 7 (I/O)
    #define CHILD_ID_V1 10  // ID for child virtual switch 1
    #define CHILD_ID_V2 11  // ID for child virtual switch 2
    #define CHILD_ID_V3 12  // ID for child virtual switch 3
    #define CHILD_ID_V4 13  // ID for child virtual switch 4
    #define CHILD_ID_V5 14  // ID for child virtual switch 5
    
    // ##### RELAY SETTING #####
    #define RELAY_ON 0          // Invert for some relay modules (currently inverted)
    #define RELAY_OFF 1         // Invert for some relay modules (currently inverted)
    
    // ##### OTHER VARIABLES #####
    bool state; // not sure what this does
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    
    // ##### DEFINE MYSENSORS MESSAGE CONTAINERS TO COMMUNICATE WITH GATEWAY #####
    MyMessage msgV1(CHILD_ID_V1,S_LIGHT); // msgV1 sends child ID CHILD_ID_V1 as S_LIGHT
    MyMessage msgV2(CHILD_ID_V2,S_LIGHT); // msgV2 sends child ID CHILD_ID_V2 as S_LIGHT
    MyMessage msgV3(CHILD_ID_V3,S_LIGHT); // msgV3 sends child ID CHILD_ID_V3 as S_LIGHT
    MyMessage msgV4(CHILD_ID_V4,S_LIGHT); // msgV4 sends child ID CHILD_ID_V4 as S_LIGHT
    MyMessage msgV5(CHILD_ID_V5,S_LIGHT); // msgV5 sends child ID CHILD_ID_V5 as S_LIGHT
    
    // ##### KEYPAD SETUP #####
    const byte ROWS = 4; // Four rows
    const byte COLS = 3; // Three columns
    // Define the Keymap
    char keys[ROWS][COLS] = {
      {'1', '2', '3'},
      {'4', '5', '6'},
      {'7', '8', '9'},
      {'A', 'B', 'C'}
    };
    // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
    byte rowPins[ROWS] = { A0, A1, A2, A3 };
    // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
    byte colPins[COLS] = { A4, 1, 0 };                                        //if you got any spare pins - change 0 and 1 accordingly  e.g. { A4, 8, 9 }
                                                                              // or see below
    // Create the Keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    
    void setup()  
    {  
      // ##### I/O SETUP FOR PHYSICAL I/O ON ARDUINO #####
      // Setup I/O 2
      pinMode(IoPin2, OUTPUT);                        // use I/O 2 as output for relay module
      digitalWrite(IoPin2, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID2);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 4                                     Not in use  
      // pinMode(IoPin4, OUTPUT);                        // use I/O 4 as output for relay module
      // digitalWrite(IoPin4, RELAY_OFF);                // and set switch relay output off
      // state = loadState(CHILD_ID4);                   // Load last known state (using eeprom storage) 
      // digitalWrite(IoPin4, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 5
      pinMode(IoPin5, OUTPUT);                        // use I/O 5 as output for relay module
      digitalWrite(IoPin5, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID5);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 6
      pinMode(IoPin6, OUTPUT);                        // use I/O 6 as output for relay module
      digitalWrite(IoPin6, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID6);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 7
      pinMode(IoPin7, OUTPUT);                        // use I/O 7 as output for relay module
      digitalWrite(IoPin7, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID7);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF); // write last know state
       
    
     // ----- PIN SETUP (FOR TESTING ONLY -----
      pinMode(IoPin4, OUTPUT);   //use I/O 4 as output for relay module
      digitalWrite(IoPin4, HIGH);//   and set is HIGH so relay stays off
      
    }
    
    // ###### PRESENT ALL ATTACHED SENSORS TO CONTROLLER ######
    void presentation(){
      sendSketchInfo(ProjectName, ProjectVersion); // Send the sketch version information to the gateway and Controller
    
      // ----- Actual I/O child IDs -----
      //present(CHILD_ID1, XXXXXXX); //Register child ID 1 as NONE
      present(CHILD_ID2, S_LIGHT);   //Register child ID 2 as S_LIGHT
      //present(CHILD_ID3, XXXXXXX); //Register child ID 3 as NONE
      //present(CHILD_ID4, XXXXXXX); //Register child ID 4 as NONE
      present(CHILD_ID5, S_LIGHT);   //Register child ID 5 as S_LIGHT
      present(CHILD_ID6, S_LIGHT);   //Register child ID 6 as S_LIGHT
      present(CHILD_ID7, S_LIGHT);   //Register child ID 7 as S_LIGHT
    
      // ----- virtual switch child ID (keypad)
      present(CHILD_ID_V1, S_LIGHT);  //Register child ID 10 as S_LIGHT
      present(CHILD_ID_V2, S_LIGHT);  //Register child ID 11 as S_LIGHT
      present(CHILD_ID_V3, S_LIGHT);  //Register child ID 12 as S_LIGHT
      present(CHILD_ID_V4, S_LIGHT);  //Register child ID 13 as S_LIGHT
      present(CHILD_ID_V5, S_LIGHT);  //Register child ID 14 as S_LIGHT
    } 
    
    void loop()
    { // ##### KEYPAD CODE TO SET A ACTION FOR A PRESSED KEY #####
      char key = kpd.getKey();     // Get key from keypad (if pressed)
      if (key)  {                  // compare key (defined in keypad setup)
        switch (key) {             // with multiple multiple options below
    
          case '1':                // If the pressed key compares with "1"
            VirtalSwitch1 = true;  // Change te state of boolean VirtalSwitch1 to true
            send(msgV1.set(1));    // sending virtual switch state to Gateway
            Serial.println("1");   // Debug code. Print "1" to show that button 1 was pressed
            break;                 // End of code for button 1
    
          case '4':                // If the pressed key compares with "4"
            VirtalSwitch1 = false; // Change te state of boolean VirtalSwitch1 to false
            send(msgV1.set(0));    // sending virtual switch state to Gateway
            Serial.println("4");   // Debug code. Print "4" to show that button 4 was pressed
            break;                 // End of code for button 4
    
                                   // Other keys to be added here according to the same template
            
          default:                 // If the pressed key does not match the cases above
            Serial.println(key);   // Print the key that was pressed
            break;                 // End of function
        }
      }  
      // ----- WRITE BOOLEAN TO PIN (FOR TESTING PURPOSES ONLY) -----
      digitalWrite(IoPin4, VirtalSwitch1);
    }
    
    // ##### CODE FOR RECEIVING MYSENSORS MESSAGES FROM CONTROLLER #####
    void receive(const MyMessage &message) {                         //start mysensor receiving code
      if (message.isAck()) {                                         //Check for gateway acknowledgment
         Serial.println("This is an ack from gateway");              //Print debug code (serial print) to confirm received ack
      }
      // ----- Action taken for child ID 1: Currently set as V_LIGHT to switch relay on IoPin1 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID2) {  //if message is V_LIGHT and the child ID is 2 
         state = message.getBool();                                  //guess this write the incomming boolean to state?
         digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF);             //change the the arduino pin from "on" to "off" or other way around
         saveState(CHILD_ID2, state);                                //guess this saves the state if child ID 2 to EEPPROM
         Serial.print("Incoming change for sensor:");                //debug info text
         Serial.print(message.sensor);                               //write received child ID
         Serial.print(", New status: ");                             //debug info text
         Serial.println(message.getBool());                          //write received boolean
       } 
      // ----- Action taken for child ID 5: Currently set as V_LIGHT to switch relay on IoPin5 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID5) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID5, state);
       } 
      // ----- Action taken for child ID 6: Currently set as V_LIGHT to switch relay on IoPin6 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID6) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID6, state);
       } 
      // ----- Action taken for child ID 7: Currently set as V_LIGHT to switch relay on IoPin7 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID7) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID7, state);
       } 
       
       // ----- Action taken for CHILD_ID_V1: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch1) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V1) {  //if message is V_LIGHT and the CHILD_ID_V1
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch1 = state;                                        //copy the received boolean to VirtalSwitch1 
         //saveState(CHILD_ID_V1), state);                               //guess this saves the state if child ID 2 to EEPPROM
       } 
       
    }```


  • @SuperKris
    From the first look: it could be a simple typo:

    saveState(CHILD_ID_V1), state);
    should be
    saveState(CHILD_ID_V1, state);
    it may be simply one parenthesis too much.



  • @tboha said:

    @SuperKris
    From the first look: it could be a simple typo:

    saveState(CHILD_ID_V1), state);
    should be
    saveState(CHILD_ID_V1, state);
    it may be simply one parenthesis too much.

    Wow.. I cant believe i missed that... damn.

    Ok, so everything seems to work now. I can just copy paste code and add some "virtual" buttons. Thats great! Thank you so much.

    Next i will need to add the PIR setup, but i think i have enough understanding to do this now. After that the DHT22 code. Should be a bit more trick, but i will get there.

    Just one more question about the code you written:

      present(CHILD_ID_V1, S_LIGHT, "Partylichtjes");  
    

    What does "partytlichtjes" do? I guess its supposed to send the name of the switch to the controller, but it wont compile if i add it in my sketch.



  • Yes, it is supposed to give a human readable name to your Sensor, which could be displayed by your controller.
    strange - within my "testenvironment" it compiled (5 minutes ago) without complaints.
    Would you please give the CompilerMessage?
    It is not a vital point but it´s anoying.

    some additional remarks:

    Switching to Arduino-MEGA would solve your lack of IO-Pins dramatically (and provides you with huge amounts of Memory and RAM (=Variables)).

    Another Option would be to split different functions to different Arduinos. E.g. one Arduino to serve the switches, one Arduino to control an Array of Relays.

    Delegate dumb tasks to even dumber Hardware

    Frequently read your included headerfiles (especially Mysensors.h) and read the APIs (https://www.mysensors.org/download/sensor_api_20), you will discover a bunch of additional functions.

    One function you will discover is direct "internode" communication without gateway and so without controller.

    This partially answers your question where to store your virtual items (or makes it more complicated with respect to maintenance). Deploying vital components to your peripherial Units gives great flexibility in choosing your controller. It also makes the integration "Third-Party" components easier. You mentioned your 433Mhz WallSwitch.



  • @tboha said:

    Yes, it is supposed to give a human readable name to your Sensor, which could be displayed by your controller.
    strange - within my "testenvironment" it compiled (5 minutes ago) without complaints.
    Would you please give the CompilerMessage?
    It is not a vital point but it´s anoying.

    I tried ir multiple times earlier tonight, but was unable to compile. (i dont know the error anymore). For some reason it works fine now. Very strange!

    Switching to a mega is no option at this point. I got all the hardware already soldered together on a PCB, and thats a lot of inputs and outputs to do. I also do not like the size of the mega. The ram i dont need. I'm currently at around 50% and besides the DTH sensor, i'm almost done.

    Also i like the challenge of creating what i want with minimal components. I'm pretty sure its going to work in the end!

    Same goes for multiple arduino's. I think i have a couple of I/O expander PCBs laying around, but rather not use them either as its complicates the sketch even further.

    I have read a lot about the API but i'm still very much struggling to understand anything of it. I will definitely continue to read!

    Below my current code. The keypad part is done. Next is the receiving part for the virtual switches.

    Thank you very much for your great help! I fear i will have new questions very soon!

    
    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Schuur controller"
    #define ProjectVersion "0,2"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    #define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #include <Keypad.h>
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1 
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3 
    #define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    // ##### DEFINE CHILD ID'S #####
    #define CHILD_ID1 1    // ID for child 1 (I/O)
    #define CHILD_ID2 2    // ID for child 2 (I/O) used for relay
    //#define CHILD_ID3 3  // ID for child 3 (I/O)
    //#define CHILD_ID4 4  // ID for child 4 (I/O)
    #define CHILD_ID5 5    // ID for child 5 (I/O) used for relay
    #define CHILD_ID6 6    // ID for child 6 (I/O) used for relay
    #define CHILD_ID7 7    // ID for child 7 (I/O) used for relay
    #define CHILD_ID_V1 10  // ID for child virtual switch 1. Used for Outside lighting button
    #define CHILD_ID_V2 11  // ID for child virtual switch 2. Used for Heater button
    #define CHILD_ID_V3 12  // ID for child virtual switch 3. Used for Anit Frost button
    #define CHILD_ID_V4 13  // ID for child virtual switch 4. Used for Party Lights button
    #define CHILD_ID_V5 14  // ID for child virtual switch 5. Used for Wifi AP button
    #define CHILD_ID_V6 15  // ID for child virtual switch 6. Used for Aux 1 button
    #define CHILD_ID_V7 16  // ID for child virtual switch 7. Used for Aux 2 button
    #define CHILD_ID_V8 17  // ID for child virtual switch 8. Used for 5 min button
    
    
    // ##### RELAY SETTING #####
    #define RELAY_ON 0          // Invert for some relay modules (currently inverted)
    #define RELAY_OFF 1         // Invert for some relay modules (currently inverted)
    
    // ##### OTHER VARIABLES #####
    bool state; // not sure what this does
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    bool VirtalSwitch6;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch7;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    
    // ##### DEFINE MYSENSORS MESSAGE CONTAINERS TO COMMUNICATE WITH GATEWAY #####
    MyMessage msgV1(CHILD_ID_V1,S_LIGHT); // msgV1 sends child ID CHILD_ID_V1 as S_LIGHT
    MyMessage msgV2(CHILD_ID_V2,S_LIGHT); // msgV2 sends child ID CHILD_ID_V2 as S_LIGHT
    MyMessage msgV3(CHILD_ID_V3,S_LIGHT); // msgV3 sends child ID CHILD_ID_V3 as S_LIGHT
    MyMessage msgV4(CHILD_ID_V4,S_LIGHT); // msgV4 sends child ID CHILD_ID_V4 as S_LIGHT
    MyMessage msgV5(CHILD_ID_V5,S_LIGHT); // msgV5 sends child ID CHILD_ID_V5 as S_LIGHT
    MyMessage msgV6(CHILD_ID_V6,S_LIGHT); // msgV6 sends child ID CHILD_ID_V6 as S_LIGHT
    MyMessage msgV7(CHILD_ID_V7,S_LIGHT); // msgV7 sends child ID CHILD_ID_V7 as S_LIGHT
    MyMessage msgV8(CHILD_ID_V8,S_LIGHT); // msgV8 sends child ID CHILD_ID_V8 as S_LIGHT
    
    // ##### KEYPAD SETUP #####
    const byte ROWS = 4; // Four rows
    const byte COLS = 3; // Three columns
    // Define the Keymap
    char keys[ROWS][COLS] = {
      {'1', '2', '3'},
      {'4', '5', '6'},
      {'7', '8', '9'},
      {'A', 'B', 'C'}
    };
    // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
    byte rowPins[ROWS] = { A0, A1, A2, A3 };
    // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
    byte colPins[COLS] = { A4, 1, 0 };                                        //if you got any spare pins - change 0 and 1 accordingly  e.g. { A4, 8, 9 }
                                                                              // or see below
    // Create the Keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    
    void setup()  
    {  
      // ##### I/O SETUP FOR PHYSICAL I/O ON ARDUINO #####
      // Setup I/O 2
      pinMode(IoPin2, OUTPUT);                        // use I/O 2 as output for relay module
      digitalWrite(IoPin2, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID2);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 4                                     Not in use  
      // pinMode(IoPin4, OUTPUT);                        // use I/O 4 as output for relay module
      // digitalWrite(IoPin4, RELAY_OFF);                // and set switch relay output off
      // state = loadState(CHILD_ID4);                   // Load last known state (using eeprom storage) 
      // digitalWrite(IoPin4, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 5
      pinMode(IoPin5, OUTPUT);                        // use I/O 5 as output for relay module
      digitalWrite(IoPin5, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID5);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 6
      pinMode(IoPin6, OUTPUT);                        // use I/O 6 as output for relay module
      digitalWrite(IoPin6, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID6);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 7
      pinMode(IoPin7, OUTPUT);                        // use I/O 7 as output for relay module
      digitalWrite(IoPin7, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID7);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF); // write last know state
       
    
     // ----- PIN SETUP (FOR TESTING ONLY -----
      pinMode(IoPin4, OUTPUT);   //use I/O 4 as output for relay module
      digitalWrite(IoPin4, HIGH);//   and set is HIGH so relay stays off
      
    }
    
    // ###### PRESENT ALL ATTACHED SENSORS TO CONTROLLER ######
    void presentation(){
      sendSketchInfo(ProjectName, ProjectVersion); // Send the sketch version information to the gateway and Controller
    
      // ----- Actual I/O child IDs -----
      //present(CHILD_ID1, XXXXXXX); //Register child ID 1 as NONE
      present(CHILD_ID2, S_LIGHT);   //Register child ID 2 as S_LIGHT
      //present(CHILD_ID3, XXXXXXX); //Register child ID 3 as NONE
      //present(CHILD_ID4, XXXXXXX); //Register child ID 4 as NONE
      present(CHILD_ID5, S_LIGHT);   //Register child ID 5 as S_LIGHT
      present(CHILD_ID6, S_LIGHT);   //Register child ID 6 as S_LIGHT
      present(CHILD_ID7, S_LIGHT);   //Register child ID 7 as S_LIGHT
    
      // ----- virtual switch child ID (keypad)
      present(CHILD_ID_V1, S_LIGHT, "Outside light switch");    //Register child ID 10 as S_LIGHT
      present(CHILD_ID_V2, S_LIGHT);  //Register child ID 11 as S_LIGHT
      present(CHILD_ID_V3, S_LIGHT);  //Register child ID 12 as S_LIGHT
      present(CHILD_ID_V4, S_LIGHT);  //Register child ID 13 as S_LIGHT
      present(CHILD_ID_V5, S_LIGHT);  //Register child ID 14 as S_LIGHT
      present(CHILD_ID_V6, S_LIGHT);  //Register child ID 15 as S_LIGHT
      present(CHILD_ID_V7, S_LIGHT);  //Register child ID 16 as S_LIGHT
      present(CHILD_ID_V8, S_LIGHT);  //Register child ID 17 as S_LIGHT
    } 
    
    void loop()
    { // ##### KEYPAD CODE TO SET A ACTION FOR A PRESSED KEY #####
      char key = kpd.getKey();       // Get key from keypad (if pressed)
      if (key)  {                    // compare key (defined in keypad setup)
        switch (key) {               // with multiple multiple options below
    
                                // On button for outside light
          case '1':                  // If the pressed key compares with "1"
            VirtalSwitch1 = true;    // Change te state of boolean VirtalSwitch1 to true (on)
            send(msgV1.set(1));      // Sent "on" command to gateway for CHILD_ID_V1 
            Serial.println("KEY 1"); // Debug code. Print "KEY 1" to show that button 1 was pressed
            break;                   // End of code for button 1
    
                                // Off button for outside light
          case '2':                  // If the pressed key compares with "2"
            VirtalSwitch1 = false;   // Change te state of boolean VirtalSwitch1 to false (off)
            send(msgV1.set(0));      // Sent "off" command to gateway for CHILD_ID_V1 
            Serial.println("KEY 2"); // Debug code. Print "KEY 2" to show that button 2 was pressed
            break;                   // End of code for button 2
    
                                // 5 minute button
          case '3':                  // If the pressed key compares with "3"
            send(msgV8.set(1));      // Sent "on" command to gateway for CHILD_ID_V8
            Serial.println("KEY 4"); // Debug code. Print "KEY 4" to show that button 4 was pressed
            break;                   // End of code for button 4
    
                                // On button for heating
          case '4':                  // If the pressed key compares with "4"
            VirtalSwitch2 = true;    // Change te state of boolean VirtalSwitch2 to true (on). This is the heater
            VirtalSwitch3 = false;   // Change te state of boolean VirtalSwitch3 to false (off). This is the anti frost funtion
            send(msgV2.set(1));      // Sent "on" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(0));      // Sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 4"); // Debug code. Print "KEY 4" to show that button 4 was pressed
            break;                   // End of code for button 4
    
                                // Off button for heating
          case '5':                  // If the pressed key compares with "5"
            VirtalSwitch2 = false;   // Change te state of boolean VirtalSwitch2 to false (off). This is the heater
            VirtalSwitch3 = false;   // Change te state of boolean VirtalSwitch3 to false (off). This is the anti frost funtion
            send(msgV2.set(0));      // Sent "off" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(0));      // Sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 5"); // Debug code. Print "KEY 5" to show that button 5 was pressed
            break;                   // End of code for button 5
    
                                // On button for anti frost
          case '6':                  // If the pressed key compares with "6"
            VirtalSwitch2 = false;   // Change te state of boolean VirtalSwitch2 to false (off). This is the heater
            VirtalSwitch3 = true;    // Change te state of boolean VirtalSwitch3 to true (on). This is the anti frost funtion
            send(msgV2.set(0));      // Sent "off" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(1));      // Sent "on" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 6"); // Debug code. Print "KEY 6" to show that button 6 was pressed
            break;                   // End of code for button 6
            
                                // On button for party lights
          case '7':                  // If the pressed key compares with "7"
            VirtalSwitch4 = true;    // Change te state of boolean VirtalSwitch1 to true (on)
            send(msgV4.set(1));      // Sent "on" command to gateway for CHILD_ID_V4 
            Serial.println("KEY 7"); // Debug code. Print "KEY 7" to show that button 7 was pressed
            break;                   // End of code for button 7
    
                                // Off button for party lights
          case '8':                  // If the pressed key compares with "8"
            VirtalSwitch4 = false;   // Change te state of boolean VirtalSwitch1 to false (off)
            send(msgV4.set(0));      // Sent "off" command to gateway for CHILD_ID_V4 
            Serial.println("KEY 8"); // Debug code. Print "KEY 8" to show that button 8 was pressed
            break;                   // End of code for button 8
    
                                    // Aux ON/OFF buton 1
          case '9':{                     // If the pressed key compares with "9"
            if (VirtalSwitch6 == false){ // check if the virtual switch (VirtalSwitch6) is OFF. If so, do the following
              VirtalSwitch6 = true;      // set the new status of the virtual swich to ON
              send(msgV6.set(1));        // Sent "on" command to gateway for CHILD_ID_V6 
            }
            else {                       // If the virtual switch was not OFF, it must have been ON, so do the following 
              VirtalSwitch6 = false;     // set the new status of the virtual swich to OFF
              send(msgV6.set(0));        // Sent "off" command to gateway for CHILD_ID_V6 
              }
            }
            Serial.println("KEY 9");    // Debug code. Print "KEY 9" to show that button 9 was pressed
            break;                      // End of code for button 9
    
                                 // On button for Wifi AP
          case 'A':                  // If the pressed key compares with "A"
            VirtalSwitch5 = true;    // Change te state of boolean VirtalSwitch5 to true (on)
            send(msgV5.set(1));      // Sent "on" command to gateway for CHILD_ID_V5 
            Serial.println("KEY A"); // Debug code. Print "KEY A" to show that button A was pressed
            break;                   // End of code for button A
    
                                 // Off button for Wifi AP
          case 'B':                  // If the pressed key compares with "B"
            VirtalSwitch5 = false;   // Change te state of boolean VirtalSwitch5 to false (off)
            send(msgV5.set(0));      // Sent "off" command to gateway for CHILD_ID_V5 
            Serial.println("KEY B"); // Debug code. Print "KEY B" to show that button B was pressed
            break;                   // End of code for button B    
    
                                  // Aux ON/OFF buton 2
          case 'C':{                     // If the pressed key compares with "C"
            if (VirtalSwitch7 == false){ // check if the virtual switch (VirtalSwitch7) is OFF. If so, do the following
              VirtalSwitch7 = true;      // set the new status of the virtual swich to ON
              send(msgV7.set(1));        // Sent "on" command to gateway for CHILD_ID_V7 
            }
            else {                       // If the virtual switch was not OFF, it must have been ON, so do the following 
              VirtalSwitch7 = false;     // set the new status of the virtual swich to OFF
              send(msgV7.set(0));        // Sent "off" command to gateway for CHILD_ID_V7 
              }
            }
            Serial.println("KEY C");    // Debug code. Print "KEY C" to show that button C was pressed
            break;                      // End of code for button C
            
          default:                  // If the pressed key does not match the cases above
            Serial.print(key);      // Print the key that was pressed
            Serial.println(" was pressed but not recognized by the keypadcode. Something is wrong!"); // print warning
            break;                  // End of function
        }
      }  
      // ----- WRITE BOOLEAN TO PIN (FOR TESTING PURPOSES ONLY) -----
      //digitalWrite(IoPin4, VirtalSwitch6);
    }
    
    // ##### CODE FOR RECEIVING MYSENSORS MESSAGES FROM CONTROLLER #####
    void receive(const MyMessage &message) {                         //start mysensor receiving code
      if (message.isAck()) {                                         //Check for gateway acknowledgment
         Serial.println("This is an ack from gateway");              //Print debug code (serial print) to confirm received ack
      }
      // ----- Action taken for child ID 1: Currently set as V_LIGHT to switch relay on IoPin1 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID2) {  //if message is V_LIGHT and the child ID is 2 
         state = message.getBool();                                  //guess this write the incomming boolean to state?
         digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF);             //change the the arduino pin from "on" to "off" or other way around
         saveState(CHILD_ID2, state);                                //guess this saves the state if child ID 2 to EEPPROM
         Serial.print("Incoming change for sensor:");                //debug info text
         Serial.print(message.sensor);                               //write received child ID
         Serial.print(", New status: ");                             //debug info text
         Serial.println(message.getBool());                          //write received boolean
       } 
      // ----- Action taken for child ID 5: Currently set as V_LIGHT to switch relay on IoPin5 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID5) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID5, state);
       } 
      // ----- Action taken for child ID 6: Currently set as V_LIGHT to switch relay on IoPin6 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID6) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID6, state);
       } 
      // ----- Action taken for child ID 7: Currently set as V_LIGHT to switch relay on IoPin7 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID7) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID7, state);
       } 
       
       // ----- Action taken for CHILD_ID_V1: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch1) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V1) {  //if message is V_LIGHT and the CHILD_ID_V1
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch1 = state;                                        //copy the received boolean to VirtalSwitch1 
         saveState(CHILD_ID_V1, state);                               //guess this saves the state if CHILD_ID_V1 to EEPPROM
       } 
       
    }```


  • @SuperKris : You are welcome!



  • So right now i think i have all the code for the keypad en de relays complete. It does exactly what i want. I just have one problem...

    The damn thing keeps giving serial prints. In the sketch below not from the mysensors library, but from the keypad code. The problem with this is that i really need all the digital ports, including 1 and 0. At this point there is no real way around that.

    In the keypad code i posted in my 2nd post, this issue only happened when there was a serial print active. Its currently not in the sketch at all, or is working from the library?

    Does anyone has any clue how i can give the digital pin 0 and 1 back to the keypad again?

    My current code

    
    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Shed controller"
    #define ProjectVersion "0,3"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    //#define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    // ##### DEFINE I/O PINS #####
    //#define IoPin1 4  // Arduino Digital I/O pin number for I/O 1 
    #define IoPin2 3  // Arduino Digital I/O pin number for I/O 2 
    //#define IoPin3 6  // Arduino Digital I/O pin number for I/O 3 
    #define IoPin4 5  // Arduino Digital I/O pin number for I/O 4 
    #define IoPin5 8  // Arduino Digital I/O pin number for I/O 5
    #define IoPin6 7  // Arduino Digital I/O pin number for I/O 6
    #define IoPin7 19 // Arduino Digital I/O pin number for I/O 7
    
    // ##### DEFINE CHILD ID'S #####
    #define CHILD_ID1 1    // ID for child 1 (I/O)
    #define CHILD_ID2 2    // ID for child 2 (I/O) used for relay
    //#define CHILD_ID3 3  // ID for child 3 (I/O)
    //#define CHILD_ID4 4  // ID for child 4 (I/O)
    #define CHILD_ID5 5    // ID for child 5 (I/O) used for relay
    #define CHILD_ID6 6    // ID for child 6 (I/O) used for relay
    #define CHILD_ID7 7    // ID for child 7 (I/O) used for relay
    #define CHILD_ID_V1 10  // ID for child virtual switch 1. Used for Outside lighting button
    #define CHILD_ID_V2 11  // ID for child virtual switch 2. Used for Heater button
    #define CHILD_ID_V3 12  // ID for child virtual switch 3. Used for Anti Frost button
    #define CHILD_ID_V4 13  // ID for child virtual switch 4. Used for Party Lights button
    #define CHILD_ID_V5 14  // ID for child virtual switch 5. Used for Wifi AP button
    #define CHILD_ID_V6 15  // ID for child virtual switch 6. Used for Aux 1 button
    #define CHILD_ID_V7 16  // ID for child virtual switch 7. Used for Aux 2 button
    #define CHILD_ID_V8 17  // ID for child virtual switch 8. Used for 5 min button
    
    
    // ##### RELAY SETTING #####
    #define RELAY_ON 0          // Invert for some relay modules (currently inverted)
    #define RELAY_OFF 1         // Invert for some relay modules (currently inverted)
    
    // ##### OTHER VARIABLES #####
    bool state; // Not realy sure what this exatly does. I guess temporary storage of received messages and loading from EEPROM
    
    // ##### BOOLEANS FOR VIRTUAL SWITCHES #####
    bool VirtalSwitch1;  // Boolean (status) for virtual switch 1 (controlled by keypad)
    bool VirtalSwitch2;  // Boolean (status) for virtual switch 2 (controlled by keypad)
    bool VirtalSwitch3;  // Boolean (status) for virtual switch 3 (controlled by keypad)
    bool VirtalSwitch4;  // Boolean (status) for virtual switch 4 (controlled by keypad)
    bool VirtalSwitch5;  // Boolean (status) for virtual switch 5 (controlled by keypad)
    bool VirtalSwitch6;  // Boolean (status) for virtual switch 6 (controlled by keypad)
    bool VirtalSwitch7;  // Boolean (status) for virtual switch 7 (controlled by keypad)
    
    // ##### DEFINE MYSENSORS MESSAGE CONTAINERS TO COMMUNICATE WITH GATEWAY #####
    MyMessage msgV1(CHILD_ID_V1,S_LIGHT); // msgV1 sends child ID CHILD_ID_V1 as S_LIGHT
    MyMessage msgV2(CHILD_ID_V2,S_LIGHT); // msgV2 sends child ID CHILD_ID_V2 as S_LIGHT
    MyMessage msgV3(CHILD_ID_V3,S_LIGHT); // msgV3 sends child ID CHILD_ID_V3 as S_LIGHT
    MyMessage msgV4(CHILD_ID_V4,S_LIGHT); // msgV4 sends child ID CHILD_ID_V4 as S_LIGHT
    MyMessage msgV5(CHILD_ID_V5,S_LIGHT); // msgV5 sends child ID CHILD_ID_V5 as S_LIGHT
    MyMessage msgV6(CHILD_ID_V6,S_LIGHT); // msgV6 sends child ID CHILD_ID_V6 as S_LIGHT
    MyMessage msgV7(CHILD_ID_V7,S_LIGHT); // msgV7 sends child ID CHILD_ID_V7 as S_LIGHT
    MyMessage msgV8(CHILD_ID_V8,S_LIGHT); // msgV8 sends child ID CHILD_ID_V8 as S_LIGHT
    
    // ##### KEYPAD SETUP #####
    const byte ROWS = 4; // Just settings for keypad library. Enter number of rows here
    const byte COLS = 3; // Just settings for keypad library. Enter number of columns here
    // Nameing of very key on the keypad. The output of keypad code will be the charaters used here.
    char keys[ROWS][COLS] = {
      {'1', '2', '3'},
      {'4', '5', '6'},
      {'7', '8', '9'},
      {'A', 'B', 'C'}
    };
    byte rowPins[ROWS] = { A0, A1, A2, A3 }; // The pins the row wires are connected to (from left to right)
    byte colPins[COLS] = { A4, 1, 0 };       // The pins the column wires are connected to (from top to bottom)
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // This creates the actual keypad based on specs above.
    
    
    void setup()  
    {  
      // ##### I/O SETUP FOR PHYSICAL I/O ON ARDUINO #####
      // Setup I/O 2
      pinMode(IoPin2, OUTPUT);                        // use I/O 2 as output for relay module
      digitalWrite(IoPin2, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID2);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 4                                     Not in use  
      // pinMode(IoPin4, OUTPUT);                        // use I/O 4 as output for relay module
      // digitalWrite(IoPin4, RELAY_OFF);                // and set switch relay output off
      // state = loadState(CHILD_ID4);                   // Load last known state (using eeprom storage) 
      // digitalWrite(IoPin4, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 5
      pinMode(IoPin5, OUTPUT);                        // use I/O 5 as output for relay module
      digitalWrite(IoPin5, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID5);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 6
      pinMode(IoPin6, OUTPUT);                        // use I/O 6 as output for relay module
      digitalWrite(IoPin6, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID6);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF); // write last know state
    
      // Setup I/O 7
      pinMode(IoPin7, OUTPUT);                        // use I/O 7 as output for relay module
      digitalWrite(IoPin7, RELAY_OFF);                // and set switch relay output off
      state = loadState(CHILD_ID7);                   // Load last known state (using eeprom storage) 
      digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF); // write last know state
    
    
     // ----- PIN SETUP (FOR TESTING ONLY -----
     // pinMode(IoPin4, OUTPUT);   //use I/O 4 as output for relay module
     // digitalWrite(IoPin4, HIGH);//   and set is HIGH so relay stays off
      
    }
    
    // ###### PRESENT ALL ATTACHED SENSORS TO CONTROLLER ######
    void presentation(){
      sendSketchInfo(ProjectName, ProjectVersion); // Send the sketch version information to the gateway and Controller
      // ----- Physical I/O child IDs -----
      //present(CHILD_ID1, XXXXXXX); //Register child ID 1 as NONE
      present(CHILD_ID2, S_LIGHT, "Outside lighting relay"); //Register child ID 2 as S_LIGHT, and sent name to controller.
      //present(CHILD_ID3, XXXXXXX); //Register child ID 3 as NONE
      //present(CHILD_ID4, XXXXXXX); //Register child ID 4 as NONE
      present(CHILD_ID5, S_LIGHT, "Switched outlet heater"); //Register child ID 5 as S_LIGHT, and sent name to controller.
      present(CHILD_ID6, S_LIGHT, "Sw outlet party lights"); //Register child ID 6 as S_LIGHT, and sent name to controller.
      present(CHILD_ID7, S_LIGHT, "Gate light relay");       //Register child ID 7 as S_LIGHT, and sent name to controller.
      // ----- virtual switch child ID (keypad)
      present(CHILD_ID_V1, S_LIGHT, "Outside light switch"); //Register CHILD_ID_V1 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V2, S_LIGHT, "Shed heater");          //Register CHILD_ID_V2 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V3, S_LIGHT, "Anti frost mode");      //Register CHILD_ID_V3 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V4, S_LIGHT, "Party lights");         //Register CHILD_ID_V4 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V5, S_LIGHT, "Wifi AP");              //Register CHILD_ID_V5 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V6, S_LIGHT, "Aux 1");                //Register CHILD_ID_V6 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V7, S_LIGHT, "Aux 2");                //Register CHILD_ID_V7 as S_LIGHT, and sent name to controller
      present(CHILD_ID_V8, S_LIGHT, "5 min. button");        //Register CHILD_ID_V8 as S_LIGHT, and sent name to controller
    } 
    
    void loop()
    { // ##### KEYPAD CODE TO SET A ACTION FOR A PRESSED KEY #####
      char key = kpd.getKey();       // Get key from keypad (if pressed)
      if (key)  {                    // compare key (defined in keypad setup)
        switch (key) {               // with multiple multiple options below
    
                                // On button for outside light
          case '1':                  // If the pressed key compares with "1"
            VirtalSwitch1 = true;    // Change te state of boolean VirtalSwitch1 to true (on)
            send(msgV1.set(1));      // Sent "on" command to gateway for CHILD_ID_V1 
            Serial.println("KEY 1"); // Debug code. Print "KEY 1" to show that button 1 was pressed
            break;                   // End of code for button 1
    
                                // Off button for outside light
          case '2':                  // If the pressed key compares with "2"
            VirtalSwitch1 = false;   // Change te state of boolean VirtalSwitch1 to false (off)
            send(msgV1.set(0));      // Sent "off" command to gateway for CHILD_ID_V1 
            Serial.println("KEY 2"); // Debug code. Print "KEY 2" to show that button 2 was pressed
            break;                   // End of code for button 2
    
                                // 5 minute button
          case '3':                  // If the pressed key compares with "3"
            send(msgV8.set(1));      // Sent "on" command to gateway for CHILD_ID_V8
            Serial.println("KEY 4"); // Debug code. Print "KEY 4" to show that button 4 was pressed
            break;                   // End of code for button 4
    
                                // On button for heating
          case '4':                  // If the pressed key compares with "4"
            VirtalSwitch2 = true;    // Change te state of boolean VirtalSwitch2 to true (on). This is the heater
            VirtalSwitch3 = false;   // Change te state of boolean VirtalSwitch3 to false (off). This is the anti frost funtion
            send(msgV2.set(1));      // Sent "on" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(0));      // Sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 4"); // Debug code. Print "KEY 4" to show that button 4 was pressed
            break;                   // End of code for button 4
    
                                // Off button for heating
          case '5':                  // If the pressed key compares with "5"
            VirtalSwitch2 = false;   // Change te state of boolean VirtalSwitch2 to false (off). This is the heater
            VirtalSwitch3 = false;   // Change te state of boolean VirtalSwitch3 to false (off). This is the anti frost funtion
            send(msgV2.set(0));      // Sent "off" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(0));      // Sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 5"); // Debug code. Print "KEY 5" to show that button 5 was pressed
            break;                   // End of code for button 5
    
                                // On button for anti frost
          case '6':                  // If the pressed key compares with "6"
            VirtalSwitch2 = false;   // Change te state of boolean VirtalSwitch2 to false (off). This is the heater
            VirtalSwitch3 = true;    // Change te state of boolean VirtalSwitch3 to true (on). This is the anti frost funtion
            send(msgV2.set(0));      // Sent "off" command to gateway for CHILD_ID_V2. This is the heater
            send(msgV3.set(1));      // Sent "on" command to gateway for CHILD_ID_V3. This is the anti frost funtion
            Serial.println("KEY 6"); // Debug code. Print "KEY 6" to show that button 6 was pressed
            break;                   // End of code for button 6
            
                                // On button for party lights
          case '7':                  // If the pressed key compares with "7"
            VirtalSwitch4 = true;    // Change te state of boolean VirtalSwitch1 to true (on)
            send(msgV4.set(1));      // Sent "on" command to gateway for CHILD_ID_V4 
            Serial.println("KEY 7"); // Debug code. Print "KEY 7" to show that button 7 was pressed
            break;                   // End of code for button 7
    
                                // Off button for party lights
          case '8':                  // If the pressed key compares with "8"
            VirtalSwitch4 = false;   // Change te state of boolean VirtalSwitch1 to false (off)
            send(msgV4.set(0));      // Sent "off" command to gateway for CHILD_ID_V4 
            Serial.println("KEY 8"); // Debug code. Print "KEY 8" to show that button 8 was pressed
            break;                   // End of code for button 8
    
                                    // Aux ON/OFF buton 1
          case '9':{                     // If the pressed key compares with "9"
            if (VirtalSwitch6 == false){ // check if the virtual switch (VirtalSwitch6) is OFF. If so, do the following
              VirtalSwitch6 = true;      // set the new status of the virtual swich to ON
              send(msgV6.set(1));        // Sent "on" command to gateway for CHILD_ID_V6 
            }
            else {                       // If the virtual switch was not OFF, it must have been ON, so do the following 
              VirtalSwitch6 = false;     // set the new status of the virtual swich to OFF
              send(msgV6.set(0));        // Sent "off" command to gateway for CHILD_ID_V6 
              }
            }
            Serial.println("KEY 9");    // Debug code. Print "KEY 9" to show that button 9 was pressed
            break;                      // End of code for button 9
    
                                 // On button for Wifi AP
          case 'A':                  // If the pressed key compares with "A"
            VirtalSwitch5 = true;    // Change te state of boolean VirtalSwitch5 to true (on)
            send(msgV5.set(1));      // Sent "on" command to gateway for CHILD_ID_V5 
            Serial.println("KEY A"); // Debug code. Print "KEY A" to show that button A was pressed
            break;                   // End of code for button A
    
                                 // Off button for Wifi AP
          case 'B':                  // If the pressed key compares with "B"
            VirtalSwitch5 = false;   // Change te state of boolean VirtalSwitch5 to false (off)
            send(msgV5.set(0));      // Sent "off" command to gateway for CHILD_ID_V5 
            Serial.println("KEY B"); // Debug code. Print "KEY B" to show that button B was pressed
            break;                   // End of code for button B    
    
                                  // Aux ON/OFF buton 2
          case 'C':{                     // If the pressed key compares with "C"
            if (VirtalSwitch7 == false){ // check if the virtual switch (VirtalSwitch7) is OFF. If so, do the following
              VirtalSwitch7 = true;      // set the new status of the virtual swich to ON
              send(msgV7.set(1));        // Sent "on" command to gateway for CHILD_ID_V7 
            }
            else {                       // If the virtual switch was not OFF, it must have been ON, so do the following 
              VirtalSwitch7 = false;     // set the new status of the virtual swich to OFF
              send(msgV7.set(0));        // Sent "off" command to gateway for CHILD_ID_V7 
              }
            }
            Serial.println("KEY C");    // Debug code. Print "KEY C" to show that button C was pressed
            break;                      // End of code for button C
            
          default:                  // If the pressed key does not match the cases above
            Serial.print(key);      // Print the key that was pressed
            Serial.println(" was pressed but not recognized by the keypadcode. Something is wrong!"); // print warning
            break;                  // End of function
        }
      }  
      // ----- WRITE BOOLEAN TO PIN (FOR TESTING PURPOSES ONLY) -----
      //digitalWrite(IoPin4, VirtalSwitch6);
    }
    
    // ##### CODE FOR RECEIVING MYSENSORS MESSAGES FROM CONTROLLER #####
    void receive(const MyMessage &message) {                         //start mysensor receiving code
      if (message.isAck()) {                                         //Check for gateway acknowledgment
         Serial.println("This is an ack from gateway");              //Print debug code (serial print) to confirm received ack
      }
      // ----- Relay actors -----
      // ----- Action taken for child ID 1: Currently set as V_LIGHT to switch relay on IoPin1 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID2) {  //if message is V_LIGHT and the child ID is 2 
         state = message.getBool();                                  //guess this write the incomming boolean to state?
         digitalWrite(IoPin2, state?RELAY_ON:RELAY_OFF);             //change the the arduino pin from "on" to "off" or other way around
         saveState(CHILD_ID2, state);                                //guess this saves the state if child ID 2 to EEPPROM
         Serial.print("Incoming change for sensor:");                //debug info text
         Serial.print(message.sensor);                               //write received child ID
         Serial.print(", New status: ");                             //debug info text
         Serial.println(message.getBool());                          //write received boolean
       } 
      // ----- Action taken for child ID 5: Currently set as V_LIGHT to switch relay on IoPin5 -----
      if (message.type == V_LIGHT && message.sensor == CHILD_ID5) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin5, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID5, state);
       } 
      // ----- Action taken for child ID 6: Currently set as V_LIGHT to switch relay on IoPin6 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID6) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin6, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID6, state);
       } 
      // ----- Action taken for child ID 7: Currently set as V_LIGHT to switch relay on IoPin7 ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID7) {  //no further comments. See actions for child 2. Its the same
         state = message.getBool();                                  //also no debug code (serial print) written to save space
         digitalWrite(IoPin7, state?RELAY_ON:RELAY_OFF);
         saveState(CHILD_ID7, state);
       } 
      // ----- Virtual switches -----   
      // ----- Action taken for CHILD_ID_V1: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch1) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V1) {  //if message is V_LIGHT, the child ID is CHILD_ID_V1 do the following 
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch1 = state;                                        //copy the received boolean to VirtalSwitch1 
         saveState(CHILD_ID_V1, state);                                //guess this saves the state if CHILD_ID_V1 to EEPPROM
       } 
      // ----- Action taken for CHILD_ID_V2: Currently set as V_LIGHT.  ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V2 && state == 1) {  //if message is V_LIGHT, the CHILD_ID_V2 and the message is 1 (on)
         VirtalSwitch2 = true;                                         //set VirtalSwitch2 (heating) to 1 (on)
         VirtalSwitch3 = false;                                        //set VirtalSwitch3 (anti frost) to 0. This prevents the anti frost and heating funtion being on at the same time.
         send(msgV3.set(0));                                           //sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion.
         saveState(CHILD_ID_V2, state);                                //guess this saves the state if CHILD_ID_V1 to EEPPROM
       }  
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V2 && state == 0) {  //if message is V_LIGHT, the CHILD_ID_V2 and the message is 0 (off)
         VirtalSwitch2 = false;                                        //set VirtalSwitch2 (heating) to 0 (off)
         VirtalSwitch3 = false;                                        //set VirtalSwitch3 (anti frost) to 0 ((off)
         send(msgV3.set(0));                                           //sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion.
         saveState(CHILD_ID_V2, state);                                //guess this saves the state if CHILD_ID_V1 to EEPPROM
       }
      // ----- Action taken for CHILD_ID_V3: Currently set as V_LIGHT.  ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V3 && state == 1) {  //if message is V_LIGHT, the CHILD_ID_V3 and the message is 1 (on)
         VirtalSwitch3 = true;                                         //set VirtalSwitch3 (anti frost) to 1 (on)
         VirtalSwitch2 = false;                                        //set VirtalSwitch2 (heating) to 0. This prevents the heating and anti frost funtion being on at the same time.
         send(msgV2.set(0));                                           //sent "off" command to gateway for CHILD_ID_V2. This is the heating funtion.
         saveState(CHILD_ID_V3, state);                                //guess this saves the state if CHILD_ID_V1 to EEPPROM
       }  
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V3 && state == 0) {  //if message is V_LIGHT, the CHILD_ID_V3 and the message is 0 (off)
         VirtalSwitch3 = false;                                        //set VirtalSwitch3 (anti frost) to 0 ((off)
         VirtalSwitch2 = false;                                        //set VirtalSwitch2 (heating) to 0 (off)
         send(msgV2.set(0));                                           //sent "off" command to gateway for CHILD_ID_V3. This is the anti frost funtion.
         saveState(CHILD_ID_V3, state);                                //guess this saves the state if CHILD_ID_V1 to EEPPROM
       }
      // ----- Action taken for CHILD_ID_V4: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch4) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V4) {  //if message is V_LIGHT, the child ID is CHILD_ID_V4 do the following
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch4 = state;                                        //copy the received boolean to VirtalSwitch4 
         saveState(CHILD_ID_V4, state);                                //guess this saves the state if CHILD_ID_V4 to EEPPROM
       } 
      // ----- Action taken for CHILD_ID_V5: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch5) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V5) {  //if message is V_LIGHT, the child ID is CHILD_ID_V5 do the following
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch5 = state;                                        //copy the received boolean to VirtalSwitch5 
         saveState(CHILD_ID_V5, state);                                //guess this saves the state if CHILD_ID_V5 to EEPPROM
       } 
      // ----- Action taken for CHILD_ID_V6: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch6) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V6) {  //if message is V_LIGHT, the child ID is CHILD_ID_V6 do the following
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch6 = state;                                        //copy the received boolean to VirtalSwitch6 
         saveState(CHILD_ID_V6, state);                                //guess this saves the state if CHILD_ID_V6 to EEPPROM
       }    
      // ----- Action taken for CHILD_ID_V7: Currently set as V_LIGHT. Changes the boolean of virtual switch (VirtalSwitch7) ----- 
      if (message.type == V_LIGHT && message.sensor == CHILD_ID_V7) {  //if message is V_LIGHT, the child ID is CHILD_ID_V7 do the following
         state = message.getBool();                                    //guess this write the incomming boolean to state?
         VirtalSwitch7 = state;                                        //copy the received boolean to VirtalSwitch7 
         saveState(CHILD_ID_V7, state);                                //guess this saves the state if CHILD_ID_V6 to EEPPROM
       }   
    }```


  • Found it!!! I figured there must be a serial begin somewhere in the library. It was not in MySensors.h, but MySensors.h had MySensorsCore.h included which included serval other library's. One of them was MyConfig.h I looked in al library's for a serial begin by searching "serial". In MyConfig.h i found this.

    /**********************************
    *  Serial and debug options
    ***********************************/
    
    // Enable MY_DEBUG in sketch to show debug prints. This option will add a lot to the size of the
    // final sketch but is helpful to see what is actually is happening during development
    //#define MY_DEBUG
    
    // Enable MY_SPECIAL_DEBUG in sketch to activate I_DEBUG messages if MY_DEBUG is disabled.
    // I_DEBUG requests are:
    // R: routing info (only repeaters): received msg XXYY (as stream), where XX is the node and YY the routing node
    // V: CPU voltage
    // F: CPU frequency
    // M: free memory
    // E: clear MySensors EEPROM area and reboot (i.e. "factory" reset)
    //#define MY_SPECIAL_DEBUG
    
    // Enable MY_DEBUG_VERBOSE_SIGNING flag for verbose debug prints related to signing.
    // Requires DEBUG to be enabled.
    // This will add even more to the size of the final sketch!
    //#define MY_DEBUG_VERBOSE_SIGNING
    
    // Enable this in sketch if you want to use TX(1), RX(0) as normal I/O pin
    //#define MY_DISABLED_SERIAL
    
    // Enable MY_CORE_ONLY flag if you want to use core functions without loading the framework
    //#define MY_CORE_ONLY
    
    // Turn off debug if serial pins is used for other stuff
    

    Adding "#define MY_DISABLED_SERIAL" to my sketch did the trick, and i can now use the full keypad.

    // ##### SET PROJECT NAME AND VERSION #####
    #define ProjectName "Shed controller"
    #define ProjectVersion "0,3"
    
    // ##### SET MYSENSOR SETTINGS BEFORE INCLUDIGN LIBRARY #####
    //#define MY_DEBUG // Enable debug prints to serial monitor
    #define MY_DISABLED_SERIAL
    #define MY_RADIO_NRF24 // Enable and select radio type attached
    #define MY_REPEATER_FEATURE // Enabled repeater feature for this node
    
    // ##### INCLUDE LIBRARYS #####
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    etc.....
    

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.