adds code

Setting up MQTT server on Raspberry pi Open the terminal and type the following commands.  sudo apt-get update  sudo apt-get upgrade Now we need to install mosquitto MQTT broker and mosquitto MQTT clients  sudo apt-get install mosquitto  sudo apt-get install mosquitto-clients Now we need to change the configuration  sudo nano /etc/mosquitto/mosquitto.conf Delete the last line i.e. “Include_dir /etc/mosquitto/conf.d” Now type at the end of file: allow_anonymous false password_file /etc/mosquitto/pwfile listener port 1883 After that, press Ctrl+X, then Y, then Enter to save the file and exit the editor. Now we need to create a username to access the mosquitto.  sudo mosquitto_passwd -c /etc/mosquitto/pwfile USERNAME Here you can type your own username in the place of USERNAME. I am using username – USERNAME And password – PASSWORD Now when you press enter then you will be asked for password, just type any password and press enter, now you need to re-type your password. *Note – you will not be able to see anything when you are typing your password so make sure that you will type it correctly.  sudo reboot - sudo mosquitto -d You have successfully install mosquitto MQTT broker and client . Now it is time to test your MQTT broker and client. Open two terminal windows in raspberry pi. Say terminal 1 and terminal 2 In terminal 1 type: mosquitto_sub –d –u USERNAME –P PASSWORD –t dev/test And press enter In terminal 2 mosquitto_pub –d –u USERNAME –P PASSWORD –t dev/test –m”hello” And press enter and you will see hello in terminal 1. Here I have used USERNAME and PASSWORD as my credentials but you have to use the same username and password that you have created in the previous step. Now we need to start the MQTT server as soon as out Raspberry pi boots up so that we don’t have to connect to the raspberry pi and start the service manually. To do this just open terminal and type:  sudo systemctl enable mosquitto And press enter. Now we are done with the Mosquitto MQTT Broker and client. This service will now run automatically as soon as our raspberry pi boots up. We need to install MQTT in our raspberry pi to use its publish and subscribe features and to operate our appliances without using any third party application. Now we have our own server and we will use Node-red dashboard to control the appliances in our Home. Link to the MQTT Video - If you need any help in your Iot project please feel free to contact me through email and you can also connect with me on LinkedIn Email- Linkedin - Instagram -
Previous Post Next Post