First of all, let me set things straight:
-I am new in this field, so if there are any mistakes, please help me fix them
-This tutorial is to help other new guys, so they struggle a little less
I couldn't find much help about how to use my own js files, what
libraries are available there, how exactly are devices selected in
this js, can i send data to my server so i can set some notifications
on value changes etc.
I managed to get some basic things working thanks to an article i
found by Robert Hekkers(i won't paste link, because i don't know if i
should advertise). So everything you will read below is thanks to Mr.Hekkers' articles.
So here is what i can share with you and i hope it will be useful to
some fols and if anyone can share some tutorials too, it will be
great.
This tutorial will show you how to:
-load your own js file in your z-way server;
-how to monitor if your file is loaded and your functions executed;
-how to get devices values in your js;
-how to bind js code to device value changes and send data to server, so you can work with it(in my case i
send notifications on sensor values changes);
-OPTIONAL: if anyone is interested, i will share also a basic
tutorial how the data is fetched in the PHP script and how it's sent
to my email;
*Everything is done on the raspberry using its browser/file manager/terminal
1. You can easily load your js files in the server by going to
/opt/z-way-server/automation. There you will find a file called
main.js. In this file you should put the command to load your own
file. Just add the following before the closing bracket at the end of
the file:
executeFile('fileName.js');
Now each time the server is started, it will load your fileName.js.
Create your file in the same directory where is main.js is and put some
test comment on the first line so you can check if it is loaded on
your server.
2. For monitoring if your file is loaded and your functions executed,
you will need to open the server log in the terminal and follow what's
happening there.
Open terminal and type 'tail -f /var/log/z-way-server.log' . Now the
server log will show in the terminal. Next thing is to restart your
z-way server, so you can check if your file is loaded. Type in
terminal 'sudo service z-way-server restart'. The server will restart
displaying a lot of information in the window where you executed the tail -f command, but what you need is at the bottom.
You should see something like:
[2015-04-03 11:35:44.759] [core] Executing script: //My test comment ...
If you see this, then you are ready to code your own js in the file. If not, check if you did all things right.
*Note that when you make changes, you should restart the z-way server.
3.The devices' parameters can be accessed in js by using something like:
Code: Select all
zway.devices[3].SensorMultilevel.data[1].val.value
, I usually open browser and make use of the ZwayAPI to run commands
in the browser. You can use something like:
http://192.168.1.101:8083/ZWaveAPI/Run/ ... level.data
where the ip is the ip of your raspberry, devices[3] is the device you want
to select and adding 'data' to the end will return in
your browser a JSON formatted information where you can find the
properties you can fetch for this device. Just put the above in your browser's address bar and press enter(mind that you have to specify your device device[x].name).
4.For binding events on device values changes and sending data to my
server i ended up with the following script(in fileName.js):
Code: Select all
//Bindings for value changes
zway.devices[3].SensorMultilevel.data[3].val.bind(function(){ //This
binds event if this value is changed
if(this.value){ //This condition is because this value is only boolean and i want notification only if it is true
try { //here begins the code for sending
var options = {};
options.method = "POST";
// send data to server
options.url = "http://www.example.com/razberry/index.php";
options.method = "POST";
options.data = "light_level='"+this.value+"'"; //this can be accessed in the index.php by $_POST['light_level']
var res = http.request(options);
} catch(err) { // here is the code to display the error in the console server log if something is wrong
debugPrint("Failed to send data: " + err);
}
}
});
changes to "true", the "true" value will be sent to my server by POST
type.
*Note that if you do this with values that change frequently, you will get a lot of spam, so better think of some conditions.
I hope this helps some new guys like me to better understand some things and to dive into this cool razberry world faster.
Regards,
Nick