TX RX ERR - LED for Sensors



  • Hi,

    is there a simple way to get these LEDs working on Sensors? Like the LEDs on the Serial Gateway but as said... for Sensors.

    Many Thanks

    Petz


  • Contest Winner

    @Meister_Petz

    you want to transmit to a node an instruction to turn on/off an LED... or you want to show at the node that it is in TX/RX process?



  • @BulldogLowell
    I would like the node to show that it is in TX/RX process.


  • Admin

    There isn't any super-easy way of hooking in this in the sketch. You would have to subclass the MySensors class or add the functionality you want directly.



  • Nevertheless Thanks!

    That's how I solved it:
    If message sent, blink LED
    If message received, blink LED

    That's my code for 1, 2 or 3 LEDs:

     #include <MySensor.h>
      #include <SPI.h>
      MySensor gw;
      
        //------------------------------------------------------------------------
        // YOUR DEFINITIONS
        //------------------------------------------------------------------------
        // Definitions
       
    
        //------------------------------------------------------------------------
        // POWER RX TX LED
        //------------------------------------------------------------------------
        // Definitions 
        
             // choose one Mode
               // All with one LED => LEDMode = 1
               // POWER seperate   => LEDMode = 2
               // only TX and RX   => LEDMode = 3 
               // 3 seperate LEDS  => LEDMode = 4
               // for a power LED, I would connect a LED between GND and VCC in general.
               
             const int LEDMode = 3; // 2 3 4 (0 is off)
             
             // Mode 1
             #define LEDPwrRXTXPin 4
             
             // Mode 2
             #define LEDPwrPin1 3
             #define LEDRXTXPin 4
             
             // Mode 3
             #define LEDRXPin 3
             #define LEDTXPin 4
             
             // Mode 4
             #define LEDPwrPin2 19
             #define LEDRXPin 3
             #define LEDTXPin 4
    
             int blinkRX = 0;
             int blinkTX = 0;
             
             
        
        void setup()  
        {  
        gw.begin(incomingMessage, AUTO, true);
        
        //------------------------------------------------------------------------
        // YOUR CODE
        //------------------------------------------------------------------------
        // VOID SETUP
        
    
        //------------------------------------------------------------------------
        // POWER RX TX LED
        //------------------------------------------------------------------------
        // VOID SETUP
        
        
             // All with one LED => LEDMode = 1
             if (LEDMode == 1){
               pinMode(LEDPwrRXTXPin, OUTPUT);
             }
               
             // POWER seperate => LEDMode = 2
             if (LEDMode == 2){
               pinMode(LEDPwrPin1, OUTPUT);
               pinMode(LEDRXTXPin, OUTPUT);
             }
               
             // only TX and RX => LEDMode = 3
             if (LEDMode == 3){
               pinMode(LEDRXPin, OUTPUT);
               pinMode(LEDTXPin, OUTPUT);
             }
               
             // 3 seperate LEDS => LEDMode = 4
             if (LEDMode == 4){
               pinMode(LEDPwrPin2, OUTPUT);
               pinMode(LEDTXPin, OUTPUT);
               pinMode(LEDRXPin, OUTPUT);
             }
             
    
        }
        
        
     void loop() 
      {
        gw.process();
        
        //------------------------------------------------------------------------
        // POWER RX TX LED
        //------------------------------------------------------------------------
        // VOID LOOP
        if (LEDMode == 1){
          digitalWrite(LEDPwrRXTXPin,HIGH);
          if (blinkTX == 1){
            digitalWrite(LEDPwrRXTXPin, LOW); // invert blink because power is already on
            delay(40);
            digitalWrite(LEDPwrRXTXPin, HIGH);
            blinkTX = 0;
          }
        }
        
        if (LEDMode == 2){
          digitalWrite(LEDPwrPin1,HIGH);
          digitalWrite(LEDRXTXPin,LOW);
          if (blinkTX == 1){
            digitalWrite(LEDRXTXPin, HIGH);
            delay(40);
            digitalWrite(LEDRXTXPin, LOW);
            blinkTX = 0;
          }
        }
        if (LEDMode == 3){
          digitalWrite(LEDRXPin,LOW);
          digitalWrite(LEDTXPin,LOW);
          if (blinkTX == 1){
            digitalWrite(LEDTXPin, HIGH);
            delay(40);
            digitalWrite(LEDTXPin, LOW);
            blinkTX = 0;
          }
        }
        if (LEDMode == 4){
          digitalWrite(LEDPwrPin2,HIGH);
          digitalWrite(LEDRXPin,LOW);
          digitalWrite(LEDTXPin,LOW);
          if (blinkTX == 1){
            digitalWrite(LEDTXPin, HIGH);
            delay(40);
            digitalWrite(LEDTXPin, LOW);
            blinkTX = 0;
          }
        }
        
        
        //------------------------------------------------------------------------
        // YOUR CODE
        //------------------------------------------------------------------------
        // VOID LOOP
        
        // !!! at  gw.send(msg.set...); there also needs to be a - blinkTX = 1; - !!!
    
      }
      
      
      void incomingMessage(const MyMessage &message) {
        
        //------------------------------------------------------------------------
        // POWER RX TX LED
        //------------------------------------------------------------------------
        // INCOMING MESSAGE
        if (LEDMode == 1){
          digitalWrite(LEDPwrRXTXPin, LOW);
          delay(40);
          digitalWrite(LEDPwrRXTXPin, HIGH);
        }
        if (LEDMode == 2){
          digitalWrite(LEDRXTXPin,HIGH);
          delay(40);
          digitalWrite(LEDRXTXPin, LOW);
        }
        if (LEDMode == 3){
          digitalWrite(LEDRXPin, HIGH);
          delay(40);
          digitalWrite(LEDRXPin, LOW);  
        }
        if (LEDMode == 4){
            digitalWrite(LEDRXPin, HIGH);
            delay(40);
            digitalWrite(LEDRXPin, LOW);
        }
      
        //------------------------------------------------------------------------
        // YOUR CODE
        //------------------------------------------------------------------------
        // INCOMING MESSAGE
    
      }
    

    Still missing is the ERROR. Is there a way to ask if there was a "fail" within the message?

    Thanks



  • +1 for the RX/TX LED support in code for node! 🙂


  • Contest Winner

    @andriej said:

    +1 for the RX/TX LED support in code for node! 🙂

    ++ to include the new RGB implementation along with this mod!



  • now without delay() but with LEDs are on for a number of loops (millis()) didn't work smoothly)

       #include <MySensor.h>
        #include <SPI.h>
        MySensor gw;
        
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // DEFINITIONS
        //-----------------------------------------------------------------------------------------------------------
        
          //------------------------------------------------------------------------
          // POWER RX TX LED
          //------------------------------------------------------------------------
          // Definitions 
          
               // choose one Mode
                 // All with one LED => LEDMode = 1
                 // POWER seperate   => LEDMode = 2
                 // only TX and RX   => LEDMode = 3 
                 // 3 seperate LEDS  => LEDMode = 4
                 // RX TX combined, no PWR LED  => LEDMode = 5
                 // for a power LED, I would connect a LED between GND and VCC in general.
                 
               const int LEDMode = 5; // 2 3 4 5 (0 is off)
               
               // does not need to be modified
               // Mode 1
               #define LEDPwrRXTXPin 4
               
               // Mode 2
               #define LEDPwrPin1 3
               #define LEDRXTXPin 4
               
               // Mode 3
               #define LEDRXPin 3
               #define LEDTXPin 4
               
               // Mode 4
               #define LEDPwrPin2 14
               #define LEDRXPin2 3
               #define LEDTXPin2 4
               
               // Mode 5
               #define LEDRXTXPin2 4
      
               int blinkRX = 0;
               int blinkTX = 0;
               int blinkLoopTX = 0;
               int blinkOffTX = 500; // number of Loops until blink is off
               int blinkLoopRX = 0;
               int blinkOffRX = 500; // number of Loops until blink is off
           
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // VOID SETUP - VOID SETUP - VOID SETUP - VOID SETUP - VOID SETUP
        //-----------------------------------------------------------------------------------------------------------
        
         void setup()  
        {  
          gw.begin(incomingMessage, AUTO, true);
              
          //------------------------------------------------------------------------
          // POWER RX TX LED
          //------------------------------------------------------------------------
          // VOID SETUP
              // does not need to be modified
               // All with one LED => LEDMode = 1
               if (LEDMode == 1){
                 pinMode(LEDPwrRXTXPin, OUTPUT);
               }
                 
               // POWER seperate => LEDMode = 2
               if (LEDMode == 2){
                 pinMode(LEDPwrPin1, OUTPUT);
                 pinMode(LEDRXTXPin, OUTPUT);
               }
                 
               // only TX and RX => LEDMode = 3
               if (LEDMode == 3){
                 pinMode(LEDRXPin, OUTPUT);
                 pinMode(LEDTXPin, OUTPUT);
               }
                 
               // 3 seperate LEDS => LEDMode = 4
               if (LEDMode == 4){
                 pinMode(LEDPwrPin2, OUTPUT);
                 pinMode(LEDTXPin, OUTPUT);
                 pinMode(LEDRXPin, OUTPUT);
               }
               
               // no POWERLEd only RX TX combined => LEDMode = 5
               if (LEDMode == 5){
                 pinMode(LEDRXTXPin2, OUTPUT);
               }
        }
       
        
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP
        //-----------------------------------------------------------------------------------------------------------
        
        //  Check if digital input has changed and send in new value
        void loop() 
        {
          gw.process();
          
          //------------------------------------------------------------------------
          // POWER RX TX LED
          //------------------------------------------------------------------------
          // VOID LOOP
          // does not need to be modified
          
          if (LEDMode == 1){
            if (blinkTX == 0 && blinkRX == 0){digitalWrite(LEDPwrRXTXPin,HIGH);}
            if (blinkTX == 1 || blinkRX == 1){
              digitalWrite(LEDRXTXPin, LOW);
              if (blinkLoopTX >= blinkOffTX){ blinkTX = 0; blinkRX = 0; blinkLoopTX = 0;}
              blinkLoopTX++;
            }
          }
          
          if (LEDMode == 2){
            digitalWrite(LEDPwrPin1,HIGH);
            if (blinkTX == 0 && blinkRX == 0){digitalWrite(LEDRXTXPin,LOW);}
            if (blinkTX == 1 || blinkRX == 1){
              digitalWrite(LEDRXTXPin, HIGH);
              if (blinkLoopTX >= blinkOffTX){ blinkTX = 0; blinkRX = 0; blinkLoopTX = 0;}
              blinkLoopTX++;
            }
          }
          
          if (LEDMode == 3){
            digitalWrite(LEDRXPin,LOW);
            if (blinkTX == 0){digitalWrite(LEDTXPin,LOW);}
            if (blinkTX == 1){
              digitalWrite(LEDTXPin, HIGH);
              if (blinkLoopTX >= blinkOffTX){ blinkTX = 0; blinkLoopTX = 0;}
              blinkLoopTX++;
            }
            if (blinkRX == 0){digitalWrite(LEDRXPin,LOW);}
            if (blinkRX == 1){
              digitalWrite(LEDRXPin, HIGH);
              if (blinkLoopRX >= blinkOffRX){ blinkRX = 0; blinkLoopRX = 0;}
              blinkLoopRX++;
            }
          }
          
          if (LEDMode == 4){
            digitalWrite(LEDPwrPin2,HIGH);
            if (blinkTX == 0){digitalWrite(LEDTXPin2,LOW);}
            if (blinkTX == 1){
              digitalWrite(LEDTXPin2, HIGH);
              if (blinkLoopTX >= blinkOffTX){ blinkTX = 0; blinkLoopTX = 0;}
              blinkLoopTX++;
            }
            if (blinkRX == 0){digitalWrite(LEDRXPin2,LOW);}
            if (blinkRX == 1){
              digitalWrite(LEDRXPin2, HIGH);
              if (blinkLoopRX >= blinkOffRX){ blinkRX = 0; blinkLoopRX = 0;}
              blinkLoopRX++;
            }
            
          }
          if (LEDMode == 5){
            if (blinkTX == 0 && blinkRX == 0){digitalWrite(LEDRXTXPin2,LOW);}
            if (blinkTX == 1 || blinkRX == 1){
              digitalWrite(LEDRXTXPin2, HIGH);
              if (blinkLoopTX >= blinkOffTX){ blinkTX = 0; blinkRX = 0; blinkLoopTX = 0;}
              blinkLoopTX++;
            }
          }
          
          //------------------------------------------------------------------------
          // YOUR CODE
          //------------------------------------------------------------------------
          // VOID LOOP
      
          // !!! at  gw.send(msg.set...); there also needs to be a - blinkTX = 1; - !!! 
          
        }
       
        //------------------------------------------------------------------------
        //------------------------------------------------------------------------
        //------------------------------------------------------------------------
        // VOID INCOMING MESSAGE - VOID INCOMING MESSAGE - VOID INCOMING MESSAGE
        //------------------------------------------------------------------------
        
        void incomingMessage(const MyMessage &message) {
          //------------------------------------------------------------------------
          // POWER RX TX LED
          //------------------------------------------------------------------------
          // INCOMING MESSAGE
          blinkRX = 1;
         
          //------------------------------------------------------------------------
          // YOUR CODE
          //------------------------------------------------------------------------
          // INCOMING MESSAGE
        
        }

  • Hero Member

    I use something similar mainly to show if messages are received. For this purpose I created a small class/ library to blink and time LED's. The class uses a similar syntax as Bounce2 for assigning and handling LED's attached to output pins. LedFlash.

    A code example:
    https://codebender.cc/sketch:85200


Log in to reply
 

Suggested Topics

  • 87
  • 5
  • 7
  • 8
  • 10
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts