大神分享 : 零基础实现WiFi远程插座
本次采用esp8266 SOC方案材料准备
1.4个微动开关(用于配网和本地开关插座)
2.8266一块(可以去马云家用找现成的,比如小黑板)
3.1个4位继电器(用于4路控制)
4.能上网的无线路由器一台(2.4G频段wifi,不支持5G频段)
5.IDE环境搭建可以去官网看,不懂的地方可以问我
话不多说,直入正题。
云端搭建大部分请参考上一篇帖子,【体验】+0基础实现GPRS远程插座,此处我只说明其中的差异部分。
产品创建和上篇文章一样,创建如下4个数据点
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/102316ctn4nwicmpl9jebp.jpg
进入MCU开发,选择SOC方案,ESP8266_32M,输入Product Secret,在设备基本信息里面
点击生成代码包
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/103049fq0hfglflz80lo36.jpg
下载生成的代码,解压文件,打开开发环境
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/104327hyqnq9nvdd8465ud.png
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/104502j4xehxbgxx4o4hhg.jpg
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/105015zdewvs08ao0wb4ol.png
这样项目就导入了,双击打开根目录下面的makefile
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/105017tz4qfz89o5gmj9jj.jpg
修改23排开始代码:
BOOT?=none
APP?=0
SPI_SPEED?=40
SPI_MODE?=QIO
SPI_SIZE_MAP?=0
修改为:
BOOT?=new
APP?=1
SPI_SPEED?=40
SPI_MODE?=QIO
SPI_SIZE_MAP?=6
保存并关闭Makefile,记住一定要是根目录下面的Makefile。
打开APP目录下面的user,找到main函数并打开,在APP目录下找到gizwits目录,然后gizwits_product.c和gizwits_product.h,打开着3个文件之后我们一个一个的来修改,由于要上报数据,一会儿我们需要创建一个布尔数组存放4个开关的状态,接线图我会在稍后发在下面。
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/105759i3j4payjko4jbkhn.jpg
接线图:
4个按键分别接在:
GPIO2GPIO0GPIO4GPIO5
4个继电器分别接在:
GPIO15 GPIO13 GPIO12 GPIO14
接下来我们先修改gizwits_product.h
在头文件下面新增,然后保存数据
#define ON 1#define OFF 0
extern bool STA;
修改gizwits_product.c
在头文件下面新增
bool STA={0}; //用于存储状态
然后往下面gizwitsEventProcess函数
修改里面的数据点内容部分,
switch(info->event)
{
case EVENT_sw1 :
currentDataPoint.valuesw1 = dataPointPtr->valuesw1;
GIZWITS_LOG("Evt: EVENT_sw1 %d \n", currentDataPoint.valuesw1);
if(0x01 == currentDataPoint.valuesw1)
{
STA= ON; //user handle
}
else
{
STA= OFF;//user handle
}
break;
case EVENT_sw2 :
currentDataPoint.valuesw2 = dataPointPtr->valuesw2;
GIZWITS_LOG("Evt: EVENT_sw2 %d \n", currentDataPoint.valuesw2);
if(0x01 == currentDataPoint.valuesw2)
{
STA= ON;//user handle
}
else
{
STA= OFF; //user handle
}
break;
case EVENT_sw3 :
currentDataPoint.valuesw3 = dataPointPtr->valuesw3;
GIZWITS_LOG("Evt: EVENT_sw3 %d \n", currentDataPoint.valuesw3);
if(0x01 == currentDataPoint.valuesw3)
{
STA= ON;//user handle
}
else
{
STA= OFF;//user handle
}
break;
case EVENT_sw4 :
currentDataPoint.valuesw4 = dataPointPtr->valuesw4;
GIZWITS_LOG("Evt: EVENT_sw4 %d \n", currentDataPoint.valuesw4);
if(0x01 == currentDataPoint.valuesw4)
{
STA= ON;//user handle
}
else
{
STA= OFF;//user handle
}
break;
找到void ICACHE_FLASH_ATTR userInit(void)函数
修改内容为:
void ICACHE_FLASH_ATTR userInit(void)
{
gizMemset((uint8_t *)¤tDataPoint, 0, sizeof(dataPoint_t));
/** Warning !!! DataPoint Variables Init , Must Within The Data Range **/
currentDataPoint.valuesw1 = STA;
currentDataPoint.valuesw2 = STA;
currentDataPoint.valuesw3 = STA;
currentDataPoint.valuesw4 = STA;
}
找到void ICACHE_FLASH_ATTR userHandle(void)函数,修改其内容为:
void ICACHE_FLASH_ATTR userHandle(void)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), !STA);
GPIO_OUTPUT_SET(GPIO_ID_PIN(13), !STA);
GPIO_OUTPUT_SET(GPIO_ID_PIN(14), !STA);
GPIO_OUTPUT_SET(GPIO_ID_PIN(15), !STA);
currentDataPoint.valuesw1 = STA;
currentDataPoint.valuesw2 = STA;
currentDataPoint.valuesw3 = STA;
currentDataPoint.valuesw4 = STA;
system_os_post(USER_TASK_PRIO_2, SIG_UPGRADE_DATA, 0);
}
到此为止云端数据就处理完成了,接下来修改main函数:首先是修改按键,其次是初始化,
4个按键功能为:短按分别控制4个继电器取反,并上报云端,实时显示开关状态,所有按键长按都进入SOFTAP配网模式,也就是热点模式。你也可以设置成2个按键SOFTAP,2个按键AIRLINK。
修改下面函数内容:
#define GPIO_KEY_NUM 2 ///< Defines the total number of key members
#define KEY_0_IO_MUX PERIPHS_IO_MUX_GPIO0_U ///< ESP8266 GPIO function
#define KEY_0_IO_NUM 0 ///< ESP8266 GPIO number
#define KEY_0_IO_FUNC FUNC_GPIO0 ///< ESP8266 GPIO name
#define KEY_1_IO_MUX PERIPHS_IO_MUX_MTMS_U ///< ESP8266 GPIO function
#define KEY_1_IO_NUM 14 ///< ESP8266 GPIO number
#define KEY_1_IO_FUNC FUNC_GPIO14 ///< ESP8266 GPIO name
改为:
#define GPIO_KEY_NUM 4 ///< Defines the total number of key members
#define KEY_0_IO_MUX PERIPHS_IO_MUX_GPIO2_U ///< ESP8266 GPIO function
#define KEY_0_IO_NUM 2 ///< ESP8266 GPIO number
#define KEY_0_IO_FUNC FUNC_GPIO2 ///< ESP8266 GPIO name
#define KEY_1_IO_MUX PERIPHS_IO_MUX_GPIO0_U ///< ESP8266 GPIO function
#define KEY_1_IO_NUM 0 ///< ESP8266 GPIO number
#define KEY_1_IO_FUNC FUNC_GPIO0 ///< ESP8266 GPIO name
#define KEY_2_IO_MUX PERIPHS_IO_MUX_GPIO4_U ///< ESP8266 GPIO function
#define KEY_2_IO_NUM 4 ///< ESP8266 GPIO number
#define KEY_2_IO_FUNC FUNC_GPIO4 ///< ESP8266 GPIO name
#define KEY_3_IO_MUX PERIPHS_IO_MUX_GPIO5_U ///< ESP8266 GPIO function
#define KEY_3_IO_NUM 5 ///< ESP8266 GPIO number
#define KEY_3_IO_FUNC FUNC_GPIO5 ///< ESP8266 GPIO name
找到2个长短按键处理函数
/**
* Key1 key short press processing
* @param none
* @return none
*/
LOCAL void ICACHE_FLASH_ATTR key1ShortPress(void)
{
GIZWITS_LOG("#### KEY1 short press ,Production Mode\n");
gizwitsSetMode(WIFI_PRODUCTION_TEST);
}
/**
* Key1 key presses a long press
* @param none
* @return none
*/
LOCAL void ICACHE_FLASH_ATTR key1LongPress(void)
{
GIZWITS_LOG("#### key1 long press, default setup\n");
gizwitsSetMode(WIFI_RESET_MODE);
}
/**
* Key2 key to short press processing
* @param none
* @return none
*/
LOCAL void ICACHE_FLASH_ATTR key2ShortPress(void)
{
GIZWITS_LOG("#### key2 short press, soft ap mode \n");
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
/**
* Key2 button long press
* @param none
* @return none
*/
LOCAL void ICACHE_FLASH_ATTR key2LongPress(void)
{
GIZWITS_LOG("#### key2 long press, airlink mode\n");
gizwitsSetMode(WIFI_AIRLINK_MODE);
}
修改成:
/*key1按键短按处理*/
LOCAL void ICACHE_FLASH_ATTR key1ShortPress(void)
{
STA=!STA;
}
/*key1按键长按处理*/
LOCAL void ICACHE_FLASH_ATTR key1LongPress(void)
{
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
/*key2按键短按处理*/
LOCAL void ICACHE_FLASH_ATTR key2ShortPress(void)
{
STA=!STA;
}
/* key2按键长按处理*/
LOCAL void ICACHE_FLASH_ATTR key2LongPress(void)
{
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
/*key3按键短按处理*/
LOCAL void ICACHE_FLASH_ATTR key3ShortPress(void)
{
STA=!STA;
}
/*key3按键长按处理*/
LOCAL void ICACHE_FLASH_ATTR key3LongPress(void)
{
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
//key4按键短按处理*
LOCAL void ICACHE_FLASH_ATTR key4ShortPress(void)
{
STA=!STA;
}
//key4按键长按处理*
LOCAL void ICACHE_FLASH_ATTR key4LongPress(void)
{
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
然后对按键和继电器的GPIO进行初始化,修改在LOCAL void ICACHE_FLASH_ATTR keyInit(void)函数位:
LOCAL void ICACHE_FLASH_ATTR keyInit(void)
{
singleKey = keyInitOne(KEY_0_IO_NUM, KEY_0_IO_MUX, KEY_0_IO_FUNC,
key1LongPress, key1ShortPress);
singleKey = keyInitOne(KEY_1_IO_NUM, KEY_1_IO_MUX, KEY_1_IO_FUNC,
key2LongPress, key2ShortPress);
singleKey = keyInitOne(KEY_2_IO_NUM, KEY_2_IO_MUX, KEY_2_IO_FUNC,
key3LongPress, key3ShortPress);
singleKey = keyInitOne(KEY_3_IO_NUM, KEY_3_IO_MUX, KEY_3_IO_FUNC,
key4LongPress, key4ShortPress);
keys.singleKey = singleKey;
keyParaInit(&keys);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14);
}
到此为止所有代码都修改完成了
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/115006css7mab7us82aubb.png
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/115203u9yhg1c6u9ho69zh.jpg
接下来是下载到ESP8266,下载方法官网有相关帖子,搞不懂的也可以来问我·QQ1083791810交流群438373554
打开下载软件,把便已生成的4个文件按照这些设置好之后就能下载了
http://www.eeboard.com/bbs/data/attachment/forum/201803/14/115704bqcge2cj2j3cd2zq.jpg
下载过后接上线长按按键就能配网了。
菜鸟看得心痒又无奈:L
页:
[1]