Various functions to do network communication
New in version nightly.
Allows sending mails
pilight.network.mail
()¶Creates a new mail object
getUserdata
()¶Returns a persistent userdata table for the lifetime of the mail object.
getFrom
()¶Returns the sender.
getHost
()¶Returns the mail server host.
getMessage
()¶Returns the mail message.
getPassword
()¶Returns the mail server password.
getPort
()¶Returns the mail server port.
getSSL
()¶Returns the mail server ssl.
getStatus
()¶After the mail was sent this function can be used in the callback to check if the mail was sent successfully to the server.
getTo
()¶Returns the recipient.
getUser
()¶Returns the mail server user.
setCallback
(string callback)¶The name of the callback being triggered by the mail library. The mail object will be passed as the only parameter of this callback function.
setUserdata
(userdata table)¶Set a new persistent userdata table for the lifetime of the mail object. The userdata table cannot be of another type as returned from the getData functions.
setFrom
()¶Sets the sender.
setHost
()¶Sets the mail server host.
setMessage
()¶Sets the mail message.
setPassword
()¶Sets the mail server password.
setPort
()¶Sets the mail server port.
setSSL
()¶Sets the mail server ssl.
setTo
()¶Sets the recipient.
setUser
()¶Sets the mail server user.
send
()¶Send the mail
local M = {}
function M.callback(mail)
print(mail.getStatus());
end
function M.run()
local mailobj = pilight.network.mail();
mailobj.setSubject("foo");
mailobj.setFrom("order@pilight.org");
mailobj.setTo("info@pilight.nl");
mailobj.setMessage("bar");
mailobj.setHost("127.0.0.1");
mailobj.setPort(25);
mailobj.setUser("pilight");
mailobj.setPassword("test");
mailobj.setSSL(0);
mailobj.setCallback("callback");
mailobj.send();
return 1;
end
return M;
Do HTTP GET or POST requests
pilight.network.http
()¶Creates a new http object
getUserdata
()Returns a persistent userdata table for the lifetime of the http object.
getMimetype
()¶Returns the mimetype of the received data. This means it does not returns the mimetype set by the setMimetype() function.
getData
()¶Returns the data received from the url. This means it does not returns the data set by the setData() function.
getUrl
()¶Returns the url.
getCode
()¶After the HTTP request was sent this function can be used in the callback to check if the return HTTP code.
getSize
()¶After the HTTP request was sent this function can be used in the callback to get the size of the received content.
setCallback
(string callback)The name of the callback being triggered by the http library. The http object will be passed as the only parameter of this callback function.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
setUrl
(string url)¶Sets the request URL. Examples:
setMimetype
(string mimetype)¶Sets the mimetype of the data being posted.
setData
(string data)¶Sets the data to be posted.
get
()¶Send a GET request to the URL.
post
()¶Send a POST request to the URL with the data set.
local M = {}
function M.callback(http)
print(http.getCode());
print(http.getMimetype());
print(http.getSize());
print(http.getData());
end
function M.run()
local httpobj = pilight.network.http();
httpobj.setUrl("http://127.0.0.1:10080/")
httpobj.setCallback("callback");
httpobj.get();
return 1;
end
return M;
Send and/or receive COAP messages
pilight.network.coap
()¶Creates a new coap object
getUserdata
()Returns a persistent userdata table for the lifetime of the coap object.
listen
(userdata table)¶Listens to coap messages
setCallback
(string callback)The name of the callback being triggered by the coap library. The parameters of the callback function are the coap object, received data object, ip and port of the sender.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
send
(userdata table)Sends a coap message
Note
COAP data specifications
The data the lua COAP interface parses has to be set in a low-level way. That means you have to construct a valid coap object yourself. The COAP responses are represented in the same low-level way. The COAP interface follows the same specification for it’s data as the protocol description.
The allowed COAP data fields are
ver
t
token
payload
code
msgid
options
num
val
local M = {}
function M.discover(coap, data, ip, port)
return;
end
function M.run()
local coap = pilight.network.coap();
local send = {};
send['ver'] = 1;
send['t'] = 1;
send['code'] = 0001;
send['msgid'] = 0001;
send['options'] = {};
send['options'][0] = {};
send['options'][0]['val'] = 'cit';
send['options'][0]['num'] = 11;
send['options'][1] = {};
send['options'][1]['val'] = 'd';
send['options'][1]['num'] = 11;
coap.setCallback("discover");
coap.send(send);
coap.listen();
return 1;
end
return M;
Send and/or receive MDNS messages
pilight.network.mdns
()¶Creates a new mdns object
getUserdata
()Returns a persistent userdata table for the lifetime of the mdns object.
listen
(userdata table)Listens to mdns messages
setCallback
(string callback)The name of the callback being triggered by the mdns library. The parameters of the callback function are the mdns object, received data object, ip and port of the sender.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
send
(userdata table)Sends a mdns message
Note
MDNS data specifications
The data the lua MDNS interface parses has to be set in a low-level way. That means you have to construct a valid mdns object yourself. The MDNS responses are represented in the same low-level way. The MDNS interface follows the same specification for it’s data as the protocol description.
The allowed MDNS data fields are
id
qr
opcode
aa
tc
rd
ra
z
ad
cd
rcode
rr_auth
queries
name
qu
type
class
answers
records
type
name
length
ttl
class
flush
domain
weight
priority
port
ip
options
values
The answers
and records
fields share the same fields. The combination of fields that are allowed depends on the
on the answer or record type. The options
array contains numeric keys with values.
local M = {}
function M.discover(mdns, data, ip, port)
return;
end
function M.run()
local mdns = pilight.network.mdns();
local send = {};
send['id'] = tonumber("AABB", 16);
send['qr'] = 0;
send['opcode'] = 2;
send['aa'] = 0;
send['tc'] = 1;
send['rd'] = 0;
send['ra'] = 1;
send['z'] = 0;
send['ad'] = 1;
send['cd'] = 0;
send['rcode'] = 1;
send['rr_auth'] = 0;
send['queries'] = {};
send['queries'][1] = {};
send['queries'][1]['name'] = "testname.local.foo";
send['queries'][1]['qu'] = 0;
send['queries'][1]['type'] = 33;
send['queries'][1]['class'] = 1;
send['queries'][2] = {};
send['queries'][2]['name'] = "testname1.local1.foo";
send['queries'][2]['qu'] = 0;
send['queries'][2]['type'] = 33;
send['queries'][2]['class'] = 1;
send['queries'][3] = {};
send['queries'][3]['name'] = "testname.local1.foo";
send['queries'][3]['qu'] = 0;
send['queries'][3]['type'] = 33;
send['queries'][3]['class'] = 1;
mdns.setCallback("discover");
mdns.send(send);
mdns.listen();
return 1;
end
return M;
Send and/or receive MQTT messages
pilight.network.mqtt
([userdata mqtt instance])¶Creates a new mqtt object or restore the saved instance when passing the instance parameter.
New in version nightly.
pilight.network.mqtt
()()Returns the mqtt instance as lightuserdata so it can be stored in a pilight metatable.
connect
([string ip, number port][, string id][, string willtopic, string willmessage])¶Connect to a mqtt broken. This function either takes 2, 3, or 5 arguments.
ip
and port
ip
, port
, and id
ip
, port
, id
, willtopic
, and willmessage
getUserdata
()Returns a persistent userdata table for the lifetime of the mqtt object.
publish
(string topic, string message[, table options])¶Publish to a certain message to topic. The optional options table gives the possibility to set the duplicate message, retained message, and/or QoS options like this: {MQTT_DUB, MQTT_RETAIN, MQTT_QOS1}
pubrec
(number msgid)¶Send a pubrec message.
pubrel
(number msgid)¶Send a pubrel message.
pubcomp
(number msgid)¶Send a pubcomp message.
ping
()¶Send a ping message.
subscribe
(string topic[, table options])¶Subscribes to a topic with a certain QoS. The QoS parameter can be set using the optional options table {MQTT_QOS1}
or {MQTT_QOS2}
.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
Note
MQTT packets
The MQTT packets are either MQTT_CONNECT
, MQTT_CONNACK
, MQTT_PUBLISH
, MQTT_PUBACK
, MQTT_PUBREC
, MQTT_PUBREL
, MQTT_PUBCOMP
, MQTT_SUBSCRIBE
, MQTT_UNSUBSCRIBE
, MQTT_UNSUBACK
, MQTT_PINGREQ
, MQTT_PINGRESP
, MQTT_DISCONNECT
, MQTT_CONNECTED
local M = {}
function M.timer(timer)
local data = timer.getUserdata();
local mqtt = pilight.network.mqtt(data['mqtt']);
mqtt.ping();
end
function M.callback(mqtt, data)
if data['type'] == MQTT_CONNACK then
local timer = pilight.async.timer();
timer.setCallback("timer");
timer.setTimeout(3000);
timer.setRepeat(3000);
timer.setUserdata({['mqtt']=mqtt()});
timer.start();
mqtt.subscribe("#", {MQTT_QOS2});
end
if data['type'] == MQTT_PUBACK then
end
if data['type'] == MQTT_PUBLISH then
mqtt.pubrec(data['msgid']);
end
if data['type'] == MQTT_PUBREL then
mqtt.pubcomp(data['msgid']);
end
if data['type'] == MQTT_PINGRESP then
end
end
function M.run()
mqtt.setCallback("callback");
mqtt.connect("127.0.0.1", 1883);
return 1;
end
return M;