Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Mule Runtime and a Pi to Light Up LED Bulbs

DZone's Guide to

Using Mule Runtime and a Pi to Light Up LED Bulbs

See how this IoT integration utilizes Mule's Runtime Engine, an easy-to-create Mule flow, and a Raspberry Pi to create a blinky app.

· IoT Zone
Free Resource

Discover why Bluetooth mesh is the next evolution of IoT solutions. Download the mesh overview.

MuleSoft’s lightweight Runtime Engine can be used to expose microservices and APIs on any IoT device. In this post, I will demonstrate how users can use MuleSoft and a Python script to create a simple API that lights up an LED bulb 10 times in a loop. To get started, please refer to the requirements, video tutorial, and steps below.

Requirements

  1. Raspberry Pi 3

  2. LED bulbs

  3. Resistors

  4. Jumper wires

  5. Mule Server

  6. A breadboard

Git Repository

You can find the Mule IoT LED Project at the link provided.

Project Steps

1. Build the LED blink project

  • Set up your Raspberry Pi and make sure it is and running so that you can SSH into it using an IP (preferably a static IP).
  • Ensure the Raspberry Pi is connected to the Wi-Fi router (wireless or LAN cable).

 2. Install Mule on Raspberry Pi

  • Install the Mule Server in your Raspberry Pi. It should be up as an instance, or as a service.

3. Create a Python script with GPIO commands:

  • Write a Python script that can control GPIO commands to blink the LED bulb, which should be connected with the proper resistor and jumper wire to Raspberry PI.

4. Create a Mule app and call the Python script in localhost (Raspberry Pi):

  • Deploy the Mule app in the Raspberry Pi. Make sure it is able to call the Python script to blink the LED bulb.

5. Create a proxy app on a client machine and call the Mule Raspberry Pi app using the Raspberry Pi IP address through the RAML Console

  • Call the Mule app running on the Raspberry Pi from another client machine through a REST RAML-based API on the static IP (Raspberry Pi IP address).

6. Expose REST API endpoints by making the Raspberry IP address public

  • Finally, configure the Raspberry IP address as public, enabling you to access it from any location and at any time.

As this demo has demonstrated, MuleSoft’s Runtime Engine can be used in to connect various devices, data, and applications and, in the process, expose IoT services to the outside world.

Mule Flow

HelloWorld

Image title

HelloWorld.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"  xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <http:listener-config doc:name="HTTP Listener Configuration" host="0.0.0.0" name="HTTP_Listener_Configuration" port="${http.port}"/>
    <flow name="HelloWorldFlow1">
        <http:listener config-ref="HTTP_Listener_Configuration" doc:description="This endpoint receives an HTTP message." doc:name="Receive HTTP request" path="helloWorld"/>
        <scripting:component doc:name="Python">
            <scripting:script engine="jython" file="/home/pi/LED.py"/>
        </scripting:component>
<scripting:component doc:name="Script">
<scripting:script engine="groovy"><![CDATA[
def command = "python /home/pi/newLED.py"
println "$command"
def cmd = command.execute()
]]></scripting:script>
</scripting:component>
        <set-payload doc:description="This processor sets the payload of the message to the string 'Hello World'." doc:name="Set Payload to a constant string" value="Hello World"/>
    </flow>
</mule>


api.raml

Image title


api.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    <http:listener-config name="api-httpListenerConfig" host="0.0.0.0" port="9582" doc:name="HTTP Listener Configuration"/>
    <apikit:config name="api-config" raml="api.raml" consoleEnabled="false" doc:name="Router"/>
    <flow name="api-main">
        <http:listener config-ref="api-httpListenerConfig" path="/api/*" doc:name="HTTP"/>
        <apikit:router config-ref="api-config" doc:name="APIkit Router"/>
        <logger level="INFO" doc:name="Logger"/>
        <exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
    </flow>
    <flow name="api-console">
        <http:listener config-ref="api-httpListenerConfig" path="/console/*" doc:name="HTTP"/>
        <apikit:console config-ref="api-config" doc:name="APIkit Console"/>
    </flow>
    <flow name="get:/lightup:api-config">
        <flow-ref name="lightappFlow" doc:name="Flow Reference"/>
        <set-payload value="{'result':'success'}" doc:name="Set Payload"/>
    </flow>
    <flow name="post:/lightup:api-config">
        <set-payload value="'result':'success'" doc:name="Set Payload"/>
    </flow>
    <apikit:mapping-exception-strategy name="api-apiKitGlobalExceptionMapping">
        <apikit:mapping statusCode="404">
            <apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="405">
            <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="415">
            <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="406">
            <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Not acceptable&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="400">
            <apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
    </apikit:mapping-exception-strategy>
</mule>


lightapp.xml

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <!-- <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="${http.port}" doc:name="HTTP Listener Configuration"/> -->
    <http:request-config name="HTTP_Request_Configuration" host="192.168.1.4" port="8082" doc:name="HTTP Request Configuration"/>
    <flow name="lightappFlow">
        <http:request config-ref="HTTP_Request_Configuration" path="helloWorld" method="GET" doc:name="HTTP"/>
    </flow>
</mule>


And there you have it! You've got the app and the flow up and running for your own LED app.

Take a deep dive into Bluetooth mesh. Read the tech overview and discover new IoT innovations.

Topics:
iot ,mulesoft ,led bulbs ,raspberry pi ,tutorial

Published at DZone with permission of Abhishek Sinha. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}