Code Monkey home page Code Monkey logo

neeo-mystrom-switch's Introduction

NEEO driver for myStrom WiFi Switch

Control myStrom WiFi Switches with NEEO remote.

WiFi Switch v2 devices are auto-discovered on local subnet. Manual configuration is possible in config/mystrom.json.

Tested with:

Features

  • Device type: Accessory

    If you prefer 'NEEO light style recipes' for the power switches: change ./lib/neeoDevice.js to .setType('LIGHT')

  • Auto discovery of v2 WiFi switches

  • Optional manual configuration for v1 WiFi switches or different networks

  • Power on / off / toggle buttons, power switch

  • Power consumption is exposed as sensor value and text field

  • Local mode only: driver communicates directly with WiFi switch.

  • Also works with my Raspberry Pi power switch

  • Available as ready to use docker image

TODO

  • auto discovery of v1 WiFi switches (the ones without temperature sensor)
  • option to use myStrom cloud (either for initial discovery only or for full device access)
  • setting device reachability flag with connectivity test
  • improved error & auto retry handling

Requirements

Installation

Download or clone the code from github.

git clone https://github.com/zehnm/neeo-mystrom-switch.git

Install required packages with npm

cd neeo-mystrom-switch
npm install

Configuration

Driver settings

The driver can be configured in the optional configuration file ./config/driver.json. Default values are used if the file is missing (device server port 6336, auto discovery enabled).

Configuration options:

  • neeo
    • brainIp : IP address of the NEEO brain (optional).

      Auto discovery is active if not specified. See issue: NEEOInc/neeo-sdk#36

    • callbackIp : IP address of machine running the driver (optional).

      Most likely required if auto discovery doesn't work.

    • callbackPort : local port number for device server

  • mystrom.discoveryModes : specify which device discovery modes are active (at least one mode must be active)
    • configFile : true = read devices from configuration file which have a host property defined
    • local : true = local discovery mode (listen for UDP broadcast)
  • mystrom.localDiscovery : configuration section if mystrom.discoveryModes.local = true
    • listenAddress : listen address for UDP broadcast. 0.0.0.0 = all interfaces
    • reachableTimeout : timeout in seconds to consider a device offline if no discovery message received
    • deviceTypeFilter : only consider the specified myStrom device types (at the moment only "WS2" is supported)
  • mystrom.polling : device polling configuration. Polling is not required but useful if you also control the devices with the myStrom app and would like a faster synchronization with NEEO.
    • interval : optional polling interval in seconds. 0 = disable device polling, default = 4

    • duration : optional time duration in seconds to perform polling after activity has been detected. default = 60

      Note: to perform a single status update withouth further polling after NEEO requests the device state, set duration < interval. E.g. duration = 1

Example: Default Configuration

  • auto discover NEEO brain
  • run device driver on port 6336
  • auto discover myStrom WiFi switches v2 on all network interfaces
{
  "neeo": {
    "callbackPort": 6336
  },
  "mystrom": {
    "discoveryModes": {
      "configFile": false,
      "local": true
    },
    "localDiscovery": {
      "listenAddress": "0.0.0.0",
      "reachableTimeout": 30,
      "deviceTypeFilter": ["WS2"]
    }
  }
}

Example: Manual Configuration

  • specify IP address of NEEO brain
  • specify IP address and port of device driver
  • auto discover myStrom WiFi switches v2 on specified network interface
  • also read manually configured WiFi switches from device configuration file
{
  "neeo": {
    "brainIp": "192.168.1.172",
    "callbackIp": "192.168.1.165",
    "callbackPort": 6338
  },
  "mystrom": {
    "discoveryModes": {
      "configFile": true,
      "local": true
    },
    "localDiscovery": {
      "listenAddress": "192.168.1.0",
      "reachableTimeout": 30,
      "deviceTypeFilter": ["WS2"]
    }
  }
}

Device configuration

myStrom devices can be configured in ./config/mystrom.json:

  • manually configure myStrom devices with MAC address, name and IP address
    • required for v1 WiFi switches which cannot yet be auto discovered
    • optional for v2 WiFi switches where auto discovery doesn't work (other subnets, virtualization etc.)
  • MAC address to device name mapping in auto-discovery mode if you'd like to display a human readable name within NEEO app

Configuration format:

  • mystrom.devices : array of WiFi Switch configurations:
    • id : MAC address (e.g. 30aea400112233)

    • name : displayed name in NEEO

    • type : static value "switch"

      Every other value will be ignored, i.e. setting another name disables the device.

    • host : IP or host name with optional port number

      Only required if auto-discovery is not used.

      Assigning a static IP lease in your router is recommended.

Attention: once a device is used in NEEO the id is stored in the brain and cannot be changed anymore. I.e. the device must be deleted and re-added again.

Example:

  • WiFi Switch MAC address mapping to device names
  • Manual configuration of a WiFi switch on another network
  • Manual configuration of a Raspberry Pi power switch
{
  "mystrom": {
    "devices": [
      {
        "id": "30aea400112233",
        "name": "Office",
        "type": "switch"
      },
      {
        "id": "30aea400112244",
        "name": "TV",
        "type": "switch"
      },
      {
        "id": "30aea400112255",
        "name": "Cellar",
        "type": "switch",
        "host": "172.16.16.16"
      },
      {
        "id": "rpi-switch",
        "name": "Printer",
        "type": "switch",
        "host": "OfficePi.local:8080"
      }
    ]
  }
}

Start the driver

node index.js 

Logging Level

The logging level can be specified in the environment variable LOG_LEVEL. Valid values are: error, warn, info, debug, silly. The default level is info.

LOG_LEVEL=debug node index.js 

Docker

  • Based on node:9-alpine
  • Auto-discovery only works on Linux with host networking
docker run --rm --network=host zehnm/neeo-mystrom-switch:latest
  • Docker on OSX and Windows requires manual NEEO brain and myStrom device configuration with port mapping: -p 6336:6336
  • Custom driver.json and mystrom.json files can be mapped in a local configuration directory with: -v /MyDirectory/config:/neeo-driver-mystrom/config
  • LOG_LEVEL environment variable is also supported. E.g. --env LOG_LEVEL=debug
docker run --rm -p 6336:6336 --env LOG_LEVEL=debug -v ${CFG_PATH}/config:/neeo-driver-mystrom/config zehnm/neeo-mystrom-switch:latest

neeo-mystrom-switch's People

Contributors

zehnm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

neeo-mystrom-switch's Issues

Auto discovery of myStrom WiFi Switch v1 devices

The auto discovery of WiFi Switch v2 devices is already implemented and working fine. Version 2 is the currently available device with integrated temperature sensor. V1 devices have the same REST API but don't have a temperature sensor and don't announce themself by UDP broadcast on port 7979.

Enhancement: auto discovery for v1 switches.

Official information received from mystrom.ch support:

in case of WSW - WiFi Switch v1 it broadcast itself over UPNP.

Unfortunately I've been unable to discover a v1 switch with UPNP. I've tried various tools, libraries and my own code under Linux and OSX.
My analysis so far:

  • Port scan on v1 switch revealed standard UPNP port 1900:
PORT     STATE         SERVICE
1900/udp open|filtered upnp
  • myStrom v1 switch doesn’t respond to UPNP ssdp:discover requests to standard multicast address and port 239.255.255.250:1900. Tried with search target (ST) ssdp:all and upnp:rootdevice:
#!/usr/bin/env python
import socket

msg = \
    'M-SEARCH * HTTP/1.1\r\n' \
    'HOST:239.255.255.250:1900\r\n' \
    'MAN:"ssdp:discover"\r\n' \
    'MX:5\r\n' \
    'ST:ssdp:all\r\n' \
    '\r\n'

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.settimeout(10)
s.sendto(msg, ('239.255.255.250', 1900) )

try:
    while True:
        data, addr = s.recvfrom(8192)
        print addr, data
except socket.timeout:
    pass
  • myStrom v1 switch doesn’t advertise itself (multicasting advertisement messages to 239.255.255.250:1900) with ssdp:alive:
#!/usr/bin/env python
import socket
import struct

MCAST_GRP = '239.255.255.250'
MCAST_PORT = 1900

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((MCAST_GRP, MCAST_PORT))  # use '' instead of MCAST_GRP to listen to all groups on MCAST_PORT
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
    data, addr = sock.recvfrom(8192)
    print addr, data
  • No UPNP traffic detected with Wireshark, only MDNS Queries once in a while.

I’m able to discover all other UPNP devices on my network (router, Logitech Harmony, TV, Radio, NEEO remote, Plex), with upnp tools, Wireshark and my own code. Therefore I assume something is wrong or ‘special’ with the myStrom WiFi Switch v1.

Does anyone have an idea or further information?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.