Talk to your Toaster with Mule
Tags: mulesofthackathon, iot, mqtt, sensors
Published: 2020-11-28 14:33:25

#Talk to your Toaster with Mule
Internet of Things(IoT) is an expansion of Internet- a way of intelligently fusing the real and cyber world. As day by day internet is growing rapidly, we are moving in direction of making everything available on internet, so that it marks its presence in every part of the world. 

The IoT is comprised of smart machines interacting and communicating with other machines, objects, environments and infrastructures. As a result, huge volumes of data are being generated, and that data is being processed into useful actions that can “command and control” things to make our lives much easier and safer—and to reduce our impact on the environment.

 Environmental monitoring applications of the IoT typically use sensors to assist in environmental protection by monitoring air or water quality, atmospheric or soil conditions, and can even include areas like monitoring the movements of wildlife and their habitats.
Multiple sensors are connected to Eclipse Mosquitto through the MQTT protocol, sensor’s data is received in json and exposed to the client in MuleSoft with a Restful API, the client can set rule based conditions and communicate with the sensor. 
##Component Diagram
![componenet-2](https://dev-to-uploads.s3.amazonaws.com/i/2glztnyh6e1dvj882st1.png)
##Sequential Diagram
![Sequence Diagram](https://dev-to-uploads.s3.amazonaws.com/i/682cx38c3g9oxef3q1nz.PNG)
##HARDWARE
Arduino Uno is a microcontroller board based on the ATmega328P. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator (CSTCE16M0V53-R0), a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with an AC-to-DC adapter or battery to get started.![pic1](https://dev-to-uploads.s3.amazonaws.com/i/xxc8x67y31hxfgysnxzh.jpg)
 
##Specifications:
It is an ATmega328P based Microcontroller
The Operating Voltage of the Arduino is 5V
The recommended input voltage ranges from 7V to 12V
The i/p voltage (limit) is 6V to 20V
Digital input and output pins-14
Digital input & output pins (PWM)-6
Analog i/p pins are 6
DC Current for each I/O Pin is 20 mA
DC Current used for 3.3V Pin is 50 mA
Flash Memory -32 KB, and 0.5 KB memory is used by the boot loader
SRAM is 2 KB
EEPROM is 1 KB
The speed of the CLK is 16 MHz
In Built LED
Length and width of the Arduino are 68.6 mm X 53.4 mm
The weight of the Arduino board is 25 g



##Ethernet Shield:
![Picture2](https://dev-to-uploads.s3.amazonaws.com/i/enc1ga0zyh2tilauj20a.jpg)
 
The W5100 is a versatile single-chip network interface chip with a 10/100Mbs Ethernet controller integrated internally. It is mainly used in a high integrated, high stable, high performance, and low-cost embedded system. Ethernet Shield allows internet connectivity to Arduino board by using its Ethernet library.
The hardware TCP/IP protocol stack supports the following protocols: TCP, UDP, IPV4, ICMP, ARP, IGMP and PPoE.
Specification:
•	Operating voltage 5V (supplied from the Arduino Board) 
•	Ethernet Controller: W5100 with internal 16K buffer 
•	Connection speed: 10/100Mb 
•	Connection with Arduino on SPI port 
•	The Shield contains a number of informational LEDs 
•	PWR: indicates that the board and shield are powered 
•	LINK: indicates the presence of a network link and flashes when the shield transmits or receives data 
•	FULLD: indicates that the network connection is full duplex 
•	100M: indicates the presence of a 100 Mb/s network connection (as opposed to 10 Mb/s) 
•	RX: flashes when the shield receives data 
•	TX: flashes when the shield sends data 
•	COLL: flashes when network collisions are detected

##Ultrasonic:
![ultrasonic](https://dev-to-uploads.s3.amazonaws.com/i/f6uk4e000lnaiyufsbf4.jpg)
 
This is the HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver, and a control circuit.
There are only four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground).

##Specifications:
Operating voltage: +5V
Theoretical Measuring Distance: 2cm to 450cm
Practical Measuring Distance: 2cm to 80cm
Accuracy: 3mm
Measuring angle covered: <15°
Operating Current: <15mA
Operating Frequency: 40Hz
##Infrared Sensor:
![Picture4](https://dev-to-uploads.s3.amazonaws.com/i/rinadcskngvpo09gsuyh.jpg)
 
An infrared sensor is an electronic device, that emits in order to sense some aspects of the surroundings. An IR sensor can measure the heat of an object as well as detects the motion. These types of sensors measure only infrared radiation, rather than emitting it that is called as a passive IR sensor.

##Specifications:
     *5VDC Operating voltage
     *I/O pins are 5V and 3.3V compliant
     *Range: Up to 20cm
     *Adjustable Sensing range
     *Built-in Ambient Light Sensor
     *20mA supply current
     *Mounting hole
##Temperature Sensor:
![Picture5](https://dev-to-uploads.s3.amazonaws.com/i/pp2d0qxeueqzigzydo3g.jpg)
 
LM35 is a precession Integrated circuit Temperature sensor, whose output voltage varies, based on the temperature around it. It is a small and cheap IC which can be used to measure temperature anywhere between -55°C to 150°C. It can easily be interfaced with any Microcontroller that has ADC function or any development platform like Arduino.
Power the IC by applying a regulated voltage like +5V (VS) to the input pin and connected the ground pin to the ground of the circuit.

##Specifications:
Minimum and Maximum Input Voltage is 35V and -2V, respectively. Typically, 5V.
Can measure temperature ranging from -55°C to 150°C 
Output voltage is directly proportional (Linear) to temperature (i.e.) there will be a rise of 10mV (0.01V) for every 1°C rise in temperature.
±0.5°C Accuracy
Drain current is less than 60uA  
Low cost temperature sensor
Small and hence suitable for remote applications
Available in TO-92, TO-220, TO-CAN and SOIC package
##PACKAGES 
 ##MQTT ADAPTER
	1.ArduinoJson.h
	2.SPI.h
	3.Ethernet.h
	4.PubSubClient.h

 ##MULE ADAPTER
![dep1](https://dev-to-uploads.s3.amazonaws.com/i/wjujj68l7fkyt8k1juem.png)
![dep3](https://dev-to-uploads.s3.amazonaws.com/i/pz5459cri39uxpacrstj.png)
![dep2](https://dev-to-uploads.s3.amazonaws.com/i/m2iq2bp54zqm1z0zw1pk.png)
   
##Json
![json arduino](https://dev-to-uploads.s3.amazonaws.com/i/gd1fu72xzc8fkdf3xg1g.png)
 
##Circuit
![Picture6](https://dev-to-uploads.s3.amazonaws.com/i/lla2sb7ah72y4xw2pul4.jpg)
 
 Step 1: Setting up Arduino and Arduino IDE
To start programming the Arduino board we need Arduino IDE which is open- source software, easily available to download and install.
Arduino IDE supports C and C++ for coding. It has various libraries for different devices that are supported by Arduino board which can be used as per requirement of the project.
Step 2: Installing Arduino Libraries
We can add libraries which are already present on board and we can add libraries which we have downloaded, in our project we have used following libraries
•	PubSubClient-2.8.0 by Nick O’Leary 
•	Adafruit Unified Sensor2.1.0 by Adafruit 
•	Ethernet-2.0.0 by Various 
•	MQTT-2.4.8 by Joel Gaehwiler 
•	Arduino Http Client- 2.2.0 by Adrian McEwen 
•	Arduino JSON-6.17.2 by Benoit Blanchon 
•	LM35 Sensor-1.0.0 by Wilmouth Steven 
•	Ultrasonic-3.0.0 by Erick Simoes 
•	IRremote-2.8.0 by Shirriff 
Step 3: Prepare the circuit and upload a sketch
After downloading and adding libraries in our sketch we should write the code for our program and complete our circuit as per the requirements, once both are ready then only, we can upload the code on the board after which we can verify for any error present in our code or in connections of the circuit. If errors are there then we have to do the troubleshooting and once every errors are removed then our circuit will be ready to work according to our code.
##MQTT Adapter
MQTT is messaging queue telemetric transfer, standard protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. 

MQTT clients are very small, require minimal resources so can be used on small microcontrollers.
MQTT allows for messaging between device to cloud and cloud to device. This makes for easy broadcasting messages to groups of things. MQTT can scale to connect with millions of IoT devices.
MQTT protocol is connected to a MQTT broker at the specified port. Here, test.mosquitto.org is used to connect to a Topic to publish the sensor’s data in json format.
For the data sending the sensor’s data to the client is subscribed to the Topic named as: esp/test

##Eclipse Mosquitto Library:
Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.
The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.
The Mosquitto project also provides a C library for implementing MQTT clients, and the very popular mosquitto_pub and mosquitto_sub command line MQTT clients.

##Connect to the Mosquitto Topic: 
Step 1: Complete the circuit and connect the Ethernet shield to the laptop/Desktop.
Step 2: Open the Arduino IDE and write the code. Arduino allows two methods 
1.	Setup()
2.	Loop() 
The Arduino IDE looks are like the below image.
![Picture8](https://dev-to-uploads.s3.amazonaws.com/i/9ups2rv2lguuxezshs0q.png)
 
Step 3: Tools  Serial Monitor
The output shows the connection establishment, the published message to the broker and message received from that Topic. 
![Picture9](https://dev-to-uploads.s3.amazonaws.com/i/8i5bii6e7mv4g27dwknr.png)
 
Now, the sensor’s data is in the mosquitto broker waiting for Mule Adpater to set-up conenction and expose it. 

##Iot-Mule-Adpater:
The use case of the custom IoT-Mule Adaptor/Connector is –
It connects the MQTT to Mule 4 and exposes sensor’s data to the client and allows the client to communicate back to the sensor by giving commands based on parameters. A custom connector is created to setup connection to the mosquito broker for exposing incoming sensor’s data from IoT device via a Topic i.e., esp/test and the client’s conditions are sent via another Topic i.e., mule/test back to the IoT device. This solution enables communication to-fro from the sensor to Mule and from Mule to sensor.
Figure 1: Below represents the inside package structure of IoT-Mule Adapter/connector.
![Picture10](https://dev-to-uploads.s3.amazonaws.com/i/6hwvss7r6mpur0cyjzgi.jpg)
 
The following steps involved in creation of connector are:

The connector’s code is shared in github. 
https://github.com/SMandalika5/Mule-Hackathon
 
Step 1: After completing the connector code go to Project right clickProperties
![Picture11](https://dev-to-uploads.s3.amazonaws.com/i/f1inhjdjp0137tt50dg5.png)
 
This will take you to that location where you can set your jar file.

Step 2: After going to the location, type cmd in the window search field and give the command.

![Picture13](https://dev-to-uploads.s3.amazonaws.com/i/89zj3kcqm4v5g4hcc1wq.png)

  
Step3: mvn install 
After successful build run, install the dependencies in the local repository which can be found in your package structure. 
mvn install:install-file -Dfile=path of the jar file  DgroupId=groupId value DartifactId=artifactId value  -Dversion=version value -Dpackaging=jar

 
####Example:
mvn install:install-file -Dfile=E:\iot-mule-connector-1.3.1-mule-plugin.jar -DgroupId=com.iot.connector -DartifactId=iot-mule-connector -Dversion=1.0.0 -Dpackaging=jar
![Picture14](https://dev-to-uploads.s3.amazonaws.com/i/94vw4y16uuukix1m0uju.png)![Picture15]
 
Step 4: Create a  new Mule Project.
![Picture14](https://dev-to-uploads.s3.amazonaws.com/i/94vw4y16uuukix1m0uju.png)
Step 5: Give a proejct name
![Picture16](https://dev-to-uploads.s3.amazonaws.com/i/b5sy62kaxh3jn5z4n7ou.jpg)
 
Step 6: The required depemdecy for conenction set is pasted in the pom.xml file.
![dep1](https://dev-to-uploads.s3.amazonaws.com/i/44un7d23s2uswqyvrm86.png)
![Picture17](https://dev-to-uploads.s3.amazonaws.com/i/ldkifk0zfb8qspe76mja.jpg)

  
Step 7: Drag and Drop the custom Component
![Picture18](https://dev-to-uploads.s3.amazonaws.com/i/siwuelxdpwfzwilu7eo4.png)
 
Step 8: Drag and drop the logger to log the messages.
![Picture19](https://dev-to-uploads.s3.amazonaws.com/i/93yqev2c8odlwjsrlx5w.png)
 
Step 9: Give the payload and the condition as per desired  by the client based on the Telemetric data. 
![Picture24](https://dev-to-uploads.s3.amazonaws.com/i/k08eap7oveipp7ujfqd9.png)
![Picture26](https://dev-to-uploads.s3.amazonaws.com/i/f2om661ptes9j0ccs3pw.png)
![Picture25](https://dev-to-uploads.s3.amazonaws.com/i/ukxqym1atbe0frrn2qhv.png)
   
The connection has been set up.  
The message from mule is sent back to the IoT Device and the output looks like this.
![json mule to arduino](https://dev-to-uploads.s3.amazonaws.com/i/26nxlhcl9jufshgjy6bm.png)
 
##Future Scope:
This is a basic idea how communication with IoT devices can be done using MuleSoft. The project can be extended by adding Machine Learning and conducting analytics on the Telemetric data by storing in the database. This way the client views the prediction of the temperature based on historic temperature. Other several services can be provided, where he can communicate to the IoT device as per the predictions and not only as rule-based commands.