Control Smart Config mode [SOLVED]

Hi,

After read several topics talking about the possibility of control the smart config mode, i’m not clarify how i get full control of smart config process.

I want to activate the smart config for a period of time and then if there wasn’t any device to set new SSID and wifi key, i want to turn it off.

I’m already able to enter in smartconfig mode during my code activating the flag WLAN_SMART_CONFIG_START, there is any timeout configuration or any away to stop the smart config mode, or it’s really needed to change the bootloader as refereed by @kennethlimcp in this topic: https://community.spark.io/t/is-there-a-way-to-set-wlan-smart-config-start-and-continue-running-my-code/5048

Thanks!

Look at docs.spark.io there’s a function call to enter Listening mode but I’m on mobile so I can’t paste the syntax :wink:

hi @kennethlimcp, are you talking of the function WiFi.listen() in spark_wiring_wifi.h referred here:

Yes i think that should work! http://docs.spark.io/firmware/#wifi-listen

Hi,
I had to implement a timeout at work. Unrolling all the calls didn’t seem to lend itself as an easy solution, so I changed the while() loop in Start_Smart_Config() in core-firmware/src/spark_wlan.cpp. I only wanted to time out if there were credentials, and I found an easy way to reset was to do a deep sleep for 1 second. Get into listen mode as described above; these changes will reset the core after 5 minutes if the core has credentials (remove the tests if you don’t need that feature).

 void Start_Smart_Config(void)
 {
+    unsigned long SCtimeout;
+    boolean EnableSCtimeout = false;
+
     WLAN_SMART_CONFIG_FINISHED = 0;
     WLAN_SMART_CONFIG_STOP = 0;
     WLAN_SERIAL_CONFIG_DONE = 0;
     WLAN_CONNECTED = 0;
     WLAN_DHCP = 0;
     WLAN_CAN_SHUTDOWN = 0;
 
     SPARK_CLOUD_SOCKETED = 0;
     SPARK_CLOUD_CONNECTED = 0;
     SPARK_FLASH_UPDATE = 0;
     SPARK_LED_FADE = 0;
 
     LED_SetRGBColor(RGB_COLOR_BLUE);
     LED_On(LED_RGB);
 
     WiFi.disconnect();
 
     /* Create new entry for AES encryption key */
     nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16);
 
     /* Write AES key to NVMEM */
     aes_write_key((unsigned char *)(&smartconfigkey[0]));
 
     wlan_smart_config_set_prefix((char*)aucCC3000_prefix);
 
     /* Start the SmartConfig start process */
     wlan_smart_config_start(1);
 
     WiFiCredentialsReader wifi_creds_reader(wifi_add_profile_callback);
 
     /* Wait for SmartConfig/SerialConfig to finish */
+    SCtimeout = millis() + 5*60*1000;           // set timeout to 5 minutes in the future
+    EnableSCtimeout = WiFi.hasCredentials();    // enable timeout only if I have credentials
     while (WiFi.listening())
     {
         if(WLAN_DELETE_PROFILES)
         {
             int toggle = 25;
             while(toggle--)
             {
                 LED_Toggle(LED_RGB);
                 Delay(50);
             }
             WiFi.clearCredentials();
             WLAN_DELETE_PROFILES = 0;
+            EnableSCtimeout = false;            // I no longer have credentials so don't timeout
         }
         else
         {
             LED_Toggle(LED_RGB);
             Delay(250);
             wifi_creds_reader.read();
         }
+        // reset if I have I have credentials and I've been here too long
+        if( EnableSCtimeout && ((long)(millis() - SCtimeout) >= 0) )
+            Spark.sleep(SLEEP_MODE_DEEP,1);
     }
 
     LED_On(LED_RGB);
 

Hi,

@kennethlimcp, this function WiFi.listen() simply activate the flag to call the smartconfig mode, it doesn’t control the process.

@ScottR, I use some inspiration in your code, and using the last core-firmware from the master branch, i adjust it. Because i don’t want to reset the core to stop the smartconfig process, so i implement this way:

void Start_Smart_Config(void)
{
+   unsigned long SCtimeout;
+   int time_out= 20000; // milliseconds
+   bool EnableSCtimeout = false;
+   bool stop_smartConfig= false;

	WLAN_SMART_CONFIG_FINISHED = 0;
	WLAN_SMART_CONFIG_STOP = 0;
	WLAN_SERIAL_CONFIG_DONE = 0;
	WLAN_CONNECTED = 0;
	WLAN_DHCP = 0;
	WLAN_CAN_SHUTDOWN = 0;

	SPARK_CLOUD_SOCKETED = 0;
	SPARK_CLOUD_CONNECTED = 0;
	SPARK_FLASH_UPDATE = 0;
	SPARK_LED_FADE = 0;

	LED_SetRGBColor(RGB_COLOR_BLUE);
	LED_On(LED_RGB);

	WiFi.disconnect();

	/* Create new entry for AES encryption key */
        nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16);

	/* Write AES key to NVMEM */
	aes_write_key((unsigned char *)(&smartconfigkey[0]));

	wlan_smart_config_set_prefix((char*)aucCC3000_prefix);

	/* Start the SmartConfig start process */
	wlan_smart_config_start(1);

	WiFiCredentialsReader wifi_creds_reader(wifi_add_profile_callback);


+    SCtimeout = millis() + time_out;    // Define timeout
+    EnableSCtimeout = WiFi.hasCredentials();    // enable timeout only if I have credentials

	/* Wait for SmartConfig/SerialConfig to finish */
	while (WiFi.listening())
	{
		if(WLAN_DELETE_PROFILES)
		{
			int toggle = 25;
			while(toggle--)
			{
				LED_Toggle(LED_RGB);
				Delay(50);
			}
			WiFi.clearCredentials();
			WLAN_DELETE_PROFILES = 0;
+                      EnableSCtimeout = false;            // I no longer have credentials so don't timeout
		}
		else
		{
			LED_Toggle(LED_RGB);
			Delay(250);
			wifi_creds_reader.read();
		}

+        // reset if I have I have credentials and I've been here too long
+       if( EnableSCtimeout && ((long)(millis() - SCtimeout) >= 0) ){
+            stop_smartConfig=true;
+            break;
+        }
    }


   LED_On(LED_RGB);

+    if(stop_smartConfig){
+        wlan_smart_config_stop();
+    } else {

        /* read count of wlan profiles stored */
        nvmem_read(NVMEM_SPARK_FILE_ID, 1, WLAN_PROFILE_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET]);

        if(WLAN_SMART_CONFIG_FINISHED)
        {
            /* Decrypt configuration information and add profile */
            SPARK_WLAN_SmartConfigProcess();
        }
+    }

	WiFi.connect();

	WLAN_SMART_CONFIG_START = 0;
}

I will perform more tests to check if its working properly. Because i’m using semi-automatic mode and i have some issues: https://github.com/spark/core-firmware/issues/310

Thanks!

1 Like