Json via TCP/IP socket

I’m trying to send some very basic Json commands to my Mediaportal HTPC. This is possible after installing ‘wifiremote’ on top of MediaPortal.

Commands are sent from a client to WifiRemote. Depending on the type and content of the message, actions are invoked on MediaPortal or messages are sent back to the client. Each command is a separate JSON object (‘message’) that is delimited by carriage return and line break (\r\n).

Unfortunately it does not accept commands via HTTP but only via TCP/IP.

I’m really new to LUUP programming and TCP/IP sockets.

I’ve searched the fora but very difficult to get started.

Could someone give me an idea on what LUUP code is needed to send (for example) {“type”:“command”, “command”:“pauze”} to wifiremote (on IP: XXX.XXX.XX.XXX:PORT).

Wifiremote can also receive incoming messages but as such these are not important for me.

Thanks!

You should be able to use the LuaSocket module for writing and reading sockets.

http://w3.impa.br/~diego/software/luasocket/introduction.html

Be aware that you really can’t block indefinitely waiting to listen to a socket, without some creative steps to work around the limitation.

Check this thread out for simple TCP IP characters

http://forum.micasaverde.com/index.php?topic=22854.0

Update : wmcCmd (with something like “command:pause”), 40500 (port #), 192.168.0.100 (with MediaPortal server), and possible change “\r” to “\r\n”

local wmcCmd = "YourCommand" local socket = require("socket") tcp = assert(socket.connect(192.168.0.100, 40500)) tcp:send(wmcCmd .. "\r") tcp:close()

Thanks for this!

I have tried to run the code below via 'test luup code" but I get “code failed” (and no pauze).

local wmcCmd = "command:pauze" local socket = require("socket") tcp = assert(socket.connect(192.168.1.40, 8017)) tcp:send(wmcCmd .. "\r\n") tcp:close()

I have checked if the TCP connection is up and this seems the case (from another system):

Last login: Tue Sep 2 20:46:19 on ttys000 telnet 192.168.1.40 8017 Trying 192.168.1.40... Connected to 192.168.1.40. Escape character is '^]'. {"Type":"welcome","Server_Version":12,"AuthMethod":0,"MPExtendedServicesInstalled":{"MAS":true,"TAS":true,"WSS":true}}

Any suggestions how to progress/troubleshoot?

Sorry, my sample was bad. Put your IP address in quotes as it’s a sting, not an integer:

tcp = assert(socket.connect("192.168.1.40", 8017))

If you put a

[tt]return 0[/tt]

at the end of the code block, does it still report “code failed”?

Ok, few steps further.

First of all adding the quotes and ‘return 0’ made the code run. However still no success. It turns out that I need to authenticate first. Via Telnet I have this working.

It’s done by:

{"Type":"identify","Name":"Vera","Application":"VeraGos","Version":"0.0.0.1","Authenticate":{"AuthMethod":"none"}}

If I then send {"Type":"command", "Command":"pause"} (after the authentication) via telnet it actually works (Mediaportal Pauses)! Whohoo!

How do I modify the code above to send both the authentication & the pause command?

Thanks again!

local socket = require("socket")
tcp = assert(socket.connect("192.168.1.40", 8017))
tcp:send("YourCommand1" .. "\r\n")
luup.sleep(100)
tcp:send("YourCommand2" .. "\r\n")
tcp:close()

or

local socket = require("socket")
tcp = assert(socket.connect("192.168.1.40", 8017))
tcp:send("YourCommand1" .. "\r\n")
tcp:close()

local socket = require("socket")
tcp = assert(socket.connect("192.168.1.40", 8017))
tcp:send("YourCommand2" .. "\r\n")
tcp:close()

You’ll need to escape your quotes per: http://www.lua.org/pil/2.4.html
In short, the " in YourCommand need to be replaced with "

YES, we have lift off! Many thank AgileHumor.

Below code works. Easy to expand based on this!

local Inlog="{\"Type\":\"identify\",\"Name\":\"Vera\",\"Application\":\"VeraGos\",\"Version\":\"0.0.1.1\",\"Authenticate\":{\"AuthMethod\":\"none\"}}" local wmcCmd = "{\"Type\":\"command\", \"Command\":\"pause\"}" local socket = require("socket") tcp = assert(socket.connect("192.168.1.40", 8017)) tcp:send(Inlog .. "\r\n") luup.sleep(100) tcp:send(wmcCmd .. "\r\n") tcp:close() return 0