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. Door sensor make USB disconnect while reading serial output?

Door sensor make USB disconnect while reading serial output?

Scheduled Pinned Locked Moved Development
19 Posts 4 Posters 2.2k 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.
  • G Offline
    G Offline
    Guilou
    wrote on last edited by
    #1

    Hello,

    I have two Arduino Nano and I just want to build a domotic system with Home Assistant and MySensors.
    So, I build a serial gateway with one Arduino, just by copy/pasting the documentation example. By reading the debug, I think it's quite good.

    For the sensor, my code is:

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    #define MY_BAUD_RATE 9600
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 10
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define CHILD_ID 3
    
    const int buttonPin = 3;    // the number of the pushbutton pin
    const int ledPin = 13;      // the number of the LED pin
    
    // Variables will change:
    int ledState = HIGH;         // the current state of the output pin
    int buttonState;             // the current reading from the input pin
    int lastButtonState = LOW;   // the previous reading from the input pin
    
    // the following variables are unsigned longs because the time, measured in
    // milliseconds, will quickly become a bigger number than can be stored in an int.
    unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
    unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
    
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup() {
      pinMode(buttonPin, INPUT);
      pinMode(ledPin, OUTPUT);
    
      // set initial LED state
      digitalWrite(ledPin, ledState);
    
      Serial.begin(MY_BAUD_RATE);
    }
    
    void presentation() {
      //sendSketchInfo("TestSketch",  "1.0");
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(CHILD_ID, S_DOOR);  
    }
    
    void loop() {
      // read the state of the switch into a local variable:
      int reading = digitalRead(buttonPin);
    
      // check to see if you just pressed the button
      // (i.e. the input went from LOW to HIGH), and you've waited long enough
      // since the last press to ignore any noise:
    
      // If the switch changed, due to noise or pressing:
      if (reading != lastButtonState) {
        // reset the debouncing timer
        lastDebounceTime = millis();
      }
    
      if ((millis() - lastDebounceTime) > debounceDelay) {
        // whatever the reading is at, it's been there for longer than the debounce
        // delay, so take it as the actual current state:
    
        // if the button state has changed:
        if (reading != buttonState) {
          buttonState = reading;
    
          // only toggle the LED if the new button state is HIGH
          if (buttonState == HIGH) {
            //send(msg.set(buttonState == HIGH ? 1 : 0));
            
            delay(1000);
            ledState = !ledState;
            Serial.print("State = ");
            Serial.println(ledState);
            send(msg.set(ledState));
          }
        }
      }
    
      // set the LED:
      digitalWrite(ledPin, ledState);
    
      // save the reading. Next time through the loop, it'll be the lastButtonState:
      lastButtonState = reading;
    }
    

    The serial output is like this.

    But something weird happens just after this serial reading. The USB seems to be disconnected and then reconnect to another ttyUSB. So I can't read all the serial at once. I tried to lower the baud rate, as you can see, but there is still the bug.

    I tried to swap Arduino, change USB port, change USB cable and there is still this strange behaviour.
    Do you know why?

    rejoe2R 1 Reply Last reply
    0
    • G Guilou

      Hello,

      I have two Arduino Nano and I just want to build a domotic system with Home Assistant and MySensors.
      So, I build a serial gateway with one Arduino, just by copy/pasting the documentation example. By reading the debug, I think it's quite good.

      For the sensor, my code is:

      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      #define MY_BAUD_RATE 9600
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #define MY_NODE_ID 10
      
      #include <SPI.h>
      #include <MySensors.h>
      
      #define CHILD_ID 3
      
      const int buttonPin = 3;    // the number of the pushbutton pin
      const int ledPin = 13;      // the number of the LED pin
      
      // Variables will change:
      int ledState = HIGH;         // the current state of the output pin
      int buttonState;             // the current reading from the input pin
      int lastButtonState = LOW;   // the previous reading from the input pin
      
      // the following variables are unsigned longs because the time, measured in
      // milliseconds, will quickly become a bigger number than can be stored in an int.
      unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
      unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
      
      MyMessage msg(CHILD_ID, V_TRIPPED);
      
      void setup() {
        pinMode(buttonPin, INPUT);
        pinMode(ledPin, OUTPUT);
      
        // set initial LED state
        digitalWrite(ledPin, ledState);
      
        Serial.begin(MY_BAUD_RATE);
      }
      
      void presentation() {
        //sendSketchInfo("TestSketch",  "1.0");
        // Register binary input sensor to gw (they will be created as child devices)
        // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
        // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
        present(CHILD_ID, S_DOOR);  
      }
      
      void loop() {
        // read the state of the switch into a local variable:
        int reading = digitalRead(buttonPin);
      
        // check to see if you just pressed the button
        // (i.e. the input went from LOW to HIGH), and you've waited long enough
        // since the last press to ignore any noise:
      
        // If the switch changed, due to noise or pressing:
        if (reading != lastButtonState) {
          // reset the debouncing timer
          lastDebounceTime = millis();
        }
      
        if ((millis() - lastDebounceTime) > debounceDelay) {
          // whatever the reading is at, it's been there for longer than the debounce
          // delay, so take it as the actual current state:
      
          // if the button state has changed:
          if (reading != buttonState) {
            buttonState = reading;
      
            // only toggle the LED if the new button state is HIGH
            if (buttonState == HIGH) {
              //send(msg.set(buttonState == HIGH ? 1 : 0));
              
              delay(1000);
              ledState = !ledState;
              Serial.print("State = ");
              Serial.println(ledState);
              send(msg.set(ledState));
            }
          }
        }
      
        // set the LED:
        digitalWrite(ledPin, ledState);
      
        // save the reading. Next time through the loop, it'll be the lastButtonState:
        lastButtonState = reading;
      }
      

      The serial output is like this.

      But something weird happens just after this serial reading. The USB seems to be disconnected and then reconnect to another ttyUSB. So I can't read all the serial at once. I tried to lower the baud rate, as you can see, but there is still the bug.

      I tried to swap Arduino, change USB port, change USB cable and there is still this strange behaviour.
      Do you know why?

      rejoe2R Offline
      rejoe2R Offline
      rejoe2
      wrote on last edited by
      #2

      @guilou First, I can not see the serial output you tried to link, the log parser page remains blank.

      Do you also see this kind of rebooting behaviour, if you diconnect the arduino and then reconnect it without reflashing the sketch? If it then works and the Nano is equipped with a FTDI USB-serial-chip, it could be a not connected PIN of the FTDI.

      Otherwise this could be a powering issue (not enough juice for sending).

      In case this doesn't help, you might add info about IDE Version and Board Definition Version (IDE: Board Manager) of the Nano you use.

      Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Guilou
        wrote on last edited by
        #3

        @rejoe2 said in Door sensor make USB disconnect while reading serial output?:

        @guilou First, I can not see the serial output you tried to link, the log parser page remains blank.

        That is weird, on my computer all links are ok.

        Anyway, I tried to disconnect/reconnect the Arduino without reflashing the sketch, and I have still the same behaviour: the output stops, and at the same time the Arduino change its port…

        26773 TSF:MSG:SEND,10-10-255-2550 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
        41 TSM:INIT
        55 TSF:WUR:MS=0
        76 TSM:INIT:TSP OK
        97 TSM:INIT:STATID=10
        119 TSF:SID:OK,ID=10
        14
        

        The "14" on a single like lets me think the serial communication is interrupted at this exact point.

        I tried other cables and power source without any success.

        sundberg84S 1 Reply Last reply
        0
        • G Guilou

          @rejoe2 said in Door sensor make USB disconnect while reading serial output?:

          @guilou First, I can not see the serial output you tried to link, the log parser page remains blank.

          That is weird, on my computer all links are ok.

          Anyway, I tried to disconnect/reconnect the Arduino without reflashing the sketch, and I have still the same behaviour: the output stops, and at the same time the Arduino change its port…

          26773 TSF:MSG:SEND,10-10-255-2550 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
          41 TSM:INIT
          55 TSF:WUR:MS=0
          76 TSM:INIT:TSP OK
          97 TSM:INIT:STATID=10
          119 TSF:SID:OK,ID=10
          14
          

          The "14" on a single like lets me think the serial communication is interrupted at this exact point.

          I tried other cables and power source without any success.

          sundberg84S Offline
          sundberg84S Offline
          sundberg84
          Hardware Contributor
          wrote on last edited by sundberg84
          #4

          @guilou I would suspect a power issue, when the node draws to much current (ie. radio transmits) there is a voltage drop and reboot/freeze. Do you have capacitors installed on the radio and/or voltage regulator? What kind of hardware do you use?

          The Nano is also 5v so you need to regulate voltage down for VCC and datalines if you use a nrf or rfm radio.

          Controller: Proxmox VM - Home Assistant
          MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
          MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
          RFLink GW - Arduino Mega + RFLink Shield, 433mhz

          G 1 Reply Last reply
          0
          • rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #5

            Also this is version 2.1.1.
            Afaik RFM69 is more stable in the dev Version. You may try if things get better there...

            Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

            1 Reply Last reply
            0
            • sundberg84S sundberg84

              @guilou I would suspect a power issue, when the node draws to much current (ie. radio transmits) there is a voltage drop and reboot/freeze. Do you have capacitors installed on the radio and/or voltage regulator? What kind of hardware do you use?

              The Nano is also 5v so you need to regulate voltage down for VCC and datalines if you use a nrf or rfm radio.

              G Offline
              G Offline
              Guilou
              wrote on last edited by
              #6

              @sundberg84 I use Arduino Nano V3, breadboards and NRF24L01+ with a 4.7 µF capacitor. For debugging, a use USB port from my computer or RaspberryPI. I tried with a USB charger for cellphones, and nothing seems to change (but I am blind without serial :smile: )

              About software, I use Arduino IDE v1.8.5 and MySensors v2.1.1.

              sundberg84S 2 Replies Last reply
              0
              • G Guilou

                @sundberg84 I use Arduino Nano V3, breadboards and NRF24L01+ with a 4.7 µF capacitor. For debugging, a use USB port from my computer or RaspberryPI. I tried with a USB charger for cellphones, and nothing seems to change (but I am blind without serial :smile: )

                About software, I use Arduino IDE v1.8.5 and MySensors v2.1.1.

                sundberg84S Offline
                sundberg84S Offline
                sundberg84
                Hardware Contributor
                wrote on last edited by
                #7

                @guilou - everything seems fine except that nrf24l01+ is not 5v tolerant (according to specs - sometimes it works!). I recommend to exclude this problem by using a logic level converter for the datalines since the nano sends 5v over all datalines and the nrf accepts 3.3 this could be a problem.

                Controller: Proxmox VM - Home Assistant
                MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                1 Reply Last reply
                0
                • G Guilou

                  @sundberg84 I use Arduino Nano V3, breadboards and NRF24L01+ with a 4.7 µF capacitor. For debugging, a use USB port from my computer or RaspberryPI. I tried with a USB charger for cellphones, and nothing seems to change (but I am blind without serial :smile: )

                  About software, I use Arduino IDE v1.8.5 and MySensors v2.1.1.

                  sundberg84S Offline
                  sundberg84S Offline
                  sundberg84
                  Hardware Contributor
                  wrote on last edited by sundberg84
                  #8

                  Sorry, brainfreeze - 5v dataline should be fine... confused with the RFM radio...

                  0_1515589653051_90c5e495-d594-4c5f-9263-00deca6c3483-image.png

                  Controller: Proxmox VM - Home Assistant
                  MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                  MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                  RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                  1 Reply Last reply
                  2
                  • G Offline
                    G Offline
                    Guilou
                    wrote on last edited by
                    #9

                    I use the 3v3 output of the Arduino to power the radio module :)

                    sundberg84S 1 Reply Last reply
                    0
                    • G Guilou

                      I use the 3v3 output of the Arduino to power the radio module :)

                      sundberg84S Offline
                      sundberg84S Offline
                      sundberg84
                      Hardware Contributor
                      wrote on last edited by
                      #10

                      @guilou - should work just fine..!
                      But still the behaviour smells like a power issue if you use the standard sketch... did you try another radio and or nano?

                      Controller: Proxmox VM - Home Assistant
                      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                      G 1 Reply Last reply
                      0
                      • sundberg84S sundberg84

                        @guilou - should work just fine..!
                        But still the behaviour smells like a power issue if you use the standard sketch... did you try another radio and or nano?

                        G Offline
                        G Offline
                        Guilou
                        wrote on last edited by
                        #11

                        @sundberg84 I swapped Nano between the gateway and the sensor and this bug always occurs with the sensor sketch. I have spare radio modules, so I will try with another one, thank you for this suggestion ;)

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          Guilou
                          wrote on last edited by
                          #12

                          Hello,

                          I tried with two spares modules and I am still stuck with this curious behaviour.

                          1 Reply Last reply
                          0
                          • hekH Offline
                            hekH Offline
                            hek
                            Admin
                            wrote on last edited by
                            #13

                            Did you try adding a larger capacitor on the radio?

                            G 1 Reply Last reply
                            0
                            • hekH hek

                              Did you try adding a larger capacitor on the radio?

                              G Offline
                              G Offline
                              Guilou
                              wrote on last edited by
                              #14

                              @hek said in Door sensor make USB disconnect while reading serial output?:

                              Did you try adding a larger capacitor on the radio?

                              Yes, but still the same.

                              I grab another Arduino Nano (not sure it's a guenuine one) and the serial output is correct! The sensor can not find the gateway, but this is another story :)

                              Do you know why some Arduino Nano V3 "compatible" will not work?

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                Guilou
                                wrote on last edited by
                                #15

                                Hey there,

                                How can I know which Arduino Nano are good for MySensors?
                                Mine are from Banggood and the serial output seems to be chaotic.

                                I have one from Gravitech and it seems to be ok…

                                How to choose? In the MySensors store, this is clone form Asia like mine from Banggood…

                                1 Reply Last reply
                                0
                                • rejoe2R Offline
                                  rejoe2R Offline
                                  rejoe2
                                  wrote on last edited by
                                  #16

                                  So far, most of the cheap clones I use (or used) seemed to work fine on USB side, so most likely there is no real "rule".
                                  In case your problem is related to powering, you may replace the nano by a combination of CP2102 USB-to-serial converter and a pro mini or use a Pro Micro. The CP2102 also provides more current@3.3V (up to 100mA) and (similar to original FTDI's) allows programming of serial numbers and Product ID's to uniquely identify them in /dev/serial/by-id.

                                  Your problem report also reminds me to problems other users had with their LAN-GW's. For them afaik downgrading the board definition for the nanos (not the IDE) to version <=1.6.11 helped a lot. You might give that a try...

                                  Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                                  G 1 Reply Last reply
                                  0
                                  • rejoe2R rejoe2

                                    So far, most of the cheap clones I use (or used) seemed to work fine on USB side, so most likely there is no real "rule".
                                    In case your problem is related to powering, you may replace the nano by a combination of CP2102 USB-to-serial converter and a pro mini or use a Pro Micro. The CP2102 also provides more current@3.3V (up to 100mA) and (similar to original FTDI's) allows programming of serial numbers and Product ID's to uniquely identify them in /dev/serial/by-id.

                                    Your problem report also reminds me to problems other users had with their LAN-GW's. For them afaik downgrading the board definition for the nanos (not the IDE) to version <=1.6.11 helped a lot. You might give that a try...

                                    G Offline
                                    G Offline
                                    Guilou
                                    wrote on last edited by
                                    #17

                                    @rejoe2 said in Door sensor make USB disconnect while reading serial output?:

                                    Your problem report also reminds me to problems other users had with their LAN-GW's. For them afaik downgrading the board definition for the nanos (not the IDE) to version <=1.6.11 helped a lot. You might give that a try...

                                    Ok, I don't really understand yet what is the board definition version, but I will try. Thank you :)

                                    rejoe2R 1 Reply Last reply
                                    0
                                    • G Guilou

                                      @rejoe2 said in Door sensor make USB disconnect while reading serial output?:

                                      Your problem report also reminds me to problems other users had with their LAN-GW's. For them afaik downgrading the board definition for the nanos (not the IDE) to version <=1.6.11 helped a lot. You might give that a try...

                                      Ok, I don't really understand yet what is the board definition version, but I will try. Thank you :)

                                      rejoe2R Offline
                                      rejoe2R Offline
                                      rejoe2
                                      wrote on last edited by
                                      #18

                                      @guilou said in Door sensor make USB disconnect while reading serial output?:

                                      Ok, I don't really understand yet what is the board definition version, but I will try. Thank you :)

                                      See Tools - Board - Boards Manager. If you click on the Board Definitions listed (here: for Arduino Nano), a dropdown with all the versions available will appear - the function is similar to the Library Manager.

                                      Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        Guilou
                                        wrote on last edited by
                                        #19

                                        Hey,

                                        Just for information: I solved all my problem ny soldering. Don't know if this is the breadboard or the Dupont wire jumpers, but with wires soldered, everything is working now :)

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


                                        25

                                        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