In my last blog post, we have seen how arduino and raspberry pi functions. Here, in this blog I’ll be discussing about MQTT, necessary installations to implement MQTT on our wireless devices.
What is MQTT?
MQTT stands for Message Queuing telemetry transport. It is one of the most commonly used protocol in IoT project. Let me explain you the functioning of MQTT with the help of a picture.
- MQTT server is also known as the MQTT Broker which acts as a middleware from client to other client.
- When a device/client wants to send a message, it is called a publisher.
- When a device/client wants to receive data, it is called a subscriber.
- The same device can act as both publisher and subscriber at different times.
- The devices communicate with other devices through something called “Topic”
Steps to have Publisher-Subscriber model working on PC:
- Install nodejs from the official Node js website: https://nodejs.org/en/download/
- After that, install Node red: https://nodered.org/docs/getting-started/local#installing-with-npm
- Install mosquitto, a message broker which uses MQTT protocol. Link: https://mosquitto.org/download/
- After installing npm and node red, Open command prompt and type node-red.
- You should now be able to see node-red running just like in the pictures below.
- Now open a different command prompt, go to the folder where you’ve installed mosquitto.
- Type mosquito -v, you should now be able to see it running
- As listed on command prompt, open the node-red server on a website browser.
- Now let us create a work flow
- Select an inject component from the nodes given on the left side.
- Select an mqtt(publisher) from the list
- Select a debug from the output list
- Select another mqtt(subscriber), listed under the output
- Open inject component and mention details as shown in the picture below.
- Similarly, provide topic and mention the url for subscriber and publisher
- URL has to be the one mentioned on mosquito server
- Connect inject to publisher and debug block to subscriber.
- Deploy it.
- You should now be able to see the flow between publisher and subscriber.
Let us see the working by connecting it to a device(ESP 8266):
- Open tools> Select manage libraries> Search for PubSub> Select and install PubSubClient.
- Make sure you are connected to ESP8266 board.
- Open File> Go to examples> Select PubSub Client> Select mqtt_esp8266 example
- Provide the WIFI name as ssid, password is the wifi password and mqtt broker server is the ip address of your computer.
- Compile and flash the code on to ESP8266.
- At this stage, I got an error the following error
How to setup MQTT on Mac computer?
I purchased a new Mac book pro and here are the steps to setup MQTT on it:
- Install Node(npm): open terminal> type brew update > type brew install node > sit back and wait 🕒 > type node -v , which will show you the latest version of node downloaded on your computer
- Install Node-red: open terminal > type sudo npm install -g –unsafe-perm node-red > after downloading, type node-red. You should that node-red is up and running.
- Open a web browser and go to the local host server.
- Setting up mosquitto: I followed the commands mentioned in this link – https://simplifiedthinking.co.uk/2015/10/03/install-mqtt-server/ and was successfully able to set up mqtt.