MQTT binding now available

How to integrate SPC Web Gateway with different Home Automation Systems
gol
Webbplatsadministratör
Posts: 72
Joined: Sun 01 Jun 2014, 19:38

MQTT binding now available

Post by gol »

I have written a nodejs script that mirror SPC status to a MQTT broker, e.g Mosquitto.
You find the code here: https://github.com/Goran58/node-spc-mqtt

//Goran
roryd
Posts: 9
Joined: Tue 21 Jun 2016, 16:05

Re: MQTT binding now available

Post by roryd »

Hi Goran,

I've successfully installed the web gateway software, and it is communicating properly with the SPC system. I've tried to install the node-spc-mqtt module from GitHub, and all goes fine until I try to start ./node-spc-mqtt.js. I get this:

Code: Select all

Connect Error: Error: socket hang up
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: socket hang up
    at TLSSocket.onHangUp (_tls_wrap.js:1048:19)
    at TLSSocket.g (events.js:260:16)
    at emitNone (events.js:72:20)
    at TLSSocket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickCallback (node.js:355:17)
1466523129: Socket error on client mqttjs_d544cc8f, disconnecting.
rory@openhab:~/node-spc-mqtt$
Any idea what I'm doing wrong?

Rory.
gol
Webbplatsadministratör
Posts: 72
Joined: Sun 01 Jun 2014, 19:38

Re: MQTT binding now available

Post by gol »

Hi Rory,
It seems you have an issue with the ssl/tls credentials. Please check that config.json setup is in accordance with your gateway settings. Try also to replace localhost in config.json with your IP address.

//Goran
roryd
Posts: 9
Joined: Tue 21 Jun 2016, 16:05

Re: MQTT binding now available

Post by roryd »

Hi Goran,

Yep, that was it - I changed "localhost" to my IP and it works fine :D

Thanks for that.

Rory.
erwin111
Posts: 4
Joined: Wed 07 Dec 2016, 11:48

Re: MQTT binding now available

Post by erwin111 »

Hi Goran,
I'am a licenced user of yr. SPC-Web-Gateway.
I have currently running a modified script on debian wheezy on RPI Mdl-1 w.o. problems for more than a jear.
I need to migrate to a faster platform, now testing to migrate the installation to an RPI-3 with jessie.
this is my environment:

Code: Select all

uname -a  Linux CL-RPI-2 4.4.34-v7+ #930 SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux
mosquitto version 1.4.10
node -v v5.12.0
nodejs v5.12.0
npm -v 3.8.6
installed version: spc-web-gateway-rp.1.2 
Now I'im testing yr. code from github:
My problem is on statement: var data = JSON.parse(reply); The code terminates with a TOKEN error as soon the 'invalid' json ccomes in.
As a temporary fix, i added a check for valid json and trow away the response.

Code: Select all

function getSpcStatus(
.
.
.
        res.on('end', function(){
            if (isJson(reply)) {  // check if valid json
               console.log("Get returned valid jsonstring: " + reply);
               var data = JSON.parse(reply);  // <-- Problem is here !!! 
               if (data.status === 'success'){
                  callback && callback(data.data);
               } else {
                  console.log("Unable to get data from SPC: " + uri);
               }
            } else {
               console.log("Get returned invalid string: " + reply);
            }
        });
.
.
Sample of valid/invalid json:

Code: Select all

Get returned valid jsonstring: {"status":"success","data":{"area":[{"id":"1","name":"Area1","mode":"1",<identical string here>"last_unset_user_name":"Lisa","last_alarm":"1479193713"}]}}
Get returned invalid string:   {"status":"success","data":"area":[{"id":"1","name":"Area1","mode":"1",<identical string here>"last_unset_user_name":"Lisa","last_alarm":"1479193713"}]}
Additional observation: 9 out of 10 get requests return valid json, 1 of 10 a invalid json. I've seen that only on area requests, never on zone requests???

any ideas ??
Thanks & best regards
erwin
gol
Webbplatsadministratör
Posts: 72
Joined: Sun 01 Jun 2014, 19:38

Re: MQTT binding now available

Post by gol »

Hi Erwin!
Interesting issue :? . What I can see in the invalid message one parenthesis is missing. How often is this happening, all the time or only intermittently?
//Göran
erwin111
Posts: 4
Joined: Wed 07 Dec 2016, 11:48

Re: MQTT binding now available

Post by erwin111 »

Hi Goran,

as said in my reply-edit,
1 out of 10 times I get the invalid json, but always on area requests, never on zone requests...
very strange!
erwin
gol
Webbplatsadministratör
Posts: 72
Joined: Sun 01 Jun 2014, 19:38

Re: MQTT binding now available

Post by gol »

Yes it seems very strange. I haven't heard this issue from other users of Raspberry Pi 3. Do you think you can log the EDP communication to see what the SPC-system is sending when you get the invalid the message?
You just have to start the spc-web-gateway with the -d option and redirect the output to a file.
erwin111
Posts: 4
Joined: Wed 07 Dec 2016, 11:48

Re: MQTT binding now available

Post by erwin111 »

Hi Goran,

not sure jet, it seems it was a user problem.....
When testing with the new system, i copied the definitions from the old system.
I was in error using the spc-web-gateway instance from the old system with the js-code from the new system...
So the gateway instance on the old raspberry had 2 clients!

Since I changed to the correct setup, the problem is gone! (last 12 hours)

Best regards & thanks
erwin
gol
Webbplatsadministratör
Posts: 72
Joined: Sun 01 Jun 2014, 19:38

Re: MQTT binding now available

Post by gol »

Glad to hear you found the problem.
//Göran
Post Reply