From 973efb93b3288fe20794901752319e83c56c40cf Mon Sep 17 00:00:00 2001 From: Zip <76966589@qq.com> Date: Sat, 5 Jan 2019 16:57:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=20add:wifi=20EasyLink=20=E5=8A=9F=E8=83=BD=20add:wifi=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=20add:wifi=E6=95=B0=E6=8D=AE=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + TC1/TC1.mk | 29 +++++++++ TC1/key.c | 36 +++++++++++ TC1/key.h | 14 +++++ TC1/main.c | 57 +++++++++++++++++ TC1/main.h | 29 +++++++++ TC1/mico_config.h | 80 ++++++++++++++++++++++++ TC1/wifi.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++ TC1/wifi.h | 28 +++++++++ 9 files changed, 429 insertions(+) create mode 100644 README.md create mode 100644 TC1/TC1.mk create mode 100644 TC1/key.c create mode 100644 TC1/key.h create mode 100644 TC1/main.c create mode 100644 TC1/main.h create mode 100644 TC1/mico_config.h create mode 100644 TC1/wifi.c create mode 100644 TC1/wifi.h 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_init中启动easylink + } + + 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