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.

Compiler/CC3220S: IAR compiler

Part Number: CC3220S

Tool/software: TI C/C++ Compiler

Dear Community,

I using CC3220s

I have to use low power Wi-Fi.

The cc3220s is known to use LSI mode.

when connected to my Wi-Fi Ap, It was reduced to about 120 uA using the source below.

====================================================================================================================

PmPolicyParams.MaxSleepTimeMs = 1000;
sl_WlanPolicySet(SL_WLAN_POLICY_PM, SL_WLAN_LONG_SLEEP_INTERVAL_POLICY, (uint8_t *)&PmPolicyParams, sizeof(PmPolicyParams));

====================================================================================================================

However, when aws mqtt is connected, using the above source increases to about 16mA.

How to reduce the current to about 120uA when connecting aws mqtt?

thanks.

  • Sang Tae Kim,

    The AWS example does not have any optimizations for power consumption. The main loop cycles through aws yield as quick as possible. To enable low power, you will need to do a enablePowerPolicy setting, then do a create a mechanism to sleep for a desired time. For example, you could sleep for just 5 seconds at a time, which would lower power. The ideal way is to sleep until you have to wake up again, by using a sl_Select() to pend on your aws socket, and sleep for the MQTT Timeout interval.

    Please find attached a reference i had created in the past to accomplish this

    /cfs-file/__key/communityserver-discussions-components-files/968/0677.subscribe_5F00_publish_5F00_sample.c

    BR,

    Vince 

  • Vincent Rodriguez.

    Thank you for your answer.

    I tested your source after modifying it as below.

    5148.subscribe_publish_sample.c
    /*
     * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     *
     * Licensed under the Apache License, Version 2.0 (the "License").
     * You may not use this file except in compliance with the License.
     * A copy of the License is located at
     *
     *  http://aws.amazon.com/apache2.0
     *
     * or in the "license" file accompanying this file. This file is distributed
     * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     * express or implied. See the License for the specific language governing
     * permissions and limitations under the License.
     */
    #include <string.h>
    #include <unistd.h>
    
    #include "aws_iot_log.h"
    #include "aws_iot_version.h"
    #include "aws_iot_mqtt_client_interface.h"
    #include "aws_iot_config.h"
    #include "ti/drivers/net/wifi/sl_socket.h"
    #include "ti/net/slnetsock.h"
    #include "ti/net/bsd/sys/socket.h"
    #include "ti/net/bsd/sys/select.h"
    #include "ti/net/bsd/netinet/in.h"
    #include "ti/net/bsd/arpa/inet.h"
    #include "ti/drivers/power.h"
    char HostAddress[255] = AWS_IOT_MQTT_HOST;
    uint32_t port = AWS_IOT_MQTT_PORT;
    uint32_t publishCount = 0;
    int RecieveFlag = 0;
    
    static AWS_IoT_Client client;
    //Important globals
    int16_t sd = -1;
    int16_t SelectReturned = 0;
    void SimpleLinkSocketTriggerEventHandler(SlSockTriggerEvent_t *pSlTriggerEvent)
    {
        SelectReturned = 1;
        switch (pSlTriggerEvent->Event)
        {
        case SL_SOCKET_TRIGGER_EVENT_SELECT:
        {
            //LOG_MESSAGE("Notify User SL_SOCKET_TRIGGER_EVENT_SELECT \n\r");
            IOT_INFO("Socket Trigger Event!");
            break;
        }
    
        default:
            break;
        }
    }
    void MQTTcallbackHandler(AWS_IoT_Client *pClient, char *topicName,
                             uint16_t topicNameLen,
                             IoT_Publish_Message_Params *params, void *pData)
    {
        IOT_INFO("Subscribe callback");
        IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int )params->payloadLen,
                 (char * )params->payload);
        if (strncmp((char *) params->payload, "Wakeup", 6) == 0)
        {
            RecieveFlag = 1;
        }
    }
    
    void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data)
    {
        IOT_WARN("MQTT Disconnect");
        IoT_Error_t rc = SUCCESS;
    
        if (NULL == data)
        {
            return;
        }
    
        AWS_IoT_Client *client = (AWS_IoT_Client *) data;
        if (aws_iot_is_autoreconnect_enabled(client))
        {
            IOT_INFO(
                    "Auto Reconnect is enabled, Reconnecting attempt will start now");
        }
        else
        {
            IOT_WARN("Auto Reconnect not enabled. Starting manual reconnect...");
            rc = aws_iot_mqtt_attempt_reconnect(client);
            if (NETWORK_RECONNECTED == rc)
            {
                IOT_WARN("Manual Reconnect Successful");
                IOT_WARN("Manual Reconnect Failed - %d", rc);
            }
            else
            {
                IOT_WARN("Manual Reconnect Failed - %d", rc);
            }
        }
    }
    int SleepConfig(void)
    {
    
    }
    #if 0
    int Protected_Sl_Select(void)
    {
        struct sockaddr_in Addr;
        Addr.sin_family = SL_AF_INET;
        Addr.sin_port = sl_Htons(5001);
        Addr.sin_addr.s_addr = htonl(SL_IPV4_VAL(127, 0, 0, 1));
        //Check if there is a pending sl_Select()
        if(!SelectReturned && sd >= 0)
        {
            //Cause Sl_Select to return
            int16_t stat = connect(sd, ( sockaddr_in *)&Addr, sizeof(sockaddr_in));
                if(stat < 0){
                    IOT_INFO("\n\rcould not connect second socket: %d\r\n", stat);
                }
                IOT_INFO("Causing Select to return ");
    
                //close(sd);
                sd = -1;
        }
        //Create new loopback socket and listen on it.
        if (sd <= 0)
        {
            //Need to create the socket and bind it
            Addr.sin_addr.s_addr = htonl(INADDR_ANY);
            sd = socket(SL_AF_INET, SL_SOCK_STREAM, 0);
            if (sd < 0)
            {
                IOT_INFO("second socket create failed");
            }
            int stat = bind(sd, (sockaddr_in *) &Addr,
                           sizeof(sockaddr_in));
            if (stat < 0)
            {
                IOT_INFO("\n\rCould not bind socket\r\n");
            }
    
            stat = listen(sd, 1);
            if (stat < 0)
            {
                IOT_INFO("\n\rCould not listen socket\r\n");
            }
        }
    
        SlNetSock_SdSet_t rxSet;
        struct timeval timeVal = {0};
        SlNetSock_sdsClrAll(&rxSet);
        SlNetSock_sdsSet(sd, &rxSet);
        SlNetSock_sdsSet(client.networkStack.tlsDataParams.skt, &rxSet);
        Display_printf(display, 0, 0, "calling sl_select\r\n");
        int returnval = select(sd+1, &rxSet, NULL, NULL, &timeVal);
        return returnval;
    }
    #endif
    
    static void _aws_iot_mqtt_force_client_disconnect(AWS_IoT_Client *pClient) {
     // pClient->clientStatus.clientState = CLIENT_STATE_DISCONNECTED_ERROR;
    //  pClient->networkStack.disconnect(&(pClient->networkStack));
    //  pClient->networkStack.destroy(&(pClient->networkStack));
    }
    static IoT_Error_t _aws_iot_mqtt_handle_disconnect(AWS_IoT_Client *pClient) {
        IoT_Error_t rc;
    
        FUNC_ENTRY;
    
        rc = aws_iot_mqtt_disconnect(pClient);
        if(rc != SUCCESS) {
            // If the aws_iot_mqtt_internal_send_packet prevents us from sending a disconnect packet then we have to clean the stack
            _aws_iot_mqtt_force_client_disconnect(pClient);
        }
    
        if(NULL != pClient->clientData.disconnectHandler) {
            pClient->clientData.disconnectHandler(pClient, pClient->clientData.disconnectHandlerData);
        }
    
        /* Reset to 0 since this was not a manual disconnect */
        pClient->clientStatus.clientState = CLIENT_STATE_DISCONNECTED_ERROR;
        FUNC_EXIT_RC(NETWORK_DISCONNECTED_ERROR);
    }
    
    
    #define WAKE_LIMIT_MSEC   6000
    int SleepInterval(uint32_t msecSleep)
    {
    //setup hibernate timer to wakeup at msecSleep
    
        int check = 0;
    //setup select call
        int nfds = client.networkStack.tlsDataParams.skt + 1;
        SlNetSock_Timeval_t timeVal = {0};
          timeVal.tv_sec = msecSleep / 1000;
          timeVal.tv_usec = (msecSleep % 1000) * 100;
        SlNetSock_SdSet_t rxSet;
        SlNetSock_SdSet_t exceptSet;
        int iStatus = 0;
    
        SlNetSock_sdsClrAll(&rxSet);
        SlNetSock_sdsSet(client.networkStack.tlsDataParams.skt, &rxSet);
        IOT_INFO("Selecting On skt: %d \r\n", client.networkStack.tlsDataParams.skt);
        check = SlNetSock_select(nfds, &rxSet, NULL, NULL, &timeVal);
        IOT_INFO("sl_Select value = %d", check);
    
        return (SUCCESS);
    
    }
    void runAWSClient(void)
    {
        IoT_Error_t rc = SUCCESS;
        int i = 0;
        bool infinitePublishFlag = true;
        char *topicName = "update/delta";
        int topicNameLen = strlen(topicName);
    
        IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault;
        IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault;
    
        IoT_Publish_Message_Params paramsQOS0;
        IoT_Publish_Message_Params paramsQOS1;
    
        IOT_INFO("\nAWS IoT SDK Version %d.%d.%d-%s\n", VERSION_MAJOR,
                 VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
    
        mqttInitParams.enableAutoReconnect = true; // 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 = 5000;
        mqttInitParams.tlsHandshakeTimeout_ms = 5000;
        mqttInitParams.isSSLHostnameVerify = true;
        mqttInitParams.disconnectHandler = disconnectCallbackHandler;
        mqttInitParams.disconnectHandlerData = (void *) &client;
    
        rc = aws_iot_mqtt_init(&client, &mqttInitParams);
        if (SUCCESS != rc)
        {
            IOT_ERROR("aws_iot_mqtt_init returned error : %d ", rc);
        }
    
        connectParams.keepAliveIntervalInSec = 10; //1740;
        connectParams.isCleanSession = true;
        connectParams.MQTTVersion = MQTT_3_1_1;
        connectParams.pClientID = AWS_IOT_MQTT_CLIENT_ID;
        connectParams.clientIDLen = (uint16_t) strlen(AWS_IOT_MQTT_CLIENT_ID);
        connectParams.isWillMsgPresent = false;
    
        IOT_INFO("Connecting...");
        rc = aws_iot_mqtt_connect(&client, &connectParams);
        if (SUCCESS != rc)
        {
            IOT_ERROR("Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL,
                      mqttInitParams.port);
        }
    
        /*
         *  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(&client, true);
        if (SUCCESS != rc)
        {
            IOT_ERROR("Unable to set Auto Reconnect to true - %d", rc);
        }
    
        IOT_INFO("Subscribing...");
        rc = aws_iot_mqtt_subscribe(&client, topicName, topicNameLen, QOS0,
                                    MQTTcallbackHandler, NULL);
        if (SUCCESS != rc)
        {
            IOT_ERROR("Error subscribing (%d)", rc);
        }
    
        char cPayload[100];
        sprintf(cPayload, "%s : %d ", "hello from SDK", i);
    
        paramsQOS0.qos = QOS0;
        paramsQOS0.payload = (void *) cPayload;
        paramsQOS0.isRetained = 0;
    
        paramsQOS1.qos = QOS1;
        paramsQOS1.payload = (void *) cPayload;
        paramsQOS1.isRetained = 0;
    
        if (publishCount != 0)
        {
            infinitePublishFlag = false;
        }
        //Enable Low Power Features
        //Power_enablePolicy();
        SlWlanPmPolicyParams_t PmPolicyParams;
        memset(&PmPolicyParams, 0, sizeof(SlWlanPmPolicyParams_t));
        PmPolicyParams.MaxSleepTimeMs = 1000;  //max sleep time in mSec
        sl_WlanPolicySet(SL_WLAN_POLICY_PM, SL_WLAN_LONG_SLEEP_INTERVAL_POLICY,
                         (_u8*) &PmPolicyParams, sizeof(PmPolicyParams));
        int ret = sl_DeviceStatStart(0);
        while ((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc
                || SUCCESS == rc) && (publishCount > 0 || infinitePublishFlag))
        {
    		IOT_INFO("yield\r\n");
            rc = aws_iot_mqtt_yield(&client, 200);
            IOT_INFO("-->ping timer = %d msecs", left_ms(&client.pingTimer));
            if (NETWORK_ATTEMPTING_RECONNECT == rc)
            {
                /* If the client is attempting to reconnect, skip rest of loop */
                continue;
            }
            else if(rc == SUCCESS)
            {
                if (RecieveFlag == 1)
                {
                    sprintf(cPayload, "121 Responded");
                    /* Recalculate string len to avoid truncation in subscribe callback */
                    paramsQOS0.payloadLen = strlen(cPayload);
                    IOT_INFO("SEND RESPONSE TO WAKEUP\r\n");
                    rc = aws_iot_mqtt_publish(&client, topicName, topicNameLen,
                                              &paramsQOS0);
                    RecieveFlag = 0;
                }
                //If sleep is long enough, sleep til MQTT Timeout
                if (left_ms(&client.pingTimer) > WAKE_LIMIT_MSEC)
                {
                    IOT_INFO("-->sleep Waking up in %d msecs",
                             left_ms(&client.pingTimer));
                    //Get Time to next MQTT Timeout Interval and call sl_Select
                    SleepInterval(left_ms(&client.pingTimer));
                }
            }
    
    //        IOT_INFO("-->sleep");
    //        sleep(1);
    //        sprintf(cPayload, "%s : %d ", "hello from SDK QOS0", i++);
    //        /* Recalculate string len to avoid truncation in subscribe callback */
    //        paramsQOS0.payloadLen = strlen(cPayload);
    //        rc = aws_iot_mqtt_publish(&client, topicName, topicNameLen,
    //                &paramsQOS0);
    //        if (publishCount > 0) {
    //           publishCount--;
    //        }
    //
    //        sprintf(cPayload, "%s : %d ", "hello from SDK QOS1", i++);
    //        /* Recalculate string len to avoid truncation in subscribe callback */
    //        paramsQOS1.payloadLen = strlen(cPayload);
    //        do {
    //            rc = aws_iot_mqtt_publish(&client, topicName, topicNameLen,
    //                    &paramsQOS1);
    //            if (publishCount > 0) {
    //                publishCount--;
    //            }
    //        } while (MQTT_REQUEST_TIMEOUT_ERROR == rc &&
    //                (publishCount > 0 || infinitePublishFlag));
        }
    
        if (SUCCESS != rc)
        {
            IOT_ERROR("An error occurred in the loop. Error code = %d\n", rc);
        }
        else
        {
            IOT_INFO("Publish done\n");
        }
    }
    

    but, the current still consumes about 16mA.

    I modified as below

    int SleepInterval(uint32_t msecSleep)
    {
    //setup hibernate timer to wakeup at msecSleep

    int check = 0;
    //setup select call
    int nfds = client.networkStack.tlsDataParams.skt + 1;
    SlNetSock_Timeval_t timeVal = {0};
    timeVal.tv_sec = msecSleep / 1000;
    timeVal.tv_usec = (msecSleep % 1000) * 100;
    SlNetSock_SdSet_t rxSet;
    SlNetSock_SdSet_t exceptSet;
    int iStatus = 0;

    SlNetSock_sdsClrAll(&rxSet);
    SlNetSock_sdsSet(client.networkStack.tlsDataParams.skt, &rxSet);
    IOT_INFO("Selecting On skt: %d \r\n", client.networkStack.tlsDataParams.skt);
    Power_enablePolicy();  <=== added
    check = SlNetSock_select(nfds, &rxSet, NULL, NULL, &timeVal);
    Power_disablePolicy();  <=== added
    IOT_INFO("sl_Select value = %d", check);

    return (SUCCESS);

    }

    1. if Power_disablePolicy function not use, it rebooted

        why reboot?

    2. It stops at the source below.

     rc = aws_iot_mqtt_yield(&client, 200);

    3. What are the additional modifications to reduce the current?

    thanks

  • Vincent Rodriguez.

    Thank you for your answer.

    I tested your source after modifying it as below.

    5148.subscribe_publish_sample.c

    but, the current still consumes about 16mA.

    I modified as below

    int SleepInterval(uint32_t msecSleep)
    {
    //setup hibernate timer to wakeup at msecSleep

    int check = 0;
    //setup select call
    int nfds = client.networkStack.tlsDataParams.skt + 1;
    SlNetSock_Timeval_t timeVal = {0};
    timeVal.tv_sec = msecSleep / 1000;
    timeVal.tv_usec = (msecSleep % 1000) * 100;
    SlNetSock_SdSet_t rxSet;
    SlNetSock_SdSet_t exceptSet;
    int iStatus = 0;

    SlNetSock_sdsClrAll(&rxSet);
    SlNetSock_sdsSet(client.networkStack.tlsDataParams.skt, &rxSet);
    IOT_INFO("Selecting On skt: %d \r\n", client.networkStack.tlsDataParams.skt);
    Power_enablePolicy();  <=== added
    check = SlNetSock_select(nfds, &rxSet, NULL, NULL, &timeVal);
    Power_disablePolicy();  <=== added
    IOT_INFO("sl_Select value = %d", check);

    return (SUCCESS);

    }

    1. if Power_disablePolicy function not use, it rebooted

        why reboot?

    2. It stops at the source below.

     rc = aws_iot_mqtt_yield(&client, 200);

    3. What are the additional modifications to reduce the current?

    thanks

  • Hi,

    You should use Power_enablePolicy() once and don't disable it. this allows the mcu to go to sleep.

    BR,

    Vince