Local Sensors on Raspberry Pi Gateway

  • Re: Raspberry Pi Ethernet Gateway with local sensors

    Currently I have an RPi as an irrigation controller. It controls 10 relays in a cross-bar configuration to control 25 valves. This program is written in C++.

    Running on the same RPI is another process controlling the water lift system to move water a higher level (which will be the source of the water for the gravity fed irrigation) This process uses the nRf24L01 radio to talk to 4 Arduinos, each of which monitors the water levels in a holding tank and controls a pump relay) This is also written in C++ and relies heavily on the libraries (and help) of TMRh20

    This latter process screams MySensors, and Home Assistant. I am in the process of converting.

    But my irrigation controller will still be on the same RPi. Surely there must be a way to have intra-computer communication from a sensor to a gateway? The most simple way would be to put another RF24 radio on the RPi, assuming that I can run a second radio by using CE1 instead of CE0 and maybe a different pin for CSN

    I found the discussion Pi as a node but it didn't have any concrete examples. In similar topic there was no discussion. There were a couple of suggestions of modifying mysgw using the --my-gateway=none parameter, but I need a gateway, so I'd have two instances of mysgw and it still leaves with the problem of getting the data to the functional gateway.

    There were a couple of notes that the RPi is very expensive compared to a Nano ($50 vs $5) Even still, I need 10 I/O's and after the radio, and debug I/O, the Nano would only have 6 (3-8). No, wait! I could use 4 analog ports, too.

    Thus I could port my irrigation controller to a Nano. Or, I could using an Nano as a gateway gateway and just modify my existing code to talk to it over the USB port. Not the elegant solution I sought.

    Still RPi running a gateway and one or more local sensors. Has anyone done this?


  • Mod

    @OldSurferDude yes, the rpi gateway can have local sensors.

    I don't know how to program io on a rpi, but it seems you have that part figured out already. I think all you need to do is to insert your program into https://github.com/mysensors/MySensors/blob/development/examples_linux/mysgw.cpp before you make the gateway. Whenever you need to send or receive data, use MySensors send and receive just like you would on a node. No need for an extra nrf24.

  • @mfalkvidd

    I am wary of success on that program as it looks like it's written for an Arduino, thus the libraries are for an Arduino. For completeness sake, I try to note some differences

    Arduino on boot
    execute the setup function once
    start executing code in the loop function. When you get to the end of loop start again at the start of loop

    Linux C++ on boot
    Start executing code in the function main
    When you get to the end of the function main, exit the program

    To make the linux c++ code to be more Arduino like, the first lines of code after main are those that would normally be in setup. After all the setup steps one, uses a while loop that never terminates while(1){ loop code here } . while(1) creates an infinite loop because the evaluated value is always true..

    Also, the Arduino uses EEPROM and the RPi uses a file for non-volatile memory.

    On a tangent here, I make the assumption that the Node ID is stored in the EEPROM on the Arduino. Can one force a Node ID? This is important because my Arduino code is identical for all Arduinos (reading a preset NodeID from EEPROM) and the master uses the Node ID to perform a function that is location dependent.

    Arcing back in, if I am correct in that this code is written for an Arduino, I'll have to write code that saves and restores the Node ID, (and other parameters?)

    Thanks for your suggestion, I'll be looking in to it.


  • Mod

    @OldSurferDude I am not sure what the real difference between c/c++ code with the Arduino libraries, and c/c++ code without the Arduino libraries. But I don't think there is much difference. it is still code, and it is possible to add other libraries as well. So if your code needs any libraries, you should be able to include them like you would normally do.

    From what I understand, the MySensors hardware abstraction layer for rpi includes an emulated EEPROM. So you can use the Arduino EEPROM stuff if you want to, or just use plain c or c++ code to write to a file.

    For nodes, it is possible to force node id by setting MY_NODE_ID. However, a MySensors gateway always has node id 0 so this is not applicable for a gateway. You would not need to write any code that saves and restores the node id.

  • @mfalkvidd

    I finally got it to work. Look at my post on the Home Assistant forum.

    There is an amazing thing that someone in the MySensors world did that makes RPi I/O very easy! Just like the Arduino, one uses the code:

    pinMode(MphysicalPin, OUTPUT);

    I couldn't believe my eyes when I saw that. And then got double flabbergasted when I discovered that pin is the physical pin of the RPi! No mapping of GPIOi to the pins.

    So the I/O was easier than both of us could have imagined! @mfalkvidd if you know who did this, send them praise and thanks.


Log in to reply

Suggested Topics

  • 87
  • 5
  • 7
  • 10
  • 1
  • 8