Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. gateway serial and dimmer example

gateway serial and dimmer example

Scheduled Pinned Locked Moved Development
23 Posts 4 Posters 3.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MCF
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MCF
      wrote on last edited by
      #2

      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 );
        }
      }
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        MCF
        wrote on last edited by
        #3

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

        mfalkviddM 1 Reply Last reply
        0
        • M MCF

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

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          @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.

          M 1 Reply Last reply
          0
          • mfalkviddM mfalkvidd

            @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.

            M Offline
            M Offline
            MCF
            wrote on last edited by
            #5

            @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
            
            ?
            mfalkviddM 1 Reply Last reply
            0
            • M MCF

              @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
              
              ?
              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by
              #6

              @MCF exactly

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MCF
                wrote on last edited by
                #7

                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?

                mfalkviddM 1 Reply Last reply
                0
                • M MCF

                  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?

                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by mfalkvidd
                  #8

                  @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.

                  M 1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

                    @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.

                    M Offline
                    M Offline
                    MCF
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MCF
                      wrote on last edited by
                      #10

                      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

                      mfalkviddM 1 Reply Last reply
                      0
                      • M MCF

                        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

                        mfalkviddM Offline
                        mfalkviddM Offline
                        mfalkvidd
                        Mod
                        wrote on last edited by mfalkvidd
                        #11

                        @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.

                        1 Reply Last reply
                        0
                        • mfalkviddM Offline
                          mfalkviddM Offline
                          mfalkvidd
                          Mod
                          wrote on last edited by
                          #12

                          Other references that might haelp:
                          https://www.mysensors.org/apidocs-beta/group__MyMessagegrp.html
                          https://www.mysensors.org/download/sensor_api_20#message-manipulation

                          M 1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MCF
                            wrote on last edited by
                            #13

                            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!

                            1 Reply Last reply
                            0
                            • mfalkviddM mfalkvidd

                              Other references that might haelp:
                              https://www.mysensors.org/apidocs-beta/group__MyMessagegrp.html
                              https://www.mysensors.org/download/sensor_api_20#message-manipulation

                              M Offline
                              M Offline
                              MCF
                              wrote on last edited by
                              #14

                              @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!

                              1 Reply Last reply
                              0
                              • PlantexP Offline
                                PlantexP Offline
                                Plantex
                                wrote on last edited by
                                #15

                                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 !

                                M 1 Reply Last reply
                                1
                                • PlantexP Plantex

                                  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 !

                                  M Offline
                                  M Offline
                                  MCF
                                  wrote on last edited by
                                  #16

                                  @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!

                                  PlantexP 1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    towme
                                    wrote on last edited by
                                    #17

                                    @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

                                    M 1 Reply Last reply
                                    0
                                    • M MCF

                                      @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!

                                      PlantexP Offline
                                      PlantexP Offline
                                      Plantex
                                      wrote on last edited by
                                      #18

                                      @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 ?

                                      M 1 Reply Last reply
                                      0
                                      • PlantexP Plantex

                                        @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 ?

                                        M Offline
                                        M Offline
                                        MCF
                                        wrote on last edited by
                                        #19

                                        @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!!

                                        1 Reply Last reply
                                        0
                                        • T towme

                                          @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

                                          M Offline
                                          M Offline
                                          MCF
                                          wrote on last edited by
                                          #20

                                          @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

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          10

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular