gateway serial and dimmer example



  • hi, i'm trying to add the led dimmer example on my gateway sketch, but i don't realy know how? can someone show me the way?



  • this is the code i use :

    /**
    * The MySensors Arduino library handles the wireless radio link and protocol
    * between your home built sensors/actuators and HA controller of choice.
    * The sensors forms a self healing radio network with optional repeaters. Each
    * repeater and gateway builds a routing tables in EEPROM which keeps track of the
    * network topology allowing messages to be routed to nodes.
    *
    * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
    * Copyright (C) 2013-2015 Sensnology AB
    * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    *
    * Documentation: http://www.mysensors.org
    * Support Forum: http://forum.mysensors.org
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * version 2 as published by the Free Software Foundation.
    *
    *******************************
    *
    * DESCRIPTION
    * The ArduinoGateway prints data received from sensors on the serial link.
    * The gateway accepts input on seral which will be sent out on radio network.
    *
    * The GW code is designed for Arduino Nano 328p / 16MHz
    *
    * Wire connections (OPTIONAL):
    * - Inclusion button should be connected between digital pin 3 and GND
    * - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
    *
    * LEDs (OPTIONAL):
    * - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
    * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
    * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
    * - ERR (red) - fast blink on error during transmission error or recieve crc error
    *
    */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    #define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    // Enable serial gateway
    #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
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Inverses the behavior of leds
    //#define MY_WITH_LEDS_BLINKING_INVERSE
    
    // Flash leds on rx/tx/err
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    
    #include <MySensors.h>
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(1, V_DIMMER);
    MyMessage lightMsg(1, V_LIGHT);
    
    void setup()
    {
    	// Setup locally attached sensors
     request( 1, V_DIMMER );
    }
    
    void presentation()
    {
    	// Present locally attached sensors
     // Register the LED Dimmable Light with the gateway
     present( 1, S_DIMMER );
    }
    
    void loop()
    {
    	// Send locally attached sensor data here
    }
    void receive(const MyMessage &message)
    {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
        //  Retrieve the power or dim level from the incoming request message
        int requestedLevel = atoi( message.data );
    
        // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
        requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    
        // Clip incoming level to valid range of 0 to 100
        requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
        requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
        Serial.print( "Changing level to " );
        Serial.print( requestedLevel );
        Serial.print( ", from " );
        Serial.println( currentLevel );
    
        fadeToLevel( requestedLevel );
    
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        send(lightMsg.set(currentLevel > 0));
    
        // hek comment: Is this really nessesary?
        send( dimmerMsg.set(currentLevel) );
    
    
      }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel( int toLevel )
    {
    
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    


  • it seems to work, but sometimes the led turns off without any reason....


  • Mod

    @MCF my guess is that the gateway receives a message for another sensor. This triggers the receive function.

    Try printing message.sensor inside the receive function to verify that this is the case.

    The solution would be to check that message.sensor == getNodeId() and only handle the incoming message when that is true.



  • @mfalkvidd you mean something like```
    void receive(const MyMessage &message)
    {
    if message.sensor==getNodeId(0){ //the node id of my passerelle
    if (message.type == V_LIGHT || message.type == V_DIMMER) {

    //  Retrieve the power or dim level from the incoming request message
    
    ?

  • Mod

    @MCF exactly



  • if i write getNodeId(0), there is too many arguments. so i tryied just getNodeId().
    this doesn't work, and i think i know why: message.sensor is a child ID isn't it?


  • Mod

    @MCF yes, you're correct. It should be message.destination, not message.sensor. Sorry about that.

    If you have multiple leds and want to control them individually you can use message.sensor though.



  • @mfalkvidd thank you very much, i was searching in vain a list of the mymessage class!!!! do you know where can i find that ?



  • so i wrote

    if (message.destination==0){
    

    and the light still doesn't work. every destination message on the network is the gateaway, right? so how can i just dim the led attached on her ? maybe i dont need to send a message?? I'm a litle bit confuzed


  • Mod

    @MCF the message.destination link in my last post shows the .h file where the message variables are defined.

    Maybe print all of them and see if there is anything that differs between "real" and "false" messages?

    message.getCommand() might be useful as well.


  • Mod



  • hi, i'm still trying to understand how to drive my gateway led strip! maybe i miss to tell you that i wanted to dimm that only with my controler, which is a domoticz on a rpy. is it the same way as usual, or is there another way that sending message to the gateway because i can't understand how to do!



  • @mfalkvidd hello, i'm sorry to say that, but i don't know how to do.. i try to understand how mysensors work but i really need your help!



  • Hi @MCF

    I've just started a topic with two sketches for LED Dimmer. Have a look and test one of two sketches. 1st one is based on Yours with a little change to save the last dimmer status after on/off. 2nd is basing on Dimmable Light sketch (using EPROM to save the status).
    Good Luck !



  • @plantex hi, my problem is to dimm led on the gateway sketch, because i need to sort messages. i can't find exemples on the forum, it would be very strange than no one did that already!



  • @MCF

    Maybe it is not a problem of the code (because that looks ok), but with the wiring, or too little power to the led stripe.
    How long is your LED stripe?
    Also a mistake I have made, when I set up mine, was that every mosfet you use in your setup needs to be attached to a separate cooler / heat sink, because the back of the mosfet is a middle pin also.
    Another thing is I see in your code that you present your dimmer as child id 1, then you should use 1 and not 0 in your debug messages.
    Plug your arduino to your computer and open Serial monitor in the arduino ide while testing your setup.

    (Also do not confuse analog and digital pins on the arduino, both can be number 3 ) 😉

    cheers
    tom



  • @mcf
    One more thing.
    Probably this is obvious for lots of forum users but sometimes I had some problems with some sketches when uploaded to gateway-arduino (connected to Domoticz) and debugger was enabled.

    Try to comment out #define MY_DEBUG see if there is any change.

    From other side do You need that sketch on gateway ? maybe transfer it to node with nrf24 ?



  • @plantex just because it's too bad not using those outputs! but you're right, i think i 'll solder one mini pro, and that question will be over!!



  • @towme hi, this is not a hardware problem, my led stripe is two meter long, and i've checked the mofset with another sketch. The problem for the debug message is that i don't know how to open serial monitor on the gateway connected to the rpy (i use an arduino uno ), so it's hard to check debug message live



  • @MCF Maybe install an arduino software for raspbian and try to open serial monitor for gateway ?



  • after hours (days???) of fighting against the machine (rage?), a waf dangerously low, i got a solution... it's not the most acceptable for me, BUT IT WORKS so be it! i just change the sensor ID from 1 to 100, i'm sure that no message could be send or transfert or whatever exepted the one i chose, and trust me it's good to feel the power of the light!! i hope that some one could explain how to understand every details of Mymessage.h , anyway now this is the code...```
    /**

    • The MySensors Arduino library handles the wireless radio link and protocol
    • between your home built sensors/actuators and HA controller of choice.
    • The sensors forms a self healing radio network with optional repeaters. Each
    • repeater and gateway builds a routing tables in EEPROM which keeps track of the
    • network topology allowing messages to be routed to nodes.
    • Created by Henrik Ekblad henrik.ekblad@mysensors.org
    • Copyright (C) 2013-2015 Sensnology AB
    • Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    • Documentation: http://www.mysensors.org
    • Support Forum: http://forum.mysensors.org
    • This program is free software; you can redistribute it and/or
    • modify it under the terms of the GNU General Public License
    • version 2 as published by the Free Software Foundation.

    • DESCRIPTION
    • The ArduinoGateway prints data received from sensors on the serial link.
    • The gateway accepts input on seral which will be sent out on radio network.
    • The GW code is designed for Arduino Nano 328p / 16MHz
    • Wire connections (OPTIONAL):
      • Inclusion button should be connected between digital pin 3 and GND
      • RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
    • LEDs (OPTIONAL):
      • To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
      • RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
      • TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
      • ERR (red) - fast blink on error during transmission error or recieve crc error

    */

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    #define MY_RF24_PA_LEVEL RF24_PA_MAX

    // Enable serial gateway
    #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

    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300

    // Inverses the behavior of leds
    //#define MY_WITH_LEDS_BLINKING_INVERSE

    // Flash leds on rx/tx/err
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED

    #include <MySensors.h>

    #define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

    static int16_t currentLevel = 0; // Current dim level...
    MyMessage dimmerMsg(100, V_DIMMER);
    MyMessage lightMsg(100, V_LIGHT);

    void setup()
    {
    // Setup locally attached sensors
    request( 100, V_DIMMER );
    }

    void presentation()
    {
    // Present locally attached sensors
    // Register the LED Dimmable Light with the gateway
    present( 100, S_DIMMER );
    }

    void loop()
    {
    // Send locally attached sensor data here
    }
    void receive(const MyMessage &message)
    {
    if (message.sensor == 100 ){

    if (message.type == V_LIGHT || message.type == V_DIMMER) {

    //  Retrieve the power or dim level from the incoming request message
    int requestedLevel = atoi( message.data );
    
    // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
    requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    
    // Clip incoming level to valid range of 0 to 100
    requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
    requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
    Serial.print( "Changing level to " );
    Serial.print( requestedLevel );
    Serial.print( ", from " );
    Serial.println( currentLevel );
    
    fadeToLevel( requestedLevel );
    
    // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
    send(lightMsg.set(currentLevel > 0));
    
    // hek comment: Is this really nessesary?
    send( dimmerMsg.set(currentLevel) );
    

    }
    }

    }

    /***

    • This method provides a graceful fade up/down effect
      */
      void fadeToLevel( int toLevel )
      {

    int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;

    while ( currentLevel != toLevel ) {
    currentLevel += delta;
    analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
    delay( FADE_DELAY );
    }
    }



  • (if some one can explain how

    Insert Code Here
    

    works, that could be fine too... no laughs please!


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 3
  • 2
  • 3
  • 6

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts