Tutorial-load js files/get device values/requests to servers

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
xchatter
Posts: 25
Joined: 27 Mar 2015 16:27

Tutorial-load js files/get device values/requests to servers

Post by xchatter »

Hi dudes,

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
When I am not sure what data index should i use, what properties etc.
, 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);
                        }
                }
});
So basicly the result is-> If the specified value of the sensor
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
tromper
Posts: 5
Joined: 11 Oct 2017 20:38

Re: Tutorial-load js files/get device values/requests to servers

Post by tromper »

Hey! nice tutorial! would be nice if you write a tutorial on how the data is fetched in the PHP script and how it's sent
to an email
trice001
Posts: 1
Joined: 22 Nov 2017 10:43

Re: Tutorial-load js files/get device values/requests to servers

Post by trice001 »

There are numerous great reasons why you should cripple java content in your programs. Appraisals aren't excessively precise yet something close to 5, making it impossible to 7 percent of web clients have impaired java content. That implies 1 out of each 20 guests won't not have the capacity to see your site. So guarantee that your site work legitimately with or without java content. There are numerous other web methods which can finish the vast majority of what you would likely utilize java content to do: Image rollovers: this can be expert by CSS by utilizing the 'drift' state to move the position of a component or demonstrate another component. Drift menus: there is no need of java content to make float menus. It is conceivable with the assistance of CSS. In some cases, you need to make cool impact with the assistance of javascript, you can make its menus however you should simply focus on how the menus function without java content. Ensure that it can even now be utilized to explore your site.
Thanks& Regards,
Angel anave
Web Design Company Toronto|Toronto Web Development | Web Development Toronto
Post Reply