RS485/RS232/Serial transport class for mysensors.org


  • Hardware Contributor

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



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



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



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


  • Admin

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



  • 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 🙂


  • Admin

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


  • Hardware Contributor

    @hausinger if you know about coding..



  • Hi,

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


  • Admin

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



  • 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;
    

  • Admin

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



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


  • Admin

    Currently, no..



  • @hek Thanks for the quick response. I'm looking for some direction (this might not be the right forum).
    I want to create a node to control my Itho Ecofan ventilation box. I now have a working program (thanks to the help of Rogier Reedijk The nodo now uses a CC1101 transceiver to control the box and thus uses the SPI bus. I'm looking for some direction to use this transmitter as part of my sensor network so I can control it from my Domoticz. Hope you can give me some directions how to proceed.


  • Admin

    Why do you need to hook it up to RS485 wired network? Can't you just forwards commands to the CC11001 from any radio node or the gateway?



  • Is it then possible to use the two radio's on one node and share the SPI bus?


  • Admin

    Yes, if the CC11001 behaves correctly.



  • Thanks, will try that



  • Hello,
    I have a problem running gatewayserial to rs-485 and the node with the usual lightsensor.
    NODE # 1 as a gateway and tried to upload a sketch of the library 2.0 and 1.5.4, and the monitor serial I can not see any data, I use to check on the serial PuTTy com6 because the converter rs-485 to USB is connected to the PC.

    NODE # 2 as a light sensor, during the upload Sketch by arduino programmer for serial monitor can see the data being sent but after plugging arduino to rs-485 as below no longer visible.

    My question is whether I have something wrong with the connection or sketch, can someone show an example of a complete sketch for the gateway, and a node?

    Connection mine looks like this:
    TX and RX Connect was to pins 0 and 1 in arduino or 8 and 9, DE is always PIN 2.

    alt text


  • Admin

    Looks like you got the pins to the Arduino wrong.

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


  • Admin

    I need a nice picture done in fritzing (or similar) for an article on the main site on how to connect the RS485 module (atmega 328 enough I guess).

    Anyone volunteering in making one?



  • so this is a 1 to 1 setup right? There is nothing here for multiple serial nodes correct?


  • Admin

    Aren't they just daisy chained?



  • @hek
    On the pictures is connect to TX and RX pins arduino but I tried to connect to pins 8,9,2 and result was the same 😞
    On thsi connections with pins 8,9,2 ofcourse and library mysensors 2.0 should works fine ?


  • Admin

    Yes, people have reported the RS485 transport working above.

    I just created this article with a summary of this thread
    https://www.mysensors.org/build/rs485



  • @elektro12345

    So what's with the DE+RE pins on that USB-RS485 converter? Will it even work in this configuration? I do have such converter but so far I've used the arduino as a SerialGateway to connect to RPi.



  • I am not sure if I am seeing a normal behavior. I don't know if it is expected that the sensor is taking such long time to become visible to the GW. I have recreated the examples from https://www.mysensors.org/build/rs485

    The only difference is that I've added SketchInfo for the GW and used Serial1 on MEGA board instead of AltSoftSerial.

    The debug on the motion sensor is as follows:

    MCO:BGN:INIT NODE,CP=RSNNA--,VER=2.0.1-beta
    TSM:INIT
    TSM:INIT:TSP OK
    TSF:ASID:OK,ID=1
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:NO REPLY
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:NO REPLY
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:NO REPLY
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:FAIL
    TSM:FAILURE
    TSM:FAILURE:PDT
    TSM:FAILURE:RE-INIT
    TSM:INIT
    TSM:INIT:TSP OK
    TSF:ASID:OK,ID=1
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSF:MSG:FPAR RES,ID=0,D=0
    TSF:MSG:FPAR OK,ID=0,D=1
    TSM:FPAR:OK
    TSM:ID
    TSM:ID:OK,ID=1
    TSM:UPL
    TSF:PING:SEND,TO=0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    TSF:CHKUPL:FAIL
    !TSM:UPL:FAIL
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSF:MSG:FPAR RES,ID=0,D=0
    TSF:MSG:FPAR OK,ID=0,D=1
    TSM:FPAR:OK
    TSM:ID
    TSM:ID:OK,ID=1
    TSM:UPL
    TSF:PING:SEND,TO=0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    TSF:CHKUPL:FAIL
    !TSM:UPL:FAIL
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSF:MSG:FPAR RES,ID=0,D=0
    TSF:MSG:FPAR OK,ID=0,D=1
    TSM:FPAR:OK
    TSM:ID
    TSM:ID:OK,ID=1
    TSM:UPL
    TSF:PING:SEND,TO=0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    TSF:CHKUPL:FAIL
    !TSM:UPL:FAIL
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSF:MSG:FPAR RES,ID=0,D=0
    TSF:MSG:FPAR OK,ID=0,D=1
    TSM:FPAR:OK
    TSM:ID
    TSM:ID:OK,ID=1
    TSM:UPL
    TSF:PING:SEND,TO=0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    TSF:CHKUPL:FAIL
    !TSM:UPL:FAIL
    TSM:FPAR
    TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSF:MSG:FPAR RES,ID=0,D=0
    TSF:MSG:FPAR OK,ID=0,D=1
    TSM:FPAR:OK
    TSM:ID
    TSM:ID:OK,ID=1
    TSM:UPL
    TSF:PING:SEND,TO=0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    TSF:MSG:READ,0-0-1,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    TSF:MSG:PONG RECV,HP=1
    TSF:CHKUPL:OK
    TSM:UPL:OK
    TSM:READY
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    TSF:MSG:SEND,1-1-0-0,s=255,c=0,t=17,pt=0,l=10,sg=0,ft=0,st=OK:2.0.1-beta
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=11,pt=0,l=13,sg=0,ft=0,st=OK:Motion Sensor
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
    TSF:MSG:SEND,1-1-0-0,s=1,c=0,t=1,pt=0,l=0,sg=0,ft=0,st=OK:
    MCO:REG:REQ
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    TSF:MSG:READ,0-0-1,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    MCO:PIM:NODE REG=1
    MCO:BGN:STP
    MCO:BGN:INIT OK,ID=1,PAR=0,DIS=1,REG=1
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    0
    TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    

    I don't know how to obtain debug from the Gateway Serial but as far as the GW messages on the console it looked as follows:

    0;255;3;0;14;Gateway startup complete.
    0;255;0;0;18;2.0.1-beta
    0;255;3;0;11;SerialGateway
    0;255;3;0;12;0.1
    1;255;3;0;6;0
    1;255;3;0;11;Motion Sensor
    1;255;3;0;12;1.0
    1;1;0;0;1;
    1;1;1;0;16;0
    1;1;1;0;16;0
    1;1;1;0;16;0
    1;1;1;0;16;0
    1;1;1;0;16;0
    

    As you see it takes a fair amount of time (around 30-40 secs) to obtain the readings from the sensor. This happens each time I reset the sensor node but it works instantly if I reset the GW (i.e. the readings appear instantly after the reset). Is it expected behavior?


  • Admin

    30 seconds sound like a long time.. Not sure what could be wrong. I don't have any test setup here unfortunately.



  • Hi,
    I am wondering if I can just use the mySensors Lib to pass on the data via another connection. Currently I have a Loxone Controller which accepts UDP and TCP/IP connections (and others, but I chose these). As gateway I have an ArduinoMega and the network is running on RS485.
    So my understanding is that I can setup the network with the mySensors code and all the data is collected by the gateway which passes the data via UDP to the controller. V.v. the controller sends the data to the gateway which passes tha data on to the correspondent node.
    Is there any project similar to this?
    Thank you very much for your help!
    Best regards Kduino



  • @Bart I do not know what You mean with DE+RE pins on the converter.
    On the converter You connect A and B calbe from bus.
    DE and RE pins fromMAX485 You connect with sealf and connect to pin in my case D2 on Arduino.



  • I have a question, when I wont send do domoticz from Arduino presentation lightsensor I can send by serial something like this:

    1;2;0;0;16;V_LIGHT_LEVEL
    

    and domoticz will understend that is the lightsensor LUX ?



  • @hek

    I was able to solve that long initialization problem with removing AltSoftSerial completely and using another Mega board on Serial1 serving as a GW. It looks like the AltSoftSerial on Nano was not working well with Serial1 on Mega.

    Strange that although now it works almost instantly, the reading of data from the HA takes longer than before... Not sure what could be causing this.

    It looks that these RS485 modules have the resistors already added between A and B cables and +5 and GND so I don't think that could be an issue.


  • Admin

    Thanks for the feedback.

    So how should the modules be inter-connected? Do you have time to create some drawing for the instruction page?



  • @hek
    I will do the schematic once I solve all issues as I am still losing events while connected to Home Assistant. Need to check whether it is because of pymysensors library (Home Assistant side) or if there is still some GW <-> Sensors problem. At this moment my Sensors board has around 48 binary states one can set/read from the controller and at the same time they are changeable via input pins (falling edge toggles the switch).

    To give a quick overview of my setup:

    2 x (MEGA 2560 + RS485 module)
    alt text

    I have the following connections for each mega+rs485 module:

    • +5/GND connected to respective pins on rs485 module
    • D2 connected to RE/DE
    • D18 <-> RO
    • D19 <-> DI

    The A and B lines are connected to each other. (A-A, B-B). And most importantly I am using the above mentioned minor change in the MyTransportRS485.cpp to replace altSoftSerial with Serial1 (https://forum.mysensors.org/topic/1947/rs485-rs232-serial-transport-class-for-mysensors-org/99)

    and just if anyone would like to try it here's the config I am using:

    // Gateway:
    #define MY_RS485
    #define MY_RS485_DE_PIN 2
    #define MY_RS485_BAUD_RATE 9600
    #define MY_GATEWAY_SERIAL
    // below not official for getting the Serial1
    #define MY_RS485_SERIAL Serial1
    
    // Sensor node
    #define MY_DEBUG
    #define MY_RS485
    #define MY_RS485_DE_PIN 2
    #define MY_RS485_BAUD_RATE 9600
    // below not official for getting the Serial1
    #define MY_RS485_SERIAL Serial1
    


  • @Bart
    Does it work with serial1 on mega2560?

    I can't get a single bit out of the harware serial1.
    Is it only the smal patch in MyTransportRS485.cpp?



  • @karlheinz2000
    Yes. Got it working with this patch. Although not reliably when other board was using the altsoftserial.



  • Got it working too 😄
    Problem was: RX1 and TX1 labels on the chinese Mega2560 board are swapped. 😠
    All other RX/TX labels are right.



  • @karlheinz2000 Are any of your boards using AltSoftSerial instead of HW serial? I was not able to get a reliable connection with software serial library....



  • I use a mega with hw-serial and a nano (gateway) with AltSoftSerial.
    It works fine the last 24 hours. Still running...
    I did not test other configurations.



  • @hek Looks like something has been fixed in regards to altSoftSerial. Today I've pulled latest development and everything works fine. The gateway has no problem finding nodes and all messages reach their destinations.


  • Admin

    Hmm, strange. Wonder if this PR affected the behaviour of AltSoftSerial (in a good way).

    https://github.com/mysensors/MySensors/pull/599



  • @hek I will try to check if this change did it.



  • I am wondering if a node-2-node communication should be working. I have compiled the ping/pong example for RS485 but it appears the nodes are not communicating.


  • Admin

    Did you set one of them to be repeater (or gateway)?



  • @hek ok. all good now - the ping pong example working. I forgot about the gateway flag.

    BTW, checked to see if the GW has any issues without PR 599 but it seems it is still working OK. I have not changed the HW connections so must be some other change.



  • Is any data buffer/FIFO implemented in RS485 gateway?
    My controller (fhem) is transmitting data too fast to the gateway, so some data is not transmitted on RS485.
    I checked RX/TX from controller to gateway -> all data is going into gateway. But not all data goes out to my sensor node.

    I have only one sensor (and gateway) on RS485.
    Baudrate is 19200 on RS485 and to controller.
    Sensor node uses HW serial. Gateway is on NANO with AltSoftSerial.



  • @karlheinz2000
    I had no chance to look to the AltSoftSerial code, but if we are talking about hardware serial - arduino as a standard has 64 bytes buffer. I think it's possible that your gateway is not fast enough to process packets at high rate from FHEM. Also even if they are processed on time - at 19200 you may loose packets due low serial speed. It's simple no room for next packet.



  • @LeoDesigner
    I did a hardware update. Now sensor and gateway are arduino mega. Everything is running on 115200 hardware serial.
    RS485 communication looks good. Data is going into sensor node, but does not appear in MySensors.
    I tried to increase RX buffer via #define SERIAL_RX_BUFFER_SIZE 256, with no effect in functionality. Does this define works as expected? Because this buffer size number has no effect on the 'dynamic memory' usage after compiling the sketch. It shows always the same number of used bytes, independent of the RX buffer size.



  • [Solved]
    Initial Problem were incoming messages at sensor node during reading an image from SD card (170ms). Increase of serial RX buffer size solved the problem.

    Good to know:
    The serial buffer size is not defined in:
    c:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.h
    You can change the numbers with no effect to your sketch.

    You have to change:
    c:\Users...\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.14\cores\arduino\HardwareSerial.h

    Then it will work 😃



  • I'm curious whether it is possible to use this RS485 transport with Ethernet or MQTT gateway or Serial gateway is the only option?
    For example, controller talks with MySensors Gateway over Ethernet,
    MySensors Gateway talks with the Nodes over RS485.
    Thanks!


  • Admin

    @AndrewZ

    Yes, that should work just fine.



  • @hek
    thanks for this really fast and motivating response, I will try!



  • I'm trying to get my Ethernet gateway (UNO with W5100) working with RS485 transport.
    While RS485 part seems to be working fine between the node and gateway, the Ethernet part seems to be dead. I suspect this is related to the fact that Ethernet shield uses pin 10 which is unusable with RS485. Am I right? Any workaround?



  • Today the new rs485 shield arrived, i will solder it up over the weekend and start testing early next week:)


Log in to reply
 

Suggested Topics

17
Online

11.4k
Users

11.1k
Topics

112.7k
Posts