Sony's Developer World forum

    • Home
    • Forum guidelines

    LTE TLS connection to MQTT Broker without CACert

    Spresense
    3
    4
    1036
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      SudoObey last edited by

      I am using the Spresense main board with the LTE extension. I am using the Arduino IDE to flash the program to the device. I am trying to connect to a MQTT client using TLS but without having a CERT file. I just use a username and password. Is this something that is possible? Currently I am using LTETLSClient and MqttClient arduino libraries.

      #include <LTE.h>
      #include <ArduinoMqttClient.h>
      #include <Arduino_JSON.h>
      
      // APN name
      #define APP_LTE_APN "apn.com"
      
      // APN IP type
      #define APP_LTE_IP_TYPE (LTE_NET_IPTYPE_V4V6)
      
      // APN authentication type
      #define APP_LTE_AUTH_TYPE (LTE_NET_AUTHTYPE_NONE) 
      
      // RAT to use
      #define APP_LTE_RAT (LTE_NET_RAT_CATM) 
      
      LTE lteAccess;
      LTETLSClient client;
      MqttClient mqttClient(client);
      
      // MQTT settings (WEST IOT)
      const char broker[] = "mqtt.broker.com";
      int port = 8883;
      const char uname[] = "username";
      const char client_id[] = "clientId";
      const char pass[] = "password";
      
      void setup()
      {
        char apn[LTE_NET_APN_MAXLEN] = APP_LTE_APN;
        LTENetworkAuthType authtype = APP_LTE_AUTH_TYPE;
      
        // initialize serial communications and wait for port to open:
        Serial.begin(115200);
        while (!Serial) 
        {
            ; // wait for serial port to connect. Needed for native USB port only
        }
        while (true) 
        {
          /* Power on the modem and Enable the radio function. */
          if (lteAccess.begin() != LTE_SEARCHING) 
          {
            Serial.println("Could not transition to LTE_SEARCHING.");
            Serial.println("Please check the status of the LTE board.");
            for (;;) {
              sleep(1);
            }
          }
      
          /* The connection process to the APN will start.
           * If the synchronous parameter is false,
           * the return value will be returned when the connection process is started.
           */
          if (lteAccess.attach(APP_LTE_RAT,
                               apn,
                               "",
                               "",
                               authtype,
                               APP_LTE_IP_TYPE) == LTE_READY) 
          {
            Serial.println("attach succeeded.");
            break;
          }
      
          /* If the following logs occur frequently, one of the following might be a cause:
           * - APN settings are incorrect
           * - SIM is not inserted correctly
           * - If you have specified LTE_NET_RAT_NBIOT for APP_LTE_RAT,
           *   your LTE board may not support it.
           * - Rejected from LTE network
           */
          Serial.println("An error has occurred. Shutdown and retry the network attach process after 1 second.");
          lteAccess.shutdown();
          sleep(1);
        }
      
        Serial.print("Attempting to connect to the MQTT broker: ");
        Serial.println(broker);
        mqttClient.setUsernamePassword(uname, pass);
        mqttClient.setId(client_id);
        if (!mqttClient.connect(broker, port)) 
        {
          Serial.print("MQTT connection failed! Error code = ");
          Serial.println(mqttClient.connectError());
          while (1);
        }
        Serial.println("You're connected to the MQTT broker!");
        Serial.println();
      }
      
      C 1 Reply Last reply Reply Quote
      • C
        CamilaSouza DeveloperWorld @SudoObey last edited by

        Hey, SudoObey

        I believe the TLS protocol requires a certificate.
        Are you unsure of where to get the certificate you need?

        S 1 Reply Last reply Reply Quote
        • S
          SudoObey @CamilaSouza last edited by

          @CamilaSouza
          Thank you for getting back to me. I am fairly new to this MQTT and LTE thing. Is there a way to have a TLS connection using PSK without a Cert file?

          1 Reply Last reply Reply Quote
          • O
            odi.odi 2 4 4 0 3 4 1 1 1 0 1 1 1 6 4 5 4 5 4 6 3 3 1 1 1 1 1 0 5 last edited by

            Hi,
            have you tried using the 'unsecure' MQTT port 1883?
            Using this one you should not need a certificate or PSK.
            BR

            1 Reply Last reply Reply Quote
            • First post
              Last post
            Developer World
            Copyright © 2021 Sony Group Corporation. All rights reserved.
            • Contact us
            • Legal