Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Client-Server Communication over MQTT-SN Protocol : IOT Part 41

By Priya

In the previous tutorial, installation and configuration of RSMB Broker for MQTT-SN protocol implementation was discussed. In this tutorial, two devices will be setup to communicate over MQTT-SN protocol using RSMB Broker. One of these devices will be configured as MQTT-SN Client and other as the MQTT-SN Server. 
The MQTT-SN needs a gateway when WSN (Wireless Sensor Network) devices want to send data to MQTT server which run on TCP/IP protocol or want to communicate with MQTT clients. The WSN devices do not have TCP/IP stack with them, they simply run over Zigbee, Z-wave, Bluetooth Low Energy or UDP network. MQTT-SN goes over these protocols and the Gateway create a bridge between MQTT-SN protocol and MQTT protocol. The WSN devices send the message to the gateway and the gateway forwards the message to the listening brokers.
Image showing Client-Server Communication over MQTT-SN using Raspberry Pi as MQTT-SN Gateway
Fig. 1: Image showing Client-Server Communication over MQTT-SN using Raspberry Pi as MQTT-SN Gateway
Components Required – 
1) RSMB broker- MQTT-SN along with MQTT broker
2) Mosquitto broker- MQTT broker
3) Raspberry pi 3 as a MQTT-SN gateway
4) One PC (running on Linux) as a MQTT broker
5) Mobile as a MQTT client
6) MQTT-SN tools– MQTT-SN clients.
Client-Server Setup
The following steps will be performed to setup Client-Server system –
1) Configure the Raspberry pi to listen for packets on MQTT-SN port as well as MQTT port.
2) Configure the Mosquitto broker to act as MQTT broker to listen for packets on particular port.
3) Create a bridge between MQTT-SN and MQTT so that any message received on MQTT-SN port can be forwarded to MQTT ports.
4) Set up the MQTT-SN publish client to send packet on MQTT-SN port.
5) Set up the MQTT-SN subscription client to receive the packet on MQTT-SN port.
6) Configure the MQTT protocol in mobile client. We are using MQTT dashboard app to configure mobile as MQTT client.
7) Write down the MQTT broker address on the mobile client and the port on which the broker is listening.
8) Subscribe to the topic which MQTT-SN client has registered on the RSMB broker.
9) Publish the data to the RSMB broker and see if the data is received on MQTT-SN subscription side or not . Then, see if the data is received on mobile MQTT client or not.
10) The subscription client receives the packet from Gateway.
For configuring the Raspberry Pi to act as Gateway, follow the below steps –
1) Download the RSMB broker in home directory or any preferable directory.
2) From terminal, move into src folder /home/user/mosquitto.rsmb/rsmb/src
3) Run make command to make the file executable. The broker_mqtts executable file can be seen there. It will output as follow –
Screenshot of Command to Make the File Executable
Fig. 2: Screenshot of Command to make the File Executable
4) When the broker_mqtts is run by the command – sudo./broker_mqtts, broker by default listen on 1883 port. 
Now, the default configuration need to be changed so that broker can listen for MQTT-SN on user defined port and can create a bridge for MQTT broker. This broker will start listening on port 1887 for MQTT, 1886 for MQTT-SN, 1888 for local bridge of MQTT-SN to MQTT and 1889 for remote bridge of MQTT-SN to MQTT. The configuration file might look like as follow –
Screenshot of MQTT-SN Configuration File
Fig. 3: Screenshot of MQTT-SN Configuration File
Now, the broker is ready with the configuration file. Next, the Mosquitto broker need to be configured on the PC. The Mosquitto file need to be configured to listen on port 1889. Edit the Mosquitto configuration file by commenting out everything and write the following line – 
Screenshot of Line that has to be Commented in MQTT-SN Configuration File
Fig. 4: Screenshot of Line that has to be Commented in MQTT-SN Configuration File
Start the Mosquitto broker with configuration file loaded – 
Screenshot of Command to Start the Mosquitto Broker with Loaded Configuration File
Fig. 5: Screenshot of Command to Start the Mosquitto Broker with Loaded Configuration File
The output will look like the following – 
Screenshot of Mosquitto Broker
Fig. 6: Screenshot of Mosquito Broker
Now, run the broker with loaded configuration file using the following command –
Screenshot of Command to Run the Broker with Loaded Configuration File
The output on the terminal will look like the following – 
Screenshot of Loading Configuration on Mosquitto Broker
Fig. 7: Screenshot of Command to Run the Broker with Loaded Configuration File

Now, it can be seen that RSMB broker has established bridge connection to Mosquitto as well as local Mosquitto running broker. Now, Gateway is active and Mosquitto broker is listening. It’s time to test whether gateway is running properly or not. The MQTT-SN client tool need to be used on the raspberry pi to test the local gateway. To publish the data on local gateway, run the following command on the terminal –

Screenshot of Command to Publish the Data on Local Gateway
Fig. 8: Screenshot of Loading Configuration on Mosquitto Broker
In the above command, 
-h – is for gateway IP address
-p – is port, on which MQTT-SN broker is listening
-t – is topic
-m – is message
-d – is debugging every level
When the command is run, the following output is obtained –
Screenshot of Publishing Messages on RSMB Broker
Fig. 9: Screenshot of Command to Publish the Data on Local Gateway
The whole process runs in the background when client connects to broker and register the packet to the server. Now the same topic can be subscribed by running the following command –
Screenshot of Command to Subscribe the Topic
Fig. 10: Screenshot of Command to Subscribe the Topic 
The output on the terminal will look like the following – 
Screenshot of Subscribing Topics on RSMB Broker
Fig. 11: Screenshot of Subscribing Topics on RSMB Broker
Now, subscribe the same topic “hello” in the mobile client as well which is running on MQTT protocol. Connect mobile client with any MQTT server like Mosquitto broker of PC or Mosquitto broker of Raspberry Pi. Then, the same message will be received on the mobile client side as well, when the message on the mqtt-sn client is published.
In this way, two devices can communicate with each other over MQTT-SN protocol and forward the data to MQTT server as well. Here, there are multiple MQTT clients which subscribe to the same topic. They all will receive the same published message on different port. The RSMB broker transmits the UDP packet to TCP or MQTT. But if there is some other protocol like Zigbee or BLE then some kind of MQTT-SN forwarder will be needed in between gateway and WSN device so that forwarder can convert the different network into UDP network so that further communication can be processed in the same way.
In the next tutorial, learn to setup Secure Client-Server Communication over TLS Security Protocol.

Filed Under: Tutorials

 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers
  • First single-core MPU with MIPI CSI-2 camera interface and audio

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • A circuit that can adjust a resistance and probing a voltage node
  • DC to DC buck converter
  • A circuit that can probe 2 currents and adjust the resistor
  • Microsoft Project 2019 dependencies
  • MOSFET ORing circuit simulation (LTspice)

RSS Electro-Tech-Online.com Discussions

  • Enclosure sought
  • Need help using a common power supply for two devices
  • Fletcher's Law
  • Setting the 18F24K20 to digital.
  • Multistage BJT amplifier
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering