WiFi PRO V3 23: MQTT plublish topic using TLS

This example shows how to subscribe to an MQTT topic.

Required materials

1 x Waspmote 1 x Battery 1 x WiFi PRO V3 module

Notes

- The battery has to be connected - This example can be executed in Waspmote v15

Code

/*
    ------ WIFI Example --------

    Explanation: This example shows how to subscribe to an MQTT topic

    Copyright (C) 2021 Libelium Comunicaciones Distribuidas S.L.
    http://www.libelium.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Version:           3.0
    Implementation:    Luis Miguel Martí
*/

// Put your libraries here (#include ...)
#include <WaspWIFI_PRO_V3.h>


// choose socket (SELECT USER'S SOCKET)
///////////////////////////////////////
uint8_t socket = SOCKET0;
///////////////////////////////////////


// choose HTTP server settings
///////////////////////////////////////
char MQTT_SERVER[] = "test.mosquitto.org";
uint16_t MQTT_PORT = 8883;
///////////////////////////////////////


uint8_t error;
uint8_t status;
unsigned long previous;

///////////////////////////////////////
// define certificate for TLS
////////////////////////////////////////////////////////////////////////
char certificate[] =//
"-----BEGIN CERTIFICATE-----\r\n"\
"MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL\r\n"\
"BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG\r\n"\
"A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU\r\n"\
"BgNVBAMMDW1vc3F1aXR0by5vcmcxHzAdBgkqhkiG9w0BCQEWEHJvZ2VyQGF0Y2hv\r\n"\
"by5vcmcwHhcNMjAwN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u1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839\r\n"\
"LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE\r\n"\
"m/XriWr/Cq4h/JfB7NTsezVslgkBaoU=\r\n"\
"-----END CERTIFICATE-----";
////////////////////////////////////////////////////////////////////////


void setup()
{
  USB.println(F("Start program"));

  USB.println(F("***************************************"));
  USB.println(F("It is assumed the module was previously"));
  USB.println(F("configured in autoconnect mode."));
  USB.println(F("Once the module is configured with the"));
  USB.println(F("AP settings, it attempts to join the AP"));
  USB.println(F("automatically once it is powered on"));
  USB.println(F("Refer to example 'WIFI_02' to configure"));
  USB.println(F("the WiFi module with proper settings"));
  USB.println(F("***************************************"));

  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////
  error = WIFI_PRO_V3.ON(socket);

  if (error == 0)
  {
    USB.println(F("1. WiFi switched ON"));
  }
  else
  {
    USB.println(F("1. WiFi did not initialize correctly"));
  }

  //////////////////////////////////////////////////
  // 2. Check if connected
  //////////////////////////////////////////////////


  // get actual time
  previous = millis();

  // check connectivity
  status =  WIFI_PRO_V3.isConnected();

  // check if module is connected
  if (status == true)
  {
    USB.println(F("2. WiFi is connected OK"));

    USB.print(F("IP address: "));
    USB.println(WIFI_PRO_V3._ip);

    USB.print(F("GW address: "));
    USB.println(WIFI_PRO_V3._gw);

    USB.print(F("Netmask address: "));
    USB.println(WIFI_PRO_V3._netmask);
    
    USB.print(F(" Time(ms):"));
    USB.println(millis() - previous);
  }
  else
  {
    USB.print(F("2. WiFi is connected ERROR"));
    USB.print(F(" Time(ms):"));
    USB.println(millis() - previous);
    PWR.reboot();
  }


  error = WIFI_PRO_V3.setCA(0, certificate);
  if (error == 0)
  {
    USB.println(F("3. Set CA configured"));
  }
  else
  {
    USB.print(F("3. Set CA ERROR"));
  }



  //////////////////////////////////////////////////
  // 3. Configure HTTP conection
  //////////////////////////////////////////////////

  error = WIFI_PRO_V3.mqttConfiguration(MQTT_SERVER,"user", MQTT_PORT, WaspWIFI_v3::MQTT_TLS_ENABLED);
  if (error == 0)
  {
    USB.println(F("3. MQTT conection configured"));
  }
  else
  {
    USB.print(F("3. MQTT conection configured ERROR"));
  }

}



void loop()
{
  
  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////
  error = WIFI_PRO_V3.ON(socket);

  if (error == 0)
  {
    USB.println(F("1. WiFi switched ON"));
  }
  else
  {
    USB.println(F("1. WiFi did not initialize correctly"));
  }
  
  //////////////////////////////////////////////////
  // 2. Perform the HTTP GET
  //////////////////////////////////////////////////

  error = WIFI_PRO_V3.mqttPublishTopic("sys/test",WaspWIFI_v3::QOS_1,WaspWIFI_v3::RETAINED,"SentwithCA!");

  // check response
  if (error == 0)
  {
    USB.println(F("Publish topic done!"));
  }
  else
  {
    USB.println(F("Error publishing to topic!"));  
  }

  //////////////////////////////////////////////////
  // 3. Switch OFF
  //////////////////////////////////////////////////
  WIFI_PRO_V3.OFF(socket);

  delay(10000);

}

Output

H#
Start program
***************************************
It is assumed the module was previously
configured in autoconnect mode.
Once the module is configured with the
AP settings, it attempts to join the AP
automatically once it is powered on
Refer to example 'WIFI_02' to configure
the WiFi module with proper settings
***************************************
1. WiFi switched ON
2. WiFi is connected OK
IP address: 192.168.1.248
GW address: 192.168.1.1
Netmask address: 255.255.255.0
 Time(ms):171
3. Set CA configured
3. MQTT conection configured
1. WiFi switched ON
Publish topic done!

Last updated