{"id":733,"date":"2016-04-21T14:04:56","date_gmt":"2016-04-21T12:04:56","guid":{"rendered":"http:\/\/www.moosth.net\/calabrothers\/?p=733"},"modified":"2016-05-25T22:55:28","modified_gmt":"2016-05-25T20:55:28","slug":"733","status":"publish","type":"post","link":"https:\/\/www.moosth.net\/calabrothers\/733\/","title":{"rendered":"Python, Crossbar and Autobahn&#8230; IoT.begin()"},"content":{"rendered":"<h1 class=\"vk_ans\"><span data-dobid=\"hdw\"><em>Internet of things<\/em> <\/span><\/h1>\n<p><span data-dobid=\"hdw\">is defined as<i>\u00a0&#8220;<\/i><\/span>proposed development of the Internet in which everyday objects have network connectivity, allowing them to send and receive data&#8221;.<\/p>\n<p>This extreme vision requires a huge effort especially in definition of common, standard protocols to interconnect devices. Many frameworks have been proposed by several big IT companies, i.e. Microsoft Azure, Google Cloud, IBM Watson etc&#8230;<\/p>\n<p>Here I am going to present a real-time\u00a0open source implementation of the <a href=\"http:\/\/tools.ietf.org\/html\/rfc6455\">WebSocket<\/a> and <a href=\"http:\/\/wamp.ws\/\">Web Application Messaging Protocol<\/a> named <a href=\"http:\/\/autobahn.ws\/\">Autobahn<\/a>. Since one of my personal goals for 2016 is to learn Python (2.x and 3.x), I decided to begin my Python coding directly with an IoT node&#8230; what a challenging choice \ud83d\ude09<\/p>\n<div class=\"vk_ans\"><\/div>\n<h3 class=\"vk_ans\">First of all, why WAMP?<\/h3>\n<p class=\"vk_ans\">WAMP provides Unified Application Routing in an open WebSocket protocol that works with\u00a0different languages. Using WAMP you can build distributed systems out of application components which are <b>loosely coupled<\/b> and communicate in (soft) <b>real-time<\/b>.<\/p>\n<p class=\"vk_ans\">WAMP offers two communication patterns for application components to talk to each other:<\/p>\n<ul>\n<li class=\"vk_ans\">Publish &amp; Subscribe (PubSub)<\/li>\n<li class=\"vk_ans\">Remote Procedure Calls (RPC)<\/li>\n<\/ul>\n<p class=\"vk_ans\">WAMP is easy to use, simple to implement and based on modern Web standards: WebSocket, JSON and URIs.<\/p>\n<p class=\"vk_ans\">WAMP is <strong>ideal for distributed, multi-client and server applications<\/strong>, such as multi-user database-driven business applications, sensor networks (IoT), instant messaging or MMOGs (massively multi-player online games).<\/p>\n<div class=\"vk_ans\"><\/div>\n<h3 class=\"vk_ans\">Choosing the WAMP router<\/h3>\n<div class=\"vk_ans\">A WAMP router is nothing but a server which perform message passing as a router&#8230;<\/div>\n<div class=\"vk_ans\">There exist <a href=\"http:\/\/wamp-proto.org\/implementations\/#routers\">many implementation of WAMP routers<\/a>.<\/div>\n<div class=\"vk_ans\">I decided to start with <a href=\"http:\/\/crossbar.io\/\">crossbar<\/a>, since it is recommended by autobahn project.<\/div>\n<div class=\"vk_ans\"><\/div>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n# pip install twisted\r\n# pip install crossbar \r\n<\/pre>\n<p>To run crossbar, you can execute<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n# crossbar init\r\n# crossbar start \r\n<\/pre>\n<p>Note that, crossbar requires Python 2.x&#8230; by starting with default values, the crossbar WAMP router<br \/>\nwill instantiate a TCP server on the 8080 port (realm1).<\/p>\n<p>&nbsp;<\/p>\n<h3>Installing Autobahn<\/h3>\n<p>Working with Python is amazing, the package dependencies are handled by pip very efficiently&#8230;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n# pip install autobahn\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Python Subscriber<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n#!\/usr\/bin\/env python2\r\nimport sys\r\nfrom twisted.internet.defer import inlineCallbacks\r\nfrom autobahn.twisted.wamp import ApplicationSession, ApplicationRunner\r\n\r\n\r\nclass MyComponent(ApplicationSession):\r\n    @inlineCallbacks\r\n    def onJoin(self, details):\r\n        print(&quot;session ready&quot;)\r\n\r\n        def oncounter(count):\r\n            print(&quot;event received: {0}&quot;, count)\r\n\r\n        try:\r\n            yield self.subscribe(oncounter, u'com.myapp.oncounter')\r\n            print(&quot;subscribed to topic&quot;)\r\n        except Exception as e:\r\n            print(&quot;could not subscribe to topic: {0}&quot;.format(e))\r\n\r\nif __name__ == '__main__':\r\n    print (&quot;Running with Python %s\\n&quot; % sys.version)\r\n    runner = ApplicationRunner(url=u&quot;ws:\/\/localhost:8080\/ws&quot;, realm=u&quot;realm1&quot;)\r\n    runner.run(MyComponent)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Python Publisher<\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n#!\/usr\/bin\/env python2\r\nimport sys\r\nfrom autobahn.twisted.util import sleep\r\nfrom twisted.internet.defer import inlineCallbacks\r\nfrom autobahn.twisted.wamp import ApplicationSession, ApplicationRunner\r\n\r\n\r\nclass MyComponent(ApplicationSession):\r\n    @inlineCallbacks\r\n    def onJoin(self, details):\r\n        print(&quot;session ready&quot;)\r\n\r\n        counter = 0\r\n        while True:\r\n            self.publish(u'com.myapp.oncounter', counter)\r\n            counter += 1\r\n            yield sleep(1)\r\n\r\n\r\nif __name__ == '__main__':\r\n    print (&quot;Running with Python %s\\n&quot; % sys.version)\r\n    runner = ApplicationRunner(url=u&quot;ws:\/\/localhost:8080\/ws&quot;, realm=u&quot;realm1&quot;)\r\n    runner.run(MyComponent)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Have fun with Python and IoT!<\/h3>\n<p>As a senior C\/C++ developer with a solid background in scripting languages, I found Python amazing! With a few lines of code you can really do amazing things!<\/p>\n<p>Autobahn, WAMP, crossbar&#8230; is that really complicated? Not at all! The good news is that within 5 minutes an IoT framework can be setup with no problems!<\/p>\n<p>Have fun with IoT and enjoy a &#8220;standalone&#8221; framework which does not require a service provider but just a network of nodes \ud83d\ude09<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Internet of things is defined as\u00a0&#8220;proposed development of the Internet in which everyday objects have network connectivity, allowing them to send and receive data&#8221;. This extreme vision requires a huge effort especially in definition of common, standard protocols to interconnect devices. Many frameworks have been proposed by several big IT companies, i.e. Microsoft Azure, Google [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[2,20],"tags":[],"class_list":["post-733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-software"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s3QYke-733","jetpack_featured_media_url":"https:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2016\/04\/IoT.png","_links":{"self":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":5,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":739,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/733\/revisions\/739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/media\/742"}],"wp:attachment":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}