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.
  • elmarculinoE Offline
    elmarculinoE Offline
    elmarculino
    wrote on last edited by
    #83

    @hek None. Works out of the box. But I could not make it work with Home Assistant.

    Pin 9 >>> DI
    Pin 8 >>> RO
    Pin 2 >>> DE and RE

    INFO:mysensors.mysensors:/dev/ttyUSB0 is open...
    INFO:mysensors.mysensors:Connected to /dev/ttyUSB0
    INFO:mysensors.mysensors:n:0 c:255 t:3 s:9 p:read: 1-1-0 s=0,c=1,
    WARNING:mysensors.mysensors:Error decoding message from gateway, probably received bad byte.
    WARNING:mysensors.mysensors:Error decoding message from gateway, probably received bad byte.
    WARNING:mysensors.mysensors:Error decoding message from gateway, probably received bad byte.```
    1 Reply Last reply
    0
    • elmarculinoE Offline
      elmarculinoE Offline
      elmarculino
      wrote on last edited by
      #84

      @LeoDesigner My 1.5.2 RS485 Humidity sensor send the same messages as the example Humidity sketch, but shows a lot of 'X' and '?' characters at 115200 in Serial Console.

      The console with the example Humidity sketch is clean:

      sensor started, id=1, parent=0, distance=1
      send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=8,sg=0,st=ok:Humidity
      send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
      send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
      send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=ok:
      send: 1-1-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:26.0
      T: 26.00
      send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:44.0
      H: 44.00
      

      Do you know what can be causing it? Am I doing anything wrong, again?

      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      #include <MyHwATMega328.h>
      #include <SerialTransport.h>
      
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      #define HUMIDITY_SENSOR_DIGITAL_PIN 3
      unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
      
      MyTransportSerial transport(Serial,5,2); 
      MyHwATMega328 hw;
      MySensor gw(transport, hw);
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      
      
      void setup()  
      { 
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("Humidity", "1.0");
      
        // Register all sensors to gw (they will be created as child devices)
        gw.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      {  
        delay(dht.getMinimumSamplingPeriod());
      
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
            Serial.println("Failed reading temperature from DHT");
        } else if (temperature != lastTemp) {
          lastTemp = temperature;
          if (!metric) {
            temperature = dht.toFahrenheit(temperature);
          }
          gw.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 if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
      
        gw.sleep(SLEEP_TIME); //sleep a bit
      }
      
      L 1 Reply Last reply
      0
      • elmarculinoE elmarculino

        @LeoDesigner My 1.5.2 RS485 Humidity sensor send the same messages as the example Humidity sketch, but shows a lot of 'X' and '?' characters at 115200 in Serial Console.

        The console with the example Humidity sketch is clean:

        sensor started, id=1, parent=0, distance=1
        send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=8,sg=0,st=ok:Humidity
        send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
        send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
        send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=ok:
        send: 1-1-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:26.0
        T: 26.00
        send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:44.0
        H: 44.00
        

        Do you know what can be causing it? Am I doing anything wrong, again?

        #include <SPI.h>
        #include <MySensor.h>  
        #include <DHT.h>  
        #include <MyHwATMega328.h>
        #include <SerialTransport.h>
        
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 1
        #define HUMIDITY_SENSOR_DIGITAL_PIN 3
        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
        
        MyTransportSerial transport(Serial,5,2); 
        MyHwATMega328 hw;
        MySensor gw(transport, hw);
        DHT dht;
        float lastTemp;
        float lastHum;
        boolean metric = true; 
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        
        
        void setup()  
        { 
          gw.begin();
          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        
          // Send the Sketch Version Information to the Gateway
          gw.sendSketchInfo("Humidity", "1.0");
        
          // Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID_HUM, S_HUM);
          gw.present(CHILD_ID_TEMP, S_TEMP);
          
          metric = gw.getConfig().isMetric;
        }
        
        void loop()      
        {  
          delay(dht.getMinimumSamplingPeriod());
        
          float temperature = dht.getTemperature();
          if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT");
          } else if (temperature != lastTemp) {
            lastTemp = temperature;
            if (!metric) {
              temperature = dht.toFahrenheit(temperature);
            }
            gw.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 if (humidity != lastHum) {
              lastHum = humidity;
              gw.send(msgHum.set(humidity, 1));
              Serial.print("H: ");
              Serial.println(humidity);
          }
        
          gw.sleep(SLEEP_TIME); //sleep a bit
        }
        
        L Offline
        L Offline
        LeoDesigner
        wrote on last edited by LeoDesigner
        #85

        @elmarculino
        My library is using a standard hardware serial port of arduino - you have to disable debug option in MySensors config and use the serial port only for RS485. The 'garbage' you are receiving are actual binary communication packets intended only for RS485 bus. You have to disconnect your serial to usb adapter in case if you are using Arduino Pro. Please take a closer look to the video and schematic coming with the library. You can use and 'sniff' your serial console - but you must to disable any additional serial debug prints to the console in production mode. Remember - your serial console is a RS485 bus with this library.

        1 Reply Last reply
        0
        • radekzmR radekzm

          @Michal

          I'll help when I get back from work.

          MichalM Offline
          MichalM Offline
          Michal
          wrote on last edited by
          #86

          @radekzm
          OK, what I did:
          on one Arduino nano I have this sketch
          https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewaySerialRS485/GatewaySerialRS485.ino
          on second arduino nano I have conbined two sketches:
          https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/DistanceSensor/DistanceSensor.ino
          and
          https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/MotionSensorRS485/MotionSensorRS485.ino
          and the results is:

          /**
           * 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.
           *
           *******************************
           *
           * REVISION HISTORY
           * Version 1.0 - Henrik Ekblad
           * 
           * DESCRIPTION
           * This is an example of sensors using RS485 as transport layer
           * 
           * Motion Sensor example using HC-SR501 
           * http://www.mysensors.org/build/motion
           * 
           * The transport uses AltSoftSerial to handle two serial links 
           * on one Arduino. Use the following pins for RS485 link
           * 
           *  Board          Transmit  Receive   PWM Unusable
           * -----          --------  -------   ------------
           * Teensy 3.0 & 3.1  21        20         22
           * Teensy 2.0         9        10       (none)
           * Teensy++ 2.0      25         4       26, 27
           * Arduino Uno        9         8         10
           * Arduino Leonardo   5        13       (none)
           * Arduino Mega      46        48       44, 45
           * Wiring-S           5         6          4
           * Sanguino          13        14         12 * 
           * 
           */
          
          
          // Enable RS485 transport layer
          #define MY_RS485
          
          // Define this to enables DE-pin management on defined pin 
          #define MY_RS485_DE_PIN 2
          
          // Set RS485 baud rate to use
          #define MY_RS485_BAUD_RATE 9600
          
          #include <SPI.h>
          #include <MySensor.h>
          #include <NewPing.h>
          
          #define CHILD_ID 1
          #define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
          #define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
          #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
          unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
          
          NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
          MyMessage msg(CHILD_ID, V_DISTANCE);
          int lastDist;
          boolean metric = true; 
          
          void setup()  
          {  
            metric = getConfig().isMetric;
          }
          
          void presentation() {
            // Send the sketch version information to the gateway and Controller
            sendSketchInfo("Distance Sensor", "1.0");
          
            // Register all sensors to gw (they will be created as child devices)
            present(CHILD_ID, S_DISTANCE);
          }
          
          void loop()     
          {     
            int dist = metric?sonar.ping_cm():sonar.ping_in();
            Serial.print("Ping: ");
            Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
            Serial.println(metric?" cm":" in");
          
            if (dist != lastDist) {
                send(msg.set(dist));
                lastDist = dist;
            }
          
            sleep(SLEEP_TIME);
          }
          

          On serial port (/dev/ttyUSB0) Controller (first nano) I see:
          0;255;3;0;9;Starting gateway (RSNGA-, 2.0.0-beta)
          0;255;3;0;9;Radio init successful.
          0;255;3;0;14;Gateway startup complete.
          0;255;3;0;9;Init complete, id=0, parent=0, distance=0

          on serial port(/dev/ttyUSB1) in node is see:
          ...
          Ping: 33 cm
          Ping: 34 cm
          Ping: 34 cm
          Ping: 33 cm
          Ping: 33 cm
          Ping: 33 cm
          Ping: 5 cm
          Ping: 91 cm
          Ping: 88 cm
          Ping: 89 cm
          ......

          But I expect to see some message on controller. What can I check? Do you see and mistakes ?

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mariusz
            wrote on last edited by Mariusz
            #87

            Hi,
            I am new in mysensors and arduino however I already use domoticz with mysensors and rflink.
            Everything is working fine however now I plan to renovate my house and I would like to put wire connection using max485 and arduino. Problem is that it is now working :( I have tried 1.5.4 version and also development branch. It seems that signal is not getting to gateway. I nothing see on arduino gateway except of initalization gateway. I have started to use two nano but I also tried combination nano and mega without success. I would be very appreciate if someone who done it could more describe how to make that it works.
            I tried https://arduino-info.wikispaces.com/SoftwareSerialRS485Example and it works fine.
            I use for nano combination pins 2 (de/re), 8 (ro),9(di)

            radekzmR 1 Reply Last reply
            0
            • M Mariusz

              Hi,
              I am new in mysensors and arduino however I already use domoticz with mysensors and rflink.
              Everything is working fine however now I plan to renovate my house and I would like to put wire connection using max485 and arduino. Problem is that it is now working :( I have tried 1.5.4 version and also development branch. It seems that signal is not getting to gateway. I nothing see on arduino gateway except of initalization gateway. I have started to use two nano but I also tried combination nano and mega without success. I would be very appreciate if someone who done it could more describe how to make that it works.
              I tried https://arduino-info.wikispaces.com/SoftwareSerialRS485Example and it works fine.
              I use for nano combination pins 2 (de/re), 8 (ro),9(di)

              radekzmR Offline
              radekzmR Offline
              radekzm
              wrote on last edited by
              #88

              @Mariusz jak jesteś Polakiem ? Jeżeli tak to mogę Ci pomóc po polsku będzie łatwiej :)

              m26872M 1 Reply Last reply
              0
              • radekzmR radekzm

                @Mariusz jak jesteś Polakiem ? Jeżeli tak to mogę Ci pomóc po polsku będzie łatwiej :)

                m26872M Offline
                m26872M Offline
                m26872
                Hardware Contributor
                wrote on last edited by
                #89

                @radekzm Please use private chat/PM if you don't intend everyone to read it.

                1 Reply Last reply
                0
                • radekzmR Offline
                  radekzmR Offline
                  radekzm
                  wrote on last edited by radekzm
                  #90

                  @m26872
                  I'm sorry and promises to improve :)

                  @Mariusz
                  In my case the solution was in MyConfig.h file (in my case C:\Program Files (x86)\Arduino\libraries\MySensors):

                  1. Disable function MY_DISABLED_SERIAL
                    commenting lines 49
                    // #define MY_DISABLED_SERIAL

                  2. Disable function MY_DEBUG
                    commenting lines 36
                    //#define MY_DEBUG

                  3. Correct connection is on my pictures enclosed in the above comment

                  Board | Transmit | Receive | PWM Unusable
                  Arduino Uno | 9 | 8 | 10 <----- Form https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html .... (& other ATMEGA328)
                  Arduino Leonardo | 5 | 13 | (none)
                  Arduino Mega | 46 | 48 | 44, 45

                  DE and RE -> Pin 2
                  RO -> Pin 8
                  DI -> Pin 9

                  1. To check the hardware and connections run the "Example Program" from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

                  2. Use development branch

                  1 Reply Last reply
                  1
                  • 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.

                    skatunS Offline
                    skatunS Offline
                    skatun
                    wrote on last edited by
                    #91

                    @LeoDesigner
                    Hi, I am trying to do almost the same, but I would like to use softwareSerial instead of altsoft. The reason why I would do this is that i can barely fit the arduino nano inside my case, and an additional rs485 module would not fit and that I have 8 nodes connected on the softwareserial . I have tested software serial on arduino and it seems to work fine up to 15meters, I also adjusted the baudrate to 9600 since my node does not send out/receive large amount of data. I read that you did some work on software serial and were wondering if you could take a look at this thread.

                    1 Reply Last reply
                    0
                    • CrankyCoderC Offline
                      CrankyCoderC Offline
                      CrankyCoder
                      wrote on last edited by
                      #92

                      Is there any how-to's on this transport class as far as the hardware goes? How does it work with multiple nodes? Is it like a ring pattern? or do all the nodes have to have a dedicated connection to the gateway.

                      Home Automation Tinkerer
                      www.CrankyCoder.net

                      Controller: HomeAssistant in Kubernetes
                      Gateway: MQTTClientGateway
                      MySensors: 2.3

                      1 Reply Last reply
                      0
                      • tbowmoT Offline
                        tbowmoT Offline
                        tbowmo
                        Admin
                        wrote on last edited by
                        #93

                        Depends on your physical transport layer. I would say a multidrop link where everything is in parallel..

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          hausinger
                          wrote on last edited by
                          #94

                          Is it possible, to built a esp8266 wifi gateway and communicate GW to node with rs485?
                          In altsoft esp8266 is not decleared, so i can't use it out of the box.

                          Btw: I tested my_rs485 with nano as GW and Pro Mini as node, and it worked great. Thank you :)

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

                            @hausinger I don't think so. I haven't heard of anyone trying. I guess you'd have to replace alt-soft-serial with something else.

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

                              @hausinger if you know about coding..

                              • plain C lib for esp8266, it can be adapted to work with Arduino Esp: https://github.com/plieningerweb/esp8266-software-uart
                              • or use stock arduino esp8266 software serial lib (https://github.com/plerup/espsoftwareserial), and inspire yourself from lib above, and code your CE pin management..
                              1 Reply Last reply
                              0
                              • BartB Offline
                                BartB Offline
                                Bart
                                wrote on last edited by
                                #97

                                Hi,

                                Any reason why this lib is not using Serial1 or Serial2 on Arduino Mega 2560 instead of AltSoftSerial?

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

                                  @Bartek-Celary
                                  No, not really. Just that not many that uses 2560.

                                  1 Reply Last reply
                                  0
                                  • BartB Offline
                                    BartB Offline
                                    Bart
                                    wrote on last edited by Bart
                                    #99

                                    How about adding the following define to use the Serial1/2 on Mega. I have not tested yet but assuming the AltSoftSerial lib has the same interface/functionality it should work.

                                    #define MY_RS485_SERIAL Serial1
                                    
                                    diff --git a/core/MyTransportRS485.cpp b/core/MyTransportRS485.cpp
                                    index 814f721..ea35c9e 100644
                                    --- a/core/MyTransportRS485.cpp
                                    +++ b/core/MyTransportRS485.cpp
                                    @@ -92,7 +92,11 @@ unsigned char _recSender;
                                     unsigned char _recCS;
                                     unsigned char _recCalcCS;
                                     
                                    +#if defined(MY_RS485_SERIAL)
                                    +HardwareSerial& _dev = MY_RS485_SERIAL;
                                    +#else
                                     AltSoftSerial _dev;
                                    +#endif
                                     
                                     
                                     unsigned char _nodeId;
                                    
                                    1 Reply Last reply
                                    0
                                    • hekH Offline
                                      hekH Offline
                                      hek
                                      Admin
                                      wrote on last edited by
                                      #100

                                      Looks like a neat solution. If it verifies ok, please create a pull request agains the development branch.

                                      1 Reply Last reply
                                      0
                                      • lanL Offline
                                        lanL Offline
                                        lan
                                        wrote on last edited by
                                        #101

                                        I understand that the RS485 is a separate gateway to communicate. Is it also possible to have the wireless gateway and RS485 gateway combined?

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

                                          Currently, no..

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


                                          6

                                          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