First Steps in MPLAB® X IDE

Last modified by Microchip on 2023/11/10 11:11

MQTT Library for MCC

The first task in this section is to add the MQTT library into MPLAB® Code Configurator (MCC)

Download the pdf MQTT MCC Library Release Notes v 2.0.1

This document contains the basic information for the MQTT Library for MCC. The information shown here is based on the release notes and the example below is from  Section 5: Running the Example.

Download the Library

Import it into MPLAB X IDE

If you are unfamiliar with the process, read the article How to Add a Library in MCC.

Create a new project for AVR-IoT WG and open MCC

In Device Resources, click the Green Plus Icon next to MQTT

Green Plus next to MQTT in MPLAB

The MQTT Foundation service should be added. When you change the Transport Service from Custom Service to Wireless, the WINC15XX and SPI0 peripheral libraries should appear. WINC1510 is connected to the microcontroller ATmega4808 in AVR-IoT WG. 

In the AVR-IoT WG, the WINC1510 device and the ATmega4808 microcontroller are contained.  ATmega4808 microcontroller is programmed, but it is not capable of connecting to Wi-Fi® by itself. It connects to the WINC1510 using SPI, and WINC1510 connects to the Wi-Fi.

Transport Service as Custom Service in MPLAB

Back to Top

MQTT Window

Settings in the MQTT window in MCC:

Transport ServiceSelects how MQTT is going to be used (Wired or Wireless)
MQTT Broker AddressThis is the IP, “the name” of the broker device that we are going to connect to
Port TypeThis defines if TLS protocol is used. Useful for security
Port numberA broker and a client can communicate through different ports, so one client can publish and subscribe to different topics and for different topics, and one broker can do the same with different clients.
UsernameIf identification is required for the broker, as shown in pwfile.example, that field will be the used username
PasswordIf identification is required for the broker, as shown in pwfile.example, that field will be the used password
MQTT Transmit BufferThis is the maximum size of the message that will be published. If the message is bigger than that, MQTT will collapse. In our example, the message will be an array of strings. Each string is a byte, so the number in the MQTT Transmit Buffer is the maximum number of characters that the published message can have.
MQTT Receive BufferThis is the maximum size of the message that can be received. If the message is bigger than that, MQTT will collapse. If the message is an array of strings, each string is a byte, so the number in the MQTT Transmit Buffer is the maximum number of characters that the received message can have.
Scheduler ServiceThis setting selects what tool will be used to measure time to make periodic publishing.

As shown in the accompanying image, you just change Transport Service, MQTT Broker Address, Scheduler Service, and tick the Generate Example box. Set these settings as in the image below except for the MQTT Broker Address, which is explained after the image. Changing the Scheduler Service to Foundation Services Timeout for the first time should make the RTC peripheral appear. The rest of the settings do not need to be changed, even if you can get a more personalized program.

MQTT Broker Address in MPLAB

*MQTT broker address. If using Eclipse Mosquitto™ MQTT broker, your computer is your broker. To know its address, do as explained in Know your host IP. Open the command prompt, and type ipconfig. The necessary address is contained in the IPv4 address's digits.

Be cautious because the broker address could vary over time, especially if the IP address is dynamic or the Wi-Fi changes.

IPv4 Address in Command Line

Back to Top

WINC1510 Window

Settings in the MQTT window in MCC:

SSIDName of the Wi-Fi you are connecting to
AuthenticationSelect if the Wi-Fi uses a password, WPA of WEP. Usually, your device will use WPA2. If so, select WPA_PSK
PasswordPassword of the Wi-Fi you are connecting to

Change these three settings in the WIN15XX Software Settings window.

SSID, Authentication, and Password shown in MPLAB

Back to Top

RTC Window and SPI0

While you were configuring the different options, the RTC and SPI0 peripherals should have appeared. RTC is a timer used to know when to send data through MQTT, and SPI0 is the peripheral used to communicate with the WINC1510 device. If you don’t see RTC and SPI0 peripherals, you may need to click on the small arrow at the left side of “Peripherals”.

In this case, you won’t need to change SPI0, only RTC. Select 1 kHz rate for source rate because that’s how the MQTT example that you are going to generate was designed and select times for “Compare” and “Period” that are inside of the new range, between 2 ms and 65 s.

RTC Clock, Compare, and Period shown in MPLAB

Back to Top

Pin Module

As previously mentioned, the microcontroller communicates with the ATWINC1510 W-Fi module through the Serial Peripheral Interface (SPI) protocol, using the SPI0 peripheral. SPI protocol uses 4 wires; nCS, MOSI, MISO, and SCK.  All these wires are properly configured except nCS, which you need to add. nCS is connected to the PA7 pin.  Although it is not needed in this exercise, more information about the SPI communication protocol can be found on the Microchip SPI Peripheral page.

To communicate with the MCU, the WINC1510 device also has the RST pin for resetting, INT pin for interrupts, EN for enabling, and WAKE pin for waking up. These pins are used for the MCU and are connected to pin PA1, pin PF2, pin PF3, and pin PF4, respectively.

Apart from nCS, RST, INT, EN, and WAKE pins, we will also use LEDs. LEDs are connected to PD0, PD1, PD2 and PD3.

The schematic is not necessary for this example but for a better understanding of the connections, you can review it below or download the AVR® IoT WG schematic from the Microchip website.

AVR IOT WG Schematic

We need to tell the MCU that we are going to use these eight pins: RST, INT, nCS and WAKE and D0, D1, D2 and D3 as GPIO output. Click in Pin Manager: Grid View and add the nine pins that we need. While you are selecting, you should see how pins are being added if you click Pin Module.

It is recommended to make D0, D1, D2, and D3 start high, by ticking the respective box.

Your final pin setup should look like the accompanying image:

Pin Module

Back to Top

D0, D1, D2, and D3 pins

D0, D1, D2, and D3 are the pins for the LEDs. They light up whenever they are LOW  (when the value of their pin is 0). You can check the Start HIGH box, so the LEDs start OFF. Otherwise, they will be LOW (turned on) when you execute the program. These pins won’t be lit up in any section of this document, but you can write these next lines in any part of your code to light them up. This should help debugging your program if needed.

PORTD.OUT=PORTD.OUT & 0xF7;     //turn first LED on
PORTD.OUT=PORTD.OUT & 0xFB;     //turn second LED on
PORTD.OUT=PORTD.OUT & 0xFD;     //turn third LED on
PORTD.OUT=PORTD.OUT & 0xFE;     //turn fourth LED on

PORTD.OUT=PORTD.OUT | 0xF8;     //turn first LED off
PORTD.OUT=PORTD.OUT | 0xF4;     //turn second LED off
PORTD.OUT=PORTD.OUT | 0xF2;     //turn third LED off
PORTD.OUT=PORTD.OUT | 0xF1;     //turn fourth LED off

Back to Top

Generate Code

You should not see this window:
Configuration Dialog Box

If you see it, it’s likely because a step was missed. To see what step may have been missed, you can click Notifications [MCC]. The notifications that you have that are not in the accompanying image, could be the steps that you are missing.

Notifications Window

If you have the above suggestions in the Notifications window, you are fine, and the Confirmation window should not show.

Back to Top

Write Code in main.c

Modify our main.c to execute app_mqttExampleInit(); once, and app_mqttScheduler(); several times.

#include "mcc_generated_files/mcc.h"
int main(void)
   while (1){

Back to Top

Run the Basic Program

Run the program. It should send a publishing every 8-10 seconds. If you want to make it do it more frequently, change the Keep Alive (s) parameter in the MQTT window in MCC, but no less than 2. Publishing speed depending on keepalive is not usual in MQTT. This is set and explained in the Publishing Speed section.

If you are using Mosquitto you should see something like this:

Send/Receive messages from Mosquitto in Command Prompt

Back to Top