• Tiada Hasil Ditemukan

Raspberry Pi 3 Model B

In document DECLARATION OF ORIGINALITY (halaman 98-112)

Chapter 5: System Specifications and Implementation

5.2 The Implementation and Results

5.2.3 Raspberry Pi 3 Model B

On Raspberry Pi, a program is needed to send a “GET” request to the base station, in order to get the status of each garbage bin. A program is designed in RPi and runned in Python IDE 3.4.2. A picture below shows the python program is running continuously sending the “GET” request to the base station, and the base station return the data containing the information about each bin, until the user choose to exit it.

Figure 5.46: Python program runs on Python IDE 3.4.2

As shown in figure above, one can observe that the data received is in format of array, ex:

[‘2’, ‘8’, ‘46.67’, ‘0’, ’44.00’]. The arrangement of the data in form of array is as shown below.

Figure 5.47: Array arrangement of data for RPi on server site

As UDP internet protocol is used to communicate between the base station and server side. Hence, socket library needs to be implemented. Socket library provides a method to

set the internet configuration for UDP, namely IP Address, Port Number. To setup the socket configuration on RPi, the following steps are required in the python program.

Table 5.7: Socket configurations for RPi in steps

Step Function to be executed Function definition

1 from socket import * Import Socket library for internet


2 address=( '', 8989) Specific the ip address and port number to request the data.

'': Base station’s ip address.

‘8989’: Base station’s port number

3 client_socket = socket(AF_INET,


The socket(domain, type, protocol) call creates a socket in the specified domain

5 client_socket.sendto( data.encode(), address) Encode the request data, and send to the base station’s ip address.

data.encode(): Encode the request data, ex: “status”.

address: The address that contains the base station’s ip address and port number.

6 rec_data, addr = client_socket.recvfrom(2048) Read the response data from remote site with maximum size of 2048 bytes and store it in rec_data.

7 dat = rec_data.decode('utf-8') Decode the rec_data to ‘utf-8’ which RPi can accept and store the data to dat for later user.

The following images show that the RPi python program has successfully received the data sent from the base station.

Figure 5.48: Data collect from base station

Figure 5.49: RPi successfully get the data from the base station

The two images above shows that two sites (base station and server site) are with the same data, this shows that the data transmission from base station to RPi is successful.

Besides of receiving data from the base station, RPi also need to categorize its received data and send to the Openhab2 (IoT framework) at the same time. To send the data to Openhab2, the transfer protocol MQTT was used. Hence, MQTT library needs to be installed on RPi and imported to the python program. The installation of MQTT can be performed by typing the command: $ sudo pip3 install paho-mqtt. Then the python program will be able to import the MQTT client library to perform the data sending procedure to Openhab2.

MQTT has 2 types of roles, MQTT Broker and MQTT Client. MQTT Client subscribes the data request to MQTT Broker, MQTT Broker provide the data that the MQTT Client requests. In short, MQTT Broker is a service provider and MQTT Client is the service subscriber. In this system, Openhab2 will be the MQTT Client that will request the data from MQTT Broker, which is the RPi (Python Program) itself. To send the data, the MQTT needs to have paths for it to post and get the data, and this path is called “topic”.

In this system, 3 topics have been created (for 3 garbage bins). The figure below demonstrate the concept of topics of MQTT.

Figure 5.50: MQTT Topics and relationship

Using the topics, the path to “POST” and “GET” can be defined. Hence, the data transmission between openhab2 and RPi (python program) can be done.

To implement the MQTT in the python program, the following steps are required.

Table 5.8: MQTT configurations and setup in python program

Step Function to be executed Function definition

1 import paho.mqtt.client as mqtt Import MQTT Client library

2 mqttc = mqtt.Client() Create MQTT Client object

3 mqttc.connect("", 1883) Connect to MQTT service with port number 1883 (MQTT) and ip address of

“” (RPi)

4 mqttc.publish("/siteA/bin/level", dat, qos=0, retain=False )

Publish the data to each respective topic.

"/siteA/bin/level": Topic path dat: Data to be sent

qos: Quality of service

retain: Retain the information sent

5 mqttc.disconnect() Disconnect the MQTT service

5.2.4 Openhab2

Openhab 2 is an IoT framework for the rapid development of IoT solutions. This system is designed partly with Openhab 2, to create an UI that allow the user to monitor the command prompt, type the command $ sudo /bin/systemctl start openhab2.service, then the service will be launched. After the service is launched, it is now ready to open up the main menu of the Openhab 2 type typing localhost:8080 on the browser for development purpose.

Some explanations for the terms being used:

Items - Items is a configuration file that specific which components to be included in the software. Each of these components can be binded to certain value or label for displaying purpose, namely binding with the MQTT to get the value display on these components.

Sitemaps - Sitemaps is a configuration file that is used to design the layout of the software, this allows the components of the software to be grouped together and and displayed to control the GUI layout of the software.

Persistence - Persistence file is used to keep the data updated and populated in the MySQL database. The data stored in the database can be used to display the historic data stored in form of graph for visualizing the data in a more convenient way (inspect the

HTML - HTML is a webpage’s layout design tool that is used to design the interface of the Google Map. The Google Map will be integrated into the software by implementing this HTML file together with the Javascript file. This HTML file will be used to display the UI of this system.

Javascript - This file is a Javascript file that is used to control the HTML page and allow the HTML to access the Openhab2 variables. This includes initializing the Google Map, etc.

There exist several UI framework options in Openhab 2, namely BasicUI, PaperUI and ClassicUI. For this project, BasicUI has been chosen due to its simplicity and clear interface.

MQTT is the communication protocol between local RPi’s python program and Openhab 2. Hence, the MQTT communication dependency need to be installed first before Openhab can requests the data from the python program. This can be done by inserting the command $ sudo apt-get install mosquitto mosquitto-clients. The image below shows that after the command, the dependency will be installed.

Figure 5.51: Installing MQTT - Clients dependency

After installed the above MQTT dependency, the Openhab will be able to request the data for each topic requested from the python program on RPi. The images below show that the broker (publisher) successfully sent the data “Successfully Get Data” to the client (subscriber).

Figure 5.52: Send data to MQTT subscriber

Figure 5.53: Successfully get data from the MQTT broker

Hence, the communication tunnel between python program and Openhab is enabled. In the following section, each component of the Openhab as mentioned previously will be further discussed. Items

Items need to be defined in order to display the desire data to the user. The main items to display are the filling level of the garbage bin, the status, and alert information. These items can be defined in a file called default.items. The structure of the default.items file is as shown below.

Figure 5.54: The structure of default.items file

The structure of the code as shown above is as [data type] [variable] [Message to display on software] <Icon> {MQTT request topic}. From the image above, one can see that there is a line “{mqtt="<[mosquitto:/siteA/bin1/status:state:default]"}”. This line of code tells the program that the variable “status1” should subscribe and get the data from topic

“/siteA/bin1/status”, where the python program will publish to. Sitemaps

Sitemaps is necessary to decide how the items are grouped together. It is basically a layout tool to arrange the position for the items defined. So that the items are grouped together according in the UI of the software. The structure of the sitemaps file is as shown below.

Figure 5.55: The structure of the default.sitemap file

After this configuration file is saved and compiled, the resulting layout of the software will be as shown below.

Figure 5.56: The layout of the software after the default.sitemap is configured

The software will be then provide the information for Bin’s filling level, its status (empty, partial, full or error), the fallen status, the connection status and the battery level of the bin. Persistence

Persistence configuration file is used to define and setup the database connection between Openhab 2 and MySQL database. In this file, the items to be stored and how often the database update these variables will be configured. The image below shows the configurations of the persistence file.

Figure 5.57: Configurations in default.persistence

The configuration shows that the persistence file will keep the variables “level1”,

“level2” and “level3” updated to MySQL database once any one of the variables

“changed” or “updated” and specific that these data need to be restored on system set up.

These data stored in database will then be used to show the data history for the user to inspect the peak period for the garbage pilling issue to occur. The user can then analyze the optimal time to collect the garbage and this will save their time and promote efficiency. The images below shows the data history graph for the garbage piling status.

Figure 5.58: The data history for garbage piling status using MySQL database(1)

The images above shows the data history for “Daily”, one can always change the data history graph for “Weekly” or “Monthly”. HTML

HTML is used to define to webpage that display the Google Map on top of the software.

In order for the Google Map to be able to work in this web page, the Google Map API and settings are configured in this file. The Google Map API can be generated by visiting official Google Map website. The snippets belows show that Google Map API is configured in the html file.

Figure 5.59: Google Map API configured on html file

Furthermore, to make the html to be able to get the data from Openhab, the html file needs to include the path to the Openhab’s items.default. This can be done by using the command GetOpenHABItem() to access the data from default.items.

Figure 5.60: Accessing to the default.items file

As shown in the figure above, variables “loc1”, “loc2”, “loc3”, etc were used to stored the data collected from default.items. These variables will then be used to set the status markers on the map later. Javascript

Javascript file acted as a middleman and is used to control the html file and as the communication tool between Openhab and html module. Javascript file provides functions to allow the html module to access the data from the Openhab (default.items).

The function that was implemented for the html module namely GetOpenHABItem() to allow the html module to be able to get the data from the default.items. Hence, the html module can get the latest status for each garbage bin and update its status accordingly.

The Google Map is shown on top of the software, after all the required configurations is done. The image below shows that the Google Map is being called and working as expected.

Figure 5.61: Google Map integrated into the software

In document DECLARATION OF ORIGINALITY (halaman 98-112)