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?



  • @gasuter
    I think I am able sending data from node to gateway (GW) and through it to controller.
    There is limited data space in CAN message, so I must avoid sending long strings and send all as possible like integers, longs, floats etc. It is not problem when sending from node to GW, because in the message header is field, which describes type of payload and according GW code, I assume, it can translate it to strings for controller correctly.
    But problem is, when GW receives data from controller. Payload is always string and I must convert it to int, long, float and sometimes short string for sending it through CAN bus to the node. But from serial protocol GW can not recognize, what type of this payload is. I think I need to built some translation table for each serial protocol TYPE to variable type.
    But maybe each controller sends different type of data for the same sensor value (0, 1 x "on", "off" ).
    Or better something "smart" like "when there is a dot and not letter in string, it is float", "if there are only numbers, it is int or long" ....



  • Sorry to hijack a bit, but j1708 looks like it can do bit level collision detection with a max485, it means controlling the DE line. More work than CANBus?



  • @wallyllama
    I look at j1708 spec. and I think, that this protocol was born long before CAN.
    It uses MAX485 bus drivers that way, that CAN bus drivers can works at higher speeds and better now.
    And I must say, I can see better multimaster implementation for RS485 bus than j1708 ( clever addressing, bus arbitration, CRC instead of simple SUM etc. ) And there is AVR libraries so you do not start from scratch. For J1708 I can not find any.


  • Mod

    A friend of mine who works a lot with can bus told me that j1939 supports multi pack when bigger payloads are needed



  • @gohan
    Thanks for your message.
    All CAN controllers can send max 8 bytes of data, so longer messages must be divided to multiple packets. J1939 works this way too. That is what I do not want. With heavy traffic especially GW must again assemble packets to complete message, but packet of one message will not come one by one. Packets will be coming mixed from different nodes and GW must assemble them in memory to complete message from each node. So you for example has 30 messages in different state of completion. Some of them has received 1 packet and waiting for 2nd , some has 2nd and waiting for 3rd. ....
    I am concerned that it does not work for ATmega328, and must be used processor with more RAM.


  • Mod

    I understand your concerns, but can bus is made more for industrial purposes. Maybe you need to find a more powerful replacement for the ATmega328



  • @kimot Thank you very much for your reply. It's sounds great and more I read about bus for IOT more I think Can is the best solution. But still the problem with the data length bother me. The more hack and tricks we have to add to a system the more problem we can have.

    I made a little research on internet about Can and found that there is a new version called CAN FD that is actually compatible with CAN 2 but it can be used at a higher speed between to CAN FD nodes (but that is not really interesting us for our use). The real plus is that you can send data up to 64 bytes. And I think that could be the solution of the problem, no?

    http://www.microchip.com/wwwproducts/en/MCP2561FD


  • Mod

    Agricultural vehicles now use ISOBUS (extension of j1939) that can send icons and images to the displays. Maybe you can take a look at it



  • @gasuter
    CAN FD looks good, until you start to looking for IO chips.
    MCP2561FD is only CAN bus transceiver ( like MAX485 ) with higher speed ability, then traditional CAN bus drivers.
    It has no logic "on board".
    You need something like MCP2517.
    But try find datasheet, prices etc. for this chip.
    I think, it will be very expensive way for us, like standard CAN several years ago was.


  • Mod

    Better performance usually means more expensive 😁. Maybe for the moment we just need to find a good way to make rs485 perform a little better



  • @gohan this is exactly why i brought up j1708, it is a hardware spec(can be any software on top), that uses max485 chips, you drive the de line instead of tx and it allows you to "see" collisions on the line. It would need a layer that does retransmits, but i dont see why the mysensors rs485 code clouldnt ride on top of that. There is an id that is utransmitted first that all nodes have to watch for which is duplicated efforst as the mysensors layer has that.

    I see it like this pseudo code:

    All nodes listen for an id,
    if none, then start transmitting,
    watching for dominant bits in between bits that hint someone else is transmitting, if collision back off for random time.
    if no collision transmit full message as normal mysensors message
    Other nodes must listen until message ends then they can start a transmit after random interval.

    The secret is really in tying tx (it looks like) low and control de, re is enables always so you can see other bits. Once a node has the bus, everyone shuts up until it is done. You only decides "who has the floor" and how to know when they relinquish it.

    J1708 is 9600 baud, which may be ok, but ill bet it would work at higher speeds.


  • Mod

    I am not sure if customizing too much that protocol is going to be a successful way. I'm concerned that if you can't get a team of developers working on it and maintain it in the future, people aren't going to use it because the risk of don't having any kind of support will discourage them



  • @kimot
    Much agree with you, I don't like CAN bus, let's return to RS485 bus, I already create a stand alone RS485 controller chip (CDCTL-Bx) which support arbitration (as same as CAN) and user data in packet is up to 253 bytes, the baud rate is also higher than CAN or CAN FD, and it's much more easy to use than the CAN:
    https://github.com/dukelec/cdbus_doc (Open Source Project)



  • Hi all,
    I would really be very interested in having CAN bus managed by MySensors.
    My new house is being built and I already planed to have a RS485 bus running along the house (RJ45 cable with one pair dedicated to RS485 bus and one pair for gnd/+5V). Having done some tests, I realize that I'm facing a lot of collisions and I don't want to explain to my wife that the light does not switch on when she clicks the switch because of a collision 😉
    So, I'm was looking at solutions based on CAN bus and was even ready to give up with MySensors (although I love mySensors) when I ended up on this thread.
    @kimot : have you progressed on your implementation ?
    If so, I'm OK for being your beta-tester, I only need to exchange integers between the nodes and the gateway, no strings so the limitation of the packet size is not an issue for me : 8 bits are sufficient to me.


  • Hardware Contributor

    @kimot said in CAN bus transport implementation for MYS:

    @gasuter
    CAN FD looks good, until you start to looking for IO chips.
    MCP2561FD is only CAN bus transceiver ( like MAX485 ) with higher speed ability, then traditional CAN bus drivers.
    It has no logic "on board".
    You need something like MCP2517.
    But try find datasheet, prices etc. for this chip.
    I think, it will be very expensive way for us, like standard CAN several years ago was.

    MCP2517 has been released a few months ago and is only around 2$.
    http://ww1.microchip.com/downloads/en/DeviceDoc/20005688A.pdf


Log in to reply
 

Suggested Topics

  • 8
  • 7
  • 1
  • 1
  • 5
  • 1

37
Online

11.4k
Users

11.1k
Topics

112.6k
Posts