Browse Source

Updated IO interface and created Wi-Fi Interface

curiousmuch 6 years ago
parent
commit
dec3b38cec

+ 3 - 3
Makefile

@@ -208,7 +208,7 @@ checkdirs: $(BUILD_DIR) $(FIRMWARE_BASE)
 
 $(OUTPUT): $(TARGET_OUT)
 	$(vecho) "FW $@"
-	$(Q) $(ESPTOOL) elf2image $(ESPTOOL_FLASHDEF) $< -o $(OUTPUT)
+	$(Q) python2 $(ESPTOOL) elf2image $(ESPTOOL_FLASHDEF) $< -o $(OUTPUT)
 
 $(OUTPUT_LIB): $(OBJ_LIB)
 	$(vecho) "AR $@"
@@ -229,10 +229,10 @@ $(APP_AR): $(OBJ)
 	$(Q) $(AR) cru $@ $^
 
 flash:
-	$(ESPTOOL) $(ESPTOOL_OPTS) $(ESPTOOL_WRITE)
+	python2 $(ESPTOOL) $(ESPTOOL_OPTS) $(ESPTOOL_WRITE)
 
 wipe:
-	$(ESPTOOL) $(ESPTOOL_OPTS) erase_flash
+	python2 $(ESPTOOL) $(ESPTOOL_OPTS) erase_flash
 
 fast: all flash openport
 

+ 32 - 32
hal/io.c

@@ -14,25 +14,25 @@
 #include "hal/lsm6ds3.h"
 #include "log/esp_log.h"
 
-static const char* TAG = "io.c";
+static const char* TAG = "IO Driver";
 
 // debounce timers
-static Encoder_t encoder;
+static IO_config_t io_config;
 
-void Encoder_GPIO_Handler(void);
+void IO_GPIO_Handler(void);
 static void switchA_debounce(void);
 static void switchB_debounce(void);
 
-void ICACHE_FLASH_ATTR Encoder_InitIO(void)
+void ICACHE_FLASH_ATTR IO_InitIO(void)
 {
     ETS_GPIO_INTR_DISABLE();    // disable interrupts
 
     gpio_init();                // magic function in SDK
 
-    encoder.debounce_delay = DEBOUNCEDELAY;
-    encoder.switch_a_cb = NULL;
-    encoder.switch_b_cb = NULL;
-    encoder.imu_cb = NULL; 
+    io_config.debounce_delay = DEBOUNCEDELAY;
+    io_config.switch_a_cb = NULL;
+    io_config.switch_b_cb = NULL;
+    io_config.imu_cb = NULL;
 
     // setup as GPIO pins
     PIN_FUNC_SELECT(LDO_SHUTDOWN_MUX, LDO_SHUTDOWN_FUNC);
@@ -44,28 +44,28 @@ void ICACHE_FLASH_ATTR Encoder_InitIO(void)
     gpio_output_set(BIT(LDO_SHUTDOWN), BIT(STATUS_LED), (BIT(LDO_SHUTDOWN)|BIT(STATUS_LED)), (BIT(SWITCH_A)|BIT(SWITCH_B)));
 
     // attach callback function for GPIO interrupts
-    ETS_GPIO_INTR_ATTACH(Encoder_GPIO_Handler, NULL);
+    ETS_GPIO_INTR_ATTACH(IO_GPIO_Handler, NULL);
 
     // setup debounce SW timers
-    os_timer_disarm(&encoder.switch_a_timer);
-    os_timer_disarm(&encoder.switch_b_timer);
-    os_timer_setfn(&encoder.switch_a_timer, (os_timer_func_t *)switchA_debounce, NULL);
-    os_timer_setfn(&encoder.switch_b_timer, (os_timer_func_t *)switchB_debounce, NULL);
+    os_timer_disarm(&io_config.switch_a_timer);
+    os_timer_disarm(&io_config.switch_b_timer);
+    os_timer_setfn(&io_config.switch_a_timer, (os_timer_func_t *)switchA_debounce, NULL);
+    os_timer_setfn(&io_config.switch_b_timer, (os_timer_func_t *)switchB_debounce, NULL);
 
 }
 
-void ICACHE_FLASH_ATTR Encoder_SetSwitchCallback(switch_function_t switch_a_cb, switch_function_t switch_b_cb)
+void ICACHE_FLASH_ATTR IO_SetSwitchCallback(switch_function_t switch_a_cb, switch_function_t switch_b_cb)
 {
-    encoder.switch_a_cb = switch_a_cb;
-    encoder.switch_b_cb = switch_b_cb;
+    io_config.switch_a_cb = switch_a_cb;
+    io_config.switch_b_cb = switch_b_cb;
 }
 
-void ICACHE_FLASH_ATTR Encoder_SetIMUCallback(imu_function_t imu_cb)
+void ICACHE_FLASH_ATTR IO_SetIMUCallback(imu_function_t imu_cb)
 {
-    encoder.imu_cb = imu_cb;
+    io_config.imu_cb = imu_cb;
 }
 
- void ICACHE_FLASH_ATTR Encoder_EnableIOInterrupts(void)
+ void ICACHE_FLASH_ATTR IO_EnableIOInterrupts(void)
  {
      ETS_GPIO_INTR_DISABLE();
      gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
@@ -73,19 +73,19 @@ void ICACHE_FLASH_ATTR Encoder_SetIMUCallback(imu_function_t imu_cb)
      ETS_GPIO_INTR_ENABLE();
  }
 
- void ICACHE_FLASH_ATTR Encoder_DisableIOInterrupts(void)
+ void ICACHE_FLASH_ATTR IO_DisableIOInterrupts(void)
  {
      ETS_GPIO_INTR_DISABLE();
      gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_DISABLE);
      gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_DISABLE);
  }
 
-void ICACHE_FLASH_ATTR Encoder_ShutDown(void)
+void ICACHE_FLASH_ATTR IO_Shutdown(void)
 {
     GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
 }
 
-void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
+void ICACHE_FLASH_ATTR IO_GPIO_Handler(void)
 {
 	uint8_t i;
 	uint32_t gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);						// read interupt status of pins
@@ -106,24 +106,24 @@ void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
                 {
                     if (LSM6DS3_Get_Interrupts() != LSM6DS3_NO_INT)
                     {
-                        if (encoder.imu_cb != NULL)
-                            encoder.imu_cb();
+                        if (io_config.imu_cb != NULL)
+                            io_config.imu_cb();
                         gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);     // re-enable interrupts
 
                     }
                     else
                     {
-                        if (encoder.switch_a_cb != NULL)
-                            encoder.switch_a_cb();
-                        os_timer_arm(&encoder.switch_a_timer, encoder.debounce_delay, 0);
+                        if (io_config.switch_a_cb != NULL)
+                            io_config.switch_a_cb();
+                        os_timer_arm(&io_config.switch_a_timer, io_config.debounce_delay, 0);
                     }
         		    break;
 				}
 				case SWITCH_B:
                 {
-                    if (encoder.switch_b_cb != NULL)
-                        encoder.switch_b_cb();
-                    os_timer_arm(&encoder.switch_b_timer, encoder.debounce_delay, 0);
+                    if (io_config.switch_b_cb != NULL)
+                        io_config.switch_b_cb();
+                    os_timer_arm(&io_config.switch_b_timer, io_config.debounce_delay, 0);
                     break;
 				}
 				default:
@@ -142,7 +142,7 @@ static void ICACHE_FLASH_ATTR switchA_debounce(void)
 	ETS_GPIO_INTR_DISABLE();
 
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
-	os_timer_disarm(&encoder.switch_a_timer);
+	os_timer_disarm(&io_config.switch_a_timer);
 
 	ETS_GPIO_INTR_ENABLE();
 }
@@ -153,7 +153,7 @@ static void ICACHE_FLASH_ATTR switchB_debounce(void)
 	ETS_GPIO_INTR_DISABLE();
 
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
-	os_timer_disarm(&encoder.switch_b_timer);
+	os_timer_disarm(&io_config.switch_b_timer);
 
 	ETS_GPIO_INTR_ENABLE();
 }

+ 4 - 4
include/hal/io.h

@@ -31,11 +31,11 @@ typedef struct {
     switch_function_t switch_b_cb;
     imu_function_t imu_cb;
     uint32_t debounce_delay;
-} Encoder_t;
+} IO_config_t;
 
 
-void ICACHE_FLASH_ATTR Encoder_InitIO(void);
-void ICACHE_FLASH_ATTR Encoder_ShutDown(void);
-void ICACHE_FLASH_ATTR Encoder_Restart(void);
+void ICACHE_FLASH_ATTR IO_InitIO(void);
+void ICACHE_FLASH_ATTR IO_Shutdown(void);
+void ICACHE_FLASH_ATTR IO_Restart(void);
 
 #endif

+ 10 - 2
include/util/ideasX.h → include/interface/ideasX_interface.h

@@ -32,8 +32,6 @@ typedef struct {
   bool ota;
 } ota_t;
 
-
-
 typedef struct{
   hw_ver_t hw_ver;
   fw_ver_t fw_ver;
@@ -46,3 +44,13 @@ typedef struct{
   bssid_t bssid;
   rssi_t rssi;
 } ideasX_topics_t;
+
+typedef struct {
+    uint8_t topic = "ota";
+    uint32_t* function;
+} ideasX_command_ota_t;
+
+typedef struct {
+    uint8_t topic = "shutdown";
+    uint32_t* function;
+} ideasX_command_shutdown_t; 

+ 35 - 0
include/interface/wifi_interface.h

@@ -0,0 +1,35 @@
+#ifndef WIFI_INTERFACE_H_
+#define WIFI_INTERFACE_H_
+#include "os_type.h"
+
+typedef enum {
+	WIFI_TIMEOUT_FAILURE = 0,
+	WIFI_NO_STORED_APS_FAILURE,
+	WIFI_NO_AVAILABLE_APS_FAILURE,
+	WIFI_AP_DISCONNECTED,
+	WIFI_AP_AUTHMODE_CHANGE,
+	WIFI_SCAN_FAILURE
+} wifi_failure_et;
+
+typedef void (* wifi_fail_function_t)(wifi_failure_et);
+typedef void (* wifi_success_function_t)(void);
+
+typedef struct {
+	wifi_success_function_t 	success_cb;
+	wifi_fail_function_t		fail_cb;
+	os_timer_t 					timeout_timer;
+	uint32_t 					timeout_delay;
+	bool						ip_flag;
+	bool 						ap_flag;
+	bool						wifi_process;
+	bool 						ap_found;
+	uint8_t 					wifi_retry;
+} wifi_config_t;
+
+
+void ICACHE_FLASH_ATTR WiFi_Initialize(void);
+void ICACHE_FLASH_ATTR WiFi_Disconnect(void);
+void ICACHE_FLASH_ATTR WiFi_SetCallbacks(wifi_success_function_t, wifi_fail_function_t);
+void ICACHE_FLASH_ATTR WiFi_Connect(uint32_t timeout_delay);
+
+#endif /* USER_WIFI_H_ */

+ 0 - 18
include/util/wifi.h

@@ -1,18 +0,0 @@
-#ifndef USER_WIFI_H_
-#define USER_WIFI_H_
-#include "os_type.h"
-
-typedef struct{
-	bool 	ip_flag;
-	bool 	wifi_flag;
-	uint8_t wifi_count;
-	uint8_t process_count;
-} WIFI_PROCESS_FLAGS;
-
-void ICACHE_FLASH_ATTR wifi_set_station(uint8_t* ssid, uint8_t* pass);
-void ICACHE_FLASH_ATTR disable_wifi_reconnect(void);
-void ICACHE_FLASH_ATTR start_wifi_process(void);
-void ICACHE_FLASH_ATTR show_wifi_config(void);
-
-
-#endif /* USER_WIFI_H_ */

+ 4 - 0
interface/uart_interface.c

@@ -5,6 +5,8 @@
  */
 
 #include "interface/uart_interface.h"
+#include "interface/light_interface.h"
+#include "hal/io.h"
 
 static const char* TAG = "uart_interface.c";
 
@@ -110,6 +112,8 @@ void process_command(uint8_t *str)
     else if (!strcmp(str, "AT+SD"))     // shutdown
     {
         ESP_LOGI(TAG, "Manual Shutdown...\n");
+        StatusLED_Shutdown();
+        IO_Shutdown();
     }
     else if (!strcmp(str, "AT+INFO"))   // print system information
     {

+ 418 - 0
interface/wifi_interface.c

@@ -0,0 +1,418 @@
+/******************************************************************************
+ * 2016 IdeasX v0.3.1 Module Firmware
+ *
+ * File Name: wifi.c
+ * Author: Tyler Berezowsky
+ * Description: This file is going to need alot of help by a C wizard, but it currently works...I think.
+ *
+ *     2016/8/8, v1.0 created this file
+*******************************************************************************/
+#include "interface/wifi_interface.h"
+#include "log/esp_log.h"
+#include "esp_common.h"
+
+static const char* TAG =  "WiFi Interface";
+static wifi_config_t wifi_config;
+static struct bss_info *bss_link, *head_bss_link;
+
+/******************************************************************************
+ * FunctionName : destory_linked_list
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static bool ICACHE_FLASH_ATTR destory_linked_list(void)
+{
+	if(head_bss_link != NULL)
+	{
+		while(head_bss_link != NULL)
+		{
+			bss_link = head_bss_link;
+			head_bss_link = head_bss_link->next.stqe_next;
+			os_free(bss_link);
+		}
+		return TRUE;
+	}
+	else
+	{
+		return FALSE;
+	}
+}
+/******************************************************************************
+ * FunctionName : copy_linked_list
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static struct bss_info* ICACHE_FLASH_ATTR copy_linked_list(struct bss_info *ptr_original_node)
+{
+	struct bss_info *ptr_new_head, *ptr_new_node;
+
+	if(ptr_original_node != NULL)
+	{
+		ptr_new_head = (struct bss_info *)os_malloc(sizeof(struct bss_info));
+		if (ptr_new_head == NULL)
+		{
+			ESP_LOGE(TAG, "no RAM!");
+			return NULL;
+		}
+	}
+	else
+		return NULL;
+
+	os_memcpy(ptr_new_head, ptr_original_node, sizeof(struct bss_info));
+	ptr_original_node = ptr_original_node->next.stqe_next;
+
+	ptr_new_node = ptr_new_head;
+
+	while(ptr_original_node != NULL)
+	{
+		ptr_new_node->next.stqe_next = (struct bss_info *)os_malloc(sizeof(struct bss_info));
+		ptr_new_node = ptr_new_node->next.stqe_next;
+		os_memcpy(ptr_new_node, ptr_original_node, sizeof(struct bss_info));
+		if (ptr_new_node == NULL)
+		{
+			ESP_LOGE(TAG, "No RAM!");
+			while(ptr_new_head != NULL)
+			{
+				ptr_new_node = ptr_new_head;
+				ptr_new_head = ptr_new_head->next.stqe_next;
+				os_free(ptr_new_node);
+			}
+			ESP_LOGE(TAG, "deleted linked list");
+			return NULL;
+		}
+		ptr_original_node = ptr_original_node->next.stqe_next;
+
+	}
+	return ptr_new_head;
+}
+/******************************************************************************
+ * FunctionName : wifi_timeout_cb
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static void ICACHE_FLASH_ATTR wifi_timeout_cb(void)
+{
+	ESP_LOGI(TAG, "wifi connection timed out");
+	wifi_config.wifi_process = false;
+	if (wifi_config.fail_cb != NULL)
+		wifi_config.fail_cb(WIFI_TIMEOUT_FAILURE);
+}
+/******************************************************************************
+ * FunctionName : wifi_event_handler_cb
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static void ICACHE_FLASH_ATTR wifi_event_handler_cb(System_Event_t *evt)
+{
+	switch (evt->event)
+ 	{
+		case EVENT_STAMODE_CONNECTED:
+		{
+			wifi_config.ap_flag = true;
+
+			ESP_LOGD(TAG, "connected to ssid %s, channel %d",
+			evt->event_info.connected.ssid,
+			evt->event_info.connected.channel);
+
+			break;
+		}
+		case EVENT_STAMODE_DISCONNECTED:
+		{
+			wifi_config.ap_flag = false;
+			wifi_config.ip_flag = false;
+
+			ESP_LOGD(TAG, "disconnected from ssid %s, reason %d",
+			evt->event_info.disconnected.ssid,
+			evt->event_info.disconnected.reason);
+
+			if (wifi_config.wifi_process == false)
+			{
+				wifi_station_disconnect();
+				if (wifi_config.fail_cb != NULL)
+					wifi_config.fail_cb(WIFI_AP_DISCONNECTED);
+			}
+
+			break;
+		}
+		case EVENT_STAMODE_AUTHMODE_CHANGE:
+		{
+			wifi_config.ap_flag = false;
+			wifi_config.ip_flag = false;
+
+			ESP_LOGD(TAG, "mode: %d -> %d",
+			evt->event_info.auth_change.old_mode,
+			evt->event_info.auth_change.new_mode);
+
+			if (wifi_config.wifi_process == false)
+			{
+				if (wifi_config.fail_cb != NULL)
+					wifi_config.fail_cb(WIFI_AP_AUTHMODE_CHANGE);
+			}
+
+			break;
+		}
+		case EVENT_STAMODE_GOT_IP:
+		{
+			wifi_config.ip_flag = true;
+			os_timer_disarm(&wifi_config.timeout_timer);
+
+			ESP_LOGD(TAG, "ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
+			IP2STR(&evt->event_info.got_ip.ip),
+			IP2STR(&evt->event_info.got_ip.mask),
+			IP2STR(&evt->event_info.got_ip.gw));
+
+			wifi_config.wifi_process = false;
+			if (wifi_config.success_cb != NULL)
+				wifi_config.success_cb();
+			break;
+		}
+		default:
+		{
+			ESP_LOGD(TAG, "unknown Wi-Fi failure code");
+			break;
+		}
+	}
+}
+/******************************************************************************
+ * FunctionName : wifi_process
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static ICACHE_FLASH_ATTR wifi_process(void)
+{
+	uint8_t ssid[33];
+	uint8_t ap_index;
+	struct station_config stationConfig[5];
+
+	// retrive stations stored in flash
+	uint8_t num = wifi_station_get_ap_info(stationConfig);
+	if (num == 0)
+	{
+		if (wifi_config.fail_cb != NULL)
+			wifi_config.fail_cb(WIFI_NO_STORED_APS_FAILURE);
+		return;
+	}
+
+	// loop through every value in the RSSI sorted linked list
+	ESP_LOGD(TAG, "searching stored access points")
+	while(bss_link != NULL)
+	{
+		// feed SW watch dog timer to prevent rest
+		system_soft_wdt_feed();
+
+		// retrive SSID from RSSI sorted linked  list
+		os_memset(ssid, 0, 33);
+		if (os_strlen(bss_link->ssid) <= 32)
+		{
+			os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
+		}
+		else
+		{
+			os_memcpy(ssid, bss_link->ssid, 32);
+		}
+
+		// search the linked list for an AP stored in the cache which was also
+		// found in the access point scan
+		for (ap_index=0;ap_index<num;ap_index++)
+		{
+			ESP_LOGV(TAG, "comparing aps \"%s\" and \"%s\"", ssid, stationConfig[ap_index].ssid);
+			if (!strcmp(ssid, stationConfig[ap_index].ssid))
+			{
+				ESP_LOGD(TAG, "access point located: %s", ssid);
+				wifi_config.ap_found = true;
+				os_timer_arm(&wifi_config.timeout_timer, wifi_config.timeout_delay, false);
+				wifi_station_ap_change(ap_index);
+				wifi_station_connect();
+				break;
+			}
+		}
+
+		bss_link = bss_link->next.stqe_next;
+
+		// stop looping through RSSI sorted scan results and start connection
+		// timeout timer
+		if (wifi_config.ap_found)
+		{
+			break;
+		}
+	}
+
+	if (!wifi_config.ap_found)
+	{
+		if (wifi_config.fail_cb != NULL)
+			wifi_config.fail_cb(WIFI_NO_AVAILABLE_APS_FAILURE);
+		ESP_LOGD(TAG, "access point not found")
+
+	}
+	destory_linked_list();
+}
+/******************************************************************************
+ * FunctionName : wifi_scan_done
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+static void ICACHE_FLASH_ATTR wifi_scan_done(void *arg, STATUS status)
+{
+
+ 	uint8_t ssid[33];
+ 	uint8_t bssid[6];
+
+ 	if (status == OK)	// if the scan was sucessful
+ 	{
+    	bss_link = (struct bss_info *)arg;
+    	struct bss_info *bss_link_next, *bss_link_curr, *bss_link_prev, *bss_link_head;
+    	uint32_t i,numAP,j;
+
+		// copy linked list because SDK will end up destorying it
+    	bss_link = copy_linked_list(bss_link);
+    	bss_link_head = bss_link;
+
+		// get len of linked list
+		numAP=0;
+		while(bss_link != NULL)
+		{
+			numAP++;
+			bss_link = bss_link->next.stqe_next;
+		}
+
+		ESP_LOGD(TAG, "number of APs found: %d", numAP);
+
+    	bss_link = bss_link_head;	// reset to head
+
+    	// sort linked list by rssi
+    	for(i=0;i<numAP-1;i++)
+    	{
+
+	    	bss_link_prev = NULL;
+	    	bss_link_curr = bss_link;
+	    	bss_link_next = bss_link->next.stqe_next;
+
+	 		for(j=0; j<numAP-1-i; j++)
+	 		{
+	 			system_soft_wdt_feed();
+				if (bss_link_next->rssi > bss_link_curr->rssi) {
+					if (bss_link_prev == NULL) {
+						// swap node position and head position
+						bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next;
+						bss_link_next->next.stqe_next = bss_link_curr;
+						bss_link = bss_link_next; // store new head
+
+						bss_link_prev = bss_link_next;
+						bss_link_next = bss_link_curr->next.stqe_next;
+					} else {
+						// swap node positions
+						bss_link_prev->next.stqe_next = bss_link_next;
+						bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next; // could be null
+						bss_link_next->next.stqe_next = bss_link_curr;
+
+						bss_link_prev = bss_link_next;
+						bss_link_next = bss_link_curr->next.stqe_next;
+					}
+				} else {
+					// move along like nothing happened
+					bss_link_prev = bss_link_curr;
+					bss_link_curr = bss_link_next;
+					bss_link_next = bss_link_next->next.stqe_next;
+				}
+			}
+		}
+
+		bss_link_head = bss_link; // store head
+
+		// print sorted list of stations
+    	while (bss_link != NULL) {
+      		os_memset(ssid, 0, 33);
+      		os_memset(bssid, 0, 6);
+
+      		if (os_strlen(bss_link->ssid) <= 32) {
+        		os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
+      		} else {
+        		os_memcpy(ssid, bss_link->ssid, 32);
+      		}
+
+      		ESP_LOGD(TAG, "(%d,\"%s\",%x, %d)", bss_link->authmode, ssid, bssid, bss_link->rssi);
+      		bss_link = bss_link->next.stqe_next;
+  		}
+  		bss_link = bss_link_head;		// reset to head
+  		head_bss_link = bss_link_head;	// ahhh, this is lazy.
+
+		wifi_set_event_handler_cb(wifi_event_handler_cb);
+  		wifi_process();
+  	}
+  	else // if scan was unsuccessful
+  	{
+  		ESP_LOGD(TAG, "scan failed");
+		if (wifi_config.fail_cb != NULL)
+			wifi_config.fail_cb(WIFI_SCAN_FAILURE);
+		wifi_config.wifi_process = false;
+  	}
+}
+/******************************************************************************
+ * FunctionName : WiFi_Initialize
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+void ICACHE_FLASH_ATTR WiFi_Initialize(void)
+{
+	wifi_set_opmode(STATION_MODE);
+	wifi_station_ap_number_set(5);
+	wifi_station_disconnect();
+	wifi_station_set_auto_connect(false);
+
+	wifi_config.fail_cb = NULL;
+	wifi_config.success_cb = NULL;
+}
+/******************************************************************************
+ * FunctionName : WiFi_Disconnect
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+void ICACHE_FLASH_ATTR WiFi_Disconnect(void)
+{
+	wifi_config.ip_flag = false;
+	wifi_config.ap_flag = false;
+	wifi_station_disconnect();
+}
+/******************************************************************************
+ * FunctionName : WiFi_SetCallbacks
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+void ICACHE_FLASH_ATTR WiFi_SetCallbacks(wifi_success_function_t success_cb, wifi_fail_function_t fail_cb)
+{
+	wifi_config.success_cb = success_cb;
+	wifi_config.fail_cb = fail_cb;
+}
+/******************************************************************************
+ * FunctionName : WiFi_Connect
+ * Description  :
+ * Parameters   :
+ * Returns      :
+*******************************************************************************/
+void ICACHE_FLASH_ATTR WiFi_Connect(uint32_t timeout_delay)
+{
+	wifi_config.timeout_delay = timeout_delay;
+	wifi_config.ip_flag = false;
+	wifi_config.ap_flag = false;
+	wifi_config.ap_found = false;
+	wifi_config.wifi_process = true;
+
+	wifi_set_event_handler_cb(NULL);
+
+	os_timer_disarm(&wifi_config.timeout_timer);
+	os_timer_setfn(&wifi_config.timeout_timer, (os_timer_func_t *)wifi_timeout_cb, NULL);
+
+	// check for stored APs
+	// if none call no_credentials_cb(), shutdown WIFI, return
+	ESP_LOGI(TAG, "starting Wi-Fi scan");
+	wifi_station_scan(NULL, wifi_scan_done);				// start Wi-Fi scan.
+}

+ 61 - 28
user/user_main.c

@@ -41,39 +41,66 @@
 #include "hal/lps25hb.h"
 #include "hal/ws2812.h"
 
+#include "interface/wifi_interface.h"
+
 #include "user_config.h"
 
 static const char* TAG = "main.c";
 
-// void wifi_handle_event_cb(System_Event_t *evt)
-// {
-//     switch(evt->event)
-//     {
-//       case EVENT_STAMODE_CONNECTED:
-//         break;
-//       case EVENT_STAMODE_DISCONNECTED:
-//         break;
-//       case EVENT_STAMODE_AUTHMODE_CHANGE:
-//         break;
-//       case EVENT_STAMODE_GOT_IP:
-//         break;
-//       case EVENT_SOFTAPMODE_PROBEREQRECVED:
-//         break;
-//       case EVENT_SOFTAPMODE_STACONNECTED:
-//         break;
-//       case EVENT_SOFTAPMODE_STADISCONNECTED:
-//         break;
-//       default:
-//         break;
-//     }
-// }
+
+void ICACHE_FLASH_ATTR wifi_success_cb(void)
+{
+    ESP_LOGV(TAG, "Wi-Fi Success");
+}
+
+void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
+{
+    ESP_LOGV(TAG, "Wi-Fi failure")
+    switch (failure_code)
+    {
+        case WIFI_AP_DISCONNECTED:
+        {
+            ESP_LOGI(TAG, "Wi-Fi connection lost");
+            break;
+        }
+        case WIFI_AP_AUTHMODE_CHANGE:
+        {
+            ESP_LOGI(TAG, "Wi-Fi credential failure or change");
+            break;
+        }
+        case WIFI_NO_STORED_APS_FAILURE:
+        {
+            ESP_LOGI(TAG, "no stored Wi-Fi credentials");
+            break;
+        }
+        case WIFI_NO_AVAILABLE_APS_FAILURE:
+        {
+            ESP_LOGI(TAG, "no Wi-Fi available");
+            break;
+        }
+        case WIFI_TIMEOUT_FAILURE:
+        {
+            ESP_LOGI(TAG, "Wi-Fi timeout");
+            break;
+        }
+        case WIFI_SCAN_FAILURE:
+        {
+            ESP_LOGW(TAG, "Wi-Fi scan failure");
+            break;
+        }
+        default:
+        {
+            ESP_LOGW(TAG, "unknown Wi-Fi failure code");
+        }
+    }
+}
 
  void ICACHE_FLASH_ATTR app_init(void)
 {
     uart_init(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1);
 
 
-    ESP_LOGI(TAG, "LSM6DS3, MAX17043, and LPS25HB Testing...");
+    ESP_LOGD(TAG, "LSM6DS3, MAX17043, and LPS25HB Testing...");
     //while(true)
     {
         LSM6DS3_Enable_I2C_Bridge(1);
@@ -99,14 +126,20 @@ static const char* TAG = "main.c";
     }
 
 
-    ESP_LOGI(TAG, "Encoder IO Testing...")
+    ESP_LOGI(TAG, "IO Testing...")
     {
-        Encoder_InitIO();
+        IO_InitIO();
         //Encoder_SetSwitchCallback(os_printf("SWITCH_A"), os_printf("SWITCH_B"));
         //Encoder_SetIMUCallback(os_printf("IMU"));
         LSM6DS3_EnableMotion();
-        Encoder_EnableIOInterrupts();
+        IO_EnableIOInterrupts();
+
+    }
 
+    ESP_LOGI(TAG, "WiFi Interface Testing...")
+     {
+         WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb);
+         WiFi_Connect(30*1000); // thirty seconds
     }
 
 
@@ -114,7 +147,7 @@ static const char* TAG = "main.c";
 
 void user_init(void)
 {
-    wifi_set_opmode(STATION_MODE);
-    wifi_station_ap_number_set(5);
+    WiFi_Initialize();
     system_init_done_cb(app_init);
+    //WiFi_Initialize();
 }

+ 0 - 485
util/wifi.c

@@ -1,485 +0,0 @@
-/******************************************************************************
- * 2016 IdeasX v0.3.1 Module Firmware
- *
- * File Name: wifi.c
- * Author: Tyler Berezowsky
- * Description: This file is going to need alot of help by a C wizard, but it currently works...I think.
- *
- *     2016/8/8, v1.0 created this file
-*******************************************************************************/
-#include "util/wifi.h"
-#include "esp_common.h"
-
-#define WIFI_TIMEOUT 30*1000 		// time waited before swiping left in seconds
-#define WIFI_RECONNECT_LIMIT 5		// number of times connection to AP is attempted before moving on
-
-MQTT_Client mqttClient;	//  setup in user_main.c
-
-static struct station_config stationConf;
-static WIFI_PROCESS_FLAGS wifi_process_flags;
-static ETSTimer wifi_process_timer;
-static struct bss_info  *bss_link, *head_bss_link;
-
-static void ICACHE_FLASH_ATTR wifi_process(void);
-static void ICACHE_FLASH_ATTR wifi_process_timer_cb(void);
-
-
-/******************************************************************************
- * FunctionName : destory_linked_list
- * Description  :
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static bool ICACHE_FLASH_ATTR destory_linked_list(void)
-{
-	if(head_bss_link != NULL)
-	{
-		while(head_bss_link != NULL)
-		{
-			bss_link = head_bss_link;
-			head_bss_link = head_bss_link->next.stqe_next;
-			os_free(bss_link);
-		}
-		return TRUE;
-	}
-	else
-	{
-		return FALSE;
-	}
-}
-/******************************************************************************
- * FunctionName : copy_linked_list
- * Description  :
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static struct bss_info* ICACHE_FLASH_ATTR copy_linked_list(struct bss_info *ptr_original_node)
-{
-	struct bss_info *ptr_new_head, *ptr_new_node;
-
-	if(ptr_original_node != NULL)
-	{
-		ptr_new_head = (struct bss_info *)os_malloc(sizeof(struct bss_info));
-		if (ptr_new_head == NULL)
-		{
-			os_printf("WIFI PROCESS: No RAM!\r\n");
-			return NULL;
-		}
-	}
-	else
-		return NULL;
-
-	os_memcpy(ptr_new_head, ptr_original_node, sizeof(struct bss_info));
-	ptr_original_node = ptr_original_node->next.stqe_next;
-
-	ptr_new_node = ptr_new_head;
-
-	while(ptr_original_node != NULL)
-	{
-		ptr_new_node->next.stqe_next = (struct bss_info *)os_malloc(sizeof(struct bss_info));
-		ptr_new_node = ptr_new_node->next.stqe_next;
-		os_memcpy(ptr_new_node, ptr_original_node, sizeof(struct bss_info));
-		if (ptr_new_node == NULL)
-		{
-			os_printf("WIFI PROCESS: No RAM!\r\n");
-			while(ptr_new_head != NULL)
-			{
-				ptr_new_node = ptr_new_head;
-				ptr_new_head = ptr_new_head->next.stqe_next;
-				os_free(ptr_new_node);
-			}
-			os_printf("WIFI PROCESS: Cleaned up list\r\n");
-			return NULL;
-		}
-		ptr_original_node = ptr_original_node->next.stqe_next;
-
-	}
-	return ptr_new_head;
-}
-/******************************************************************************
- * FunctionName : wifi_set_station
- * Description  :
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR wifi_set_station(uint8_t* ssid, uint8_t* pass)
-{
-	bss_link = NULL;
-
-	os_sprintf(stationConf.ssid, "%s", ssid);			// load ssid into stationConf.ssid
-	os_sprintf(stationConf.password, "%s", pass);		// load pass into stationConf.password
-
-	wifi_station_set_config_current(&stationConf);				// connect to AP specified in stationConf
-	wifi_station_set_auto_connect(TRUE);
-	wifi_station_connect();
-}
-/******************************************************************************
- * FunctionName : disable_wifi_reconnect
- * Description  :
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR disable_wifi_reconnect(void)
-{
-	wifi_station_disconnect();
-	wifi_station_set_auto_connect(FALSE);
-}
-/******************************************************************************
- * FunctionName : show_wifi_config
- * Description  : print current wifi configurations
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR show_wifi_config(void)
-{
-	uint8_t i, j;
-	j = sysCfg.registered_stations;
-
-	os_printf("Number of APs stored: %d\r\n", j);
-	for (i=0;i<j;i++)
-		os_printf("AP: %d\n\tSSID: %s\n\tPASS: %s\n", i, sysCfg.sta_ssid[i], sysCfg.sta_pwd[i]);
-	/* This is not accurate. If the module is not connected to an AP it should display unconnected for
-		current station
-	*/
-	i = sysCfg.current_station;
-	j = wifi_station_get_connect_status();
-	os_printf("Current AP: %d\r\nWiFi Status %d\r\n", i, j);
-}
-/******************************************************************************
- * FunctionName : wifi_process_timer_cb
- * Description  :
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static void ICACHE_FLASH_ATTR wifi_process_timer_cb()
-{
-	os_printf("WIFI PROCESS: Wi-Fi connection timed out.\r\n");
-	wifi_process_flags.wifi_flag = FALSE;
-
-
-	wifi_station_set_auto_connect(FALSE);
-	wifi_station_disconnect();
-	wifi_process();
-}
-/******************************************************************************
- * FunctionName : wifi_process
- * Description  : Looks for AP stored in bss_link
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static void ICACHE_FLASH_ATTR wifi_process(void)
-{
-	uint8_t ssid[33];
-	uint8_t i;
-
-	os_timer_disarm(&wifi_process_timer);								// disarm timeout timer
-
-	if ((wifi_process_flags.wifi_flag == FALSE) && (bss_link != NULL)) // if AP connection failed and bss_link is not empty
-	{
-		while(bss_link != NULL)	// do this until bss_link is empty or break
-		{
-
-			// ssid could be missing null character
-			os_memset(ssid, 0, 33);
-			if (os_strlen(bss_link->ssid) <= 32)
-			{
-				os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
-			}
-			else
-			{
-				os_memcpy(ssid, bss_link->ssid, 32);
-			}
-
-			// compare current ssid from bss_link to registered stations
-			for(i=0;i<sysCfg.registered_stations;i++)
-			{
-				//os_printf("Comparing %s and %s\r\n", ssid, sysCfg.sta_ssid[i]);
-				if (!strcmp(ssid, sysCfg.sta_ssid[i]))
-				{
-					wifi_process_flags.wifi_flag = TRUE; 	// set connect attempt flag
-					wifi_process_flags.wifi_count = 0; 		// rst connect attempt count
-
-					os_sprintf(stationConf.ssid, "%s", sysCfg.sta_ssid[i]);
-					os_sprintf(stationConf.password, "%s", sysCfg.sta_pwd[i]);	// if AUTH_OPEN pwd should be NULL
-					sysCfg.current_station = i;									// store current station number for sysCfg save and UART operations
-					wifi_station_set_config_current(&stationConf); 				// load stationConf into SDK, but don't save in SDK flash
-					wifi_station_set_auto_connect(TRUE);
-					wifi_station_connect();
-
-					bss_link = bss_link->next.stqe_next; 						// inc to next station in case process fails
-
-					//set wifi_process_timer for timeout.
-					os_timer_setfn(&wifi_process_timer, (os_timer_func_t *)wifi_process_timer_cb, NULL);
-					os_timer_arm(&wifi_process_timer, WIFI_TIMEOUT, 0); 		// arm timeout timer
-					break; // break for loop
-				}
-
-			}
-
-			// break while if station found, otherwise look for another station.
-			if (wifi_process_flags.wifi_flag == TRUE)
-			{
-				break;	// break while loop
-			}
-			else
-			{
-				bss_link = bss_link->next.stqe_next;
-			}
-		}
-
-	}
-	if ((wifi_process_flags.wifi_flag == FALSE) && (bss_link == NULL))
-	{
-		// this is called twice...which could be a problem.
-		sysCfg.module_state.searching = FALSE;
-		sysCfg.module_state.asleep = FALSE;
-		destory_linked_list();
-		os_printf("WIFI PROCESS: Station list removed\r\n");
-		os_printf("WIFI PROCESS: No Wi-Fi APs available.\r\n");
-		// try again in a second....three times...if motion flag is set keep trying otherwise you should goto sleep.
-
-		if (lsm6ds3_read_motion())		// change this to a poll on the LSM6DS3 line.
-			start_wifi_process();
-		else
-		{
-			GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 0xFFFFFFFF);	// clear pending interrupts
-			sysCfg.module_state.asleep = TRUE;
-			ETS_GPIO_INTR_ENABLE(); 								// enable interrupts to permit PB and LSM6DS3 to wake ESP from sleep.
-			/*
-
-			 *	The ESP8266 should be placed into light sleep or modem sleep here
-
-			 */
-		}
-	}
-
-}
-/******************************************************************************
- * FunctionName : wifi_handle_event_cb
- * Description  : callback function for wifi events.
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static void ICACHE_FLASH_ATTR wifi_handle_event_cb(System_Event_t *evt)
-{
-	switch (evt->event)
- 	{
-		case EVENT_STAMODE_CONNECTED:
-		{
-			wifi_process_flags.wifi_count = 0;	// rst wifi connection cnt
-
-			os_printf("WIFI PROCESS: connect to ssid %s, channel %d\n",
-			evt->event_info.connected.ssid,
-			evt->event_info.connected.channel);
-			break;
-		}
-		case EVENT_STAMODE_DISCONNECTED:
-		{
-			/*
-				There is an issue where if the module is connected to an AP and then loses it, it continously scans for the
-				last access point it attempted to connect to.
-			*/
-			sysCfg.module_state.connected = FALSE;	// set global connected flag false
-			sysCfg.module_state.searching = TRUE;
-			/*
-				The only way to cancel the auto-connect functionaity of the SDK's OS is to
-				send the command wifi_station_disconnect(), this ALWAYS calles the wifi event
-				handler. Checking for this flag lets us ignore it.
-			*/
-			if (wifi_process_flags.wifi_flag == TRUE)
-			{
-				MQTT_Disconnect(&mqttClient);
-				wifi_process_flags.wifi_count++;
-
-				os_printf("WIFI PROCESS: disconnect from ssid %s, reason %d, attempt %d/%d\r\n",
-				evt->event_info.disconnected.ssid,
-				evt->event_info.disconnected.reason,
-				wifi_process_flags.wifi_count,
-				WIFI_RECONNECT_LIMIT);
-
-				if (wifi_process_flags.wifi_count >= WIFI_RECONNECT_LIMIT)	// allow WIFI_RECONNECT_LIMIT for reconnect and 1st attempts
-				{
-					if (wifi_process_flags.ip_flag == TRUE)	// if was connected, but then lost connection
-					{
-						start_wifi_process();				// rst apd mode process, flags will automatically be reset
-					}
-					else									// if never got a connection finish looking at all the available stations.
-					{
-						wifi_process_flags.wifi_flag = FALSE;
-						wifi_station_set_auto_connect(FALSE);
-						wifi_station_disconnect();
-						wifi_process();
-					}
-				}
-			}
-			break;
-		}
-		case EVENT_STAMODE_AUTHMODE_CHANGE:
-		{
-			os_printf("WIFI PROCESS: mode: %d -> %d\n",
-			evt->event_info.auth_change.old_mode,
-			evt->event_info.auth_change.new_mode);
-			break;
-		}
-		case EVENT_STAMODE_GOT_IP:
-		{
-			os_timer_disarm(&wifi_process_timer);								// disarm timeout timer
-			wifi_process_flags.ip_flag = TRUE;
-
-			sysCfg.module_state.connected = TRUE;								// set global connected flag
-			sysCfg.module_state.searching = FALSE;								// set global searching flag false
-
-			os_printf("WIFI PROCESS: ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
-			IP2STR(&evt->event_info.got_ip.ip),
-			IP2STR(&evt->event_info.got_ip.mask),
-			IP2STR(&evt->event_info.got_ip.gw));
-
-			destory_linked_list();
-			os_printf("\r\nWIFI PROCESS: Station list removed\r\n");
-
-			lsm6ds3_motion_disable(); 											// disable LSM6DS3 significant motion functionality
-			MQTT_Connect(&mqttClient); 											// start MQTT client
-
-			break;
-		}
-		default:
-		{
-			os_printf("WIFI PROCESS: Oh, crap. I have no idea what is going on,\r\n");
-			break;
-		}
-	}
-}
-/******************************************************************************
- * FunctionName : wifi_station_scan_done
- * Description  : Cb function of scan, organizes a linked list of stations
- 	from scan function. Make sure to free linked list when done.
- * Parameters   :
- * Returns      :
-*******************************************************************************/
-static void ICACHE_FLASH_ATTR wifi_station_scan_done(void *arg, STATUS status)
-{
-
- 	uint8_t ssid[33];
- 	uint8_t bssid[6];
-
- 	if (status == OK)	// if the scan was sucessful
- 	{
-    	bss_link = (struct bss_info *)arg;
-    	struct bss_info *bss_link_next, *bss_link_curr, *bss_link_prev, *bss_link_head;
-    	uint32_t i,numAP,j;
-
-    	bss_link = copy_linked_list(bss_link);	// SDK provived linked list will be destroyed automatically
-
-    	bss_link_head = bss_link;				// store head for traversing list
-
-		// get len of linked list
-		numAP=0;
-		while(bss_link != NULL) {
-			numAP++;
-			bss_link = bss_link->next.stqe_next;
-		}
-		os_printf("Number of APs found: %d\r\n", numAP);
-
-    	bss_link = bss_link_head;	// reset to head
-
-    	// sort linked list by rssi
-    	for(i=0;i<numAP-1;i++)
-    	{
-
-	    	bss_link_prev = NULL;
-	    	bss_link_curr = bss_link;
-	    	bss_link_next = bss_link->next.stqe_next;
-
-	 		for(j=0; j<numAP-1-i; j++)
-	 		{
-	 			system_soft_wdt_feed(); 												// reset sw watchdog timer
-				if (bss_link_next->rssi > bss_link_curr->rssi) {
-					if (bss_link_prev == NULL) {
-						// swap node position and head position
-						bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next;
-						bss_link_next->next.stqe_next = bss_link_curr;
-						bss_link = bss_link_next; // store new head
-
-						bss_link_prev = bss_link_next;
-						bss_link_next = bss_link_curr->next.stqe_next;
-					} else {
-						// swap node positions
-						bss_link_prev->next.stqe_next = bss_link_next;
-						bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next; // could be null
-						bss_link_next->next.stqe_next = bss_link_curr;
-
-						bss_link_prev = bss_link_next;
-						bss_link_next = bss_link_curr->next.stqe_next;
-					}
-				} else {
-					// move along like nothing happened
-					bss_link_prev = bss_link_curr;
-					bss_link_curr = bss_link_next;
-					bss_link_next = bss_link_next->next.stqe_next;
-				}
-			}
-		}
-
-		bss_link_head = bss_link; // store head
-
-		// print sorted list of stations
-    	while (bss_link != NULL) {
-      		os_memset(ssid, 0, 33);
-      		os_memset(bssid, 0, 6);
-
-      		if (os_strlen(bss_link->ssid) <= 32) {
-        		os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
-      		} else {
-        		os_memcpy(ssid, bss_link->ssid, 32);
-      		}
-
-      		os_printf("WiFi Scan: (%d,\"%s\",%x, %d)\n", bss_link->authmode, ssid, bssid, bss_link->rssi);
-      		bss_link = bss_link->next.stqe_next;
-  		}
-  		bss_link = bss_link_head;		// reset to head
-  		head_bss_link = bss_link_head;	// ahhh, this is lazy.
-
-  		os_printf("Activating event handler\r\n");
-		wifi_set_event_handler_cb(wifi_handle_event_cb);
-  		os_printf("Analysis scan results\r\n");
-  		wifi_process();
-  	}
-  	else // if scan was unsuccessful
-  	{
-  		sysCfg.module_state.searching = FALSE;
-  		os_printf("Scan Failed\r\n");
-  		GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 0xFFFFFFFF);	// clear pending interrupts
-		sysCfg.module_state.asleep = TRUE;
-		ETS_GPIO_INTR_ENABLE(); 								// enable interrupts to permit PB and LSM6DS3 to wake ESP from sleep.
-
-  	}
-}
-/******************************************************************************
- * FunctionName : start_wifi_process
- * Description  : scans for available AP and attempts to connect to the AP with
- 	the highest RSSI.
- * Parameters   :  restart - resets wifi_process_count
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR start_wifi_process(void)
-{
-
-	ETS_GPIO_INTR_DISABLE(); 					// disable I/O interrupts
-	lsm6ds3_motion_enable();					// enable LSM6DS3 Significant Motion Process
-
-	wifi_process_flags.process_count = 0;
-	wifi_process_flags.wifi_flag = FALSE; 		// clear wifi_process_flags
-	wifi_process_flags.ip_flag = FALSE;
-	wifi_process_flags.wifi_count = 0;
-
-	sysCfg.module_state.searching = TRUE;		// setup global flags for searching
-	sysCfg.module_state.connected = FALSE;
-	sysCfg.module_state.broker = FALSE;
-
-	wifi_station_set_auto_connect(FALSE);	// disable wifi auto connect
-	wifi_station_ap_number_set(0);			// don't store Wi-Fi configurations in SDK flash
-
-	os_printf("Start Wi-Fi Scan\r\n");
-	wifi_set_opmode(STATION_MODE);
-	wifi_station_scan(NULL, wifi_station_scan_done);				// start Wi-Fi scan.
-}