第一次提交

add:wifi EasyLink 功能
add:wifi连接
add:wifi数据保存
This commit is contained in:
Zip
2019-01-05 16:57:36 +08:00
commit 973efb93b3
9 changed files with 429 additions and 0 deletions

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
#TC1
This is TC1 program.

29
TC1/TC1.mk Normal file
View File

@@ -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

36
TC1/key.c Normal file
View File

@@ -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);
}

14
TC1/key.h Normal file
View File

@@ -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

57
TC1/main.c Normal file
View File

@@ -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 ) )
{ //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ť״̬
os_log( "wifi_start_easylink" );
wifi_status = WIFI_STATE_NOEASYLINK; //wifi_init<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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;
}

29
TC1/main.h Normal file
View File

@@ -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
//<2F>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B9B9>
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

80
TC1/mico_config.h Normal file
View File

@@ -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

154
TC1/wifi.c Normal file
View File

@@ -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 <20><><EFBFBD>ɻص<C9BB>
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);
//<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<66><69><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>ȡ<EFBFBD><C8A1>IP<49><50>ַ <20>ص<EFBFBD>
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<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
static void wifi_status_callback( WiFiEvent status, void *arg )
{
if ( status == NOTIFY_STATION_UP ) //wifi<66><69><EFBFBD>ӳɹ<D3B3>
{
//wifi_status = WIFI_STATE_CONNECTED;
} else if ( status == NOTIFY_STATION_DOWN ) //wifi<66>Ͽ<EFBFBD>
{
wifi_status = WIFI_STATE_NOCONNECT;
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
}
}
//100ms<6D><73>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD>
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<66><69><EFBFBD>ó<EFBFBD>ʼ<EFBFBD><CABC>
// 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״̬<D7B4><CCAC>led<65><64>˸<EFBFBD><CBB8>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
mico_rtos_init_timer( &wifi_led_timer, 100, (void *) wifi_led_timer_callback, NULL );
//easylink <20><><EFBFBD>ɻص<C9BB>
mico_system_notify_register( mico_notify_EASYLINK_WPS_COMPLETED, (void *) wifi_easylink_completed_handle, NULL );
//wifi<66><69><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>ȡ<EFBFBD><C8A1>IP<49><50>ַ <20>ص<EFBFBD>
mico_system_notify_register( mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL );
//wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL );
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD>
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
}

28
TC1/wifi.h Normal file
View File

@@ -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