This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3235S: using AWS SDK interface function.

Part Number: CC3235S

Hi,

In CC3235S and SDK6.10, AWS functions for user command are being implemented by referring to the subscribe_publish_sample example of AWS SDK 4.30.

(The test was successful using the subscribe_publish_sample example)

 

The AWS function simply implemented two commands.

1. Connect 2. Command

 

In the Connect function,

initialization and connection are performed using aws_iot_mqtt_init, aws_iot_mqtt_connect, and aws_iot_mqtt_autoreconnect_set_status, and a yield thread is created after the connection is completed.

 

In the Command function,

depending on options, Subscribe, Publish, and Unsubscribe are performed using aws_iot_mqtt_subscribe, aws_iot_mqtt_publish, and aws_iot_mqtt_unsubscribe respectively.

 

After AWS Connect, Publish using Command was successful.

However, even if Subscribe is used, the callback function is not called in CC3235S. (When subscribe is performed in the Connect function, the callback function is called)

Also, when using Unsubscribe, the registered topic is no longer received, but the aws_iot_mqtt_unsubscribe function always returns -1.

 

Below is my simple code for the Command function.

AWS_IoT_Client gAWSIoTClient;

void * AWSIoTYieldThread(void *arg)
{
    IOT_INFO("%s\n\r", __FUNCTION__);

    IoT_Error_t rc = FAILURE;

    while(1)
    {
        rc = aws_iot_mqtt_yield(&gAWSIoTClient, 100);
        if (SUCCESS != rc) {
            IOT_ERROR("An error occurred in the loop. Error code = %d\n", rc);
        }
        GPIO_toggle(CONFIG_GPIO_RLED);
            
        sleep(1);
    }
}

int32_t cmdAWSIoTConnectCallback(void *arg)
{
    IoT_Error_t rc = FAILURE;
    IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault;
    IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault;

    IOT_INFO("\nAWS IoT SDK Version %d.%d.%d-%s\n", VERSION_MAJOR,
            VERSION_MINOR, VERSION_PATCH, VERSION_TAG);

    mqttInitParams.enableAutoReconnect = false; // We enable this later below
    mqttInitParams.pHostURL = HostAddress;
    mqttInitParams.port = port;
    mqttInitParams.pRootCALocation = AWS_IOT_ROOT_CA_FILENAME;
    mqttInitParams.pDeviceCertLocation = AWS_IOT_CERTIFICATE_FILENAME;
    mqttInitParams.pDevicePrivateKeyLocation = AWS_IOT_PRIVATE_KEY_FILENAME;
    mqttInitParams.mqttCommandTimeout_ms = 20000;
    mqttInitParams.tlsHandshakeTimeout_ms = 5000;
    mqttInitParams.isSSLHostnameVerify = true;
    mqttInitParams.disconnectHandler = disconnectCallbackHandler;
    mqttInitParams.disconnectHandlerData = NULL;

    rc = aws_iot_mqtt_init(&gAWSIoTClient, &mqttInitParams);
    if (SUCCESS != rc) {
        IOT_ERROR("aws_iot_mqtt_init returned error : %d ", rc);
        return(-1);
    }

    connectParams.keepAliveIntervalInSec = 600;
    connectParams.isCleanSession = true;
    connectParams.MQTTVersion = MQTT_3_1_1;
    connectParams.pClientID = ClientID;
    connectParams.clientIDLen = (uint16_t)strlen(ClientID);
    connectParams.isWillMsgPresent = false;

    IOT_INFO("Connecting...");
    rc = aws_iot_mqtt_connect(&gAWSIoTClient, &connectParams);
    if (SUCCESS != rc) {
        IOT_ERROR("Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL,
                mqttInitParams.port);
        return(-1);
    }
    /*
     *  Enable Auto Reconnect functionality. Minimum and Maximum time of
     *  exponential backoff are set in aws_iot_config.h:
     *  #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
     *  #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
     */
    rc = aws_iot_mqtt_autoreconnect_set_status(&gAWSIoTClient, true);
    if (SUCCESS != rc) {
        IOT_ERROR("Unable to set Auto Reconnect to true - %d", rc);
    }
    IOT_INFO("Connect!");

    /* It code is working
    IOT_INFO("Subscribing...");
    char *topicName = "sdkTest/sub";
    int topicNameLen = strlen(topicName);
    rc = aws_iot_mqtt_subscribe(&gAWSIoTClient, topicName, topicNameLen, QOS0,
            iot_subscribe_callback_handler, NULL);
    if (SUCCESS != rc) {
        IOT_ERROR("Error subscribing : %d ", rc);
        return(-1);
    }
    */

    pthread_t awsyieldThread;
    awsyieldThread = OS_createTask(7, 1024, AWSIoTYieldThread, NULL, 1);
    if (awsyieldThread < 0)
    {
        DBG_PRINT("awsyieldThread Fail %d\n\r");

        return(-1);
    }

    return 0;
}

int32_t cmdAWSIoTCmdCallback(void *arg)
{
    ...
    /* Parse options */
    ...
    
    if (!error)
    {
        IoT_Error_t rc = FAILURE;

        if (fSub)
        {
            IOT_INFO("Subscribing...");
            rc = aws_iot_mqtt_subscribe(&gAWSIoTClient, topicName, topicNameLen, QOS0,
                    iot_subscribe_callback_handler, NULL);
            if (SUCCESS != rc) {
                IOT_ERROR("Error subscribing : %d ", rc);
            }

            rc = aws_iot_mqtt_resubscribe(&gAWSIoTClient);
            if (SUCCESS != rc) {
                IOT_ERROR("Error resubscribing : %d ", rc);
            }
        }

        if (fPub)
        {
            IOT_INFO("Publishing...");

            IoT_Publish_Message_Params paramsQOS0;

            paramsQOS0.qos = QOS0;
            paramsQOS0.payload = (void *)pubPayload;
            paramsQOS0.payloadLen = strlen(pubPayload);
            paramsQOS0.isRetained = 0;

            rc = aws_iot_mqtt_publish(&gAWSIoTClient, topicName, topicNameLen,
                    &paramsQOS0);
            if (SUCCESS != rc) {
                IOT_ERROR("Error publishing : %d ", rc);
            }
        }

        if (fUnsub)
        {
            IOT_INFO("Unsubscribe...");
            aws_iot_mqtt_unsubscribe(&gAWSIoTClient, topicName, topicNameLen);
            if (SUCCESS != rc) {
                IOT_ERROR("Error unsubscribe : %d ", rc);
            }
        }
    }

    return error;
}

 

Is there something I'm missing about using the aws_iot_mqtt_subscribe function?

Any advice would be appreciated.

Thank you.