Little help with http binding

575 views
Skip to first unread message

Renato Riolino

unread,
Apr 22, 2015, 6:22:37 PM4/22/15
to ope...@googlegroups.com
Hi, this is the first time I'm using the http binding. It's not working and I don't know how to debug it.

First, let me explain what I want to do.  I have a python addon for Kodi installed that allows me to turn ON and turn OFF my TV via HDMI CEC. The TV is not a smart TV but is fully CEC compatible.  If I open this url on my webbrowser, the TV turns on:

http://kodi:kodi@xbmc-jantar:80/jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}

and it turns off with this url:

http://kodi:kodi@xbmc-jantar:80/jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"standby"}},"id":1}


Knowing that the addon is working, I have created an item using http binding:

Switch tvJantar "TV Jantar" (G_Sala, G_Cozinha) { http=">[ON:GET:http://kodi:kodi@xbmc-jantar:80/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Addons.ExecuteAddon\",\"params\":{\"addonid\":\"script.json-cec\",\"params\":{\"command\":\"activate\"}},\"id\":1}] >[OFF:GET:http://kodi:kodi@xbmc-jantar:80/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Addons.ExecuteAddon\",\"params\":{\"addonid\":\"script.json-cec\",\"params\":{\"command\":\"standby\"}},\"id\":1}]" }


When I click on the tvJantar switch nothing happens.  Even on log files nothing. No error messages.

What am I doing wrong?

Thanks

Mark

unread,
Apr 22, 2015, 9:34:14 PM4/22/15
to ope...@googlegroups.com
You may need to URL Encode the JSON request Parameter value.  If you enter that into a Browser, they'll typically do that before putting the value on the wire, but I'm guessing you'll need to do it manually with the HTTP Binding.

For this bit of JSON:
    {"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}

Using an online URL-Encoding tool, the encoded version is:

    %7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Addons.ExecuteAddon%22%2C%22params%22%3A%7B%22addonid%22%3A%22script.json-cec%22%2C%22params%22%3A%7B%22command%22%3A%22activate%22%7D%7D%2C%22id%22%3A1%7D


Try glueing that back into your original "ON" string, and do the counterpart encoding for the "OFF" one.

Renato Riolino

unread,
Apr 23, 2015, 7:50:03 AM4/23/15
to ope...@googlegroups.com
Hi Mark!

I tried with the encoded url you gave me but still nothing happens.

Is there any debug log that I could enable?

One more thing:  Using the login and password on the http url like I did (http://login:pass@hostname) is the correct way of passing login/pass with the http binding, right?

Thanks

Renato

Mark

unread,
Apr 23, 2015, 11:47:32 AM4/23/15
to ope...@googlegroups.com
There is a limited amount of DEBUG level logging in both the HTTP Binding, and in the org.openhab.io.net.HttpUtil class it uses.  If you run openHAB using the start_debug script, these should be enabled.

You could also try using Wireshark, or tcpdump, on one of the endpoints (either on your openHAB machine or on xbmc-jantar itself).  That'll tell you what's really hitting the wire but, if you've not done it before, can be a little bit of a learning curve.


On Wednesday, April 22, 2015 at 3:22:37 PM UTC-7, Renato Riolino wrote:

Renato Riolino

unread,
Apr 24, 2015, 8:25:03 AM4/24/15
to ope...@googlegroups.com
I've started OH with the start_debug script.  This is what I got on the log after turn ON the switch for the http bind:

17:46:17.875 [DEBUG] [o.i.r.i.resources.ItemResource:197  ] - Received HTTP POST request at 'items/tvJantar2' with value 'ON'.
17:46:17.907 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:437  ] - Publishing message 10 to topic '/nexhome/openhab/tvJantar2/command'
17:46:18.305 [INFO ] [runtime.busevents             :22   ] - tvJantar2 received command ON
17:46:19.165 [DEBUG] [o.o.i.r.i.b.GeneralBroadcaster:41   ] - broadcaster 'org.openhab.io.rest.internal.broadcaster.GeneralBroadcaster$1@1e0b438' is empty
17:46:19.278 [DEBUG] [o.o.i.r.i.b.GeneralBroadcaster:41   ] - broadcaster 'org.openhab.io.rest.internal.broadcaster.GeneralBroadcaster$1@1a13ec8' is empty
17:46:19.420 [DEBUG] [.r.i.resources.SitemapResource:146  ] - Received HTTP GET request at 'sitemaps/default/0101' for media type 'null'.
17:46:19.458 [DEBUG] [.r.i.resources.SitemapResource:146  ] - Received HTTP GET request at 'sitemaps/default/0100' for media type 'null'.

Could be this GeneralBroadcaster error be related?

I'm thinking of installing an apache on another machine and them try to GET the same URL on it (since it logs all access).

[]'s

Renato

Mark

unread,
Apr 24, 2015, 12:50:50 PM4/24/15
to ope...@googlegroups.com
Looks like the configurations/logback_debug.xml file has some excludes for org.openhab.io.net Packages:
            <logger name="org.openhab.io.net" level="INFO" />

So it's filtering out some of the DEBUG (...level="DEBUG") logging you should see.  Most of the stuff you're seeing in the log snippet relate to the UI/Client interaction with openHAB, so they're not an issue... at least for what you're looking into.

Using tcpdump, or Wireshark, on one of the endpoints will be easier than installing Apache.

Typically tcpdump (run from the client) is just:

tcpdump -s0 -X host <ipAddress> and port 80

and it'll spit out the Hex and Ascii of the packets sent to the ipAddress specified (port 80, in the above example).  You can do that from the server also (usage assumes *nix/Mac or something like WinPCap for Windows users) just tweak the parameters a little.

If one end of this has a UI, it'll be easier to fire up Wireshark, since it's graphical.
 

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
To view this discussion on the web visit https://groups.google.com/d/msgid/openhab/84d07def-e803-4635-91bc-510265fc5764%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Renato Riolino

unread,
Apr 24, 2015, 2:27:40 PM4/24/15
to ope...@googlegroups.com
I've changed the logback_debug.xml to level="DEBUG" on org.openhab.io.net but still didn't shown anything new on the log:

15:22:19.894 [DEBUG] [o.i.r.i.resources.ItemResource:197  ] - Received HTTP POST request at 'items/tvJantar2' with value 'ON'.
15:22:20.072 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:437  ] - Publishing message 9 to topic '/nexhome/openhab/tvJantar2/command'
15:22:20.589 [DEBUG] [o.o.i.r.i.b.GeneralBroadcaster:41   ] - broadcaster 'org.openhab.io.rest.internal.broadcaster.GeneralBroadcaster$1@135dabf' is empty
15:22:20.832 [DEBUG] [.r.i.resources.SitemapResource:146  ] - Received HTTP GET request at 'sitemaps/default/0100' for media type 'null'.

I have changed the target hostname to the hostname of another machine that I have home with apache installed.   No connection was made.

Sorry but I couldn't run tcpdump on the target machine still because it is running OpenElec (on a RPi) and the tcpdump utility is not available (and there is no package manager to easyly install).

This is my addon folder:

org.openhab.action.pushover-1.6.2.jar
org.openhab.action.squeezebox-1.6.2.jar
org.openhab.action.xbmc-1.6.2.jar
org.openhab.action.xmpp-1.6.2.jar
org.openhab.binding.asterisk-1.6.2.jar
org.openhab.binding.astro-1.6.2.jar
org.openhab.binding.exec-1.6.2.jar
org.openhab.binding.http-1.6.2.jar
org.openhab.binding.jointspace-1.6.2.jar
org.openhab.binding.mqtt-1.6.2.jar
org.openhab.binding.mqttitude-1.6.2.jar
org.openhab.binding.networkhealth-1.6.2.jar
org.openhab.binding.ntp-1.6.2.jar
org.openhab.binding.squeezebox-1.6.2.jar
org.openhab.binding.tcp-1.6.2.jar
org.openhab.binding.weather-1.6.2.jar
org.openhab.binding.wol-1.6.2.jar
org.openhab.binding.xbmc-1.6.2.jar
org.openhab.io.squeezeserver-1.6.2.jar
org.openhab.persistence.rrd4j-1.6.2.jar

http and tcp bindings are there. Any other file missing? Is it possible for the binding be disabled? How can I check this?

Thanks

Renato

Mark

unread,
Apr 25, 2015, 3:40:27 PM4/25/15
to ope...@googlegroups.com
Sorry but I couldn't run tcpdump on the target machine still because it is running OpenElec (on a RPi) and the tcpdump utility is not available (and there is no package manager to easyly install).

Fair enough.  I've not worked with OpenElec, but there seems to be something called the "unofficial-addons" that may include the TCPDump stuff

Looks like there are some rudimentary setup/install instructions as well.  Might be worth a try, esp if you're going to be setting up a few of these connections


I enabled .io.net logging in my OH install, and restarted OH, and the logging is minimal... In my case, I'm using it via Rules, not via Items (the Item support doesn't do what I need) but IIUC they all funnel through the same org.openhab.io.net.http.HttpUtil code.

The logging looks like the last two lines here (basically, the Request URI and the Response body)

12:35:00.003 DEBUG o.o.m.r.i.e.ExecuteRuleJob[:53]- Executing scheduled rule 'Pull Data from Eagle'

12:35:00.009 DEBUG o.openhab.io.net.http.HttpUtil[:190]- About to execute 'http://192.168.5.130/cgi-bin/cgi_manager'

12:35:00.299 DEBUG o.openhab.io.net.http.HttpUtil[:224]- {"meter_status":"Connected",

"demand":"-3.7140",

... 

"threshold_lower_demand":"-4.685000"}



 

Renato Riolino

unread,
Apr 27, 2015, 8:05:52 AM4/27/15
to ope...@googlegroups.com
I'll try tonight this unofficial-addon to install tcpdump there.  While, could you give an example of the use of the http binding via rules?

Thanks

Manuel Carlos Rodriguez

unread,
Apr 27, 2015, 11:18:06 AM4/27/15
to ope...@googlegroups.com
Hi,

I'm planning to do the same with a different approach and I wanted to share it with you.

xmbc includes cec-client which allows you to interact with the tv from a command line. The commands to turn on and off are the following:
  • echo "on 0" | cec-client -s
  • echo "standby 0" | cec-client -s
I was planning to use the above command and execute them remotely (I have 2 RPI, one for xmbc and another for openhab).

To know the tv status, I'm planning to create an script for the xbmc RPI which query the TV status and publish the result using MQTT. 

The command to get the status is: echo scan | cec-client -s -d 1 | head -11 | tail -1

What do you think? I know it is different, but I dont see the reason to use xmbc REST API for this.



Renato Riolino

unread,
Apr 27, 2015, 12:48:25 PM4/27/15
to ope...@googlegroups.com
Hi Manuel,

I thought on doing like you said. The problem is that when you use the cec-client from command line, XBMC loses the cec connection and you won't be able to use your TV Remote to control xbmc unless you restart xbmc.

That's why I found that using cec thru xbmc is the best solution.

Another solution that I was thinking was to disable the CEC on XBMC and write a daemon software that will talk with cec and when it receives some key press, it would translate them to remote events on XBMC and will too accept external connections from querying and commands (like turn on, turn off, etc..)

I had started writing a software for this a while ago (in C, using libcec and MQTT to comunicate with OH), but didn't have time to finish.

[]'s

Renato Riolino

unread,
Apr 30, 2015, 8:19:31 AM4/30/15
to ope...@googlegroups.com
Hi,

I finally got it working, but I had to use a shell script to make the http request.

If anyone is interested, here is what I did (OH running on Linux and Kodi on a Raspberry Pi):

1- Download the script.json-cec.zip from here: https://github.com/joshjowen/script.json-cec

2- Install it on your kodi (System -> Add-ons -> Install from ZIP)

3- On your OH machine, create a file called kodi-cec.sh on the configurations/scripts

4- Copy and paste this on the new file:

#!/bin/bash

OH_CFG="/opt/openhab/configurations/openhab.cfg"
WGET="/usr/bin/wget"
PARAMS="-q -O /dev/null"
JSON_ON='jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}'
JSON_OFF='jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"standby"}},"id":1}'

usage()
{
  echo "Usage: $0 kodi-host ON|OFF"
  echo ""
  exit 1
}

if [ "$2" == "" ]; then
  usage
fi

if ! cat $OH_CFG | grep "xbmc:${1}" >/dev/null 2>/dev/null; then
  echo "Kodi-host $1 not found on ${OH_CFG}!"
  echo ""
  exit 2
fi

if ! [ "$2" == "ON" ] && ! [ "$2" == "OFF" ]; then
  echo "Valid commands are ON or OFF only!"
  echo ""
  exit 3
fi

KODI_HOST=`cat $OH_CFG | grep "xbmc:${1}.host" | awk -F'=' ' { print $2 } '`
KODI_PORT=`cat $OH_CFG | grep "xbmc:${1}.rsPort" | awk -F'=' ' { print $2 } '`
KODI_USER=`cat $OH_CFG | grep "xbmc:${1}.username" | awk -F'=' ' { print $2 } '`
KODI_PASS=`cat $OH_CFG | grep "xbmc:${1}.password" | awk -F'=' ' { print $2 } '`

CMD="${WGET} ${PARAMS} http://"
if ! [ "${KODI_USER}" == "" ]; then
  CMD="${CMD}${KODI_USER}"
  if ! [ "${KODI_PASS}" == "" ]; then
    CMD="${CMD}:${KODI_PASS}"
  fi
  CMD="${CMD}@"
fi
CMD="${CMD}${KODI_HOST}:${KODI_PORT}/"
if [ "$2" == "ON" ]; then
  CMD="${CMD}${JSON_ON}"
else
  CMD="${CMD}${JSON_OFF}"
fi

$CMD

5- Change OH_CFG to point to your openhab.cfg file. It uses this file to get the host/port/login/pass for your kodi machine.

6- Make the script executable (chmod +x)

7- Then, call the script on your item files like I did:

Switch tvJantar "TV Jantar" (G_Sala, G_Cozinha) { exec=">[OFF:./configurations/scripts/kodi-cec.sh oe-jantar OFF] >[ON:./configurations/scripts/kodi-cec.sh oe-jantar ON]" }


It's working great. I can turn ON and OFF the TV via CEC and I still can control kodi using my TV remote. 

Now I'm trying to find a way to get some feedback from the TV (like if it was turned ON with the remote, to update the item file with the new status) and to control the TV volume via CEC.

Looking at kodi source, looks like only the activate and stadby commands are implemented with libcec, so I will probably need to make some daemon soft to talk directly with libcec to do so.  

I'll post here when I get done.

[]'s

Renato

Manuel Carlos Rodriguez

unread,
May 1, 2015, 5:24:17 AM5/1/15
to ope...@googlegroups.com
Well done!!!!

Please, keep me posted on your progress. It will be also interesting being able to increase/decrease volume, ...

What about using the network health and ping the TV?


Manuel Carlos Rodriguez

unread,
May 7, 2015, 3:44:18 AM5/7/15
to ope...@googlegroups.com
Hi,

I was able to get the status by changing the items as follow:

Switch tvJantar "TV Jantar" (G_Sala, G_Cozinha) { exec=">[OFF:./configurations/scripts/kodi-cec.sh oe-jantar OFF] >[ON:./configurations/scripts/kodi-cec.sh oe-jantar ON]", nh="192.168.1.x" }

I found this third options really useful, but haven't found where it is documented. Can you point to a place that indicate how to configure this kind of binding or why this works?

Aaron Heyworth

unread,
Jun 23, 2015, 2:09:36 PM6/23/15
to ope...@googlegroups.com
I have a similar request for help.  This time its for HTTP with Arduino and not Kodi.  I was going to use the Souliss Binding for working with Arduino but I am already using the DMX Binding, and Souliss clashes with DMX for some reason.

In my browser I can control outputs of the Arduino on/off by typing http://192.168.0.100/digitalWrite/2/1 and when I hit enter the LED switches on. Similarly when I type the same URL in the browser and change the last digit to a 0 it switches the LED off.

How do I get OpenHAB to send these URL commands when I use a toggle switch?  I've tried the HTTP Binding which I cannot see any result with.  I just dont know what to do.

Would someone mind proving me a sample line of code for an item, and maybe the binding config?

Thank you very much in advance.

wolffromthenorth

unread,
Sep 11, 2015, 4:18:16 PM9/11/15
to openhab
with all respect , correct code for users who want to execute a addon script on kodi with Json command.

#!/bin/bash
OH_CFG="/volume1/@appstore/OpenHAB/configurations/openhab.cfg"
WGET="/usr/bin/wget"
PARAMS="-q -O /dev/null"
JSON_ON='jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.securitycam","params":{"command":"activate"}},"id":1}'
kind regards
Reply all
Reply to author
Forward
0 new messages