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. RS485/RS232/Serial transport class for mysensors.org

RS485/RS232/Serial transport class for mysensors.org

Scheduled Pinned Locked Moved Development
rs485 serialrs485
143 Posts 27 Posters 102.9k Views 27 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.
  • tbowmoT Offline
    tbowmoT Offline
    tbowmo
    Admin
    wrote on last edited by
    #21

    @LeoDesigner

    If using the standard bootloader, wouldn't it break the FW update if one of the other nodes on the bus starts to transmit?

    L 1 Reply Last reply
    0
    • tbowmoT tbowmo

      @LeoDesigner

      If using the standard bootloader, wouldn't it break the FW update if one of the other nodes on the bus starts to transmit?

      L Offline
      L Offline
      LeoDesigner
      wrote on last edited by
      #22

      @tbowmo
      Node will listen RS485 bus before it will try to transmit anything (just like Ethernet).
      So other nodes will behave yourself :)
      But we have another difficulty: standard bootloader know nothing about TX/RX (dePin) management. So, we need to modify it to be able correctly emulate Arduino upload process. Maybe a simple schematics with diode will work in that case.
      I am still busy with other improvements.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TimO
        Hero Member
        wrote on last edited by
        #23

        @hek I've tested your code and can't get it working yet.

        I've installed the gateway and a DHT22 sketch on two nanos and connected them directly via D8 and D9.

        // Enable RS485 transport layer
        #define MY_RS485
        
        // Set RS485 baud rate to use
        #define MY_RS485_BAUD_RATE 9600
        
        
        #include <MySensor.h>
        #include <DHT.h>  
        
        #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 1
        #define HUMIDITY_SENSOR_DIGITAL_PIN 3
        #define RS485_DE_PIN 4
        unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds)
        
        // Initialize motion message
        DHT dht;
        boolean metric = true; 
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        
        void setup()  
        {  
          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
        }
        
        void presentation()  {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("Humidity Sensor", "1.0");
        
          // Register all sensors to gw (they will be created as child devices)
          present(CHILD_ID_HUM, S_HUM);
          present(CHILD_ID_TEMP, S_TEMP);
        }
        
        void loop()     
        {     
          float temperature = dht.getTemperature();
          if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT");
          } else  {
            send(msgTemp.set(temperature, 1));
            Serial.print("T: ");
            Serial.println(temperature);
          }
          
          float humidity = dht.getHumidity();
          if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
          } else {
              send(msgHum.set(humidity, 1));
              Serial.print("H: ");
              Serial.println(humidity);
          }
          // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
          sleep(SLEEP_TIME);
        }
        

        The sketch is working fine according to serial output. But nothing is seen on the gateway.

        So, to find out, where there is a problem, I connected my Programmer (mySmartUSB MK3) to D8/D9 of the sensor and I'm able to see that there is output but the chars are not readable so I tried different baudrates than 9600. That didn' help. What I can say is, that the output is generated every 3 seconds, so the ports are right.

        To make sure I'm not doing something wrong with the programmer, I connected it to D0/D1 and switched baudrate to 115200 and it shows the serial output as expected.

        Any idea where to look next?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TimO
          Hero Member
          wrote on last edited by
          #24

          So, I've tried to further invest the problem and there is something wrong with _dev.write() in transportSend(). It is too much C-Kungfu for me but there is something wrong with the conversion of data.

          • _dev.write("bla") looks good in terminal
          • _dev.write(to) throws trash in the terminal
          • Serial.println(to) looks good in terminal

          Here is the output of the soft-serial:

          #���X��z
                         �Humidity Sensor��X
          ��
               �1.0�X��b�X��b�X
                                               ��*�������X
                                                               ��*�gfB�X
                                                                              ��*�������X
              ��*�gfB�X
                             
          

          Another thing:

          The management of the DE-Pin is missing right?

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

            to is a uint_t...so I guess it could generate trash in the terminal when showing the low ASCII values. Guess you'd have to use some other program to view the bytes sent.

            Yes, de-pin was stripped away. Would it be useful in your setup?

            (thankful for your help out testing it... I need some more time before I can setup a test rig)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TimO
              Hero Member
              wrote on last edited by
              #26

              Holy crap ...
              It was too obvious. :-(

              Gateway can't work with this code:
              https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewaySerialRS485/GatewaySerialRS485.ino

              #define MY_DEFAULT_ERR_LED_PIN 7  // Error led pin
              #define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
              #define MY_DEFAULT_TX_LED_PIN  9  // the PCB, on board LED
              

              @hek: Your code works!

              The de-pin is set to high if a node wants to send something, otherwise it is set to ground.
              I'll try to add that code.

              hekH 1 Reply Last reply
              0
              • T TimO

                Holy crap ...
                It was too obvious. :-(

                Gateway can't work with this code:
                https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewaySerialRS485/GatewaySerialRS485.ino

                #define MY_DEFAULT_ERR_LED_PIN 7  // Error led pin
                #define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
                #define MY_DEFAULT_TX_LED_PIN  9  // the PCB, on board LED
                

                @hek: Your code works!

                The de-pin is set to high if a node wants to send something, otherwise it is set to ground.
                I'll try to add that code.

                hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by hek
                #27

                @TimO

                :blush: I Sorry.. should have changed those pin assignments of course... I'll push an update ASAP.

                L 1 Reply Last reply
                0
                • hekH hek

                  @TimO

                  :blush: I Sorry.. should have changed those pin assignments of course... I'll push an update ASAP.

                  L Offline
                  L Offline
                  LeoDesigner
                  wrote on last edited by
                  #28

                  @hek
                  I am wondering why did you remove the code responsible for dePin management ?
                  It's not possible to use MAX485 boards without tx/rx management.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TimO
                    Hero Member
                    wrote on last edited by
                    #29

                    @LeoDesigner : yeah, you're right, that is needed.

                    But there seems to be another problem, I can't get the current development code to work. I've simply interconnected two Nanos at D8/D9. The gateway and the motion example come up fine according to serial output and the motion sensor definiatly sends something via AltSerial but the gateway doesn't seem to recognize it.

                    A simple test, where I pipe the input on hardware serial of one nano to AltSerial on the second nano and from there to hardware serial works just fine, so hardware and AltSerial is working.

                    Sender:

                    #include <AltSoftSerial.h>
                    
                    AltSoftSerial altSerial;
                    
                    void setup() {
                      Serial.begin(115200);
                      Serial.println("Demo begins");
                    
                      altSerial.begin(115200);
                    
                    }
                    
                    void loop() {
                      char c;
                    
                      if (Serial.available()) {
                        c = Serial.read();
                        altSerial.print(c);
                      }
                    
                    }
                    

                    Receiver:

                    #include <AltSoftSerial.h>
                    
                    AltSoftSerial altSerial;
                    
                    void setup() {
                      Serial.begin(115200);
                      Serial.println("Demo begins");
                    
                      altSerial.begin(115200);
                    
                    }
                    
                    void loop() {
                      char c;
                    
                      if (altSerial.available()) {
                        c = altSerial.read();
                        Serial.print(c);
                      }
                    
                    }
                    
                    antiA 1 Reply Last reply
                    1
                    • hekH Offline
                      hekH Offline
                      hek
                      Admin
                      wrote on last edited by
                      #30

                      Added DE-pin management

                      https://github.com/mysensors/Arduino/pull/266

                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        shabba
                        wrote on last edited by
                        #31

                        I am very interested in doing this as I have many sensors local to my gateway. I use domoticz and see from http://www.domoticz.com/forum/viewtopic.php?f=6&t=8018 that people are doing this. I was just wondering would this do as a USB adaptor? Seems very cheap :

                        http://www.aliexpress.com/item/1-pcs-USB-to-RS485-USB-485-Converter-Adapter-Support-Win7-XP-Vista-Linux-for-Mac/1684582905.html?spm=2114.031010208.3.1.hHoz94&ws_ab_test=searchweb201556_10_79_78_77_80_61,searchweb201644_0,searchweb201560_9

                        Also was looking at :
                        http://www.aliexpress.com/item/FREE-SHIPPING-5PCS-LOT-MAX485-module-RS485-module-TTL-turn-RS-485-module-MCU-development-accessories/1718665649.html

                        Appreciate any feedback.

                        Thanks.

                        1 Reply Last reply
                        0
                        • L LeoDesigner

                          Hi everyone !

                          I needed a wired solution for my several nodes.
                          Here is the serial rs485/rs232 wired network transport for mysensors.
                          https://github.com/leodesigner/mysensors-serial-transport
                          It is based on the Majenko ICSC serial library.
                          Can you please test it? It is a beta version - but it is working for me.
                          (However, I am still waiting for my rs485 boards to arrive)
                          You can find more technical information at
                          http://sourceforge.net/p/arduino-icsc/wiki/RS-485/

                          To use it, you have to:

                          1. Put SerialTransport.cpp and SerialTransport.h to folder/directory/path SerialTransport in your library.
                          2. Add #include <SerialTransport.h> to your .ino sketch
                          3. Replace transport class with:
                            MyTransportSerial transport(Serial,0,-1); // serial port, node, dePin (-1 disabled)

                          Please let me know about bugs and how it is working for you.

                          M Offline
                          M Offline
                          MarkV
                          wrote on last edited by
                          #32

                          @LeoDesigner

                          I'm searching for a usb transport class, could i use this??

                          I'd like to connect my arduino (with sensors/actuators connected to it) to my rasberrypi through usb, without the wireless part, but still see them as mysensors sensors.

                          Is this possible or could someone make a transport class for it???

                          L 1 Reply Last reply
                          0
                          • M MarkV

                            @LeoDesigner

                            I'm searching for a usb transport class, could i use this??

                            I'd like to connect my arduino (with sensors/actuators connected to it) to my rasberrypi through usb, without the wireless part, but still see them as mysensors sensors.

                            Is this possible or could someone make a transport class for it???

                            L Offline
                            L Offline
                            LeoDesigner
                            wrote on last edited by LeoDesigner
                            #33

                            @MarkV
                            Regarding the USB to RS485 adapters:
                            I think they are using a some kind of serial to USB chip like CH340/341. So your RPi will be able to see RS485 bus as the regular serial port. It may work. It worth trying, you can always build an Ethernet GW later in other case.

                            @shabba
                            I am using exactly the same MAX485 modules. It is much more reliable media comparing to radio communication.

                            @hek
                            Thanks for adding DE pin management code.

                            1 Reply Last reply
                            0
                            • scalzS Offline
                              scalzS Offline
                              scalz
                              Hardware Contributor
                              wrote on last edited by
                              #34

                              hi.

                              I am not sure if it can be useful.. I have read this note recently, about rs485 powering chip (3.3v or 5v). And I found this interesting, for knowledge..
                              https://e2e.ti.com/support/interface/industrial_interface/f/142/t/93260
                              So what I understand here, and it makes sense, is that : even if some rs485 chips are able to be powered with 3.3v or 5v, if you want to power it with 3.3v, it can work of course with a 5v chip, but it is better to use a true 3.3v chip only. Because they are more sensitive to be rs485 compliant (better internal transistor).

                              Always good to know :smiley:

                              1 Reply Last reply
                              0
                              • T TimO

                                @LeoDesigner : yeah, you're right, that is needed.

                                But there seems to be another problem, I can't get the current development code to work. I've simply interconnected two Nanos at D8/D9. The gateway and the motion example come up fine according to serial output and the motion sensor definiatly sends something via AltSerial but the gateway doesn't seem to recognize it.

                                A simple test, where I pipe the input on hardware serial of one nano to AltSerial on the second nano and from there to hardware serial works just fine, so hardware and AltSerial is working.

                                Sender:

                                #include <AltSoftSerial.h>
                                
                                AltSoftSerial altSerial;
                                
                                void setup() {
                                  Serial.begin(115200);
                                  Serial.println("Demo begins");
                                
                                  altSerial.begin(115200);
                                
                                }
                                
                                void loop() {
                                  char c;
                                
                                  if (Serial.available()) {
                                    c = Serial.read();
                                    altSerial.print(c);
                                  }
                                
                                }
                                

                                Receiver:

                                #include <AltSoftSerial.h>
                                
                                AltSoftSerial altSerial;
                                
                                void setup() {
                                  Serial.begin(115200);
                                  Serial.println("Demo begins");
                                
                                  altSerial.begin(115200);
                                
                                }
                                
                                void loop() {
                                  char c;
                                
                                  if (altSerial.available()) {
                                    c = altSerial.read();
                                    Serial.print(c);
                                  }
                                
                                }
                                
                                antiA Offline
                                antiA Offline
                                anti
                                wrote on last edited by
                                #35

                                @TimO I cloned the current development branch, and tried to use a modified motion sketch, and a gateway sketch, simply interconnecting two mini-pros with TTL serial : the same problem appears.

                                I can only see garbage sent on the AltSerial pins, so the gateway don't see the message.

                                Before I dig more and start debugging, did anybody made progress on that problem ?

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  TimO
                                  Hero Member
                                  wrote on last edited by
                                  #36

                                  @anti: I'm glad, I'm not the only one! :-)
                                  I've tried to find the problem, but had no luck.

                                  What is bugging me: it was working with a early version, before it was merged into development, but I don't see any changes.

                                  I've not tested the latest / current version though.

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andriej
                                    wrote on last edited by andriej
                                    #37

                                    Hi!

                                    Glad to see 'wired' version of MySensor protocol coming.
                                    One question - how long wires and how many sensors on one pair/multiple pairs will be supported?

                                    I'm thinking about cabling for new home, which means many meters of cable to every sensor/door/actuator/ligth/meter etc.

                                    In best scenario - one cat5e - one termination (sensor/node/etc.)
                                    Currently I'm looking for a cabled version of protocol to support it and be 101% stable (no hiccups and freezes over the protocol), so I can rely on my home.

                                    MySensors seems to be great to implement, but I'm worried about the signal... I was thinking about letting something around 12-24-30V thru one pair of Cat5 to power all nodes from one power-source (with backup batteries).

                                    Or - are there any better alternatives for RS485, that doesn't need resistors, have no limitations of 32 devices etc?

                                    :-)

                                    L 1 Reply Last reply
                                    0
                                    • A andriej

                                      Hi!

                                      Glad to see 'wired' version of MySensor protocol coming.
                                      One question - how long wires and how many sensors on one pair/multiple pairs will be supported?

                                      I'm thinking about cabling for new home, which means many meters of cable to every sensor/door/actuator/ligth/meter etc.

                                      In best scenario - one cat5e - one termination (sensor/node/etc.)
                                      Currently I'm looking for a cabled version of protocol to support it and be 101% stable (no hiccups and freezes over the protocol), so I can rely on my home.

                                      MySensors seems to be great to implement, but I'm worried about the signal... I was thinking about letting something around 12-24-30V thru one pair of Cat5 to power all nodes from one power-source (with backup batteries).

                                      Or - are there any better alternatives for RS485, that doesn't need resistors, have no limitations of 32 devices etc?

                                      L Offline
                                      L Offline
                                      LeoDesigner
                                      wrote on last edited by LeoDesigner
                                      #38

                                      @andriej
                                      According to: https://en.wikipedia.org/wiki/RS-485
                                      "It offers data transmission speeds of 35 Mbit/s up to 10 m and 100 kbit/s at 1200 m. "
                                      also check this: https://www.maximintegrated.com/en/app-notes/index.mvp/id/367
                                      You may design a bus with up to 256 nodes.
                                      CAT5 is the best choice anyway for any type of communication (I mean you can run regular Ethernet on them too).
                                      Check the video at the beginning of the post. It's a working example of two remote nodes connected only with one CAT5 cable over 50m. They are working at 115200 - however speed might be up to 1 Mbit/s.

                                      A 1 Reply Last reply
                                      0
                                      • L LeoDesigner

                                        @andriej
                                        According to: https://en.wikipedia.org/wiki/RS-485
                                        "It offers data transmission speeds of 35 Mbit/s up to 10 m and 100 kbit/s at 1200 m. "
                                        also check this: https://www.maximintegrated.com/en/app-notes/index.mvp/id/367
                                        You may design a bus with up to 256 nodes.
                                        CAT5 is the best choice anyway for any type of communication (I mean you can run regular Ethernet on them too).
                                        Check the video at the beginning of the post. It's a working example of two remote nodes connected only with one CAT5 cable over 50m. They are working at 115200 - however speed might be up to 1 Mbit/s.

                                        A Offline
                                        A Offline
                                        andriej
                                        wrote on last edited by
                                        #39

                                        @LeoDesigner but how would I connect all sensors in house and relays too?
                                        SOme resistors needed? Star topology? Few gateways?

                                        :-)

                                        L 1 Reply Last reply
                                        0
                                        • A andriej

                                          @LeoDesigner but how would I connect all sensors in house and relays too?
                                          SOme resistors needed? Star topology? Few gateways?

                                          L Offline
                                          L Offline
                                          LeoDesigner
                                          wrote on last edited by
                                          #40

                                          @andriej
                                          The star topology could be a problem. However for low speed nodes this may work anyway.
                                          You may need something like RS485 hub :) or a GW with many RS485 bus ports.
                                          Or just a separate MAX485 board for each node. You need to test it.

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


                                          9

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 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