Communication with Fhem via websockets ?

Begonnen von AlxDmr, 03 November 2014, 19:58:17

Vorheriges Thema - Nächstes Thema

AlxDmr

Hello,
is there a way to establish communication with fhem via websocket (or a classical TCP) so that it is possible to receive events from devices as well as sending command (e.g. turn off a light). I know that it is possible to establish a telnet connection but it seems to me not suitable to retrieve and send such data. Maybe I am wrong as I don't know this protocol very well...?
Actually, I am trying to integrate enOcean devices using Fhem for a personnal smart Home project. I still developped some code using NodeJS and would like to interoperate with Fhem.

    Alex.

ntruchsess

you may checkout integrating NodeJS with FHEM using websockets via mqtt-protocoll using latest mosquitto (compiled with websocket-support). As far as I know there are some existing solutions to access mqtt from nodeJS. Use fhem-modul MQTT and MQTT_BRIDGE to translate your existing fhem enocean-devices to mqtt-topics (both in- and outbound).

- Norbert
while (!asleep()) {sheep++};

rudolfkoenig

Alternatives:
- plain TCP/IP (known in FHEM telnet), with or without SSL
- HTTP/HTTPS via longpoll (used by the default WEB frontend).

AlxDmr

The MQTT option sounds quite good to me :)
However I updated Fhem but I have no MQTT module.
I'm brand new in the world of FHEM so maybe I missed something?

fruit

I know nothing of MQTT but update should have brought the MQTT modules in, they should be in /opt/fhem/FHEM/ as 00_MQTT.pm, 10_MQTT_BRIDGE.pm and 10_MQTT_DEVICE.pm

If they are not there try update force
Feel free to follow up in German if you prefer

AlxDmr

It was may fault, I was using Unix apt-get update ...
after using telnet : update  I got everything I need :)
Thanks, now I can start to play :)

ntruchsess

#6
just to let you know: I started implementing websocket-support for fhem. It will allow to execute the quivalent commands as telnet and get notified for all events. Not ready yet (I have to build a websocket-testclient first), but maybe worth a first look: 98_websocket.pm
The main difference to using the mqtt-bridge and accessing mqtt via websockets is that mqtt will only publish and subscribe selected fhem-devices while direct websocket access will allow to control fhem completely. The advantage of the mqtt-way is that it is standard and easy to understand when you have used mqtt before. The websocket-interface will be more lowlevel and fhem-like.

- Norbert
while (!asleep()) {sheep++};

rudolfkoenig

@Norbert: iOS devices have problems with communicating to FHEMWEB vial longpoll: as long a connection with longpoll is open, no second one will be opened by the iOS device, meaning that you cannot load images from FHEMWEB with activated longpoll. The current workaround is to use SVG icons, as they are transmitted "in-line". I hope that by using websockets, which is (are?) supported by all current browsers, we can avoid this problem.
It would be nice if FHEMWEB could somehow use your module, so that it is not implemented twice in FHEM.

AlxDmr

Sorry again for the stupid question but...
now I get the required file, however when I try to add :
    define localMQTT  MQTT  192.168.1.11:1883
in fhem.cfg, but I get the error :
    ERROR:

    Cannot load module MQTT

I searched for specific loading module command but did not found anything. Where did I went wrong ?


ntruchsess

#9
you need to install Net-MQTT from CPAN first.

(as an alternative you currently might check out the files from fhem-svn as being available on cpan they are not part of fhem-update and are likely to be removed from fhem-svn in the future).
while (!asleep()) {sheep++};

ntruchsess

Zitat von: rudolfkoenig am 08 November 2014, 11:56:05
It would be nice if FHEMWEB could somehow use your module, so that it is not implemented twice in FHEM.
@rudolfkoenig: sure, as any other webapp FHEMWEB will be able to use websockets. It replaces longpoll entirely.

here is first working version. Does execute all fhem-commands (like telnet):
98_websocket.pm
simple testclient:
websocket.html

required modules from cpan:JSON and Protocol::WebSocket (and Data::Dumper ...)

have fun!

- Norbert
while (!asleep()) {sheep++};

rudolfkoenig

And how can FHEMWEB rewrite the events, supplying its own data, e.g. the state as icon or inline SVG?

ntruchsess

as this is just the starting point getting the communication running this is open for discussion. From my point of view webbsocket acces is primary for pushing pure data, not design, but I'm open fur suggestions. Did you check whether iOS does limit open websockets as it does with longpolling requests?
while (!asleep()) {sheep++};

herrmannj

it is not a big deal to simply replace the existing longpoll with websockets. It can be entirely done within fhemweb.js - provided that the websocket delivers the same data. (simple hook within fhemweb.pm). That way the direction browser fhem (the way back) would be the usual xhr way.

but I would suggest to do a broader rewrite and give it the ability to transfer more structured data.


justme1968

this (structured JSON? data) would make the parsing of the message in fhemweb.js mich simpler and should also avoid the current duplication Insider the messages.
hue, tradfri, alexa-fhem, homebridge-fhem, LightScene, readingsGroup, ...

https://github.com/sponsors/justme-1968