MySensors Gateway on RPi over SPI

Hello,

First off, I am quite new to this HomeAssistant thing. I’ve always just made my own solutions but I decided to give HA a shot since it already has a nice front-end and it can run on the RPi.

On the MySensors webpage, it shows the possibility of connecting an NRF024 to the RPi to act as a gateway. Now how would I add this to HA? I know I can edit configuration.yaml to add the mysensors section and define the gateway, although the gateway is wired to SPI, not UART or USB or over MQTT. How would I define that device path and baud rate?

Would it just be /dev/spidev0.0 and then no baud rate defined?

Hi!

I suggest you follow the instructions here for serial gateway:

This will expose a serial device which you can then use in the mysensors config section in home assistant.

I’m not sure what you mean with:

the gateway is wired to SPI

Do you mean the radio is attached via SPI? In that case, that’s the normal procedure, but in this case it’s done directly to the RPi GPIO instead of to an arduino.

The gateway is either an arduino with radio connected and a gateway sketch or an RPi with a radio connected and a program/service running the gateway code.

Yes, the radio is connected directly to the RPi GPIO header using the SPI0 connections.

I assume this program is written by me, otherwise who would know how often we need to check the nodes. My question is where does HA fit in with that? I figure that I list my sensors, that I want the gateway to check, in the configuration.yaml file and then HA will execute those reads?

I think this is my main area of confusion here since MySensors to seem to be a standalone thing and the HA is this massive complex shell meant to integrate with lots of things. I’m just not sure where to put the hooks in between them and what hooks are already there.

No, you get the program via clone, build, run etc, if you follow the instruction in the link above.

Updates are pushed to the gateway by the nodes, so no polling is necessary. You don’t need to list your nodes, that’s taken care of by the mysensors integration in home assistant. You just need to specify what gateway you are using and how home assistant should connect to it, and some other optional configuration options. See:

What you do need to do is write the sketches for the nodes. But there are ready examples for the simple cases over at mysensors.org, so you can get a long way with very little coding experience. Although I suggest you first try the example sketches that I’ve made available for each mysensors platform in home assistant. There are some steps required to properly present a node/sensor to the mysensors integration in home assistant that is not always done in the general examples over at mysensors.org.

See:

1 Like

Ohhhhh ok ok, that makes way more sense.
There should be some kind of topology flow for each component. That’d really help.

Okay so all that is left to do is to dig through the source code of MySensors 2.0 and see why their documentation doesn’t show RFM69 as supported…

I’m not sure what you mean here (sorry). If you mean what platforms are available for a component, eg if mysensors has a light platform or not, those are seen in the right bar, under “Related components” in desktop mode in homeassistant.io, when you browse a component page.

Regarding RFM69 directly connected to RPi, that’s not supported at the moment, but there’s work in progress on this, as far as I understand. See last comment in this closed PR.

Edit:
I see there’s a very recent PR on this subject:

Some flow like this, but way more accurate. I made this in like two minutes.

I saw that it wasn’t supported in the documentation but it did have functionality in MyTopologyRFM69.cpp and was already set in the configure program. I hadn’t found that pull request though, good find! I guess I’ll be diggin through that tonight.

1 Like

I like those flow charts. If you have time and want to make images of the different flows and make a PR to https://github.com/home-assistant/home-assistant.github.io, I think it will make the docs better and I’ll be happy to review that.

We can discuss the topology here before you make the images, if you like.

I wouldn’t mind making documentation like that at all! I do need to understand HA much more though, haha. Right now I’m just trying to fully understand how I can take further action on any status updates HA receives instead of just monitoring all of the inputs.

I had some spare time tonight so I figured I’d dig into this a little further.

The RFM69 gateway was merged into the development branch of MySensors about a week ago. Just using straight MySensors API, one could now setup an RFM69 radio as the gateway attached to the RPi.GPIO. After chugging through Home Assistant code, it looks like HA will set up the gateway in the mysensors.py file of HA at about line 182:

    gateway = mysensors.SerialGateway(
                        device, event_callback=None, persistence=persistence,
                        persistence_file=persistence_file,
                        protocol_version=version, baud=baud_rate)

After running through the “pymysensors” file that HA references, I found where it sets up the SerialGateway (it wasn’t hard, the file is named gateway_serial.py). This is we actually communicate with the later that does the real signaling on the serial interface. Unfortunately the python module used is the serial module which is for UART operations only.

If a user wants to directly attach a gateway to the RPi GPIO header through the SPI connections, rather than the UART connect to a microcontroller loaded with Arduino, I think we’ll need some new functionality that enables directly attached SPI radios.

I wouldn’t be against writing this myself, but it’d be incredibly tailored to the Raspberry Pi

Didn’t we already talk about this above? It’s not the gateway that is attached to GPIOs, it’s the radio. There’s no need to change home assistant or pymysensors to do so. You will run a program on the RPi that will emulate a serial port or create an tcp socket server, or run a mqtt client. It’s through these three options home assistant will interface with mysensors.

We discussed attaching the radio to the GPIO. I must be misunderstanding what a gateway is then.

Isn’t the radio the gateway? As in, the gateway could be an Arduino connected, by UART, to a radio or it could be just be the radio connected by SPI with no Arduino involved. If that is true, then we only have functionality to do the UART signaling and not the SPI signaling.

Trying to emulate these services wouldn’t be the most straightforward option. Supplying a SPI driver for HA would be very beneficial for its growth and futureproof.

The gateway is the hardware and software that provides the interface between the mysensors network and the controller, in this case home assistant.

The controller doesn’t interface directly with the radio. The interface is the serial API of mysensors. It can happen over serial connection, a tcp socket or via mqtt.

Mysensors takes care of the radio communication, drivers for that etc. The program on the RPi to do this already exists and is developed by the mysensors community.

Go to mysensors.org and read more about the architecture of mysensors, to get the full picture.

Okay so I totally didn’t know that when the documentation said a serial port, it meant I could use a tty device for IPC.
So if there are any other firmware engineers reading this, these serial ports aren’t necessarily UARTs.