Message Queuing Telemetry Transport (MQTT) in MPLAB® X IDE

Last modified by Microchip on 2023/12/15 16:08

Objective

This tutorial uses the AVR® Internet of Things (IoT) WG development board to set up the environment for Message Queuing Telemetry Transport (MQTT) in MPLAB® X IDE.  The AVR IoT WG will be set up as the client and Eclipse Mosquitto™ MQTT as the broker, although any other broker may be used. 

Once you have set up an AVR IoT WG as an MQTT client, you can use it for a variety of IoT applications that involve sending and receiving data over the internet.

Here are some example applications:

  • Monitor and Control IoT Devices: You can use the AVR IoT WG to subscribe to MQTT topics that represent sensor data or control commands from other IoT devices. For example, you can subscribe to a temperature sensor topic and turn on a fan when the temperature exceeds a certain threshold.
  • Send Data to Cloud Services: You can use the AVR IoT WG to publish data to cloud services such as AWS IoT, Google Cloud IoT, or Microsoft® Azure® IoT. This allows you to store and analyze the data in the cloud and trigger actions based on the data.
AVR Iot WB Development Board

AVR Iot WG Development Board

  • Implement home automation: You can use the AVR IoT WG to control smart home devices such as lights, thermostats, and locks. For example, you can publish a message to turn on the lights when you enter a room.
  • Build Industrial Automation Systems: You can use the AVR IoT WG to monitor and control industrial equipment such as motors, pumps, and valves. For example, you can subscribe to a topic that represents the status of a motor and turn it off if it overheats.

Overall, the AVR IoT WG as an MQTT client provides a flexible and scalable platform for building IoT applications that require real-time communication and data exchange.

The AVR IoT WG board has a code example that can be generated with the MCC library and uses the WINC1510 to establish an MQTT connection to any known broker, subscribe to topics, and publish.

This example will use mqttLibrary-2.0.1.mc3lib library, the latest version in June 2023, in MCC 5.4.11 in MPLAB X IDE 6.00. This information is only reliable for these versions.

If Microchip releases another version of the library, it may stop working, or this version may not work with the next MPLAB or MCC versions.

Materials Required

Hardware

AVR IoT WG development board

Reference

AVR IoT Development Board Introduction Video
MQTT MCC Library Release Notes v 2.0.1

Software

MPLAB® X Integrated Development Environment (IDE)
Eclipse Mosquitto™ MQQT broker
MyMQTT Play Store App
Wireshark
MQTT Explorer
Training Project Files

Back to Top

Training

Section 1: MQTT Environment

  • Installation
  • Mosquitto.config
  • pwfile.example
  • Execute Mosquitto as broker
  • Testing Mosquitto

Section 2: My MQTT

  • Know your host IP
  • Configuring MyMQTT
  • Subscribing

Section 3: Other MQTT Tools

  • MQTT Explorer
  • Wireshark

Section 4: First Steps in MPLAB X IDE

  • Add MQTT Library in MCC
  • MQTT Window
  • WINC1510 Window
  • RTC Window and SPI0
  • Pin Module (D0, D1, D2 and D3 Pins)
  • Generate Code
  • Write Code in main.c
  • Run Basic Program

Section 5: MCC Generated Example Background

  • app_mqttExampleInit()
  • app_mqttSchedulre()

Section 6: Publish

  • Publish Always Same Message
  • Publish Different Messages
  • Publish to Different Topics
  • Publishing Speed

Section 7: Add New States

  • Declare New States
  • Socket Handler
  • Write New State

Section 8: Add Subscribe Function

  • Create Subscribe Packet
  • Send Subscribe Packet
  • Subscribe Just Once

Section 9: Receiving, Processing, and Reading Packets

  • Add line in socketHandler()
  • Add line in MQTT_ReceptionHandler()
  • Write code for processing received packets
  • Read Received Message
    • Declare Variables
    • Declare Callback
    • Define Callback
    • Testing Result
    • Nothing to Process

Section 10: Add Pinging Feature

Back to Top