commit 973efb93b3288fe20794901752319e83c56c40cf Author: Zip <76966589@qq.com> Date: Sat Jan 5 16:57:36 2019 +0800 第一次提交 add:wifi EasyLink 功能 add:wifi连接 add:wifi数据保存 diff --git a/README.md b/README.md new file mode 100644 index 0000000..7420ba5 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +#TC1 +This is TC1 program. \ No newline at end of file diff --git a/TC1/TC1.mk b/TC1/TC1.mk new file mode 100644 index 0000000..cf9cf98 --- /dev/null +++ b/TC1/TC1.mk @@ -0,0 +1,29 @@ +############################################################################### +# +# The MIT License +# Copyright (c) 2016 MXCHIP Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is furnished +# to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +############################################################################### + + +NAME := App_TC1 + +$(NAME)_SOURCES := main.c\ + wifi.c\ + key.c \ No newline at end of file diff --git a/TC1/key.c b/TC1/key.c new file mode 100644 index 0000000..5b37e03 --- /dev/null +++ b/TC1/key.c @@ -0,0 +1,36 @@ + +#define os_log(format, ...) custom_log("KEY", format, ##__VA_ARGS__) + +#include "main.h" +//#include "key.h" + +void led(char x) +{ + if(x==-1)MicoGpioOutputTrigger(Led); + else if(x) MicoGpioOutputHigh(Led); + else MicoGpioOutputLow(Led); +} + +static void key_long_press(void) +{ + os_log("key_long_press"); + MicoGpioOutputHigh(MICO_GPIO_5); +} +static void key_short_press(void) +{ + //os_log("test"); + MicoGpioOutputTrigger(MICO_GPIO_5); +} + +void key_init(void) +{ + button_init_t button_config={ + .gpio=Button, + .long_pressed_func=key_long_press, + .pressed_func=key_short_press, + .long_pressed_timeout=800, + }; + + button_init(IOBUTTON_USER_1,button_config); +} + diff --git a/TC1/key.h b/TC1/key.h new file mode 100644 index 0000000..39ca13d --- /dev/null +++ b/TC1/key.h @@ -0,0 +1,14 @@ + +#ifndef __KEY_H_ +#define __KEY_H_ + + +#include "mico.h" +#include "MiCOKit_EXT.h" + +extern void led(char x); +extern void key_init(void); + + + +#endif diff --git a/TC1/main.c b/TC1/main.c new file mode 100644 index 0000000..b62f867 --- /dev/null +++ b/TC1/main.c @@ -0,0 +1,57 @@ +#include "main.h" +#include "key.h" +#include "wifi.h" + +#define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__) + +system_config_t * sys_config; +user_config_t * user_config; + +mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; + +int application_start( void ) +{ + + os_log( "Start" ); + + OSStatus err = kNoErr; + + /* Create mico system context and read application's config data from flash */ + sys_config = mico_system_context_init( sizeof(user_config_t) ); + user_config = ((system_context_t *) sys_config)->user_config_data; + require_action( user_config, exit, err = kNoMemoryErr ); + os_log( "user config:%d",user_config->val ); + err = mico_system_init( sys_config ); + require_noerr( err, exit ); + + + for ( int i = 0; i < Relay_NUM; i++ ) + { + MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL ); + //MicoGpioOutputHigh(Relay[i]); + } + + MicoGpioInitialize( (mico_gpio_t) Button, INPUT_PULL_UP ); + if ( !MicoGpioInputGet( Button ) ) + { //ʱť״̬ + os_log( "wifi_start_easylink" ); + wifi_status = WIFI_STATE_NOEASYLINK; //wifi_initeasylink + } + + MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL ); + led( 0 ); + + wifi_init( ); + key_init( ); +// wifi_start_easylink(); + while ( 1 ) + { +// mico_thread_msleep(500); +// MicoGpioOutputTrigger(MICO_GPIO_5); +// mico_gpio_output_toggle( MICO_SYS_LED ); +// mico_rtos_delay_milliseconds(1000); + } + exit: + return 0; +} + diff --git a/TC1/main.h b/TC1/main.h new file mode 100644 index 0000000..9db1f43 --- /dev/null +++ b/TC1/main.h @@ -0,0 +1,29 @@ +#ifndef __MAIN_H_ +#define __MAIN_H_ + +#include "mico.h" +#include "MiCOKit_EXT.h" + +#define Led MICO_GPIO_5 +#define Button MICO_GPIO_23 + +#define Relay_0 MICO_GPIO_6 +#define Relay_1 MICO_GPIO_7 +#define Relay_2 MICO_GPIO_8 +#define Relay_3 MICO_GPIO_9 +#define Relay_4 MICO_GPIO_10 +#define Relay_5 MICO_GPIO_18 +#define Relay_NUM 6 + + +//ûṹ +typedef struct { + char val; +} user_config_t; + +extern system_config_t * sys_config; +extern user_config_t * user_config; + +extern mico_gpio_t Relay[Relay_NUM]; + +#endif diff --git a/TC1/mico_config.h b/TC1/mico_config.h new file mode 100644 index 0000000..8f8aeab --- /dev/null +++ b/TC1/mico_config.h @@ -0,0 +1,80 @@ +/** + ****************************************************************************** + * @file mico_config.h + * @author + * @version V1.0.0 + * @date + * @brief This file provide constant definition and type declaration for MICO + * running. + ****************************************************************************** + * + * The MIT License + * Copyright (c) 2016 MXCHIP Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************** + */ + +#pragma once + +#define APP_INFO "MiCO BASIC Demo" + +#define FIRMWARE_REVISION "MICO_BASIC_1_0" +#define MANUFACTURER "MXCHIP Inc." +#define SERIAL_NUMBER "20140606" +#define PROTOCOL "com.mxchip.basic" + +/************************************************************************ + * Application thread stack size */ +#define MICO_DEFAULT_APPLICATION_STACK_SIZE (2000) + +/************************************************************************ + * Enable wlan connection, start easylink configuration if no wlan settings are existed */ +//#define MICO_WLAN_CONNECTION_ENABLE + +#define MICO_WLAN_CONFIG_MODE CONFIG_MODE_AWS + +#define EasyLink_TimeOut 60000 /**< EasyLink timeout 60 seconds. */ + +#define EasyLink_ConnectWlan_Timeout 20000 /**< Connect to wlan after configured by easylink. + Restart easylink after timeout: 20 seconds. */ + +/************************************************************************ + * Device enter MFG mode if MICO settings are erased. */ +//#define MFG_MODE_AUTO + +/************************************************************************ + * Command line interface */ +#define MICO_CLI_ENABLE + +/************************************************************************ + * Start a system monitor daemon, application can register some monitor + * points, If one of these points is not executed in a predefined period, + * a watchdog reset will occur. */ +#define MICO_SYSTEM_MONITOR_ENABLE + +/************************************************************************ + * Add service _easylink._tcp._local. for discovery */ +#define MICO_SYSTEM_DISCOVERY_ENABLE + +/************************************************************************ + * MiCO TCP server used for configuration and ota. */ +#define MICO_CONFIG_SERVER_ENABLE +#define MICO_CONFIG_SERVER_PORT 8000 + diff --git a/TC1/wifi.c b/TC1/wifi.c new file mode 100644 index 0000000..f048100 --- /dev/null +++ b/TC1/wifi.c @@ -0,0 +1,154 @@ +#include "main.h" +#include "wifi.h" +#include "key.h" + +#define os_log(format, ...) custom_log("WIFI", format, ##__VA_ARGS__) + +char wifi_status = WIFI_STATE_NOCONNECT; + +mico_timer_t wifi_led_timer; + +static void wifi_connect_sys_config( void ) +{ + if ( strlen( sys_config->micoSystemConfig.ssid ) > 0 ) + { + os_log("connect ssid:%s key:%s",sys_config->micoSystemConfig.ssid,sys_config->micoSystemConfig.user_key); + network_InitTypeDef_st wNetConfig; + memset( &wNetConfig, 0, sizeof(network_InitTypeDef_st) ); + strcpy( wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid ); + strcpy( wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key ); + wNetConfig.wifi_mode = Station; + wNetConfig.dhcpMode = DHCP_Client; + wNetConfig.wifi_retry_interval = 6000; + micoWlanStart( &wNetConfig ); + wifi_status = WIFI_STATE_CONNECTING; + } else + wifi_status = WIFI_STATE_FAIL; +} +void wifi_start_easylink( ) +{ + wifi_status = WIFI_STATE_EASYLINK; + micoWlanStartEasyLink( 20000 ); + led( 1 ); +} + +//easylink ɻص +void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg ) +{ + os_log("wifi_easylink_wps_completed_handle:"); + if ( nwkpara == NULL ) + { + os_log("EasyLink fail"); + micoWlanStopEasyLink( ); + return; + } + + os_log("ssid:%s",nwkpara->wifi_ssid); + os_log("key:%s",nwkpara->wifi_key); + os_log("local_ip_addr:%s",nwkpara->local_ip_addr); + os_log("net_mask:%s",nwkpara->net_mask); + os_log("gateway_ip_addr:%s",nwkpara->gateway_ip_addr); + os_log("dnsServer_ip_addr:%s",nwkpara->dnsServer_ip_addr); + + //wifi + strcpy( sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid ); + strcpy( sys_config->micoSystemConfig.user_key, nwkpara->wifi_key ); + sys_config->micoSystemConfig.user_keyLength = strlen( nwkpara->wifi_key ); + mico_system_context_update( sys_config ); + + wifi_status = WIFI_STATE_NOCONNECT; + os_log("EasyLink stop"); + micoWlanStopEasyLink( ); +} +//wifiӻȡIPַ ص +static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg ) +{ + os_log("got IP:%s", pnet->ip); + wifi_status = WIFI_STATE_CONNECTED; + +// /* Store SSID and KEY*/ +// mico_rtos_lock_mutex( &inContext->flashContentInRam_mutex ); +// memcpy( inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen ); +// memset( inContext->flashContentInRam.micoSystemConfig.bssid, 0x0, 6 ); +// memcpy( inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen ); +// inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen( nwkpara->wifi_key ); +// inContext->flashContentInRam.micoSystemConfig.dhcpEnable = true; +// mico_rtos_unlock_mutex( &inContext->flashContentInRam_mutex ); +// system_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key); + +} +//wifi״̬ıص +static void wifi_status_callback( WiFiEvent status, void *arg ) +{ + if ( status == NOTIFY_STATION_UP ) //wifiӳɹ + { + //wifi_status = WIFI_STATE_CONNECTED; + } else if ( status == NOTIFY_STATION_DOWN ) //wifiϿ + { + wifi_status = WIFI_STATE_NOCONNECT; + if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer ); + } +} +//100msʱص +static void wifi_led_timer_callback( void* arg ) +{ + static unsigned int num = 0; + num++; + switch ( wifi_status ) + { + case WIFI_STATE_FAIL: + os_log("wifi connect fail"); + led( 0 ); + mico_rtos_stop_timer( &wifi_led_timer ); + break; + case WIFI_STATE_NOCONNECT: + wifi_connect_sys_config( ); + break; + + case WIFI_STATE_CONNECTING: + if ( num > 2 ) + { + num = 0; + led( -1 ); + } + break; + case WIFI_STATE_NOEASYLINK: + wifi_start_easylink( ); + break; + case WIFI_STATE_EASYLINK: + led( 1 ); + break; + case WIFI_STATE_CONNECTED: + led( 0 ); + mico_rtos_stop_timer( &wifi_led_timer ); + break; + } +} + +void wifi_init( void ) +{ + //wifióʼ +// network_InitTypeDef_st wNetConfig; + +// memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st)); +// wNetConfig.wifi_mode = Station; +// snprintf(wNetConfig.wifi_ssid, 32, "Honor 9" ); +// strcpy((char*)wNetConfig.wifi_key, "19910911"); +// wNetConfig.dhcpMode = DHCP_Client; +// wNetConfig.wifi_retry_interval=6000; +// micoWlanStart(&wNetConfig); + + //wifi״̬led˸ʱʼ + mico_rtos_init_timer( &wifi_led_timer, 100, (void *) wifi_led_timer_callback, NULL ); + //easylink ɻص + mico_system_notify_register( mico_notify_EASYLINK_WPS_COMPLETED, (void *) wifi_easylink_completed_handle, NULL ); + //wifiӻȡIPַ ص + mico_system_notify_register( mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL ); + //wifi״̬ıص + mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL ); + + //ʱʼwifi + if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer ); + +} + diff --git a/TC1/wifi.h b/TC1/wifi.h new file mode 100644 index 0000000..7785884 --- /dev/null +++ b/TC1/wifi.h @@ -0,0 +1,28 @@ + +#ifndef __WIFI_H_ +#define __WIFI_H_ + + +#include "mico.h" +#include "MiCOKit_EXT.h" + + +enum { + WIFI_STATE_FAIL, + WIFI_STATE_NOCONNECT, + WIFI_STATE_CONNECTING, + WIFI_STATE_CONNECTED, + WIFI_STATE_NOEASYLINK, + WIFI_STATE_EASYLINK, + WIFI_STATE_EASYLINKING, +}; + + + +extern char wifi_status; +extern void wifi_init(void); +extern void wifi_start_easylink(void); + + + +#endif