CAN bus transport implementation for MYS



  • This is the continuation of the debate on wire bus for MYSensors based on CAN bus from here:
    https://forum.mysensors.org/topic/5051/rs485-stress-test/20



  • What I wrote at that topic and what led to this separate discussion:

    "If you all think, that reading message back on "real" (= not 10cm cables on table) RS485 bus by sender guarantee delivering to recipient, you are wrong. RS485 bus do not work like this and message in long distance from sending node with other parallel sending node my looks different on both places. You must use drivers for CAN bus instead of. I wrote about this in different thread.
    Then some modified SoftwareSerial library with checking every sent bit and immediately stopping sending when collision detected does best work. But unfortunately nobody wrote this piece of software still ....
    I need robust wired network connected to Domoticz via serial gateway too, so I choose CAN protocol on CANbus. CAN controller is not so expensive today. I am using extended 29bit ID, first 7 bits are receivers ID, so I can set mask and filter registers so, that I receive only messages for my node or broadcast.
    Others bits in ID and in payload I use for senders ID, sensor ID,message type , data type, data and etc.
    I am planning send only short messages from individual sensors, so data length of complete CAN message is enough for me.
    But MySensors library is growing and growing and very complex for me, to implement this into it. So I try make my own library for CAN to serial gateway and CAN connecting nodes to gateway and use MySensors serial protocol to the controller only.
    I am planing Arduino and pure AVR libraries.
    I am still coding, when I have got free time, so stay tuned....."



  • And this is, what I'm wrote there:

    I'm very interested in your development.
    What kind of can bus module are you using? I found 2 different modules for very less money. The very small module is called SN65HVD230 (controlled by serial rx, tx) and cost approximately 1,75$ from China. The other module with bigger size is called MCP2515 (controlled by spi) and cost approximately 1,26 $ from China. Both are very cheap.
    What is you currently status in this case?
    I'm planning to use it for my new house in some month.

    Thank you



  • @hausinger

    SN65HVD230 is only CAN bus driver. It has no inteligence, so you need MCP2515 or microcontroller with CAN periphery.
    By the way I think, that using CAN drivers is better for RS485 connection in MYSensors then RS485 drivers.
    For 3 reasons for me:

    1. You do not need a pin for direction control - you can use a common serial library. For galvanic isolation, it is enough just to separate the two signals.
    2. The status of the bus is always precisely defined. What reads the sending node is exactly what read all the other nodes on the bus. It is not true when using RS485 drivers in multimaster bus.
    3. CAN bus drivers are constructed such, that when the processor transmits a dominant state for too long time, the driver disconnects the processor from the bus and connectes it after another change of the pin TX. So node with defective or hanged program can not block the entire bus.


  • @hausinger
    For now, most important for me is connect 14 thermostats for electrical floor heating through serial gateway to some controller. It is bus with stable number of nodes (14 ) with the same design. Nothing time critical. No many changes in the future. For this bus I will use some "light" variant MODBUS protocol. Only read and write registers. Gateway will act like master and will translate modbus messages to the serial port for controller.
    Each thermostat has its own intelligence and bus is used for data collectio nonly. Setting the desired temperature and control and report critical conditions etc.
    And CAN bus drivers only.
    That is I am working now on.
    Simultaneously, I work on the transmission of CAN messages, but the problem got to the main solution later.
    ( I have 2 other buses. One for PIR and smoke detectors in each room and second is universal bus through entire home and outside too.
    It is mostly in separate wall sockets ( RJ11 6p6c ). On both I will use CAN bus with CAN controllers)



  • @kimot
    thank you for your answers in this case.

    You think, the SN65HVD230 is better than the max485? Can I use the SN65HVD230 with mysensors and Transport class rs485 without changes in the code? If yes, how do I do with the "DE-Pin" ?

    I don't know, why you are using 3 Bus Systems at your home? Why not 1 Stable Bus?

    I want a stable bus for several things in my new home. PIR, Standard button Switch, Contacts (Window, Smoke), Dimmers, RGB Dimmers, Temperaturesensors, Relays should be my nodes. The Problem in this case is, that I want to use my nodes even when the bus have Errors.
    So if you seperate the communication from sensors to Gateway (with your self made library, as you wrote here) and the Gateway communication with the Controller with the mysensors libs, that would be excactly what I'm locking for.

    My idea besides of that is, that nodes can work without the bus. For examble the push button Switch can Switch the light, even when the bus is down. With mysensors this is not possible, because nodes have to register to the Gateway. Mysensors should be a "add on" for the nodes in my home, to control all on one place - openhab.

    I also want to have a stable bus, because it would be very bad, when i press a push button and nothing happens (=is dark in the room), because the bus is busy. Sure it doesn't matter if a temp sensor can't transmit it's value to the Controller. But for Switch / PIR it is very important.


  • Hardware Contributor

    I agree with @kimot : the RS485 bus is not designed to have many masters on the same bus.
    So, when you want to use RS485, you have 2 choices :

    • The master polls the slaves to ask them if they have something to say (you give them the "right" to talk)
    • Each device tries to talk when needed, with the risk of collision, and the message could not be correctly transmitted.

    Using a MCP2551 driver (without a MCP2515 controller) allows to use the benefits of CAN bus : If 2 masters talk at the same time, one of them will "see" that one of the bit he tried to push on the bus has been overwritten by another one, and will have to try again a few times later. This is not collision avoidance, but collision detection.
    Of course, since we use only a CAN driver and not a controller, we must do the checks via software (listen to what is being heard as we're talking).

    I have some arduinos laying around, and some MCP2551. I must do some tests.


  • Hardware Contributor

    in case, this is also an interesting note about CAN, for 3.3v transceivers, http://www.ti.com/lit/an/slla337/slla337.pdf
    esp32 has a CAN controller, so it will just need the transceiver i think (I'm waiting their lib release). geek sidenote :)

    Keep the good work, I also think CAN bus is very reliable compared to halfduplex rs485.


  • Hardware Contributor

    Yes, this device is interresting.

    None the less, we're thinking of using only a CAN driver without controller, in a kind of RS485 bus...
    This way, we make an hybride bus : simplicity of RS485, with multi-master possibilities of CAN thanks to the way his driver works !
    So, having a CAN controller on ESP32 is ( from my point of view) not-so-much usefull.


  • Hardware Contributor

    @hausinger said:

    My idea besides of that is, that nodes can work without the bus. For examble the push button Switch can Switch the light, even when the bus is down. With mysensors this is not possible, because nodes have to register to the Gateway. Mysensors should be a "add on" for the nodes in my home, to control all on one place - openhab.

    The only way to handle this kind of problem, is to make device with INPUT and OUTPUT at the same time !
    You would then have to think of an intelligent way of handling what happens when the INPUT see events :
    Do you have to transmit it to the gateway, wait for a reply, and then fall back to a "fail" mode if the gateway doesn't reply after a delay ?
    Do you prefer handling the event simply by turning ON/OFF the output, and just inform the gateway of what happening ? this way, even if the bus is down, your device will continue to work... but you're loosing the benefits of having an intelligent controller/gateway that can make decisions...



  • @kimot Would it be hard to write CANbus version of the transport class based on rs485 implementation? Is there some extra things you need to handle on top of "basic serial communication"?



  • @napo7

    @napo7 said:

    @hausinger said:

    My idea besides of that is, that nodes can work without the bus. For examble the push button Switch can Switch the light, even when the bus is down. With mysensors this is not possible, because nodes have to register to the Gateway. Mysensors should be a "add on" for the nodes in my home, to control all on one place - openhab.

    The only way to handle this kind of problem, is to make device with INPUT and OUTPUT at the same time !
    You would then have to think of an intelligent way of handling what happens when the INPUT see events :
    Do you have to transmit it to the gateway, wait for a reply, and then fall back to a "fail" mode if the gateway doesn't reply after a delay ?
    Do you prefer handling the event simply by turning ON/OFF the output, and just inform the gateway of what happening ? this way, even if the bus is down, your device will continue to work... but you're loosing the benefits of having an intelligent controller/gateway that can make decisions...

    Hi @napo7
    Yes, i would prefer handling it direct on the nodes. Not only ON/OFF, also dimmer (1 short press on the push button - Light on (last value) + turn on power supply of the lamps (Relais=1), 1 short press on the push button - light off + turn power supply off of the lamps (Relais=0), 1 Long press on the bush putton - dimming). The Gateway (and Controller) should be able to do the same Thing, if the bus is ok. But if it fials, i could use it localy and so i'm not in a dark room.


  • Hardware Contributor

    @pjr
    Hi,

    If we want to use CAN drivers instead of RS485 drivers, we don't have to add anything to the code.
    We will benefit of CAN drivers capabilities, but we still have to write some code to handle collisions.

    If we want to use a CAN CONTROLLER, yes, there is more to do :we must setup the CAN controller each time the board start-up : this is needed to tell the CAN controller on which kind of message we must listen to, what is our ID (CAN controller do all the filtering job...), etc...



  • @napo7 and if we use CAN CONTROLLER it does all the needed collision avoiding, detection and "resending" and it dont need to be written in the transport class? Only the initalization of the CAN CONTROLLER needs to be added?

    Is there much difference in communication reliability if we use CAN DRIVER vs. RS485 DRIVER currently without modifying the rs485 transport class?


  • Hardware Contributor

    Yes, the CAN controller handles all this stuff (collision detection and resending...)
    So, yes, only CAN initialization has to be added.

    In facts, NO, without adding code, there will be no difference between between the two drivers. I haven't looked at the class, but if we use a CAN driver, the goal is to use its advantages to "allow" collisions and handle them better.



  • Hi everyone. Have there been any developments regarding CAN Bus Controller integration to MySensors Library?
    Thanks.



  • @gonzalonal
    Yes I am.
    But I have got a lot of other works, so It's going slowly.
    The problem is, that CAN can transfer only 29 bits ID ( 3 bytes + 5 bits ) and max. 8 bytes of data.
    MySensors message has got max. 32 bytes ( 7 bytes header and 25 bytes of data ).
    I must either send MySensors message like 3 packets of CAN messages or limit properties of MySensors protocol.
    Now I am working on second variant.
    I am able code to one CAN message:
    6 bits - destination
    6 bis - sender
    (gateway, broadcast, 62 nodes - it is enough for me for one bus. I plane three buses and gateway can " translate " or "expand" nodes ID from and to controller )
    1 bit - RACK
    1 bit - IS ACK
    3 bits - command
    6 bits - type ( MY sensors has 56 types max. now )
    6 bits - sensor id ( only 64 sensors per node ... )
    3 bits - payload type ( int, uint, long .... )

    The sum of these is not 29, but 32 so I am using a little "hacking" data length field in CAN frame to obtain additional 3 bits.
    For data I have got 8 bytes.
    It is enough for conventional data types from char to floating point.
    Only text messages are limited to 8 bytes. But for us, old boys, who remember old DOS file names, it is enough.

    In this time, I am able send and receive packets in that format between nodes and now I am working on transfer RS485 library to CAN library.
    The problem is, that RS485 library sends destination address, node id, length of message and then MySensors message, witch "surprisingly" contain again destination and sender bytes. A little bit redundant for me.
    So I must remove some fields from MySensors message, put it to CAN ID and send only payload in CAN data fields.
    When I receive CAN message, I must again assemble correct MySensors message format and put it to MySensors system.
    But unfortunately only documentation is library code itself :o(

    I try a "stress test" like here:
    https://forum.mysensors.org/topic/5051/rs485-stress-test

    I send 10 000 messages from two nodes to "gateway" ( 22kbps ).
    Result - zero messages lost.
    But it is expectable with CAN.



  • @kimot what hw you are using? MCP2515 + MCP2551?





  • Hello,

    I'm really interested in the solution you are working on (can bus with mysensors). I would like as much as possible to avoid a wireless network but I still would like to use the "mysensors" libraries and protocol. As this project as a lot of users it's a warranty of longevity.

    @kimot : your trials seems great, did you have the change of getting further?


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.