A Basic domestic 240v light and switch.



  • Once again I must be missing something. Nowhere can I find and example of a simple domestic 240v light switch and light fitting.

    I have lights on my HA, can control them with PC, phone, ALEXA, no problem, but I want manual switches (wifi) on the walls also.

    I use the relay sketch for the light fitting side, all working fine.

    Can I find anything relating to the switch side..... naffink.

    I have built the hardware, arduino nano,NRF2401+ all sat on the back of a single UK light switch. Thats all good and tested also. It's the code I dont have or guidance to set it up.

    Lights, temp, alexa were all basically plug and play with minor tweaks, this, a most simple use, baffled again.

    Please help.


  • Hero Member

    @el-tigro The relay with button actuator sketch is a good place to start



  • I did have a look at that I assume I would need to insert a trigger also. It all seemed a bit over the top for the simplest of operation. All I want is if pin 3 goes high, publish light on.

    The bit I can't get my head around is that there is no details of topics etc to establish what goes where. Because I don't know, it is all a bit 'magic' how the communications work.



  • This is the yaml entry for mqtt switch from HA website

    switch:
      - platform: mqtt
        name: "Bedroom Switch"
        state_topic: "home/bedroom/switch1"
        command_topic: "home/bedroom/switch1/set"
        payload_on: "ON"
        payload_off: "OFF"
        optimistic: false
        qos: 0
        retain: true
    

    There is no similar refence for mysensors switch section.

    It's hard to ask when your not sure what your asking for.


  • Hero Member

    @el-tigro Yes MySensors is certainly a journey of discovery ! 🙂

    I am not sure exactly what part of the sketch you are having problems understanding. I do not use yaml or mqtt so will not be of any help there i am afraid.

    Below I have posted a slightly modified relay sketch, which is probably more suited to being used as a light switch, It should work even if the uplink to the gateway/controller is lost. It is fairly well commented so have a look and let me know what part you need more info on.

    The part that lets the controller know when the local switch has been used to change the state is in the loop section. This sketch, like the original uses a momentary on push button you would need to make a few small changes if you are using a toggle switch.

     send(msg.set(state), false);          // send new state to controller, no ack requested
    
    /*
     * Relay with button sketch
     * modified to work with no uplink
     * to gateway
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #define MY_TRANSPORT_WAIT_READY_MS 3000     //set how long to wait for transport ready. in milliseconds
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>                                                       
    
    #define RELAY_PIN  4      // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
    #define CHILD_ID 1        // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce();
    int oldValue = 0;
    bool state;
    
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup()
    {
      pinMode(BUTTON_PIN, INPUT);              // Setup the button pin  
      digitalWrite(BUTTON_PIN, HIGH);           // Activate internal pull-up
    
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
     
      pinMode(RELAY_PIN, OUTPUT);               // set relay pin in output mode  
      digitalWrite(RELAY_PIN, RELAY_OFF);      // Make sure relay is off when starting up
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
    }
    
    
    void loop()
    {
      debouncer.update();
      
      int value = debouncer.read();                               // Get the update value
      if (value != oldValue && value == 0) {                      // check for new button push
        state =  !state;                                          // Toggle the state
        send(msg.set(state), false);                               // send new state to controller, no ack requested
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state 
       }
     
      oldValue = value;
    }
    
    /*-------------------start of functions--------------------------*/
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      
      if (message.type == V_STATUS) {                               // check to see if incoming message is for a switch
        state = message.getBool();                                  // get the current state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to current state
        
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    
      }
    }
    


  • Hello mate and thanks for chirping in. I realised a while ago that this project was well above my current knowledge base but thats like a red rag to bull, stubborn old git that I am.

    After I got my HA, mqtt, MySensors and the gateway talking, my first few nodes found and set themselves up pretty much. I didn't have to jump into message construction, pub/sub, complex yaml code and that's the bit I'm stuck on. Once I get it, I'm away, the penny just hasn't dropped yet.

    I think I need a night in, just me and mqtt, a nice meal, a bottle of soco, a few cuddles and really get to know each other. I guess thats true cyber sex, i do have a wife and kids.... honest.

    Any reccommended sites, tutorials will be appreciated.

    Cheers



  • Here is the most basic sketch I could find and tweaked the code to run as a toggle on a domestic light switch.

    #define MY_RADIO_NRF24
    #include <MySensors.h>
    #include <SPI.h>
    #define MY_NODE_ID 25
    #define OPEN 1
    #define CLOSE 0
    #define switchpin 3
    int oldvalue = OPEN;
    MyMessage msg(MY_NODE_ID, V_TRIPPED);
    uint8_t value = OPEN;
    
    void setup()
    {
      pinMode (switchpin, INPUT_PULLUP);
    }
    
    void presentation()
    {
      sendSketchInfo("TESTER", "");
      present(MY_NODE_ID, S_DOOR);
    }
    
    void loop()
    {
      if (digitalRead(switchpin) == !oldvalue)
      {
        value = value == OPEN ? CLOSE : OPEN;
        send(msg.set(value));
        oldvalue = value;
      }
    }
    

    With this code alone,no yaml entry, the node is found by HA and shows accordingly as triggered if the switch is flicked. mqtt all good, gateway happy.

    How the fook do I attach switch result to the intended node?
    Do I need a yaml entry for the physical switch? I tried but failed.
    Do I then need to set up a trigger?

    switch:
      - platform: mqtt
        name: "TESTER"
        command_topic: "mygateway1-in/2/2/1/0/2"
        payload_on: "1"
        payload_off: "0"
        optimistic: false
        qos: 0
        retain: true
    
    

    This just gives me a new (software) switch in my dash and controls the light but physical switch (node 2 child 2) doesn't work.



  • 1485777547: Sending PINGRESP to mysensors-1
    1485777562: Received PINGREQ from mysensors-1
    1485777562: Sending PINGRESP to mysensors-1
    1485777563: Received PUBLISH from mysensors-1 (d0, q0, r0, m0, 'mygateway1-out/5/25/1/0/16', ... (1 bytes))
    1485777563: Sending PUBLISH to home-assistant-1 (d0, q0, r0, m0, 'mygateway1-out/5/25/1/0/16', ... (1 bytes))
    1485777565: Received PUBLISH from mysensors-1 (d0, q0, r0, m0, 'mygateway1-out/5/25/1/0/16', ... (1 bytes))
    1485777565: Sending PUBLISH to home-assistant-1 (d0, q0, r0, m0, 'mygateway1-out/5/25/1/0/16', ... (1 bytes))
    
    

    This is the mqtt log for one on / off cycle.


Log in to reply
 

Suggested Topics

14
Online

11.4k
Users

11.1k
Topics

112.7k
Posts