Page 1 of 1

POST Z-Way device status to another server (webpage)

Posted: 05 Jun 2017 01:10
by Deidad292
Hello everyone, I have a problem and I was wondering if maybe some of you could help me, as I am more or less new in the programming and Z-Wave world.

I am trying to create a webpage, hosted in an Apache server (with PHP and mySQL) on the Raspberry, where I can see the values of my Fibaro Flood Sensor, more or less like an interface. Following an old post on this forum and Mr. Hekkers blog, I am currently executing a .js file in my z-way-server with the following code:

Code: Select all

var callback = function(type,arg) {
 try {
   var options = {};
   // timestamp
   var ts = (new Date()).getTime();
   ts = Math.floor(ts/1000).toString();
   // query local ZWave API
   options.url = "http://192.168.1.50:8083/ZWaveAPI/Run/devices[2].instances[0].commandClasses[156].data[5].sensorState.value"+ts; //sensor value
   options.method = "GET";
   var res = http.request(options);
   // send response to webpage
   options.url = "http://192.168.1.50:80";   // my Apache server on the Raspberry
   options.method = "POST";
   options.data = JSON.stringify(res);
   res = http.request(options);
 } catch(err) {
   debugPrint("Failed to upload device state changes to webpage: " + err);
 }
};

for (var dv in zway.devices) {
 var dv = zway.devices[dv];
 dv.data.bind(callback, null, true);
}
On my server I have a php file which supposedly recieves the POST with the sensor value, but I can't make it work and I do not see anything o my page. Is there any way I can check if this script is making the GET and POST request correctly? How can I recieve the POST request with the sensor info on my webpage?

Any info would be really appreciated, as I am really blocked right now.
Thank you very much and sorry if this is not the correct place to ask.

EDITED to add code tags :D

Re: POST Z-Way device status to another server (webpage)

Posted: 05 Jun 2017 11:42
by PoltoS
Sending commands from Z-Way to Z-Way is a bad idea (I assume 192.168.1.50:8083 is Z-Way itself). Beside this your code looks ok. If you use red = http.request(...), add options.async: false;

I would also suggest to search for http.request example through modules and start with a working example.

Re: POST Z-Way device status to another server (webpage)

Posted: 05 Jun 2017 15:59
by Deidad292
Thank you for your quick answer, I will look for examples.

Yes, what I am trying to do is use a GET method to take the sensor value each time something changes, for this I use the ZWaveAPi in the GET method and then POST it to my webpage. You mentioned this is a bad idea, in which other ways could I accomplish this? Thank you very much.

EDIT: Also, I am getting a weird connection error. Each time the state of the sensor changes (for example, I force the water alarm to switch ON), I lost connectiong temporarily with port 8083 (for example, in http://192.168.1.50:8083/smarthome/ or using the ZWave API). If I restart the server (sudo service z-way-server restart) i get this error:

[E] [core] result: Error: Error: Uncaught Error: Failed to bind to 8083
at Error (native)
at automation/main.js:73:5

Any idea what can be happening here? Used to go well before I started messing with the .js.

Re: POST Z-Way device status to another server (webpage)

Posted: 05 Jun 2017 20:43
by PoltoS
Bind problem is because another instance is using port 8083.

If you do server side code, don't use ZWaveAPI via HTTP, but use JS API.execute the code you place after /Run/

Re: POST Z-Way device status to another server (webpage)

Posted: 05 Jun 2017 23:39
by Deidad292
Thank you again for your answer. Ther was no more instances using port 8083, so I figured that mi own .js function should be the one causing the bind problem, because without it everything worked fine.

To solve this, I have changed the code to:

Code: Select all

//Bindings for value changes
console.log('###########CHECK 1############');
zway.devices[2].instances[0].commandClasses[156].data[5].sensorState.bind(function(){ //Binds event
                if(this.value){ //Notification is sensor is ON (255)
                console.log('###########CHECK 2############'+this.value);
                        try { //here begins the code for sending
                                var options = {};
                                options.method = "POST";
                                options.async = false;

                                // send data to server
                                options.url = "http://192.168.1.50:80/index.php";
                                options.method = "POST";
                                options.async = false;
                                options.data = "sensor_level='"+this.value+"'";  //this can be accessed in the index.php by $_POST['sensor_level']
                                var res = http.request(options);
                                console.log('###########CHECK 3############');
                        } 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);
                        }
                }
});

Using the information of this post: https://forum.z-wave.me/viewtopic.php?f=3422&t=21443

This solved the port 8083 problem. I put those CHECK to see if all the code was executed correctly, and they appear on the log so everything should be working. Still, I cannot get any value to show on my index.php page when using $_POST['sensor_level']. How can I know if the POST is being sent?

I will also try to use a GET method as before, but this time with the JS API instead of the ZWaveAPI.

Re: POST Z-Way device status to another server (webpage)

Posted: 06 Jun 2017 00:05
by PoltoS
it is worth to check for content-type used. You can also use tcpdump/wireshark to see what was actually transmitted to your page and was the encoding same us you expect.