Sending data to server using JS automation engine

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
tromper
Posts: 5
Joined: 11 Oct 2017 20:38

Sending data to server using JS automation engine

Post by tromper »

Hello everyone, I have a problem and was wondering if maybe some of you could help me, as I am more or less new in Z-Wave world.

I am trying to create a webpage, hosted in an Apache server (with PHP and mySQL) on the Raspberry, where I would like to see the values of my Fibaro Flood Sensor, more or less like a user interface. I am currently executing a .js file (binding.js) in my z-way-server (/opt/z-way-server/automation) with the following code:

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 if 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.0.30/phpinfo.php";
                                options.method = "POST";
                                options.async = false;
                                options.data = "sensor_level='"+this.value+"'";  //access this value in a .php script using $_POST['sensor_level']
                                var res = http.request(options);

                                console.log('CHECK 3!!!!!!!!!!!');

                        } catch(err) { // display the error in the console server log if something is wrong
                                debugPrint("Failed to send data: " + err);
                        }
                }
});
On my server I have a php file which supposed to recieve the POST with the sensor value, but I can't make it work and I do not see anything o my page. I also tried with GET method too. I know this above code is doing things right because in z-way-server.log i get te following output:

Code: Select all

[2017-11-14 12:16:15.028] [D] [zway] RECEIVED: ( 01 0D 00 04 00 02 07 9C 02 02 05 FF 00 00 95 )
[2017-11-14 12:16:15.028] [D] [zway] SENT ACK
[2017-11-14 12:16:15.029] [D] [zway] SETDATA devices.2.data.lastReceived = 0 (0x00000000)
[2017-11-14 12:16:15.029] [D] [zway] SETDATA devices.2.instances.0.commandClasses.156.data.5.srcId = 2 (0x00000002)
[2017-11-14 12:16:15.029] [D] [zway] SETDATA devices.2.instances.0.commandClasses.156.data.5.sensorState = 255 (0x000000ff)
[2017-11-14 12:16:15.030] [D] [zway] SETDATA devices.2.instances.0.commandClasses.156.data.5.sensorTime = 0 (0x00000000)
[2017-11-14 12:16:15.030] [D] [zway] SETDATA devices.2.instances.0.commandClasses.156.data.5 = Empty
[2017-11-14 12:16:15.038] [I] [core] CHECK 2!!!!!!!!!!!255
[2017-11-14 12:16:15.048] [D] [zway] RECEIVED: ( 01 0D 00 04 00 02 07 60 0D 01 01 20 01 FF 40 )
[2017-11-14 12:16:15.048] [D] [zway] SENT ACK
[2017-11-14 12:16:15.049] [D] [zway] SETDATA devices.2.data.lastReceived = 0 (0x00000000)
[2017-11-14 12:16:15.049] [D] [zway] SETDATA devices.1.instances.1.commandClasses.32.data.srcNodeId = 2 (0x00000002)
[2017-11-14 12:16:15.049] [D] [zway] SETDATA devices.1.instances.1.commandClasses.32.data.srcInstanceId = 1 (0x00000001)
[2017-11-14 12:16:15.049] [D] [zway] SETDATA devices.1.instances.1.commandClasses.32.data.level = 255 (0x000000ff)
[2017-11-14 12:16:15.087] [I] [core] CHECK 3!!!!!!!!!!!
[2017-11-14 12:16:15.130] [I] [core] Notification: device-info (device-OnOff): {"dev":"Fibaro Water Alarm (2.0)","l":"on","location":""}
[2017-11-14 12:16:15.226] [I] [core] Notification: device-info (device-OnOff): {"dev":"Fibaro (2.1.1) Button","l":"on","location":""}
Which means that it is entering the sending part of the code as CHECK 3!!!! is showing up (also CHECK 2!!! with the sensor value)
And my php code is the following:

Code: Select all

<?php
echo "Works";
$host = "localhost";
$user = "******"; //not showing it in this forum
$pw = "*****"; //not showing it in this forum
$db = "newdb";
if (isset($_POST['sensor_level']) && !empty($_POST['sensor_level'])) //if it receives something then do the following
{ 
$conexion =  mysql_connect($host,$user,$pw) or die ("fail to connect");

mysql_select_db($db,$conexion) or die ("fail to connect");

mysql_query("INSERT INTO flood_sensor (ID,VALOR) VALUES('1','$_POST[sensor_level]')",$conexion);

echo "data sent correctly";
}
else{
echo "fail to receive sensor data";
}
?>
But as i try to enter this php on my browser i see that it jumps directly to "fail to receive data", which means that it is not even receiving the sensor value!

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.
Post Reply