Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Node.js module to talk to the gateway

Node.js module to talk to the gateway

Scheduled Pinned Locked Moved Development
9 Posts 5 Posters 2.1k Views 7 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • rakeshpaiR Offline
    rakeshpaiR Offline
    rakeshpai
    wrote on last edited by
    #1

    Long time lurker, first time poster.

    I've implemented a node.js module which you can include in your node projects, that does the dirty work of communicating with the serial gateway, and exposes a (hopefully) simple API to interact with your mysensors network.

    Find the project on Github or NPM.

    I've tried this with a Serial gateway, and it works fine. I haven't been able to test it with a Ethernet / WiFi gateway, but it should work fine with them as well.

    Please feel free to use this in your code, if you like, and let me know if you have any suggestions/feedback (or post an issue on Github).

    1 Reply Last reply
    5
    • hekH Offline
      hekH Offline
      hek
      Admin
      wrote on last edited by
      #2

      Welcome to the community @rakeshpai Nice when lurkers reveal themselves with a bit of code. :)

      1 Reply Last reply
      1
      • F Offline
        F Offline
        Fabien
        wrote on last edited by
        #3

        Nice work, I will look for a such solution.

        1 Reply Last reply
        0
        • danylevskyiD Offline
          danylevskyiD Offline
          danylevskyi
          wrote on last edited by
          #4

          Hi! Thank you for the contribution! I'm starting to learn Node.js and it looks like your code is great one as a starting point :+1:

          1 Reply Last reply
          0
          • rakeshpaiR Offline
            rakeshpaiR Offline
            rakeshpai
            wrote on last edited by
            #5

            @hek, @Fabien Thanks! When writing this module, I faced an issue that is probably worth bringing up. Details follow. Let me know if I'm doing it wrong.

            It appears that a sensor node's presentation data is only published when it is power-cycled. So, if my node.js module (or the app that includes it) has to restart for any reason, it can't figure out what the capabilities of nodes are, unless all nodes in the network are restarted too. To work around this, I've had to store the sensor nodes' presentation information to disk as a file in case the controller restarts. This works for now - it isn't ideal, since all nodes still need to be restarted at least once after the controller is executed the first time. Instead, I'd rather just query the nodes to get their presentation data whenever necessary. That way, not only do I avoid persistence, I also only need to rely on one source for the data - the sensor node itself - eliminating a large class of bugs due to data going out of sync with respect to the persisted config file.

            Some more details: Right now, my module has to do some trickery to persist data to disk, which involves an in-memory cache, and a timer to flush to disk. (This is necessary since presentation data can involve multiple packets, and issuing a FS write on every packet seems excessive, so some buffering seems appropriate.) To hide this behind the module's abstraction, the module does all the FS operations at a path that it deems appropriate, which is an uncomfortable choice. The module could ask the developer to specify a path for persistence, but that's yet another thing that developers have to do when using this module, with only a hard-to-communicate benefit, and that increases friction. Because we are dealing with the file system, the module also has to handle differences between Windows and Linux paths for cross-platform compatibility. Also, it isn't possible for two different apps to use the same module for the same network since data can get out of sync (the config is local to the app), unless the config files are stored in a shared location, which opens another can of worms (FS watchers or some form of IPC would be required, for example.). I could have avoided this FS jiggery-pokery by taking a dependency on a DB - the official NodeJsController, for example, requires mongodb - but that's an additional thing in the stack that needs to be up-and-running for the module to work, and requires additional configuration in the code to communicate with the DB anyway. The FS operations seem like a milder issue in comparison, in the usually-works-in-the-common-cases sense, but it would be great if I could avoid it altogether. After all, the sensor node knows its presentation, so it's much more authoritative to simply ask the node itself.

            I hope all that made sense, and I haven't overstated the problem. A solution in my mind would be to REQ a node for its presentation data when needed, and the node could RES with its presentation. The docs don't say that this is possible, but my reading of the source IIRC tells me that might be coming in v2.0. Is that right? If not, is this something we can consider for future releases? (I would have contributed, but my C/C++ is very weak.)

            @danylevskyi. Thanks. Not sure i can deal with the pressure, though. ;)

            1 Reply Last reply
            0
            • hekH Offline
              hekH Offline
              hek
              Admin
              wrote on last edited by
              #6

              In 2.0 beta, you can query the node by sending INTERNAL/PRESENT message to it. But it will only work for awake nodes that is listening.

              rakeshpaiR 1 Reply Last reply
              0
              • hekH hek

                In 2.0 beta, you can query the node by sending INTERNAL/PRESENT message to it. But it will only work for awake nodes that is listening.

                rakeshpaiR Offline
                rakeshpaiR Offline
                rakeshpai
                wrote on last edited by
                #7

                @hek Thanks! That's neat! I'm not sure about this - is it possible to ask a node for its presentation, just when I receive data from it? Will it be awake?

                For example, if a sensor says that its state is 'triggered', and is expected to sleep right afterwards, do I still have time to ask it for its presentation data? Is the sensor going to wait for an INTERNAL request? If not, what is the best time to ask for data?

                1 Reply Last reply
                0
                • hekH Offline
                  hekH Offline
                  hek
                  Admin
                  wrote on last edited by
                  #8

                  Yes, if node uses "smart sleep"

                  https://github.com/mysensors/Arduino/pull/271

                  1 Reply Last reply
                  0
                  • epierreE Offline
                    epierreE Offline
                    epierre
                    Hero Member
                    wrote on last edited by epierre
                    #9

                    Hello,

                    I did made one in perl for my experiments:

                    https://github.com/empierre/domoticz/blob/master/mysensors-gw1.4.pl

                    If you are interested in having an interface to Imperihome, or just for continuous test/delivery/packaging for all PI and more on node JS, I can help (this is the project taking me time at the moment, full rewrite from perl to node js of an app used by more than 100 people):

                    https://github.com/empierre/MyDomoAtHome

                    z-wave - Vera -> Domoticz
                    rfx - Domoticz <- MyDomoAtHome <- Imperihome
                    mysensors -> mysensors-gw -> Domoticz

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    16

                    Online

                    11.7k

                    Users

                    11.2k

                    Topics

                    113.0k

                    Posts


                    Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • MySensors
                    • OpenHardware.io
                    • Categories
                    • Recent
                    • Tags
                    • Popular