We have moved at community.getvera.com

Author Topic: [Solved] M2M and VERA ?  (Read 4307 times)

Offline fatal25

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +0/-0
[Solved] M2M and VERA ?
« on: June 06, 2012, 08:44:12 am »
Hi,

I need to use M2M (machine2machine) protocol.

Can we send TCP-IP frame with Vera (or MiOS ^^)?


Thank you.

SOLVED
Code to send command by M2M (socket TCP)

Code: [Select]
-- Utilisation de LUASocket
local socket = require("socket")
-- connexion a l'IPX
client = assert(socket.connect("192.168.0.111", 9870))
if (client == "nil" or client == nil) then
luup.log("La connexion a échoué")
else
luup.log("Connexion OK")
end
-- Une fois connectee, envoie des commandes voulues
client:send("Set21")
local answer = client:receive()
if (answer == nil or answer == "nil") then
luup.log("La réponse n°1 a échoué")
else
luup.log("La réponse n°1 est OK")
end
client:send("Set11")
local answer1 = client:receive()
if (answer1 == nil or answer1 == "nil") then
luup.log("La réponse n°2 a échoué")
else
luup.log("La réponse n°2 est OK")
end
-- On ferme la connexion
client:close()

DO NOT FORGET TO USE client:receive() after a client:send()

« Last Edit: June 07, 2012, 06:47:55 am by fatal25 »

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: M2M and VERA ?
« Reply #1 on: June 06, 2012, 08:53:44 am »
You can use LuaSocket, which is included on Vera.
« Last Edit: June 06, 2012, 08:57:49 am by mcvflorin »

Offline fatal25

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +0/-0
Re: M2M and VERA ?
« Reply #2 on: June 06, 2012, 08:58:39 am »
I like this answer :)

Thank you

Offline fatal25

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +0/-0
Re: M2M and VERA ?
« Reply #3 on: June 06, 2012, 11:12:12 am »
I try to send a M2M command like this :

Code: [Select]
local socket = require("socket")
-- Definition de l'adresse et du port du serveur, IPX ici
local address = "192.168.0.111"
local port = 9876
-- connexion a l'IPX
local etatconnexion = master:connect(address, port)
local retourip, retourport = client:getsockname()
if etatconnexion == "1" then
luup.log("La connexion a réussi sur " .. retourip .. ":".. retourport)
else
luup.log("La connexion a échoué sur " .. retourip .. ":".. retourport)
end

-- When connected, send the command
local command = "Set11"
client:send(command)
client:close()

Log says :

Code: [Select]
01 06/06/12 17:08:38.194 LuaInterface::CallFunction_Scene Scene  33 failed [string "function scene_7()..."]:229: attempt to index global 'master' (a nil value) <0x2fdbd680>

Do you see my error?

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: M2M and VERA ?
« Reply #4 on: June 06, 2012, 12:06:42 pm »
You'll want to read through the documented examples at locations like:
    http://w3.impa.br/~diego/software/luasocket/introduction.html

There's a slightly better worked TCP Client example here:
    http://www.lua.org/pil/9.4.html

The reason it's erroring is because "master" hasn't been defined, so it's nil.  The "master" and "client" reference in the documentation are more to give you an intended form than a literal statement-to-execute.

Offline fatal25

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +0/-0
Re: M2M and VERA ?
« Reply #5 on: June 06, 2012, 02:43:39 pm »
Something like this?

Code: [Select]
-- Utilisation de LUASocket
local socket = require("socket")
-- connexion a l'IPX
clientipx = assert(socket.connect("192.168.0.111", 9870))
if (clientipx == "1" or clientipx == 1) then
luup.log("Connexion ok")
else
luup.log("La connexion a échoué")
end
-- Une fois connectee, envoie la commande voulue
clientipx:send("Set11")
clientipx:close()

log : La connexion a échoué

Hum...

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: M2M and VERA ?
« Reply #6 on: June 06, 2012, 02:50:09 pm »
It'll be faster for you to experiment with this if you use command-line Lua from Windows/Mac/Linux.  You won't be able to use luup.* routines, but you'll be able to use print(...) to get output instead.

This will let you rev the test code much faster, and to become familiar with the Socket API without the burden of running under Vera.  Once it's working, you can put the code back into Vera, and add back in all the standard Luup hooks.

You can download off-the-shelf binaries from a variety of places, depending upon the platform you're using to develop.

Offline fatal25

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +0/-0
Re: M2M and VERA ?
« Reply #7 on: June 07, 2012, 06:46:04 am »
Thx Guessed.

If I can help someone:

This code sends command Set11 and Set21 to master.

That's work fine :

Code: [Select]
-- Utilisation de LUASocket
local socket = require("socket")
-- connexion a l'IPX
client = assert(socket.connect("192.168.0.111", 9870))
if (client == "nil" or client == nil) then
luup.log("La connexion a échoué")
else
luup.log("Connexion OK")
end
-- Une fois connectee, envoie des commandes voulues
client:send("Set21")
local answer = client:receive()
if (answer == nil or answer == "nil") then
luup.log("La réponse n°1 a échoué")
else
luup.log("La réponse n°1 est OK")
end
client:send("Set11")
local answer1 = client:receive()
if (answer1 == nil or answer1 == "nil") then
luup.log("La réponse n°2 a échoué")
else
luup.log("La réponse n°2 est OK")
end
-- On ferme la connexion
client:close()

DO NOT FORGET TO USE client:receive() after a client:send()