Nowadays, most of the hotels and restaurants take online orders of food. Many hotels and restaurants either facilitate pre-ordering or even render delivery services in the local areas. In this project, an Hotel Order Management System is designed where a customer can pre-order food items using a mobile app and a Raspberry Pi based Server manages to cater menu items and book orders. On every booking, the customer is acknowledged of the time their order will be ready, so they can arrive at the specified time and pick up their order.
This Hotel Order Management System is based on Internet of Things and it uses MQTT protocol for communication between the mobile app and the Raspberry Pi server. The Raspberry Pi as a server is installed at the Hotel and remains connected to the internet by a Wi-Fi access point. For this purpose, Raspberry Pi 3 is used which comes equipped with on-board Wi-Fi modem. The Raspberry Pi is connected to a display monitor where the hotel staff can view the received orders.
The Raspberry Pi and the mobile app communicate via an MQTT broker. The MQTT broker used in this project is HiveMQ. The mobile app subscribe to a topic ‘FromServer’ while publishes to a topic ‘ToServer’. The Raspberry Pi server as another MQTT client subscribe to topic ‘ToServer’ and publishes to the topic ‘FromServer’. The payload on the topic ‘ToServer’ is the menu item selected by the user from the mobile app. When the Raspberry Pi server receives an order of a menu item from the mobile app on topic – ‘ToServer’, it sends order number and time for pickup as payload on the topic – ‘FromServer’.
On the Raspberry Pi, the IoT communication with the HiveMQ broker is managed by a python script while the mobile app uses paho-MQTT client library to manage IoT communication with the HiveMQ broker. The mobile app is developed on Android Studio. The app has a single activity where the customer can select a menu item for ordering.
Components Required –
1) Raspberry Pi 3
2) Any Smart Phone
Software Required –
1) Raspbian OS
2) Leafpad/GNU Nano/Python 3 IDLE (for writing python script)
3) Android Studio (For developing Android App)
4) Paho MQTT Client Library – Android Service (For importing in Android App)
Block Diagram –
Fig. 1: Overview of Raspberry Pi Server and MQTT based IoT Hotel Order Management System
Circuit Connections –
In this project, a Raspberry Pi and a Smart Phone are used as IoT devices. Both act as MQTT clients on the IoT network while HiveMQ serve as the MQTT broker. The Raspberry Pi is connected to a display monitor where it shows the orders received through the mobile app. The Raspberry Pi is configured to run a python script at startup. This python script manages to receive orders as an MQTT subscriber, display orders received along with customer number, time to pickup order and order id on the display monitor and send back order number and time to ready order as MQTT publisher to the mobile app. The python script has been developed on Python 2.7.
Fig. 2: Logo and Image of Raspberry Pi and MQTT
The smart phone is installed with an app from which an order can be placed. The app is developed using android studio. The mobile app is using paho-MQTT client library for IoT communication like the python on the management side.
How the circuit works –
The Raspberry pi server is connected with the MQTT broker through hotel wireless router. The App is then connected to the same broker through internet connection of mobile.
The Raspberry pi server is running a python script which connects to the MQTT broker using paho-MQTT library. The script contains the menu item in it. Now when the mobile App is connected to the broker it sends a signal string on a topic “ToServer”, the Raspberry pi server is subscribed on the same topic and receives that signal. Now the server replies with the menu items on the topic “FromServer” which is subscribed by the App. From those menu items when a client orders something then it goes to the Server and if the data is valid then it sends the user timing in which their order will be ready.
Programming Guide –
In this project, a mobile app for the smart phone and a python script to run on the Raspberry Pi are developed. The mobile app developed here is an android app. For developing mobile app, first of all, download and install Android Studio. Open Android Studio and create a new project. Name the project – Hotel Order System and provide any company name. The company name can also be developer’s name. Click next. In the Target Android Devices screen, keep the default values and click Next. Select the minimum SDK supported by the smart phone in case. In the Add an Activity to Mobile screen, select Empty Activity and click Next. In the Configure Activity screen, keep the default values and click Finish. Open the project window by navigating to View > Tool Windows > Project if it is not open select Android View. Here it can be seen that the project contains the following main files –
app > java > com.example.themenu > MainActivity : This is the main activity (the entry point for the app). When the app is build and run, the system launches an instance of this Activity and loads its layout.
app > res > layout > activity_main.xml: This XML file defines the layout for the activity’s UI.
app > manifests > AndroidManifest.xml: The manifest file describes the fundamental characteristics of the app and defines each of its components.
Gradle Scripts > build.gradle: There will be two files with this name: one for the project and one for the “app” module. Each module has its own build.gradle file, but this project currently has just one module.
In the Navigate to the activity_main.xml tab if it is not already open. Make sure that the Design tab is open on the activity_main.xml display. In res -> layout -> activity_main.xml, add the layout elements which include two textview widgets, a scrollview, a listview, two edittext and a button. There is a single activity in this app.
Download the Paho-MQTT Client library – Android Services. Next open app > manifests > AndroidManifest.xml and add permissions and Paho-MQTT Client as an Android Service.
Next open MainActivity.java and write down the java code for the app. The java code has the following main highlights –
1. The library and its functions are used to subscribe on topic “FromServer” and it publishes on the topic “ToServer”.
static String MQTTHOST = “tcp://broker.hivemq.com:1883”;
String topicStr = “ToServer”;
String topicSub = “FromServer”;
2. App receives the menu items from the server
3. Then those menu items are displayed on the app screen
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, menuArray);
4. Now in the app there are two fields which receives text input those inputs are being sent to server.
msgToserver = (EditText)findViewById(R.id.orderno);
message = “ooo”+ msgToserver.getText().toString();
client.publish(topic, message.getBytes(), 0, false);
5. The server gives response according to the query
For More details on using MQTT Client Library – Paho Android Service for communicating with HiveMQ broker, check out the following tutorial –
The Raspberry Pi work as another MQTT Client. It runs a python script that works as hotel order management utility. First of all, the Raspberry Pi needs to loaded with an operating system. Here the official operating system for the Raspberry Pi – Linux Raspbian is installed on it. While installing the operating system, the Raspberry Pi should be connected to a display monitor using HDMI cable and a keyboard and mouse through USB ports.
For installing the Raspbian Operating System on MicroSD card, first download the latest image of Raspbian OS from Raspberry Pi website from the following link –
Copy the image of the latest Raspbian OS in the MicroSD card. If the MicroSD card used is 32 GB or below, it must be formatted to FAT32 (file system) before copying the image or if the MicroSD card is more than 32 GB, it should be formatted to exFAT before copying the image. Extract the OS Zip and copy it to the MicroSD card. The image can be written to the card by connecting the card to a laptop or PC using a MicroSD card reader. After copying the extracted image, insert the card in the MicroSD slot as shown below –
Fig. 3: Typical Image of Raspberry Pi 3 MicroSD Card Slot
Connect the Raspberry Pi with a display monitor using HDMI Cable, a keyboard and a mouse. Power on the board by connecting to a power adaptor. The red LED on the board will start blinking and the OS will start booting from the MicroSD card. The boot process will display on the monitor and once the boot is complete, green LED will light up on the Raspberry Pi. After successfully installing Raspbian OS on Raspberry Pi, it is recommended to perform software update. It can be done by running the following Linux commands in the Linux Terminal –
$ sudo apt-get update
$ sudo apt-get upgrade
After installing the Raspbian, it’s time to write and run the python scripts on Raspbian. A python script can be written on Raspbian using a text editor like Leafpad or GNU Nano. The python script can also be written using the default python IDE like Python 2 IDLE or Python 3 IDLE. Open the Python 3 IDLE by navigating through Menu -> Programming -> Python 3 IDLE. A window called Python 3.4.2 Shell will open up. Write the python scripts and save them to a directory.
The python script written for this project should run at the startup as the Pi 3 is powered on. The script runs an infinite loop so it never ends. There are some methods by which the Raspberry Pi can be configured to run a python script on start up. Any of the following methods can be used –
1) Editing rc.local –
The commands can be added to the file /etc/rc.local to run a program or command when the raspberry Pi boots up. This is especially useful if the Pi has to plug in to power headless, and have it run a program without configuration or a manual start. The file should be edited with root by running the following commands in the Linux Terminal –
sudo nano /etc/rc.local
Now add commands to execute the python script using complete file path and add an ampersand at the end of the command so that the script runs in a separate process and booting could continue. The following command should be added where the python script is saved as menu.py –
sudo python /home/pi/menu.py &
The command should be added just before the line exit 0 in the rc.local file.
2) Editing .bashrc –
The .bashrc is a hidden file in the home folder that contains user configuration options. Open the .bshrc file by running the following commands in the Linux terminal –
sudo nano /home/pi/.bashrc
Add the following lines after the last line in the file –
echo Running at boot
sudo python /home/pi/menu.py
3) Adding script to init.d directory –
The init.d directory contains the scripts which are started during the boot process (in addition, all programs here are executed when Pi is shutdown or rebooted). Add the script to be run at startup to the init.d directory using the following commands –
sudo cp /home/pi/securitysystem.py /etc/init.d/
Move to the init directory and open the python script by running the following commands –
sudo nano menu.py
Add the following lines to the python script to make it a Linux Standard Base (LSB) –
### BEGIN INIT INFO
# Provides: sample.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
Make the python script in the init directory executable by changing its permission by running the following command –
sudo chmod +x menu.py
Then run the following command –
sudo update-rc.d menu.py defaults
Next reboot the Pi by running the following command –
Any of the above methods can be used to make the python script run on startup. Now the Pi 3 can be connected to the display monitor where the customer orders should display. The python script has the following main highlights –
1. Import libraries for MQTT and GPIO
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import RPi.GPIO as GPIO
2. Create an array called menu and add menu items in it and send it
“Grilled Veggie Wich”,
“Aloo Tikki Burger”,
Sending the array
for i in range(0,len(menu_items)):
#print str(i+1) +”:”+ menu_items[i]+ “:” +str(item_price[i])
menu_send = (str(i+1) +”.”+ menu_items[i]+ “:” +str(item_price[i]) + “$”)
publish.single(“FromServer”, menu_send, hostname=broker_ip)
3. Send response to the server according to received data
elif (‘m’ in message):
count = (count + 1)
oldstr = message
tim1 = oldstr.replace(“m”, “0”)
tim1 = int(tim1)
tim1 = tim1 +30
m = ” minutes”
tim1 = (tim1/60)
m = ” hour”
abc = str(tim1)
order = (“Order will be ready in:”+abc+m)
publish.single(“FromServer”, “Wrong Order! Please Select a valid menu Item”, hostname=broker_ip)
4. In the end of the script these lines take care of the connections to broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_ip, broker_port, 60)
Download and Check out the app code in Android studio. Then try out the python code on Raspberry Pi. This is a simple hotel order management system which utilizes MQTT protocol for taking and managing food orders.
In the next tutorial, learn about IMAP protocol.
Project Source Code
### //Program to import paho.mqtt.client as mqtt import paho.mqtt.publish as publish import random import thread import time import time import RPi.GPIO as GPIO ## Import GPIO library import os os.system('clear') ############################# Raspberry Pi GPIO ############################ GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) ## Use board pin numbering GPIO.setup(8, GPIO.OUT) ## Setup GPIO Pin 8 to OUT GPIO.setup(10, GPIO.OUT) ## Setup GPIO Pin 10 to OUT GPIO.setup(12, GPIO.OUT) ## Setup GPIO Pin 12 to OUT GPIO.setup(16, GPIO.OUT) ## Setup GPIO Pin 16 to OUT #declarations broker_ip = "broker.hivemq.com" broker_port = 1883 oldstr = '0' tim1 = '0' abc = '0' cba = '0' order ='0' m = '0' a = "ad" b = "cb" #Main Satrt menu_items=["Coffee", "Tea", "Maxixan Rice", "Grilled Veggie Wich", "Cheese Pizza", "Aloo Tikki Burger", "French Fries", ] item_price=[200,130,100,400,200,55,90] count = 0 count2 = 0 count3 = 0 # The callback for when the client receives a CONNACK response from the server. print (""" __ __ _______ _______ _______ ___ ___ _______ _______ | | | || || || || | | | | || | | |_| || _ ||_ _|| ___|| | | | | _ ||_ _| | || | | | | | | |___ | | | | | | | | | | | || |_| | | | | ___|| |___ | | | |_| | | | | _ || | | | | |___ | | | | | | | | |__| |__||_______| |___| |_______||_______| |___| |_______| |___| """) def on_connect(client, userdata, flags, rc): #print("Connected with result code "+str(rc)) print("Connected!") # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("ToServer") GPIO.output(16,True) #GPIO pin will be on on the connection GPIO.output(8,False) GPIO.output(10,False) GPIO.output(12,False) # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global count global count2 global count3 global order global abc global m message = str(msg.payload) #print(msg.topic+" "+str(msg.payload)) if ('19932' in message.lower()): #rand1 = random.randint(0, 4) #publish.single("FromServer", reasponse_on_name[rand1], hostname="192.168.1.106") menu() elif ('m' in message): count = (count + 1) oldstr = message #print oldstr tim1 = oldstr.replace("m", "0") #print tim1 tim1 = int(tim1) tim1 = tim1 +30 m = " minutes" if(tim1>60): tim1 = (tim1/60) m = " hour" abc = str(tim1) #print abc order = ("Order will be ready in:"+abc+m) elif ('ooo' in message): count2 = count2 + 1 count3 = count3 + 1 oldstr1 = message #print message tim2 = oldstr1.replace("ooo", "0") #print tim2 tim2 = int(tim2) #print tim2 if(tim2>len(menu_items)): publish.single("FromServer", "Wrong Order! Please Select a valid menu Item", hostname=broker_ip) #print (" Invalid order:"+" with id:"+str(count2)) else: publish.single("FromServer", order, hostname=broker_ip) print ("Custmor No."+ str(count) + " wil be here in "+ abc +" "+m), print (" With the order of:"+menu_items[(tim2-1)]+" with id:"+str(count2)) print " " if(count3==1): GPIO.output(8,True) GPIO.output(12,False) if(count3==2): GPIO.output(10,True) if(count3==3): GPIO.output(12,True) GPIO.output(8,False) GPIO.output(10,False) count3 = 0 def menu(): for i in range(0,len(menu_items)): #print str(i+1) +":"+ menu_items[i]+ ":" +str(item_price[i]) menu_send = (str(i+1) +"."+ menu_items[i]+ ":" +str(item_price[i]) + "$") publish.single("FromServer", menu_send, hostname=broker_ip) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(broker_ip, broker_port, 60) client.loop_forever() ###