rfxcom / node-rfxcom

Node.js client for talking to an RFXtrx433 device
MIT License
61 stars 45 forks source link

New klikaanklikuit devices #88

Closed studioemrys closed 4 years ago

studioemrys commented 5 years ago

Hello everyone,

I just found out that new klikaanklikuit wall outlet switches use a different ID than the old ones. First of all, to use a klikaanklikuit device you should use lightning2 and AC (at least in the Netherlands). Do not forget to call for the initialise function as not mentioned in the readme.md. This example does work:

var rfxtrx = new rfxcom.RfxCom("/dev/ttyUSB0", {debug: true}),
lighting2 = new rfxcom.Lighting2(rfxtrx, rfxcom.lighting2.AC);

rfxtrx.initialise(function() { 

    lighting2.switchOn("0xF09AC8AA/1");
    lighting2.switchOff("0xF09AC8AA/1");

});

This will work nicely with the older remote controls that all have an id constructed as 0x**. But the new remote controls have an id constructed as 1x**. However if you use '1xF09AC8AA/1' for example, the result will be unpredictable ranging from no action to turning of every klikaanklikuit device in your house. It all has to do with the rfxcom.js script in the lib directory. A function called stringToBytes can handle 0x** but not 1x**. The solution however is extremely simple:

rewrite 1xF09AC8AA/1 to 1F09AC8AA/1, so you get:

var rfxtrx = new rfxcom.RfxCom("/dev/ttyUSB0", {debug: true}),
lighting2 = new rfxcom.Lighting2(rfxtrx, rfxcom.lighting2.AC);

rfxtrx.initialise(function() { 

    lighting2.switchOn("1F09AC8AA/1");
    lighting2.switchOff("1F09AC8AA/1");

});
maxwellhadley commented 5 years ago

That doesn't seem right! according to the documentation I have, the ID for a KlikAanKilkUit can be in the range 0x0000 0001 to 0x03FF FFFF, so the original remote ID of 0xF09A C8AA is out of range and should give an error! It looks like the README.md file still contains an example dating from before the implementation of range checking, which just happened to work with the current RFXtrx firmware version at that time. It looks like this masks out the bottom 2 bits of the MSB of the ID, ignoring the rest.

StringToBytes always interprets the ID as a hexadecimal string, whether or not it starts with the 0x mark. 1x is not a valid hexadecimal string, but it looks like the ParseInt() implementation in the version of NodeJS you are using does not throw an error.

If you listen to the lighting2 event, what are the actual values of evt.id received from your remote control? These too should be limited to the valid range.

studioemrys commented 5 years ago

If i use the windows version of rfxcom manager I see that the id of the remote control is 1xE9EDCA. Being a hobby programmer and far from a professional I started to find a way to use this ID. As I saw that it was rejected in the rfxcom.js file because it started with 1x, I luckily found a way to pass by this error-check and use my rfxcom. If it comes to use of hexadecimal numbers i'm an absolute noob. So I think it is interesting for the more "scripted" ones among us to see what the real problem is and how to repair it. Thanks for your comments on this, I really like the use of an rfxcom with nodejs. I'm currently writing my own domotics in javascript.

studioemrys commented 5 years ago

To answer your question, somehow I found out to do an event listener on the rfxcom by nodejs instead of the rfxcom manager. The output of the complete event object is:

{ subtype: 0, seqnbr: 0, id: '0x01E9EDCA', unitCode: 1, commandNumber: 0, command: 'Off', level: 0, rssi: 6 }

So the ID is 8 digits instead of 6 digits. And I think that 8 digits was rejected by the rfxcom.js, but I'm not certain. If so, this is a bug in the code.

maxwellhadley commented 5 years ago

The ID value of 0x01E9_EDCA you received is within the accepted range of 0x0000_0001 to 0x03FF_FFFF, so that part of it makes sense. It may be that the version of rfxmngr.exe you are using is displaying the received ID bytes incorrectly. The example code in README.md is definitely wrong though - I must fix it!

Have you come across NodeRED? It is a flow-based, graphical programming environment for NodeJS. A lot of people are using it to implement home automation/domotics systems. I also maintain a package node-red-contrib-rfxcom, based on this package, which lets you use the RFXtrx in the NodeRED environment. It is much simpler than trying to write the whole application in Javascript

maxwellhadley commented 4 years ago

Example code fixed in #90 (version 2.1.0)