Internet of things
is defined as “proposed development of the Internet in which everyday objects have network connectivity, allowing them to send and receive data”.
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…
Here I am going to present a real-time open source implementation of the WebSocket and Web Application Messaging Protocol named Autobahn. 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… what a challenging choice 😉
First of all, why WAMP?
WAMP provides Unified Application Routing in an open WebSocket protocol that works with different languages. Using WAMP you can build distributed systems out of application components which are loosely coupled and communicate in (soft) real-time.
WAMP offers two communication patterns for application components to talk to each other:
- Publish & Subscribe (PubSub)
- Remote Procedure Calls (RPC)
WAMP is easy to use, simple to implement and based on modern Web standards: WebSocket, JSON and URIs.
WAMP is ideal for distributed, multi-client and server applications, such as multi-user database-driven business applications, sensor networks (IoT), instant messaging or MMOGs (massively multi-player online games).
Choosing the WAMP router
# pip install twisted # pip install crossbar
To run crossbar, you can execute
# crossbar init # crossbar start
Note that, crossbar requires Python 2.x… by starting with default values, the crossbar WAMP router
will instantiate a TCP server on the 8080 port (realm1).
Installing Autobahn
Working with Python is amazing, the package dependencies are handled by pip very efficiently…
# pip install autobahn
Python Subscriber
#!/usr/bin/env python2 import sys from twisted.internet.defer import inlineCallbacks from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner class MyComponent(ApplicationSession): @inlineCallbacks def onJoin(self, details): print("session ready") def oncounter(count): print("event received: {0}", count) try: yield self.subscribe(oncounter, u'com.myapp.oncounter') print("subscribed to topic") except Exception as e: print("could not subscribe to topic: {0}".format(e)) if __name__ == '__main__': print ("Running with Python %s\n" % sys.version) runner = ApplicationRunner(url=u"ws://localhost:8080/ws", realm=u"realm1") runner.run(MyComponent)
Python Publisher
#!/usr/bin/env python2 import sys from autobahn.twisted.util import sleep from twisted.internet.defer import inlineCallbacks from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner class MyComponent(ApplicationSession): @inlineCallbacks def onJoin(self, details): print("session ready") counter = 0 while True: self.publish(u'com.myapp.oncounter', counter) counter += 1 yield sleep(1) if __name__ == '__main__': print ("Running with Python %s\n" % sys.version) runner = ApplicationRunner(url=u"ws://localhost:8080/ws", realm=u"realm1") runner.run(MyComponent)
Have fun with Python and IoT!
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!
Autobahn, WAMP, crossbar… is that really complicated? Not at all! The good news is that within 5 minutes an IoT framework can be setup with no problems!
Have fun with IoT and enjoy a “standalone” framework which does not require a service provider but just a network of nodes 😉