Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Du möchtest Deinen Automower in Deine Hausautomatisierung einbinden oder eine eigene App entwicklen?
Hier wird diese offene Schnittstelle zum Robonect Hx Modul beschrieben.
msan
Beiträge: 19
Registriert: Fr 12. Mai 2017, 12:30
Wohnort: Sweden
Mäher: Husqvarna Automower 420
Firmware des Mähers (MSW): MSW 7.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.3)
Robonect Firmware: V1.0 Beta 3

Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von msan »

This method is deprecated since we should use MQTT instead now when it's supported.

Hi there.

I've created a NodeJS Module for communication with Robonect enabled mowers.

The project has just started and it can be found at https://github.com/allan-gam/node-robonect-api

The purpose is to constantly watch the status of the mower and send event updates to your home automation system. In my case I use Domoticz but it can be used for any home automation system that has a web API.

Currently the script polls the Robonect status every 5 seconds. It might happen that it can be done in a better way. Ideally there would be an even stream in Roboconect API to connect to.

My project is just a start and it's my first NodeJS app. Maybe there are some experienced guys among you who'd like to improve it. In such case, collaborators/co developers are warmly welcomed.

Below is a script example how I use it to send events to Domoticz.

Code: Alles auswählen

#!/usr/bin/nodejs

var STATUS = new Array();
STATUS[0] = "Status is determined";
STATUS[1] = "Parked";
STATUS[2] = "Moving";
STATUS[3] = "Looking";
STATUS[4] = "Loading";
STATUS[5] = "Searching";
STATUS[7] = "Error";
STATUS[8] = "Lost signal";
STATUS[16] = "Switched off";
STATUS[17] = "Sleeping";

var TIMERSTATUS = new Array();
TIMERSTATUS[0] = "Disabled";
TIMERSTATUS[1] = "Active";
TIMERSTATUS[2] = "Stand by";

var mower = require('node-robonect-api');
var request = require("request");

// Options:
var options = {
	host : '192.168.1.111',
	port : '80',
	user : 'john',
	pass : 'smith',
	log  : false,
};

var robonect = new mower.robonect(options);

// Monitor Glenn, the lawn mower
robonect.on('mowerEvent', function(mowerName, category, eventType, eventValue) {
	//console.log(mowerName, category, eventType, eventValue);
	if (category === 'status' && eventType === 'status') setDomoDevice(mowerName, eventType, 568, STATUS[eventValue])
	if (category === 'timer' && eventType === 'status') setDomoDevice(mowerName, eventType, 569, TIMERSTATUS[eventValue])
	if (category === 'status' && eventType === 'battery') setDomoDevice(mowerName, eventType, 570, eventValue)
});

function getDateTime() {
	var date = new Date();
	var hour = date.getHours();
	hour = (hour < 10 ? "0" : "") + hour;
	var min = date.getMinutes();
	min = (min < 10 ? "0" : "") + min;
	var sec = date.getSeconds();
	sec = (sec < 10 ? "0" : "") + sec;
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	month = (month < 10 ? "0" : "") + month;
	var day = date.getDate();
	day = (day < 10 ? "0" : "") + day;
	return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
}

function setDomoDevice(mowername, eventType, idx, sValue) {
	request("http://domoticzhost:8080/json.htm?type=command&param=udevice&nvalue=0&idx=" + idx + "&svalue=" + sValue, function(error, response, body) {
	console.log(getDateTime() + ' ' + mowername + ' ' + eventType + ' ' + idx + ' ' + sValue);
})
}

function printOut(mowername, eventType, idx, sValue) {
	console.log(getDateTime() + ' ' + mowername + ' ' + eventType + ' ' + idx + ' ' + sValue);
}
(I got the idea from the following project and I have copied some code from it as well: https://github.com/nayrnet/node-hikvision-api/ )

Cheers!
Zuletzt geändert von msan am Mo 23. Apr 2018, 17:30, insgesamt 1-mal geändert.

Cloud
Beiträge: 16
Registriert: Sa 17. Jun 2017, 13:12
Wohnort: München
Mäher: Gardena R70Li
Firmware des Mähers (MSW): MSW 6.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.4)
Robonect Firmware: V.0.0 18.06.2017

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von Cloud »

Hi msan,

I love your work here because im using domoticz as well. Will try to run this on my raspberry in the next days. I need a nodejs server for this, right?

msan
Beiträge: 19
Registriert: Fr 12. Mai 2017, 12:30
Wohnort: Sweden
Mäher: Husqvarna Automower 420
Firmware des Mähers (MSW): MSW 7.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.3)
Robonect Firmware: V1.0 Beta 3

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von msan »

Cloud hat geschrieben:
So 18. Jun 2017, 16:23
Hi msan,

I love your work here because im using domoticz as well. Will try to run this on my raspberry in the next days. I need a nodejs server for this, right?
Hi there @cloud

I've added an installation instruction at https://github.com/allan-gam/node-robon ... /README.md
I haven't been able to try it out by myself though. If you encounter any problems following it, please let me know. Good luck!

Cloud
Beiträge: 16
Registriert: Sa 17. Jun 2017, 13:12
Wohnort: München
Mäher: Gardena R70Li
Firmware des Mähers (MSW): MSW 6.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.4)
Robonect Firmware: V.0.0 18.06.2017

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von Cloud »

Wow thanks for the fast response and the installation instructions!

First i had the following error:

Code: Alles auswählen

module.js:472
    throw err;
    ^

Error: Cannot find module 'request'
So after the nodejs installation i had to install the request module as well with the command:

Code: Alles auswählen

npm install request
to make it work.

Then i had the following error while executing the script

Code: Alles auswählen

events.js:168
      throw err;
      ^

Error: Uncaught, unspecified "error" event. (FAILED TO QUERY MOWER STATUS)
    at robonect.emit (events.js:166:17)
    at Request._callback (/home/pi/node_modules/node-robonect-api/robonect.js:40:10)
    at self.callback (/home/pi/node_modules/request/request.js:188:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:191:7)
    at Request.onRequestError (/home/pi/node_modules/request/request.js:884:8)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at Socket.socketErrorListener (_http_client.js:358:9)
    at emitOne (events.js:96:13)
I have no user and password in the robonect config so i just let the fields empty in the script config. Turns out this didnt work. After i configured a user it worked like a charm.

I have the variables now in domoticz and loving it. How can i config a faster polling interval like 30-60 seconds? (EDIT: just saw its already on 5 seconds, perfect!) And what will happen if the mower isnt reachable?

Thank you very much for your work!

msan
Beiträge: 19
Registriert: Fr 12. Mai 2017, 12:30
Wohnort: Sweden
Mäher: Husqvarna Automower 420
Firmware des Mähers (MSW): MSW 7.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.3)
Robonect Firmware: V1.0 Beta 3

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von msan »

Cloud hat geschrieben:
Mo 19. Jun 2017, 10:48
Wow thanks for the fast response and the installation instructions!
Thanks for your feedback! I've added the "request" installation command now.

The polling interval is hard coded in robonect.js and it's currently set to 10 seconds. In a future version, I hope that we won't have to poll the mower constantly but instead receiving an event stream. (See this thread)

Anyway, you can change the polling interval if you wish but it will be overwritten whenever you update node-robonect-api.

I've added the requirement for a username and a password in the Prerequisits. I might have a look later if I can remove this requirement.

If the mower fails to respond for any reason, an error will be thrown. In my example script, I didn't handle this condition so the script crashed due to unhandled error. I've updated the example script in the installation instruction so that an error instead of crashing the script, makes it try again after the polling time out. Thanks for pointing out that.

Code: Alles auswählen

robonect.on('error', (err) => {
	// Handle the error here.  
	console.error('whoops! there was an error');
});
Cheers!

Cloud
Beiträge: 16
Registriert: Sa 17. Jun 2017, 13:12
Wohnort: München
Mäher: Gardena R70Li
Firmware des Mähers (MSW): MSW 6.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.4)
Robonect Firmware: V.0.0 18.06.2017

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von Cloud »

Till we get the constant event stream your script will do the job nicely.

Some ideas to add to the script if you want to do so:

- Fill the STATUS field with "Disconnected" if the Mower isnt reachable. So i can use this event in Domoticz (like Messaging that the mower is stolen) :P .

- Get the last error in another virtual Text device so i can send messages with the specific error message.

msan
Beiträge: 19
Registriert: Fr 12. Mai 2017, 12:30
Wohnort: Sweden
Mäher: Husqvarna Automower 420
Firmware des Mähers (MSW): MSW 7.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.3)
Robonect Firmware: V1.0 Beta 3

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von msan »

Cloud hat geschrieben:
Mo 19. Jun 2017, 13:26
Till we get the constant event stream your script will do the job nicely.

Some ideas to add to the script if you want to do so:

- Fill the STATUS field with "Disconnected" if the Mower isnt reachable. So i can use this event in Domoticz (like Messaging that the mower is stolen) :P .

- Get the last error in another virtual Text device so i can send messages with the specific error message.
Thanks for your ideas.

If you update to the latest version, you will be able to get the last response status code in your custom script. See https://github.com/allan-gam/node-robon ... /README.md

Code: Alles auswählen

robonect.on('responseStatusCode', (statusCode) => {
	// Handle the response status code here
	if (statusCode != 200) {
		console.error(statusCode);
	}
});
You can do whatever you wish with that status code in your custom script. If You'd like to update a domoticz text device you could try something like this:

Code: Alles auswählen

robonect.on('responseStatusCode', (statusCode) => {
	// Handle the response status code here
	if (statusCode != 200) {
		console.error(statusCode);
		setDomoDevice(mowerName, eventType, 99999, statusCode)
	}
});
As you see, it's quite flexible. ;)

Cloud
Beiträge: 16
Registriert: Sa 17. Jun 2017, 13:12
Wohnort: München
Mäher: Gardena R70Li
Firmware des Mähers (MSW): MSW 6.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.4)
Robonect Firmware: V.0.0 18.06.2017

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von Cloud »

Thank you :-)

updating with npm update node-robonect-api gives me the following error:

Code: Alles auswählen

npm ERR! Linux 4.4.38-v7+
npm ERR! argv "/usr/bin/nodejs" "/usr/local/bin/npm" "update" "node-robonect-api"
npm ERR! node v7.10.0
npm ERR! npm  v2.15.11
npm ERR! code E404

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/node-robonect-api
npm ERR! 404 
npm ERR! 404 'node-robonect-api' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/pi/npm-debug.log
npm-debug.log:

Code: Alles auswählen

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'update',
1 verbose cli   'node-robonect-api' ]
2 info using npm@2.15.11
3 info using node@v7.10.0
4 verbose config Skipping project config: /home/pi/.npmrc. (matches userconfig)
5 silly mapToRegistry name node-robonect-api
6 silly mapToRegistry using default registry
7 silly mapToRegistry registry https://registry.npmjs.org/
8 silly mapToRegistry data Result {
8 silly mapToRegistry   raw: 'node-robonect-api',
8 silly mapToRegistry   scope: null,
8 silly mapToRegistry   name: 'node-robonect-api',
8 silly mapToRegistry   rawSpec: '',
8 silly mapToRegistry   spec: 'latest',
8 silly mapToRegistry   type: 'tag' }
9 silly mapToRegistry uri https://registry.npmjs.org/node-robonect-api
10 verbose request uri https://registry.npmjs.org/node-robonect-api
11 verbose request no auth needed
12 info attempt registry request try #1 at 09:11:17
13 verbose request id 01c1a6f356c9cb93
14 http request GET https://registry.npmjs.org/node-robonect-api
15 http 404 https://registry.npmjs.org/node-robonect-api
16 verbose headers { 'content-type': 'application/json',
16 verbose headers   'cache-control': 'max-age=0',
16 verbose headers   'content-length': '2',
16 verbose headers   'accept-ranges': 'bytes',
16 verbose headers   date: 'Tue, 20 Jun 2017 07:11:18 GMT',
16 verbose headers   via: '1.1 varnish',
16 verbose headers   age: '0',
16 verbose headers   connection: 'keep-alive',
16 verbose headers   'x-served-by': 'cache-hhn1545-HHN',
16 verbose headers   'x-cache': 'MISS',
16 verbose headers   'x-cache-hits': '0',
16 verbose headers   'x-timer': 'S1497942678.251244,VS0,VE672',
16 verbose headers   vary: 'Accept-Encoding' }
17 silly get cb [ 404,
17 silly get   { 'content-type': 'application/json',
17 silly get     'cache-control': 'max-age=0',
17 silly get     'content-length': '2',
17 silly get     'accept-ranges': 'bytes',
17 silly get     date: 'Tue, 20 Jun 2017 07:11:18 GMT',
17 silly get     via: '1.1 varnish',
17 silly get     age: '0',
17 silly get     connection: 'keep-alive',
17 silly get     'x-served-by': 'cache-hhn1545-HHN',
17 silly get     'x-cache': 'MISS',
17 silly get     'x-cache-hits': '0',
17 silly get     'x-timer': 'S1497942678.251244,VS0,VE672',
17 silly get     vary: 'Accept-Encoding' } ]
18 verbose stack Error: Registry returned 404 for GET on https://registry.npmjs.org/node-robonect-api
18 verbose stack     at makeError (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:302:12)
18 verbose stack     at CachingRegistryClient.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:280:14)
18 verbose stack     at Request._callback (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:210:14)
18 verbose stack     at Request.self.callback (/usr/local/lib/node_modules/npm/node_modules/request/request.js:187:22)
18 verbose stack     at emitTwo (events.js:106:13)
18 verbose stack     at Request.emit (events.js:194:7)
18 verbose stack     at Request.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/request/request.js:1044:10)
18 verbose stack     at emitOne (events.js:96:13)
18 verbose stack     at Request.emit (events.js:191:7)
18 verbose stack     at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/request/request.js:965:12)
19 verbose statusCode 404
20 verbose pkgid node-robonect-api
21 verbose cwd /home/pi
22 error Linux 4.4.38-v7+
23 error argv "/usr/bin/nodejs" "/usr/local/bin/npm" "update" "node-robonect-api"
24 error node v7.10.0
25 error npm  v2.15.11
26 error code E404
27 error 404 Registry returned 404 for GET on https://registry.npmjs.org/node-robonect-api
27 error 404
27 error 404 'node-robonect-api' is not in the npm registry.
27 error 404 You should bug the author to publish it (or use the name yourself!)
27 error 404
27 error 404 Note that you can also install from a
27 error 404 tarball, folder, http url, or git url.
28 verbose exit [ 1, true ]

bezibaerchen
Beiträge: 87
Registriert: Mi 4. Mai 2016, 14:57
Mäher: Gardena R40Li
Firmware des Mähers (MSW): MSW 5.xx.xx
Herstellungsjahr: 2015
Robonect-Modul: Robonect Hx (Rev.2)
Robonect Firmware: 0.9e

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von bezibaerchen »

Try node update, which would update everything. Maybe the name mismatches.
MSW-Software: R40Li
MSW-Version: 5.01.00
MSW-Datum: 2014-10-10 13:44:38
SUB-Version: 5.00.00

msan
Beiträge: 19
Registriert: Fr 12. Mai 2017, 12:30
Wohnort: Sweden
Mäher: Husqvarna Automower 420
Firmware des Mähers (MSW): MSW 7.xx.xx
Herstellungsjahr: 2016
Robonect-Modul: Robonect Hx (Rev.3)
Robonect Firmware: V1.0 Beta 3

Re: Using a node.js module (node-robonect-api) for accessing robonect events in a Home Automation Scenario

Beitrag von msan »

Cloud hat geschrieben:
Di 20. Jun 2017, 09:14
Thank you :-)

updating with npm update node-robonect-api gives me the following error:
...
It works differently here.

Can you try

Code: Alles auswählen

cd ~/node_modules/node-robonect-api&&npm update
You can list the current version by issuing:

Code: Alles auswählen

npm ls|grep robonect
It should be 0.9.0

Antworten

Zurück zu „API für Robonect® Hx“