Thursday, 28 January 2016

IoT Vaidya Update#5 : Going Bit by bit using Bit Reactive and Kura(Implementation of IoT Button )


Whenever we get started with a new programming language, the first program which we always prefer to compile is the "Hello World" ! Similarly, whenever we are getting our hands dirty on a brand new Micro-controller board, "LED Blinking" serves the purpose. IoT Button is also based on similar concept, it is for getting started with IoT .

Diving Deeper into IoT button:(Working)
  • A push button is attached to a GPIO pin of the Raspberry Pi, which acts as an input.
  • LED is attached to another GPIO pin of Raspberry Pi, which is enabled as output.(Don't forget to attach a Resistor of 1k with led to delimit the current).
  • When the button is pressed, the status is updated on the cloud, as well as it is reflected by switching On the LED attached to Pi.
To connect the Raspberry Pi with internet, light weight protocols are preferred, MQTT is one of them.Eclipse Paho project provides open-source client implementations of MQTT  in C, Java, Python etc.
Though IoT-Button can be implemented using Pi4J library and Eclipse Paho Project, this post discusses the same using Reactive Blocks.

What are Reactive Blocks ?
According to Frank Alexander Kraemer , Technology Manager, BitReactive

" Reactive Blocks is a plugin for Eclipse, which allows to systematically  create applications from building blocks. The building blocks can encapsulate Java  code and have special interface descriptions, which goes beyond traditional  interfaces.Therefore, blocks can be easily shared with other developers and be reused  easily.The type of graphical representation makes it easy to specify and  synchronize parallel processes. Because of these properties, Reactive Blocks is  especially suitable for the future applications for the Internet of Things. The tool can  also be used wherever systems must handle multiple tasks simultaneously.

Building IoT-Button using Reactive Blocks:

Reactive Blocks provides libraries for building IoT applications.From these libraries , we can import various activity blocks and use them in our applications.

For IoT button we have used following three blocks:
1. RPi GPIO Digital OUT
2. RPi GPIO Digital In
3. Robust MQTT

A Reactive building block is a combination of tightly connected diagrams and Java code.
  • A behavior diagram is used to describe the internal behavior of a block.
  • Java code are used to describe the details of operations in a behavior diagram.
  • The block contract describes in which order the input and output pins of a block can be used.

Every application built using Reactive block has an initial node, this is the point from where the application gets started(A token is generated).
  • In this application we have attached the initial node to an operation init
  • init method initializes the Robust MQTT blocks by providing the broker URL. 
  • Once the initialization is done , the token is then passed to the next method/operation setOutput(which returns a GPIO pin).
  • The Gpio pin returned from the operation setOutput is set as output inside the RPi GPIO Digital OUT block(LED is connected to this pin).
  • Once the output pin is ready the token is passed to RPi GPIO Digital In block via operation setInput. setInput returns a GPIO pin to RPi GPIO Digital In block.
  • Block sets this particular pin as input.(Push Button is attached to this pin).

Note: Now all the three blocks are in active state.(Active state is the state in which token can enter any of the three blocks via any of the pin )

  • Now whenever the Button is pressed , the token is duplicated to both the  RPi GPIO Digital and LedOn method.
  • The token received by the RPi GPIO Digital Out block ,toggles the present state of led(Switches it on).
  • The token received by the LedOn is passed to operation createMesg in the form of a string("LedOn").
  • createMesg converts the string into JSON and pass it over to Robust MQTT block as a object of type MQTTMessage.
  • After receiving the token , Robust MQTT block publishes it to the broker.
Once we are done with designing our application, we need to build it. Reactive blocks can be build into various platforms , such as J2SE, OSGi, Kura etc.
We have build "IoT Button" for Kura.

Kura Bundles can be installed in Raspberry Pi using Kura Web UI.

No comments:

Post a Comment