In this topic let's see what we have to do to set up this server. Open up your command line and type in these following lines:
sudo apt-get update
sudo apt-get upgrade
Once the update and upgrade processes are complete, go ahead and install the following packages:
sudo apt-get install mosquitto -y
This will install the Mosquitto broker onto your Raspberry Pi. This broker will take care of all the data transfer:
sudo apt-get install mosquitto-clients -y
Now, this line will install the client packages. As you can imagine, Raspberry Pi in itself will be a client to the broker. Hence, it will take care of the needful.
We have now installed the packages; yes exactly, it was that small. Now, all we need to do is configure the Mosquitto broker. To do this, you need to type in the following command:
sudo nano etc/mosquitto/mosquitto.conf
Now, this command will open the file where the Mosquitto file configuration is saved. To configure it, you need to get to the end of this file, where you will see the following:
include_dir/etc/mosquitto/conf.d
Now, you can comment out the the preceding line of code by simply adding # before the lines. Once done then go ahead and add the following lines:
allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883
Let's see what we have done here. The allow_anonymous false line tells the broker that not everyone can access the data. The next line, password_file /etc/mosquitto/pwfile is telling the broker the location of password file, which is located at /etc/mosquitto/pwfile. Finally, we will define the port of this broker, which is 1883, using the listener 1883 command.
So finally, we have completed setting up the MQTT client in our Raspberry Pi. Now we are ready to go ahead and use it for the IoT-enabled home.