aws / amazon-freertos

DEPRECATED - See README.md
https://aws.amazon.com/freertos/
MIT License
2.54k stars 1.1k forks source link

[question] option to turn off auto-connect wifi #493

Closed chegewara closed 5 years ago

chegewara commented 5 years ago

Hi, is there option to stop auto-reconnect wifi even if wifi credentials are correct? I am using WIFI_Disconnect but before i can do something app is reconnecting to AP. Thanks.

EDIT this problem exists only when at least 1 WIFINetwork is stored in NVS with WIFI_NetworkAdd into nvs, (esp32 device)

abhidixi11 commented 5 years ago

Hello @chegewara,

  1. Which branch are you referring to? ble-beta or master?
  2. Which app you are referring to ? Thanks.
chegewara commented 5 years ago

Hi,

  1. modified ble-beta branch
  2. this app should be enough to reproduce this issue https://github.com/chegewara/esp32-AWSFreeRTOS-wifi-provisioning-demo

From awsfreertos i am using basically only main.c, without apps from DEMO_RUNNER_RunDemos

Thanks.

abhidixi11 commented 5 years ago

Hello @chegewara, Thanks for quick response. I didn't see the edit earlier about WIFI_NetworkAdd(). We will look into it when we get a chance, since ble-beta is still in 'beta' and we are actively working on it, it may take some time, due to other priorities. We will update once we find more information. Thanks.

chegewara commented 5 years ago

Thanks, i can try to test against master and see if this problem exists in master too if this will help.

abhidixi11 commented 5 years ago

Hello @chegewara, Yes, if you can verify it, it will be great. Thanks!

chegewara commented 5 years ago

Sorry, but it cant be tested against master branch. This is the reason: https://github.com/aws/amazon-freertos/blob/master/lib/wifi/portable/espressif/esp32_devkitc_esp_wrover_kit/aws_wifi.c#L530

WIFI_NetworkAdd is not implemented yet.

Also what i found is that AwsIotNetworkManager is not implemented in master, and i am guessing all connections managing when wifi credentials are stored in nvs are handled by AwsIotNetworkManager.

abhidixi11 commented 5 years ago

Yes, that's correct. AwsIotNetworkManager is new feature in ble-beta and WIFI_NetworkAdd is the new update from ESP. We will look into it as time permits and update the the issue. Thanks for your patience!

chegewara commented 5 years ago

Thanks for support. Its not very urgent, because i still need some time before i deliver full app to the client.

From this log WIFI: SYSTEM_EVENT_STA_DISCONNECTED: 8 i would say that NetworkManager should not try to reconnect when disconnect reason is 8 (WIFI_REASON_ASSOC_LEAVE).

chegewara commented 5 years ago

Further investigations shows me that the issue is because of this task: https://github.com/aws/amazon-freertos/blob/feature/ble-beta/demos/common/wifi_provisioning/aws_wifi_connect_task.c#L79

which is also described in here: https://github.com/aws/amazon-freertos/tree/feature/ble-beta/demos/common/wifi_provisioning#provisioning-sequence

abhidixi11 commented 5 years ago

Hello @chegewara ,

The WiFi Connect task is designed to keep WiFi connected that's why you are experiencing this behavior. You have two options:

  1. Comment out the task here: https://github.com/aws/amazon-freertos/blob/feature/ble-beta/demos/common/network_manager/aws_iot_network_manager.c#L402
  2. You can programmatically disable WIFI by using network manager API AwsIotNetworkManager_DisableNetwork() and providing AWSIOT_NETWORK_TYPE_WIFI as the parameter. You can turn the WIFI back on by using AwsIotNetworkManager_EnableNetwork( AWSIOT_NETWORK_TYPE_WIFI ). Let us know if you have any questions. Thanks.
chegewara commented 5 years ago

Thanks for help and support, i found solution for my requirements. I am using 2 variables (1 for auto-connect and 1 for auto-reconnect):

static void prvWiFiNetworkStateChangeCallback( uint32_t ulNetworkType, AwsIotNetworkState_t xNetworkState, void* pvContext )
{
    if( xNetworkState == eNetworkStateDisabled && autoReconnect )
    {
        xSemaphoreGive( xWiFiConnectLock );
    }
}

void prvWiFiConnectTask( void * pvParams )
{
    uint16_t ulNumNetworks, ulNetworkIndex;
    TickType_t xWifiConnectionDelay = pdMS_TO_TICKS( wifiConnectDELAY_SECONDS * 1000 );
    BaseType_t xWiFiConnected;

    for(;;)
    {
        if( xSemaphoreTake( xWiFiConnectLock, portMAX_DELAY ) == pdTRUE )
        {
            xWiFiConnected = pdFALSE;
            while( xWiFiConnected == pdFALSE )
            {
                ulNumNetworks = IotBleWifiProv_GetNumNetworks();
                if( ulNumNetworks > 0 && autoConnect)

Its not perfect solution but it works for now.