Ethernet / MQTT PoE gateway for 16 reed switches

  • Hello,

    few days ago I accidentally came across mysensors (how is it that I did not find it earlier as I am looking into rpi, arduino and home automation for about 2 years?) and I was exited from the first moment . For some time now I was working on several small sensors for my home but did not really like any of the solutions I met so far - my idea was ethernet + poe + rest api + simple progressive mobile UI. Closest I got was arduino pro mini with W5500 + 5V poe splitter + custom sketch using webduino to provide REST API + mozilla iot gateway.

    I had (nearly) finished the HW for my window/door reed switches. But I got into trouble with REST API + mozilla iot gateway. Testing the REST API worked ok from browser but somehow the mozilla iot gateway was unable to load all the presented switches reliably. I have to say that until this problem I had quite a good experience with it - I had it connected to few thermometers and relays to operate heating.

    I saw the sample sketches for mysensors + and it "just clicked" to my idea. I edited the sketch and installed mycontroller and successfully tested both Ethernet Gateway and MQTT Gateway in few hours. I think I will stick with MQTT for more compatibility (I did not find any description about the mysensors communication protocol so I have no idea how standardized it is).

    So I want to share my HW + say thanks for the great project!

    Designed to be powered over usb micro (so it can run without poe) as these chargers are everywhere now.

    The barrel connector is temporary until I modify the poe splitter output to usb micro.

    Sample connection - I have such reed switches installed in my windows and doors.

    Of course I will be happy for any constructive criticism and suggestions (I do not have any education in electronics so try not to be very harsh about my circuit design and soldering though 😉 ).

  • Mod

    @tssk nice project! Some ideas that pop into mind: you could have used a nano (saves separate usb socket) and I wonder how you go with all the cabling from here? I usually prefer a more decentralized approach, where each (or maybe a couple) sensor has its own node. These can then be battery powered and cabling will be minimal.

  • @Yveaux Thanks for the tips

    you could have used a nano (saves separate usb socket)

    That is a good point, but I already bought 10x Pro Mini because I plan to also make some sensors where size matters (I would like to have sensors in standard light switch box). And my idea was to use the same HW as much as possible because of spare parts (I have no idea how reliable these things are - this is why did not solder it directly but used the headers) and also I can reuse experience from each sensor build for another (I expect that there are small details than might differ between different boards).

    I wonder how you go with all the cabling from here? I usually prefer a more decentralized approach, where each (or maybe a couple) sensor has its own node. These can then be battery powered and cabling will be minimal.

    I already have the wiring ready from the time when I build my house, actually these reed switches are drilled in to the window frame and all the cables lead to the same place - shed under stair that I call "server room" 🙂 Original idea was that it will be connected to some standard alarm system like Paradox or Honeywell.

    But with the planned room sensors I will most probably go for the battery powered sensors with some sort of radio connection (another aspect that mysensors solution will easily provide) as leading the cables now to each room would be very complicated and messy.

  • So I finally changed the splitter connector to USB micro and it is a bit more polished now 🙂


    I also connected it to openHab2 via MQTT binding. So in case any one is interested here is the configuration for the thing and items.

    Bridge mqtt:broker:openhab [ host="localhost" ] {
      Thing topic mag "Magnety" {
          Type contact : pin17  [ stateTopic="mag-out/0/17/1/0/16", on="1", off="0"]
          Type contact : pin16  [ stateTopic="mag-out/0/16/1/0/16", on="1", off="0"]
          Type contact : pin15  [ stateTopic="mag-out/0/15/1/0/16", on="1", off="0"]
          Type contact : pin14  [ stateTopic="mag-out/0/14/1/0/16", on="1", off="0"]
          Type contact  : pin1  [ stateTopic="mag-out/0/1/1/0/16", on="1", off="0"]
          Type contact  : pin0  [ stateTopic="mag-out/0/0/1/0/16", on="1", off="0"]
          Type contact  : pin2  [ stateTopic="mag-out/0/2/1/0/16", on="1", off="0"]
          Type contact  : pin3  [ stateTopic="mag-out/0/3/1/0/16", on="1", off="0"]
          Type contact  : pin4  [ stateTopic="mag-out/0/4/1/0/16", on="1", off="0"]
          Type contact  : pin5  [ stateTopic="mag-out/0/5/1/0/16", on="1", off="0"]
          Type contact  : pin6  [ stateTopic="mag-out/0/6/1/0/16", on="1", off="0"]
          Type contact  : pin7  [ stateTopic="mag-out/0/7/1/0/16", on="1", off="0"]
          Type contact  : pin8  [ stateTopic="mag-out/0/8/1/0/16", on="1", off="0"]
          Type contact  : pin9  [ stateTopic="mag-out/0/9/1/0/16", on="1", off="0"]
          Type contact : pin19  [ stateTopic="mag-out/0/19/1/0/16", on="1", off="0"]
          Type contact : pin18  [ stateTopic="mag-out/0/18/1/0/16", on="1", off="0"]
    Contact mag17 "Magnet 17" <window> { channel="mqtt:topic:openhab:mag:pin17" }
    Contact mag16 "Magnet 16" <window> { channel="mqtt:topic:openhab:mag:pin16" }
    Contact mag15 "Magnet 15" <window> { channel="mqtt:topic:openhab:mag:pin15" }
    Contact mag14 "Magnet 14" <window> { channel="mqtt:topic:openhab:mag:pin14" }
    Contact mag1  "Magnet 1"  <window> { channel="mqtt:topic:openhab:mag:pin1" }
    Contact mag0  "Magnet 0"  <window> { channel="mqtt:topic:openhab:mag:pin0" }
    Contact mag2  "Magnet 2"  <window> { channel="mqtt:topic:openhab:mag:pin2" }
    Contact mag3  "Magnet 3"  <window> { channel="mqtt:topic:openhab:mag:pin3" }
    Contact mag4  "Magnet 4"  <window> { channel="mqtt:topic:openhab:mag:pin4" }
    Contact mag5  "Magnet 5"  <window> { channel="mqtt:topic:openhab:mag:pin5" }
    Contact mag6  "Magnet 6"  <window> { channel="mqtt:topic:openhab:mag:pin6" }
    Contact mag7  "Magnet 7"  <window> { channel="mqtt:topic:openhab:mag:pin7" }
    Contact mag8  "Magnet 8"  <window> { channel="mqtt:topic:openhab:mag:pin8" }
    Contact mag9  "Magnet 9"  <window> { channel="mqtt:topic:openhab:mag:pin9" }
    Contact mag19 "Magnet 19" <window> { channel="mqtt:topic:openhab:mag:pin19" }
    Contact mag18 "Magnet 18" <window> { channel="mqtt:topic:openhab:mag:pin18" }

    Quick preview in openHab2 PaperUI

    I would also like to share the arduino code as I have some question.

    #include <Ethernet.h>
    #include <MySensors.h>
    #define SKETCH_NAME "Magnety"
    #define SKETCH_MAJOR_VER "1"
    #define SKETCH_MINOR_VER "0"
    byte pins[] = { 17, 16, 15, 14, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18 };
    int sentValues[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    MyMessage messages[16] = {
            MyMessage( 17, V_TRIPPED )
            ,MyMessage( 16, V_TRIPPED )
            ,MyMessage( 15, V_TRIPPED )
            ,MyMessage( 14, V_TRIPPED )
            ,MyMessage(  1, V_TRIPPED )
            ,MyMessage(  0, V_TRIPPED )
            ,MyMessage(  2, V_TRIPPED )
            ,MyMessage(  3, V_TRIPPED )
            ,MyMessage(  4, V_TRIPPED )
            ,MyMessage(  5, V_TRIPPED )
            ,MyMessage(  6, V_TRIPPED )
            ,MyMessage(  7, V_TRIPPED )
            ,MyMessage(  8, V_TRIPPED )
            ,MyMessage(  9, V_TRIPPED )
            ,MyMessage( 19, V_TRIPPED )
            ,MyMessage( 18, V_TRIPPED )
    void setup()
            byte pinsCount = sizeof( pins ) / sizeof( pins[0] );
            for( byte i = 0; i < pinsCount; i++ )
                    pinMode( pins[i], INPUT );
    void presentation()
            // Send the sketch version information to the gateway and Controller
            byte pinsCount = sizeof( pins ) / sizeof( pins[0] );
            for( byte i = 0; i < pinsCount; i++ )
                    //This seems not to be usefull with openhab mqtt bindings
                    //present( pins[i], S_DOOR );
                    //But this seems to be more uefull when controller is started after gateway
                    send( messages[i].set( sentValues[i] ) );
    void loop()
            int value;
            // Short delay to allow buttons to properly settle
            byte pinsCount = sizeof( pins ) / sizeof( pins[0] );
            for( byte i = 0; i < pinsCount; i++ )
                    value = digitalRead( pins[i] );
                    if ( value != sentValues[i] ) {
                            // Value has changed from last transmission, send the updated value
                            send( messages[i].set( value==HIGH ? 1 : 0 ) );
                            sentValues[i] = value;

    I noticed the gateway sends the presentation of sensors all the time but that is not of much use in my setup. On the other hand it is useful to send the actual state periodically as it is known without remembering or if the controller started later than GW. But I have a feeling that I might misused the presentation function. Does any one know if this approach is ok by the "mysensors way"?

    Of course I welcome any suggestion for improvments 🙂


Log in to reply

Suggested Topics

  • 8
  • 2
  • 90
  • 29
  • 2
  • 1