HTTP Device App - Post Action read the on or off action value

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
topcatsss
Posts: 29
Joined: 24 Nov 2016 22:27
Location: West Sussex, GB

HTTP Device App - Post Action read the on or off action value

Post by topcatsss »

I am using the HTTP Device app to control on / off of something else

The end point for the other device is http://192.168.0.57/control
the Data to be pass is json

Code: Select all

{"mode":1"}
where the mode is 0 or 1 for on or off (yes I realise this is backwards, not my end point doing)

I would like to take the input from the HTTP Device button press and either send

Code: Select all

{"mode":0"}
if on is set
or

Code: Select all

{"mode":1"}
if off is pressed


I have it reading the status okay
end point http://192.168.0.57/status
Inline Javascript to parse incoming data to 'on'/'off' strings

Code: Select all

$$.mode === 0 ? 'on' : 'off'
any help much appreciated
seattleneil
Posts: 210
Joined: 02 Mar 2020 22:41

Re: HTTP Device App - Post Action read the on or off action value

Post by seattleneil »

Unfortunately, I don't think the easy-to-use HTTPDevice app supports passing the on/off command as a JSON object in a POST request. Fortunately, there are other ways to accomplish what you want. One approach is to create a virtual binary switch using Virtual Device (JavaScript) app. You can read how to send an HTTP POST request with a "Content-type: application/json" from here:

Code: Select all

https://z-wave.me/manual/z-way/JavaScript_Engine.html#SECTION001321000000000000000
A second approach is to run a local python http server (something like: /usr/bin/python3 -m http.server --cgi 8000) and use the HTTPDevice app to http://127.0.0.1:8000/on or off or stat that invokes a cgi script that's nothing more than a shell script that runs a wget or curl command. The idea is to create the correct POST request in the CGI script from a Z-Way GET request. Be sure to set a timeout on the command. Your CGI shell script should be located in a cgi-bin directory in the home directory of the user that ran the python http server command (which I named "my_cgi_script.sh") and look something like this:

Code: Select all

#!/usr/bin/bash

case $1 in
   on)
     wget -t 2 -T 5 --connect-timeout=5 -w 2 --no-http-keep-alive --max-redirect=0 --header="Content-type: application/json" --post-data '{"mode":0}' -q -O /dev/null http://192.168.0.57/control
     echo -n "on"
     ;;
   off)
     wget -t 2 -T 5 --connect-timeout=5 -w 2 --no-http-keep-alive --max-redirect=0 --header="Content-type: application/json" --post-data '{"mode":1}' -q -O /dev/null http://192.168.0.57/control
     echo -n "off"
     ;;
   stat)
     if `wget -t 2 -T 5 --connect-timeout=5 -w 2 --no-http-keep-alive --max-redirect=0 -q -O - "http://192.168.0.57/status" | grep -q :0`; then echo -n on; else echo -n off; fi
     ;;
I suggest testing the wget/curl command from a shell prompt. Once you get the switch to turn on and off and report status, use the commands in the CGI script. To test your CGI script, run: curl http://127.0.0.1:8000/cgi-bin/my_cgi_script.sh to verify the python http server is working correctly.

Good luck.
Post Reply