December 9, 2013
10 |The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise 13 | indicated below, the Content is provided to you under the terms and conditions of the 14 | Eclipse Public License Version 1.0 ("EPL") and Eclipse Distribution License Version 1.0 ("EDL"). 15 | A copy of the EPL is available at 16 | http://www.eclipse.org/legal/epl-v10.html 17 | and a copy of the EDL is available at 18 | http://www.eclipse.org/org/documents/edl-v10.php. 19 | For purposes of the EPL, "Program" will mean the Content.
20 | 21 |If you did not receive this Content directly from the Eclipse Foundation, the Content is 22 | being redistributed by another party ("Redistributor") and different terms and conditions may 23 | apply to your use of any object code in the Content. Check the Redistributor's license that was 24 | provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise 25 | indicated below, the terms and conditions of the EPL still apply to any source code in the Content 26 | and such source code may be obtained at http://www.eclipse.org.
27 | 28 | 29 | -------------------------------------------------------------------------------- /edl-v10: -------------------------------------------------------------------------------- 1 | 2 | Eclipse Distribution License - v 1.0 3 | 4 | Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors. 5 | 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 | Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 | 16 | -------------------------------------------------------------------------------- /epl-v10: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and 10 | b) in the case of each subsequent Contributor: 11 | i) changes to the Program, and 12 | ii) additions to the Program; 13 | where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. 14 | "Contributor" means any person or entity that distributes the Program. 15 | 16 | "Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. 17 | 18 | "Program" means the Contributions distributed in accordance with this Agreement. 19 | 20 | "Recipient" means anyone who receives the Program under this Agreement, including all Contributors. 21 | 22 | 2. GRANT OF RIGHTS 23 | 24 | a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. 25 | b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. 26 | c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. 27 | d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 28 | 3. REQUIREMENTS 29 | 30 | A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: 31 | 32 | a) it complies with the terms and conditions of this Agreement; and 33 | b) its license agreement: 34 | i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; 35 | ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; 36 | iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and 37 | iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. 38 | When the Program is made available in source code form: 39 | 40 | a) it must be made available under this Agreement; and 41 | b) a copy of this Agreement must be included with each copy of the Program. 42 | Contributors may not remove or alter any copyright notices contained within the Program. 43 | 44 | Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 45 | 46 | 4. COMMERCIAL DISTRIBUTION 47 | 48 | Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. 49 | 50 | For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 51 | 52 | 5. NO WARRANTY 53 | 54 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 55 | 56 | 6. DISCLAIMER OF LIABILITY 57 | 58 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 59 | 60 | 7. GENERAL 61 | 62 | If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 63 | 64 | If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. 65 | 66 | All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. 67 | 68 | Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. 69 | 70 | This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. 71 | -------------------------------------------------------------------------------- /example/example_00.lua: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/lua 2 | -- 3 | -- example_00.lua 4 | -- ~~~~~~~~~~~~~~ 5 | -- Version: 0.2 2012-06-01 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Subscribe to a topic and publish all received messages on another topic. 20 | -- 21 | -- ToDo 22 | -- ~~~~ 23 | -- - On failure, automatically reconnect to MQTT server. 24 | -- - Error handling: MQTT.client.connect() 25 | -- - Error handling: MQTT.client.destroy() 26 | -- - Error handling: MQTT.client.disconnect() 27 | -- - Error handling: MQTT.client.handler() 28 | -- - Error handling: MQTT.client.publish() 29 | -- - Error handling: MQTT.client.subscribe() 30 | -- - Error handling: MQTT.client.unsubscribe() 31 | -- ------------------------------------------------------------------------- -- 32 | 33 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 34 | 35 | local MQTT = require "mqtt" 36 | local socket = require "socket" 37 | local lapp = require("pl.lapp") 38 | 39 | -- ------------------------------------------------------------------------- -- 40 | function is_openwrt() 41 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 42 | end 43 | -- if (not is_openwrt()) then require("luarocks.require") end 44 | -- ------------------------------------------------------------------------- -- 45 | 46 | local mqtt_client 47 | local error_message = nil 48 | 49 | function callback( 50 | topic, -- string 51 | message) -- string 52 | 53 | print("Topic: " .. topic .. ", message: '" .. message .. "'") 54 | 55 | mqtt_client:publish(args.topic_p, message) 56 | end 57 | 58 | print("\n--- example_00 v0.4-SNAPSHOT ---\n") 59 | 60 | args = lapp [[ 61 | Subscribe to topic_s and publish all messages on topic_p 62 | -H,--host (default localhost) MQTT server hostname 63 | -i,--id (default example_00) MQTT client identifier 64 | -p,--port (default 1883) MQTT server port number 65 | -s,--topic_s (default test/1) Subscribe topic 66 | -t,--topic_p (default test/2) Publish topic 67 | ]] 68 | 69 | 70 | mqtt_client = MQTT.client.create(args.host, args.port, callback) 71 | 72 | error_message = mqtt_client:connect(args.id) 73 | if error_message ~= nil then error(error_message) end 74 | 75 | mqtt_client:subscribe({ args.topic_s }) 76 | 77 | while (error_message == nil) do 78 | error_message = mqtt_client:handler() 79 | socket.sleep(1.0) -- seconds 80 | end 81 | 82 | if (error_message == nil) then 83 | mqtt_client:unsubscribe({ args.topic_s }) 84 | mqtt_client:destroy() 85 | else 86 | print(error_message) 87 | end 88 | 89 | -- ------------------------------------------------------------------------- -- 90 | -------------------------------------------------------------------------------- /example/example_01.lua: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/lua 2 | -- 3 | -- example_01.lua 4 | -- ~~~~~~~~~~~~~~ 5 | -- Version: 0.2 2012-06-01 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Subscribe to a topic on one MQTT server and publish all received messages 20 | -- to a topic on another MQTT server. 21 | -- 22 | -- ToDo 23 | -- ~~~~ 24 | -- - On failure, automatically reconnect to MQTT server(s). 25 | -- ------------------------------------------------------------------------- -- 26 | 27 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 28 | 29 | local MQTT = require "mqtt" 30 | local socket = require "socket" 31 | local lapp = require("pl.lapp") 32 | 33 | function is_openwrt() 34 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 35 | end 36 | -- if (not is_openwrt()) then require("luarocks.require") end 37 | -- ------------------------------------------------------------------------- -- 38 | 39 | print("\n--- example_01 v0.4-SNAPSHOT ---\n") 40 | 41 | args = lapp [[ 42 | Subscribe to topic_s and publish all messages on topic_p 43 | -g,--host_s (default localhost) Subscribe MQTT server hostname 44 | -H,--host_p (default iot.eclipse.org) Publish MQTT server hostname 45 | -i,--id (default example_01) MQTT client identifier 46 | -p,--port_s (default 1883) Subscribe MQTT server port number 47 | -q,--port_p (default 1883) Publish MQTT server port number 48 | -s,--topic_s (default test/1) Subscribe topic 49 | -t,--topic_p (default test/2) Publish topic 50 | ]] 51 | 52 | local error_message1 = nil 53 | local error_message2 = nil 54 | 55 | local mqtt_client1 56 | local mqtt_client2 57 | 58 | function callback( 59 | topic, -- string 60 | message) -- string 61 | 62 | print("Topic: " .. topic .. ", message: '" .. message .. "'") 63 | 64 | mqtt_client2:publish(args.topic_p, message) 65 | end 66 | 67 | mqtt_client1 = MQTT.client.create(args.host_s, args.port_s, callback) 68 | mqtt_client2 = MQTT.client.create(args.host_p, args.port_p) 69 | 70 | error_message1 = mqtt_client1:connect(args.id .. "-a") 71 | if error_message1 ~= nil then error(error_message1) end 72 | 73 | error_message2 = mqtt_client2:connect(args.id .. "-b") 74 | if error_message2 ~= nil then error(error_message2) end 75 | 76 | mqtt_client1:subscribe({ args.topic_s }) 77 | 78 | while (error_message1 == nil and error_message2 == nil) do 79 | error_message1 = mqtt_client1:handler() 80 | error_message2 = mqtt_client2:handler() 81 | socket.sleep(2.0) -- seconds 82 | end 83 | 84 | if (error_message1 == nil) then 85 | mqtt_client1:unsubscribe({ args.topic_s }) 86 | mqtt_client1:destroy() 87 | else 88 | print(error_message1) 89 | end 90 | 91 | if (error_message2 == nil) then 92 | mqtt_client2:destroy() 93 | else 94 | print(error_message2) 95 | end 96 | 97 | 98 | -------------------------------------------------------------------------------- /example/example_02.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | -- 3 | -- example_02.lua 4 | -- ~~~~~~~~~~~~~~ 5 | -- Version: 0.2 2012-06-01 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Publish a sequence of messages to a specified topic. 20 | -- Used to control some coloured RGB LEDs. 21 | -- 22 | -- ToDo 23 | -- ~~~~ 24 | -- - On failure, automatically reconnect to MQTT server. 25 | -- ------------------------------------------------------------------------- -- 26 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 27 | 28 | local MQTT = require "mqtt" 29 | local socket = require "socket" 30 | local lapp = require("pl.lapp") 31 | 32 | function is_openwrt() 33 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 34 | end 35 | -- if (not is_openwrt()) then require("luarocks.require") end 36 | -- ------------------------------------------------------------------------- -- 37 | 38 | print("\n--- example_02 v0.4-SNAPSHOT ---\n") 39 | 40 | local args = lapp [[ 41 | Subscribe to topic1 and publish all messages on topic2 42 | -H,--host (default localhost) MQTT server hostname 43 | -i,--id (default example_02) MQTT client identifier 44 | -p,--port (default 1883) MQTT server port number 45 | -s,--sleep (default 3.0) Sleep time between commands 46 | -t,--topic (default test/ex3) Topic on which to publish 47 | ]] 48 | 49 | 50 | local error_message = nil 51 | local index = 1 52 | local messages = { "c010000", "c000100", "c000001" } 53 | local mqtt_client = MQTT.client.create(args.host, args.port) 54 | 55 | error_message = mqtt_client:connect(args.id) 56 | if error_message ~= nil then error(error_message) end 57 | 58 | while (error_message == nil) do 59 | mqtt_client:publish(args.topic, messages[index]); 60 | 61 | index = index + 1 62 | if (index > #messages) then index = 1 end 63 | 64 | socket.sleep(args.sleep) -- seconds 65 | error_message = mqtt_client:handler() 66 | end 67 | 68 | if (error_message == nil) then 69 | mqtt_client:destroy() 70 | else 71 | print(error_message) 72 | end 73 | -------------------------------------------------------------------------------- /example/mqtt_publish.lua: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/lua 2 | -- 3 | -- mqtt_publish.lua 4 | -- ~~~~~~~~~~~~~~~~ 5 | -- Version: 0.2 2012-06-01 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Publish an MQTT message on the specified topic with an optional last will. 20 | -- 21 | -- References 22 | -- ~~~~~~~~~~ 23 | -- Lapp Framework: Lua command line parsing 24 | -- http://lua-users.org/wiki/LappFramework 25 | -- 26 | -- ToDo 27 | -- ~~~~ 28 | -- None, yet. 29 | -- ------------------------------------------------------------------------- -- 30 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 31 | 32 | local MQTT = require "mqtt" 33 | local socket = require "socket" 34 | local lapp = require("pl.lapp") 35 | 36 | function is_openwrt() 37 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 38 | end 39 | -- if (not is_openwrt()) then require("luarocks.require") end 40 | -- ------------------------------------------------------------------------- -- 41 | 42 | print("\n--- mqtt_publish v0.4-SNAPSHOT ---\n") 43 | 44 | 45 | local args = lapp [[ 46 | Publish a message to a specified MQTT topic 47 | -d,--debug Verbose console logging 48 | -H,--host (default localhost) MQTT server hostname 49 | -i,--id (default mqtt_pub) MQTT client identifier 50 | -m,--message (string) Message to be published 51 | -p,--port (default 1883) MQTT server port number 52 | -t,--topic (string) Topic on which to publish. 53 | -r,--retain Retain the message 54 | -w,--will_message (default .) Last will and testament message 55 | -w,--will_qos (default 0) Last will and testament QOS 56 | -w,--will_retain (default 0) Last will and testament retention 57 | -w,--will_topic (default .) Last will and testament topic 58 | ]] 59 | 60 | local retain = false 61 | local error_message=nil 62 | 63 | if (args.debug) then MQTT.Utility.set_debug(true) end 64 | if args.retain then retain=true end 65 | 66 | local mqtt_client = MQTT.client.create(args.host, args.port) 67 | 68 | if (args.will_message == "." or args.will_topic == ".") then 69 | error_message = mqtt_client:connect(args.id) 70 | else 71 | error_message = mqtt_client:connect( 72 | args.id, args.will_topic, args.will_qos, args.will_retain, args.will_message 73 | ) 74 | end 75 | if error_message ~= nil then error(error_message) end 76 | 77 | mqtt_client:publish(args.topic, args.message, retain) 78 | 79 | mqtt_client:destroy() 80 | 81 | -------------------------------------------------------------------------------- /example/mqtt_subscribe.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | -- 3 | -- mqtt_subscribe.lua 4 | -- ~~~~~~~~~~~~~~~~~~ 5 | -- Version: 0.2 2012-06-01 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Subscribe to an MQTT topic and display any received messages. 20 | -- 21 | -- References 22 | -- ~~~~~~~~~~ 23 | -- Lapp Framework: Lua command line parsing 24 | -- http://lua-users.org/wiki/LappFramework 25 | -- 26 | -- ToDo 27 | -- ~~~~ 28 | -- None, yet. 29 | -- ------------------------------------------------------------------------- -- 30 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 31 | 32 | local MQTT = require "mqtt" 33 | local socket = require "socket" 34 | local lapp = require("pl.lapp") 35 | 36 | -- ------------------------------------------------------------------------- -- 37 | function is_openwrt() 38 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 39 | end 40 | -- if (not is_openwrt()) then require("luarocks.require") end 41 | -- ------------------------------------------------------------------------- -- 42 | 43 | function callback( 44 | topic, -- string 45 | message) -- string 46 | 47 | print("Topic: " .. topic .. ", message: '" .. message .. "'") 48 | end 49 | 50 | print("\n--- mqtt_subscribe v0.4-SNAPSHOT ---\n") 51 | 52 | local args = lapp [[ 53 | Subscribe to a specified MQTT topic 54 | -d,--debug Verbose console logging 55 | -H,--host (default localhost) MQTT server hostname 56 | -i,--id (default mqtt_sub) MQTT client identifier 57 | -k,--keepalive (default 60) Send MQTT PING period (seconds) 58 | -p,--port (default 1883) MQTT server port number 59 | -t,--topic (string) Subscription topic 60 | -w,--will_message (default .) Last will and testament message 61 | -w,--will_qos (default 0) Last will and testament QOS 62 | -w,--will_retain (default 0) Last will and testament retention 63 | -w,--will_topic (default .) Last will and testament topic 64 | ]] 65 | 66 | 67 | if (args.debug) then MQTT.Utility.set_debug(true) end 68 | 69 | if (args.keepalive) then MQTT.client.KEEP_ALIVE_TIME = args.keepalive end 70 | 71 | local mqtt_client = MQTT.client.create(args.host, args.port, callback) 72 | 73 | local error_message=nil 74 | 75 | if (args.will_message == "." or args.will_topic == ".") then 76 | error_message = mqtt_client:connect(args.id) 77 | else 78 | error_message = mqtt_client:connect( 79 | args.id, args.will_topic, args.will_qos, args.will_retain, args.will_message 80 | ) 81 | end 82 | if error_message ~= nil then error(error_message) end 83 | 84 | mqtt_client:subscribe({args.topic}) 85 | 86 | while (error_message == nil) do 87 | error_message = mqtt_client:handler() 88 | socket.sleep(1.0) -- seconds 89 | end 90 | 91 | if (error_message == nil) then 92 | mqtt_client:unsubscribe({args.topic}) 93 | mqtt_client:destroy() 94 | else 95 | print(error_message) 96 | end 97 | 98 | -- ------------------------------------------------------------------------- -- 99 | -------------------------------------------------------------------------------- /example/mqtt_test.lua: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/lua 2 | -- 3 | -- mqtt_test.lua 4 | -- ~~~~~~~~~~~~~ 5 | -- Version: 0.4-SNAPSHOT 6 | -- ------------------------------------------------------------------------- -- 7 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 8 | -- All rights reserved. This program and the accompanying materials 9 | -- are made available under the terms of the Eclipse Public License v1.0 10 | -- which accompanies this distribution, and is available at 11 | -- http://www.eclipse.org/legal/epl-v10.html 12 | -- 13 | -- Contributors: 14 | -- Andy Gelme - Initial implementation 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Description 18 | -- ~~~~~~~~~~~ 19 | -- Repetitively publishes MQTT messages on the topic_p, 20 | -- until the "quit" message is received on the topic_s. 21 | -- 22 | -- References 23 | -- ~~~~~~~~~~ 24 | -- Lapp Framework: Lua command line parsing 25 | -- http://lua-users.org/wiki/LappFramework 26 | -- 27 | -- ToDo 28 | -- ~~~~ 29 | -- - On failure, automatically reconnect to MQTT server. 30 | -- ------------------------------------------------------------------------- -- 31 | 32 | package.path = package.path .. ";../paho/?.lua;paho/?.lua" 33 | 34 | local MQTT = require "mqtt" 35 | local socket = require "socket" 36 | local lapp = require("pl.lapp") 37 | local version = "0.4-SNAPSHOT" 38 | 39 | -- ------------------------------------------------------------------------- -- 40 | function is_openwrt() 41 | return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT 42 | end 43 | -- if (not is_openwrt()) then require("luarocks.require") end 44 | -- ------------------------------------------------------------------------- -- 45 | 46 | print("\n--- mqtt_test (v." .. version .. ") ---\n") 47 | 48 | local running = true 49 | local counter = 0 50 | 51 | -- Define a function which is called by mqtt_client:handler(), 52 | -- whenever messages are received on the subscribed topics 53 | function callback( 54 | topic, -- string 55 | payload) -- string 56 | 57 | print("mqtt_test:callback(): " .. topic .. ":" .. payload) 58 | 59 | if payload == "quit" then running = false end 60 | end 61 | 62 | local args = lapp [[ 63 | Test Lua MQTT client library 64 | -d,--debug Verbose console logging 65 | -i,--id (default mqtt_test) MQTT client identifier 66 | -p,--port (default 1883) MQTT server port number 67 | -s,--topic_s (default test/2) Subscribe topic 68 | -t,--topic_p (default test/1) Publish topic 69 |February 1, 2011
12 | 13 |THE ECLIPSE FOUNDATION MAKES AVAILABLE SOFTWARE, DOCUMENTATION, INFORMATION AND/OR OTHER MATERIALS FOR OPEN SOURCE PROJECTS 16 | (COLLECTIVELY "CONTENT"). USE OF THE CONTENT IS GOVERNED BY THE TERMS AND CONDITIONS OF THIS AGREEMENT AND/OR THE TERMS AND 17 | CONDITIONS OF LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW. BY USING THE CONTENT, YOU AGREE THAT YOUR USE 18 | OF THE CONTENT IS GOVERNED BY THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR 19 | NOTICES INDICATED OR REFERENCED BELOW. IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT AND THE TERMS AND 20 | CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW, THEN YOU MAY NOT USE THE CONTENT.
21 | 22 |Unless otherwise indicated, all Content made available by the Eclipse Foundation is provided to you under the terms and conditions of the Eclipse Public License Version 1.0 25 | ("EPL"). A copy of the EPL is provided with this Content and is also available at http://www.eclipse.org/legal/epl-v10.html. 26 | For purposes of the EPL, "Program" will mean the Content.
27 | 28 |Content includes, but is not limited to, source code, object code, documentation and other files maintained in the Eclipse Foundation source code 29 | repository ("Repository") in software modules ("Modules") and made available as downloadable archives ("Downloads").
30 | 31 |The terms and conditions governing Plug-ins and Fragments should be contained in files named "about.html" ("Abouts"). The terms and conditions governing Features and 40 | Included Features should be contained in files named "license.html" ("Feature Licenses"). Abouts and Feature Licenses may be located in any directory of a Download or Module 41 | including, but not limited to the following locations:
42 | 43 |Note: if a Feature made available by the Eclipse Foundation is installed using the Provisioning Technology (as defined below), you must agree to a license ("Feature Update License") during the 52 | installation process. If the Feature contains Included Features, the Feature Update License should either provide you with the terms and conditions governing the Included Features or 53 | inform you where you can locate them. Feature Update Licenses may be found in the "license" property of files named "feature.properties" found within a Feature. 54 | Such Abouts, Feature Licenses, and Feature Update Licenses contain the terms and conditions (or references to such terms and conditions) that govern your use of the associated Content in 55 | that directory.
56 | 57 |THE ABOUTS, FEATURE LICENSES, AND FEATURE UPDATE LICENSES MAY REFER TO THE EPL OR OTHER LICENSE AGREEMENTS, NOTICES OR TERMS AND CONDITIONS. SOME OF THESE 58 | OTHER LICENSE AGREEMENTS MAY INCLUDE (BUT ARE NOT LIMITED TO):
59 | 60 |IT IS YOUR OBLIGATION TO READ AND ACCEPT ALL SUCH TERMS AND CONDITIONS PRIOR TO USE OF THE CONTENT. If no About, Feature License, or Feature Update License is provided, please 70 | contact the Eclipse Foundation to determine what terms and conditions govern that particular Content.
71 | 72 | 73 |The Eclipse Foundation makes available provisioning software, examples of which include, but are not limited to, p2 and the Eclipse 76 | Update Manager ("Provisioning Technology") for the purpose of allowing users to install software, documentation, information and/or 77 | other materials (collectively "Installable Software"). This capability is provided with the intent of allowing such users to 78 | install, extend and update Eclipse-based products. Information about packaging Installable Software is available at http://eclipse.org/equinox/p2/repository_packaging.html 80 | ("Specification").
81 | 82 |You may use Provisioning Technology to allow other parties to install Installable Software. You shall be responsible for enabling the 83 | applicable license agreements relating to the Installable Software to be presented to, and accepted by, the users of the Provisioning Technology 84 | in accordance with the Specification. By using Provisioning Technology in such a manner and making it available in accordance with the 85 | Specification, you further acknowledge your agreement to, and the acquisition of all necessary rights to permit the following:
86 | 87 |Content may contain encryption software. The country in which you are currently may have restrictions on the import, possession, and use, and/or re-export to 103 | another country, of encryption software. BEFORE using any encryption software, please check the country's laws, regulations and policies concerning the import, 104 | possession, or use, and re-export of encryption software, to see if this is permitted.
105 | 106 |Java and all Java-based trademarks are trademarks of Oracle Corporation in the United States, other countries, or both.
107 | 108 | 109 | -------------------------------------------------------------------------------- /paho/mqtt.lua: -------------------------------------------------------------------------------- 1 | --- 2 | -- @module mqtt_library 3 | -- ~~~~~~~~~~~~~~~~ 4 | -- Version: 0.4-SNAPSHOT 5 | -- -------------------------------------------------------------------------- -- 6 | -- Copyright (c) 2011-2012 Geekscape Pty. Ltd. 7 | -- All rights reserved. This program and the accompanying materials 8 | -- are made available under the terms of the Eclipse Public License v1.0 9 | -- which accompanies this distribution, and is available at 10 | -- http://www.eclipse.org/legal/epl-v10.html 11 | -- 12 | -- Contributors: 13 | -- Andy Gelme - Initial API and implementation 14 | -- Kevin KIN-FOO - Authentication and rockspec 15 | -- -------------------------------------------------------------------------- -- 16 | -- 17 | -- Documentation 18 | -- ~~~~~~~~~~~~~ 19 | -- Paho MQTT Lua website 20 | -- http://eclipse.org/paho/ 21 | -- 22 | -- References 23 | -- ~~~~~~~~~~ 24 | -- MQTT Community 25 | -- http://mqtt.org 26 | 27 | -- MQTT protocol specification v3.1.1 28 | -- http://mqtt.org/documentation 29 | -- http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.pdf 30 | -- 31 | -- Notes 32 | -- ~~~~~ 33 | -- - Always assumes MQTT connection "clean session" enabled. 34 | -- - Only supports QOS level 0. 35 | -- - Publish message doesn't support "message identifier". 36 | -- - Subscribe acknowledgement messages don't check granted QOS level. 37 | -- - Outstanding subscribe acknowledgement messages aren't escalated. 38 | -- 39 | -- ToDo 40 | -- ~~~~ 41 | -- * Consider when payload needs to be an array of bytes (not characters). 42 | -- * Maintain both "last_activity_out" and "last_activity_in". 43 | -- * - http://mqtt.org/wiki/doku.php/keepalive_for_the_client 44 | -- * Update "last_activity_in" when messages are received. 45 | -- * When a PINGREQ is sent, must check for a PINGRESP, within KEEP_ALIVE_TIME.. 46 | -- * Otherwise, fail the connection. 47 | -- * When connecting, wait for CONNACK, until KEEP_ALIVE_TIME, before failing. 48 | -- This fixed by: https://github.com/iotrac/mqtt-lua/commit/3c8776bb8b67e23fff8d3fd2908145ccb8afdfa2 49 | -- After CONNECT msg, wait for CONNACK until CONNACK_WAIT_TIMEOUT. KEEP_ALIVE_TIME is too long. 50 | -- 51 | -- * Review all public APIs for asynchronous callback behaviour. 52 | -- * Implement parse PUBACK message. -- This should be: Implement QoS level 1. 53 | 54 | -- * Handle failed subscriptions, i.e no subscription acknowledgement received. 55 | 56 | -- MQTT.client:handler(): Message length mismatch 57 | -- - On socket error, optionally try reconnection to MQTT server. 58 | -- - Consider use of assert() and pcall() ? 59 | -- - Only expose public API functions, don't expose internal API functions. 60 | -- - Refactor "if self.connected()" to "self.checkConnected(error_message)". 61 | -- - Maintain and publish messaging statistics. 62 | -- - Memory heap/stack monitoring. 63 | -- - When debugging, why isn't mosquitto sending back CONNACK error code ? 64 | -- Fixed by: https://github.com/iotrac/mqtt-lua/issues/3 65 | -- 66 | -- - Implement asynchronous state machine, rather than single-thread waiting. 67 | -- - Consider using Copas http://keplerproject.github.com/copas/manual.html 68 | -- ------------------------------------------------------------------------- -- 69 | 70 | function isPsp() return(Socket ~= nil) end 71 | 72 | local socket = require("socket") 73 | -- require("io") 74 | -- require("ltn12") 75 | -- require("ssl") 76 | 77 | local version = "0.4-SNAPSHOT" 78 | 79 | local MQTT = {} 80 | 81 | local pathOfThisFile = ... 82 | local folderOfThisFile = (...):match("(.-)[^%.]+$") 83 | -- Thanks to http://stackoverflow.com/questions/9145432/load-lua-files-by-relative-path 84 | 85 | --- 86 | -- @field [parent = #mqtt_library] utility#utility Utility 87 | -- 88 | MQTT.Utility = require(folderOfThisFile .. 'utility') 89 | 90 | 91 | --- 92 | -- @field [parent = #mqtt_library] #number VERSION 93 | -- 94 | MQTT.VERSION = 0x04 95 | 96 | --- 97 | -- @field [parent = #mqtt_library] #boolean ERROR_TERMINATE 98 | -- 99 | MQTT.ERROR_TERMINATE = false -- Message handler errors terminate process ? 100 | 101 | --- 102 | -- @field [parent = #mqtt_library] #string DEFAULT_BROKER_HOSTNAME 103 | -- 104 | MQTT.DEFAULT_BROKER_HOSTNAME = "iot.eclipse.org" 105 | 106 | --- 107 | -- An MQTT client 108 | -- @type client 109 | 110 | --- 111 | -- @field [parent = #mqtt_library] #client client 112 | -- 113 | MQTT.client = {} 114 | 115 | MQTT.client.__index = MQTT.client 116 | 117 | --- 118 | -- @field [parent = #client] #number DEFAULT_PORT 119 | -- 120 | MQTT.client.DEFAULT_PORT = 1883 121 | 122 | --- 123 | -- @field [parent = #client] #number KEEP_ALIVE_TIME 124 | -- 125 | MQTT.client.KEEP_ALIVE_TIME = 60 -- seconds (maximum is 65535) 126 | MQTT.client.CONNACK_WAIT_TIMEOUT = 3 -- seconds [MQTT-3.2: "Reasonable" time] 127 | MQTT.client.CLIENT_SOCKET_TIMEOUT = 0.01 128 | --- 129 | -- @field [parent = #client] #number MAX_PAYLOAD_LENGTH 130 | -- 131 | MQTT.client.MAX_PAYLOAD_LENGTH = 268435455 -- bytes 132 | 133 | -- MQTT 3.1.1 Specification: Section 2.2.1: Fixed header control packet types 134 | --- 135 | -- @field [parent = #mqtt_library] message 136 | -- 137 | MQTT.RESERVED_00 = 0x00 -- Forbidden 138 | MQTT.CONNECT = 0x01 139 | MQTT.CONNACK = 0x02 140 | MQTT.PUBLISH = 0x03 141 | MQTT.PUBACK = 0x04 142 | MQTT.PUBREC = 0x05 143 | MQTT.PUBREL = 0x06 144 | MQTT.PUBCOMP = 0x07 145 | MQTT.SUBSCRIBE = 0x08 146 | MQTT.SUBACK = 0x09 147 | MQTT.UNSUBSCRIBE = 0x0A 148 | MQTT.UNSUBACK = 0x0B 149 | MQTT.PINGREQ = 0x0C 150 | MQTT.PINGRESP = 0x0D 151 | MQTT.DISCONNECT = 0x0E 152 | MQTT.RESERVED_FF = 0xFF --Forbidden 153 | 154 | -- MQTT 3.1.1 Specification: Section 3.2: CONNACK return error messages. 155 | -- CONNACK return code 0x0 -> Connection Accepted. Others are errors. 156 | MQTT.error_message = {} 157 | MQTT.error_message.CONNACK = { -- CONNACK return code used as the index 158 | "Connection refused, unacceptable protocol version", -- 0x01 159 | "Connection refused, identifer rejected", -- 0x02 160 | "Connection refused, server unavailable", -- 0x03 161 | "Connection refused, bad user name or password", -- 0x04 162 | "Connection refused, client is not authorized to connect" -- 0x05 163 | -- 6-255: Reserved for future use 164 | } 165 | 166 | -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- 167 | -- Create an MQTT client instance 168 | -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 169 | --- 170 | -- Create an MQTT client instance. 171 | -- @param #string hostname Host name or address of the MQTT broker 172 | -- @param #number port Port number of the MQTT broker (default: 1883) 173 | -- @param #function callback Invoked when subscribed topic messages received 174 | -- @function [parent = #client] create 175 | -- @return #client created client 176 | -- 177 | function MQTT.client.create( -- Public API 178 | hostname, -- string: Host name or address of the MQTT broker 179 | port, -- integer: Port number of the MQTT broker (default: 1883) 180 | callback) -- function: Invoked when subscribed topic messages received 181 | -- return: mqtt_client table 182 | 183 | local mqtt_client = {} 184 | 185 | setmetatable(mqtt_client, MQTT.client) 186 | 187 | mqtt_client.callback = callback -- function(topic, payload) 188 | mqtt_client.hostname = hostname or MQTT.DEFAULT_BROKER_HOSTNAME 189 | mqtt_client.port = port or MQTT.client.DEFAULT_PORT 190 | 191 | mqtt_client.connected = false 192 | mqtt_client.destroyed = false 193 | mqtt_client.last_activity = 0 194 | mqtt_client.message_id = 0 195 | mqtt_client.outstanding = {} 196 | mqtt_client.socket_client = nil 197 | 198 | return(mqtt_client) 199 | end 200 | 201 | -------------------------------------------------------------------------------- 202 | -- Specify username and password before #client.connect 203 | -- 204 | -- It is possible to have empty username and passwords per [MQTT-3.1.3.4] 205 | -- 206 | -- If called with empty _username_ or _password_, username and password flags will be set and: 207 | -- If username is empty, 0 length UTF-8 encoded string will be added to payload. 208 | -- If password is empty, 2 bytes for length + 0 length binary password(emtpy) 209 | -- will be added added to payload. [MQTT-3.1.3.5] 210 | -- 211 | -- What will happen when username="" and password="abc" is: 212 | -- 1. username & password flags will be set 213 | -- 2. username will be encoded as only two bytes of length 0 as per [MQTT 1.5.3] 214 | -- 3. password bytes will contain '0,3,97,98,99' 215 | -- 216 | -- Remains to be seen if this behaviour will be accepted by brokers. 217 | -- 218 | -- Usage example: 219 | -- local mqtt_client = MQTT.client.create(args.host, args.port, callback) 220 | -- mqtt_client.auth(mqtt_client,