Domoticz/Mysensors



  • IMG_1446.JPG



  • interesting schematic. another way of controlling lights is:
    rewire standard light switch to low voltage input of arduino node
    use node's relay to control light, works even if you don't have neutral wire in light switch backbox.



  • Hi
    Thanks. The good thing is that the light circuit is independent from Arduino and Raspberry. That was the main reason behind it. I was supposed to add a description but something went wrong and I only published the schematic. Anyway, I have got a problem with a script for this solution.
    Anybody can help with the programming?



  • @alco plenty people here ready to help



  • #define MY_DEBUG
    #define MY_GATEWAY_SERIAL

    // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 8000000L
    #define MY_BAUD_RATE 38400
    #endif

    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    #define MY_INCLUSION_BUTTON_FEATURE

    // Inverses behavior of inclusion button (if using external pullup)
    //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN 3

    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>

    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE

    #define RELAY_ON 1 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define noLightSwitch 10 //2-11
    int relayPin[] = {2,3,4,5,6,7,8,9,10,11}; // switch around pins to your desire
    int buttonPin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9}; // switch around pins to your desire
    Bounce debouncer[10];
    MyMessage msg[10];

    void before() {
    //sendHeartbeat();
    wait(100);
    // Initialize Relays with corresponding buttons
    for (int i = 0; i < noLightSwitch; i++){
    // Setup the button.
    pinMode(buttonPin[i], INPUT_PULLUP);
    wait(100);
    // Then set relay pins in output mode
    pinMode(relayPin[i], OUTPUT);
    wait(100);
    digitalWrite(relayPin[i], RELAY_OFF);
    msg[i].sensor = i; // initialize messages
    msg[i].type = V_LIGHT;
    //send(msg[i].set(loadState(i))); // make controller aware of last status
    //wait(100);
    // setup debouncer.
    debouncer[i] = Bounce(); // initialize debouncer
    debouncer[i].attach(buttonPin[i]);
    debouncer[i].interval(20);
    }
    }

    void setup() {
    // Setup locally attached sensors
    delay(100);
    presentation();
    }

    boolean inputState;
    void presentation() {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relay", "1.0");
    // Register all sensors to gw (they will be created as child devices)
    for (int i = 0; i < noLightSwitch; i++){
    present(buttonPin[i], S_LIGHT);
    inputState =! digitalRead(buttonPin[i]);
    send(msg[i].set(inputState));
    }
    wait(100);
    }

    void loop() {
    // Send locally attached sensor data here
    for (int i = 0; i < noLightSwitch; i++){
    if (debouncer[i].update()) {
    inputState =! digitalRead(buttonPin[i]);
    send(msg[i].set(inputState));
    }
    wait(100);
    }
    }

    void receive(const MyMessage &message) {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_LIGHT) {
    for (int i = 0; i < noLightSwitch; i++) {
    if (i == message.sensor){
    digitalWrite(relayPin[i], RELAY_ON);
    wait(100);
    digitalWrite(relayPin[i], RELAY_OFF);
    wait(100);
    inputState =! digitalRead(buttonPin[i]);
    send(msg[i].set(inputState));
    }
    }
    }
    }



  • put your code in code tags, makes it much easier to read, use bar above text field it's </> icon.



  • hello, am french.

    no answer from Alco ??

    I simplify the code to understand how it works

    and put the diagram too

    alt text

    code:

    /*
    *  Alco_TL_Impuls_Light.ino
    *
    * https://forum.mysensors.org/topic/11263/domoticz-mysensors
    *
    *
    * REVISION HISTORY
    * Version 1.0 - Gateway Serial par Alco 
    * Version 0.0 - Node en RS485 par JeeLet
    * 
    *LA DESCRIPTION 
      * TL :Commande Impulsionnelle de TeLerupteur d'Eclairage 
      * TS :Retour d'Etat par un contact auxilaire du TL. 
      * 
      * L'installation Electrique n'est pas modifier, 
      * les bouton poussoir de l'habitat Cmd aussi le TL. 
      *  
      *  
      */ 
    
    //#define MY_DEBUG
    #define MY_NODE_ID 24           /*pour Node ID static*/
    
    /* ----- Module RS485 ----*/
     #define MY_RS485
     #define MY_RS485_DE_PIN 2
     #define MY_RS485_BAUD_RATE 9600
    // #define MY_RS485_HWSERIAL Serial1 /* Mega2560, port Serial X? */
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 3   /*Id du capteur (enfant)*/
    
    #define RELAY_PIN  4   // pin Cmd Telerupteur     "TL"
    #define BUTTON_PIN  3  // pin Contact Auxiliaire  "TS"
    
    #define RELAY_ON 1 
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce();  // initialize debouncer
    
    MyMessage msg(CHILD_ID,V_STATUS);
    
    void setup()
      {
      pinMode(BUTTON_PIN,INPUT_PULLUP);
      pinMode(RELAY_PIN , OUTPUT);
      digitalWrite(RELAY_PIN, RELAY_OFF);
      
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(20); // minimum ?
      }
    
      bool state;
      boolean inputState;
    
    void presentation()
      {
      sendSketchInfo("Cmd TL", "1.a");
      present(BUTTON_PIN, S_LIGHT);
      }
    
    void loop()
      {
      if (debouncer.update()) 
       {
       inputState =! digitalRead(BUTTON_PIN);
       send(msg.set(inputState));
       }
      }
    
    void receive(const MyMessage &message)
      {
      if (message.isAck())
        {
         Serial.println("Ceci est un accusé de réception de la passerelle");
         Serial.println("Cmd TL 1.a");
        }
      if (message.type==V_STATUS) // V_STATUS pour MyS v2.0, annulé V_LIGHT. 
        {    
          //if (state = message.getBool () == true) {          
            //  state = message.getBool();
         
         digitalWrite(RELAY_PIN, RELAY_ON);
         wait(200);                          //delai On-Off Impuls
         digitalWrite(RELAY_PIN, RELAY_OFF);
        }
        
    // Write some debug info
       Serial.print("Changement entrant pour le capteur:");
       Serial.print(message.sensor);
       Serial.print(", New status: ");
       Serial.println(message.getBool());
       // }
      }
    // ------------ fin Pgm -----------
    

    the code is not good,dialogue errors :

    error on domoticz
    Error sending switch command, check device/hardware (idx=8) !

    Log:
    Error: MySensors: Repeating previous command (2/2)
    Error: MySensors: Command not received by Node !! (node_id: 24, child_id: 3)

    ??????

    thank you



  • ...I forgot the terminal debug

    .```
    ...set to OFF
    Changement entrant pour le capteur:3, New status: 0
    36376 TSF:MSG:SEND,24-24-0-0,s=3,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    44859 TSF:MSG:READ,0-0-24,s=3,c=1,t=2,pt=0,l=1,sg=0:1
    44864 TSF:MSG:ECHO REQ
    44883 TSF:MSG:SEND,24-24-0-0,s=3,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    44890 !MCO:PRO:RC=1
    5080 !MCO:PRO:RC=1

    ....set to ON
    5080 !MCO:PRO:RC=1
    45082 !MCO:PRO:RC=1
    45084 !MCO:PRO:RC=1
    45086 !MCO:PRO:RC=1
    45088 !MCO:PRO:RC=1
    Changement entrant pour le capteur:3, New status: 1
    45127 TSF:MSG:SEND,24-24-0-0,s=3,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1



  • @JeeLet Hi. First, you probably should have started another thread rather than hijacking this one since the situation is barely related.

    Looking here, I found that !MCO:PRO:RC=1 is caused mainly by calling send() or wait() inside the receive() function.
    I have not completely understood how things are happening but I think at best you're receiving a message while processing the current one. From several sources on this forum, you should keep your receive() function small. Process the content of the message and do all the work in loop().

    I would probably do something like that, but ⚠ I didn't compile and test the code :

    /*
     *  Alco_TL_Impuls_Light.ino
     *
     * https://forum.mysensors.org/topic/11263/domoticz-mysensors
     *
     *
     * REVISION HISTORY
     * Version 1.0 - Gateway Serial par Alco 
     * Version 0.0 - Node en RS485 par JeeLet
     * 
     *LA DESCRIPTION 
     * TL :Commande Impulsionnelle de TeLerupteur d'Eclairage 
     * TS :Retour d'Etat par un contact auxiliaire du TL. 
     * 
     * L'installation Electrique n'est pas modifiee, 
     * les boutons poussoir de l'habitat Cmd aussi le TL. 
     *  
     *  
     */ 
    
    //#define MY_DEBUG
    #define MY_NODE_ID 24           /*pour Node ID static*/
    
    /* ----- Module RS485 ----*/
    #define MY_RS485
    #define MY_RS485_DE_PIN 2
    #define MY_RS485_BAUD_RATE 9600
    // #define MY_RS485_HWSERIAL Serial1 /* Mega2560, port Serial X? */
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define STATE_CHILD_ID 0   /*Id du capteur (enfant)*/
    
    #define RELAY_PIN  4   // pin Cmd Telerupteur     "TL"
    #define CONTROL_INPUT_PIN  3  // pin Contact Auxiliaire  "TS"
    
    #define RELAY_ON 1 
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce();  // initialize debouncer
    
    MyMessage msg(STATE_CHILD_ID,V_STATUS);
    
    void setup()
    {
        pinMode(CONTROL_INPUT_PIN, INPUT_PULLUP);
        pinMode(RELAY_PIN, OUTPUT);
        digitalWrite(RELAY_PIN, RELAY_OFF);
    
        debouncer.attach(CONTROL_INPUT_PIN);
        debouncer.interval(20); // minimum ?
    }
    
    bool currentState;
    bool controllerState;
    
    void presentation()
    {
        sendSketchInfo("Cmd TL", "1.a");
        present(STATE_CHILD_ID, S_LIGHT);
    }
    
    void loop()
    {
        // Send impulse if controller changed state
        if (controllerState != currentState)
        {
            currentState = controllerState;
            digitalWrite(RELAY_PIN, RELAY_ON);
            wait(200);                          //delai On-Off Impuls
            digitalWrite(RELAY_PIN, RELAY_OFF);
        }
    
        // Inform controller if state change from input
        if (debouncer.update()) 
        {
            bool newState = !digitalRead(CONTROL_INPUT_PIN);
            if (newState != currentState)
            {
                currentState = newState;
                if (currentState != controllerState)
                {
                    send(msg.set(currentState));
                }
            }
        }
    }
    
    void receive(const MyMessage &message)
    {
        if (message.isEcho()) // keep message.isAck() for older MySensors version
        {
            Serial.println("Ceci est un accusé de réception de la passerelle");
            Serial.println("Cmd TL 1.a");
            return;
        }
    
        if (message.type==V_STATUS) // V_STATUS pour MyS v2.0, annulé V_LIGHT. 
        {    
            controllerState = message.getBool();
        }
    
        // Write some debug info
        Serial.print("Changement entrant pour le capteur:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    }
    // ------------ fin Pgm -----------
    

    You may have to tweak the conditions and tests so that your sensor is always in a consistent state, but that should resolve the recursion error message.



  • @boum said in Domoticz/Mysensors:

    First, you probably should have started another thread rather than hijacking this one since the situation is barely related.

    Hello
    I just saw your message.
    nothing seen in my mailbox.

    yes that's true for another post, but the quest has the same 🙂
    to see if moderator or alco finds useful, is to do so.

    Thanks for paying attention to my problem, cool.

    I tested the sketch and the errors "MCO: PRO: RC" its part .... bingo

    but as nature hates emptiness, something else happens (nothing serious)

    .... a story of disregard for a local change / command / switchover of the TL

    I look better at your modifications to understand the arduino.
    thank you for the comments in the scketch

    my test model
    alt text

    Well!! where is the notifications button



  • Yes errors on the commands given through domoticz
    only one in three and taken into account

    terminal copy serial

    Changement entrant pour le capteur:3, New status: 0
    Changement entrant pour le capteur:3, New status: 1
    Changement entrant pour le capteur:3, New status: 0
    Changement entrant pour le capteur:3, New status: 0
    Changement entrant pour le capteur:3, New status: 0
    Changement entrant pour le capteur:3, New status: 1
    Changement entrant pour le capteur:3, New status: 0
    Changement entrant pour le capteur:3, New status: 0
    

    ...and the local push button commands are not taken into account

    in the loop something that dies its tail ??

       // Inform controller if state change from input
        if (debouncer.update()) 
        {
            bool newState = !digitalRead(CONTROL_INPUT_PIN);
            if (newState != currentState)
            {
                currentState = newState;
                if (currentState != controllerState)
                {
                    send(msg.set(currentState));
                }
            }
        }
    

    merci Boum


Log in to reply
 

Suggested Topics

  • 5
  • 1
  • 2
  • 4
  • 8
  • 5

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts