/****************************************************************************** * FunctionName : user_init * Description : entry of user application, init user function here * Parameters : none * Returns : none *******************************************************************************/ void user_init(void) { UART_WaitTxFifoEmpty(0,50000); uart_init(74880,74880); user_DispAppInfo(); wifi_set_opmode(STATIONAP_MODE); #if ESP_MESH_SUPPORT wifi_station_set_auto_connect(0); wifi_station_disconnect(); #else wifi_station_set_auto_connect(1); #endif os_printf("SDK version:%s\n", system_get_sdk_version()); wifi_station_ap_number_set(AP_CACHE_NUMBER); system_init_done_cb(light_main_flow); }
/****************************************************************************** * FunctionName : user_esp_platform_init * Description : device parame init based on espressif platform * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_esp_platform_init(void) { os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); os_printf("IOT VERSION = %s\n",iot_version); system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); struct rst_info *rtc_info = system_get_rst_info(); os_printf("reset reason: %x\n", rtc_info->reason); if (rtc_info->reason == REASON_WDT_RST || rtc_info->reason == REASON_EXCEPTION_RST || rtc_info->reason == REASON_SOFT_WDT_RST) { if (rtc_info->reason == REASON_EXCEPTION_RST) { os_printf("Fatal exception (%d):\n", rtc_info->exccause); } os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); } /***add by tzx for saving ip_info to avoid dhcp_client start****/ struct dhcp_client_info dhcp_info; struct ip_info sta_info; system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); if(dhcp_info.flag == 0x01 ) { if (true == wifi_station_dhcpc_status()) { wifi_station_dhcpc_stop(); } sta_info.ip = dhcp_info.ip_addr; sta_info.gw = dhcp_info.gw; sta_info.netmask = dhcp_info.netmask; if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { os_printf("set default ip wrong\n"); } } os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); #if AP_CACHE wifi_station_ap_number_set(AP_CACHE_NUMBER); #endif #if 0 { char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; struct ip_info info; wifi_set_macaddr(SOFTAP_IF, sofap_mac); wifi_set_macaddr(STATION_IF, sta_mac); IP4_ADDR(&info.ip, 192, 168, 3, 200); IP4_ADDR(&info.gw, 192, 168, 3, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(STATION_IF, &info); IP4_ADDR(&info.ip, 10, 10, 10, 1); IP4_ADDR(&info.gw, 10, 10, 10, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &info); } #endif if (esp_param.activeflag != 1) { #ifdef SOFTAP_ENCRYPT struct softap_config config; char password[33]; char macaddr[6]; wifi_softap_get_config(&config); wifi_get_macaddr(SOFTAP_IF, macaddr); os_memset(config.password, 0, sizeof(config.password)); os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); os_memcpy(config.password, password, os_strlen(password)); config.authmode = AUTH_WPA_WPA2_PSK; wifi_softap_set_config(&config); #endif wifi_station_set_hostname( HOST_NAME ); wifi_set_opmode(STATION_MODE); } #if SENSOR_DEVICE user_sensor_init(esp_param.activeflag); #endif #if 0 if (wifi_get_opmode() != SOFTAP_MODE) { os_timer_disarm(&client_timer); os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); os_timer_arm(&client_timer, 100, 0); } // 2015-12-27 single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER); keys.key_num = 1; keys.single_key = single_key; key_init(&keys); #endif }
/*The main working flow of this demo: a. start mesh function, searching for existing mesh network b. if there is no mesh network, search AP cache to connect to a recorded router. c. If we still failed to establish a connection, start ESP-TOUCH wait for configure. d. If ESP-TOUCH time out, re-search mesh network and AP CACHE. e. During the whole procedure,we can control and configure the light via a restful webserver. f. ESP-NOW is the recently released function to control the light without any WiFi connection.You can find it in app_switch */ void ICACHE_FLASH_ATTR light_main_flow() { UART_WaitTxFifoEmpty(0,50000); uart_init(74880,74880); user_DispAppInfo(); wifi_set_opmode(STATIONAP_MODE); #if ESP_MESH_SUPPORT wifi_station_set_auto_connect(0); wifi_station_disconnect(); #else wifi_station_set_auto_connect(1); #endif os_printf("SDK version:%s\n", system_get_sdk_version()); #if AP_CACHE wifi_station_ap_number_set(AP_CACHE_NUMBER); #endif #if ESP_NOW_SUPPORT /*We have added esp-now feature in the light project */ /*So that the lights in a certain MAC group can be easily controlled by an ESP-NOW controller*/ /*The sample code is in APP_CONTROLLER/APP_SWITCH*/ sp_MacInit();//csc add button Mac add and delet light_EspnowInit(); #endif #if 1 #if ESP_PLATFORM /*Initialization of the peripheral drivers*/ /*For light demo , it is user_light_init();*/ /* Also check whether assigned ip addr by the router.If so, connect to ESP-server */ user_esp_platform_init_peripheral(); disp_heap(5); #endif /*Establish a udp socket to receive local device detect info.*/ /*Listen to the port 1025, as well as udp broadcast. /*If receive a string of device_find_request, it reply its IP address and MAC.*/ user_devicefind_init(); disp_heap(6); /*Establish a TCP server for http(with JSON) POST or GET command to communicate with the device.*/ /*You can find the command in "2B-SDK-Espressif IoT Demo.pdf" to see the details.*/ /*the JSON command for curl is like:*/ /*3 Channel mode: curl -X POST -H "Content-Type:application/json" -d "{\"period\":1000,\"rgb\":{\"red\":16000,\"green\":16000,\"blue\":16000}}" http://192.168.4.1/config?command=light */ /*5 Channel mode: curl -X POST -H "Content-Type:application/json" -d "{\"period\":1000,\"rgb\":{\"red\":16000,\"green\":16000,\"blue\":16000,\"cwhite\":3000,\"wwhite\",3000}}" http://192.168.4.1/config?command=light */ /***********NOTE!!**************/ /*in MESH mode, you need to add "sip","sport" and "mdev_mac" fields to send command to the desired device*/ /*see details in MESH documentation*/ /*MESH INTERFACE IS AT PORT 8000*/ #if ESP_WEB_SUPPORT //Initialize DNS server for captive portal captdnsInit(); //Initialize espfs containing static webpages espFsInit((void*)(webpages_espfs_start)); //Initialize webserver httpdInit(builtInUrls, SERVER_PORT); //user_webserver_init(SERVER_PORT); #else #ifdef SERVER_SSL_ENABLE user_webserver_init(SERVER_SSL_PORT); #else user_webserver_init(SERVER_PORT); #endif #endif #endif //In debug mode, if you restart the light within 2 seconds, it will get into softap mode and wait for local upgrading firmware. //Restart again, it will clear the system param and set to default status. #if ESP_DEBUG_MODE extern struct esp_platform_saved_param esp_param; if(esp_param.reset_flg == MODE_APMODE){ os_printf("==================\r\n"); os_printf("RESET FLG==2,STATIONAP_MODE \r\n"); os_printf("==================\r\n"); #if ESP_MESH_SUPPORT user_DeviceFindRespSet(false); #endif struct softap_config config_softap; char ssid[33]={0}; wifi_softap_get_config(&config_softap); os_memset(config_softap.password, 0, sizeof(config_softap.password)); os_memset(config_softap.ssid, 0, sizeof(config_softap.ssid)); os_sprintf(ssid,"ESP_%06X",system_get_chip_id()); os_memcpy(config_softap.ssid, ssid, os_strlen(ssid)); config_softap.ssid_len = os_strlen(ssid); config_softap.authmode = AUTH_OPEN; wifi_softap_set_config(&config_softap); os_printf("SET STATION-AP MODE\r\n"); wifi_set_opmode(STATIONAP_MODE); user_esp_platform_set_reset_flg(MODE_RESET); #if ESP_WEB_SUPPORT os_printf("WEBSERVER INIT..."); //Initialize DNS server for captive portal captdnsInit(); //Initialize espfs containing static webpages espFsInit((void*)(webpages_espfs_start)); //Initialize webserver httpdInit(builtInUrls, SERVER_PORT); #endif os_timer_disarm(&reset_flg_t); os_timer_setfn(&reset_flg_t,user_esp_platform_set_reset_flg,MODE_NORMAL); os_timer_arm(&reset_flg_t,2000,0); user_light_set_duty(0, LIGHT_RED); user_light_set_duty(0, LIGHT_GREEN); user_light_set_duty(0, LIGHT_BLUE); user_light_set_duty(22222, LIGHT_WARM_WHITE); user_light_set_duty(22222, LIGHT_COLD_WHITE); os_delay_us(5000); light_ShowDevLevel(5); return; } #if ESP_RESET_DEBUG_EN else if(esp_param.reset_flg == MODE_RESET){ os_printf("==================\r\n"); os_printf("RESET FLG==1,RESET IN 200 MS \r\n"); os_printf("==================\r\n"); user_esp_platform_set_reset_flg(MODE_APMODE); os_timer_disarm(&reset_flg_t); os_timer_setfn(&reset_flg_t,user_esp_platform_reset_default,0); os_timer_arm(&reset_flg_t,200,0); } else{ os_printf("==================\r\n"); os_printf("RESET FLG==0,NORMAL MODE \r\n"); os_printf("==================\r\n"); user_esp_platform_set_reset_flg(MODE_APMODE); os_timer_disarm(&reset_flg_t); os_timer_setfn(&reset_flg_t,user_esp_platform_set_reset_flg,0); os_timer_arm(&reset_flg_t,2000,0); } #endif #endif #if ESP_MESH_SUPPORT /*initialize mesh network. 1. search for existing mesh. 2. if failed , try connecting recorded router. */ user_MeshParamInit(); user_MeshStart(); #elif ESP_TOUCH_SUPPORT esptouch_FlowStart(); #endif }