机智云宠物屋外加4路继电器开源教程
机智云宠物屋移植教程-MCU版本1.硬件准备(1)直接采用STM32底板的gokit套件(2)采用散件自己搭建也行,散件清单以及和STM32单片机连线如下:STM32核心板http://club.gizwits.com/data/attachment/forum/201903/28/172441fk245wn8hk44u2s9.png.thumb.jpgP9813全彩灯模块----模块IIC接单片机PB8 PB9http://club.gizwits.com/data/attachment/forum/201903/28/172530l67tizzitftnktun.png.thumb.jpgL9110风扇模块----模块的2个控制引脚接单片机 PB4 PB5http://club.gizwits.com/data/attachment/forum/201903/28/172530pr1s9xrxxurw88dm.png.thumb.jpg微动开关2个----分别接单片机的PA8PB10http://club.gizwits.com/data/attachment/forum/201903/28/172530xbhaba8bj5rbg9rb.png.thumb.jpg红外光电反射传感器模块----接单片机PB7http://club.gizwits.com/data/attachment/forum/201903/28/172531ty7lft2yyl6yl1wd.png.thumb.jpgDHT11温湿度传感器模块----接单片机PB3http://club.gizwits.com/data/attachment/forum/201903/28/172531w0p1k0pkpk000i4i.png.thumb.jpg4位继电器模块5V----接单片机PA1 PA4 PB0 PB1http://club.gizwits.com/data/attachment/forum/201903/28/172531m711x71g11f18zq3.png.thumb.jpgesp8266最小系统,如果没有GAgent固件,需要下载固件,ESP8266串口烧写说明和GAgent固件下载ESP8266最小系统(烧录模式SW2闭合,也就是GPIO0拉低)与STM32串口相连接,PA2(TX)----ESP8266(RX) PA3(RX)---- ESP8266(TX)http://club.gizwits.com/data/attachment/forum/201903/28/172531b4qp4il4g1ljd4xp.png.thumb.jpg如果需要其他的模块自行添加,到此硬件所需的配件以及接线说明就完成了2.软件准备(1)keil-ARM需要包含STM32F103的pack(2)STM32CuBeMX(3)ESP8266下载软件和STM32下载软件,度娘家自己去找
3.云端产品创建登录机智云开发者中心https://dev.gizwits.com/zh-cn/developer/product/?__org=4031右上角创建新产品http://club.gizwits.com/data/attachment/forum/201903/28/172531rzz3d2d9vr33w26l.png.thumb.jpg按照12345的选择后点击保存创建产品http://club.gizwits.com/data/attachment/forum/201903/28/172532ugij3votirjdfra3.png.thumb.jpg点击去添加数据点http://club.gizwits.com/data/attachment/forum/201903/28/172532fttyltkshlohehcy.png.thumb.jpg点击管理,选择产品数据点。http://club.gizwits.com/data/attachment/forum/201903/28/172532ps81f1sz1s10ynu4.png.thumb.jpg选择Gokit Demo(基于Gokit板载元器件的智能宠物屋)应用此模板http://club.gizwits.com/data/attachment/forum/201903/28/172532tgzbpp4fpchxppfc.png.thumb.jpg成功导入数据点之后我们还需要增加4个继电器的数据点,点击右上角新增数据点,按照如图所示分别创建标识名为Relay1,Relay2,Relay3,Relay4,可写,布尔值数据点http://club.gizwits.com/data/attachment/forum/201903/28/172533mx882827rk18w1u2.png.thumb.jpg创建好了之后修改显示名称,此处我修改成继电器1 2 3 4,全部修改好之后,点击上面的应用保存数据点,到此数据点创建就完成了。http://club.gizwits.com/data/attachment/forum/201903/28/172533m7s1to6ss78j1ux8.png.thumb.jpghttp://club.gizwits.com/data/attachment/forum/201903/28/172533pq662rbrq1wm4h6h.png.thumb.jpghttp://club.gizwits.com/data/attachment/forum/201903/28/172533qiqp6vqqmmvqrjm5.png.thumb.jpg接下来我们需要生成MCU代码,先去产品基本信息里面复制Product Secret,然后进入MCU开发按照如图所示生成STM32F103C8代码包。http://club.gizwits.com/data/attachment/forum/201903/28/172533sj44a3mxw3x6cxsj.png.thumb.jpg生成好了之后我们需要下载代码包到电脑上,到此云端所有准备工作就完成了。下载代码之后再下载一份机智云宠物屋MCU驱动代码备用,下载地址http://goms-1251025085.cosgz.myqcloud.com/GoKit_MCU_STM32_V03010101测试固件及开发资源2017121218-1513076127294.rar3.代码修改将下载好的2个代码包分别解压好,复制驱动代码文件到生成的代码包的Hal文件夹下,复制驱动文件夹下的STM32F103C8x.ioc到代码包下替换STM32F103C8x.iochttp://club.gizwits.com/data/attachment/forum/201903/28/172534u4diny4tdneyydlz.png.thumb.jpghttp://club.gizwits.com/data/attachment/forum/201903/28/172534ko68ts6xtl28ztde.png.thumb.jpg
复制STM32F103C8x.ioc过去后打开STM32F103C8x.ioc。初始化4个输出引脚,配置好之后code项目生成代码http://club.gizwits.com/data/attachment/forum/201903/28/172534zpnc4rcdel5n425k.png.thumb.jpg生成好之后打开项目,找到gizwits_product.c文件,在用户函数gizwitsEventProcess里面对继电器控制进行修改,详细代码如下(直接复制可能会因为网页编码不一样导致乱码)case EVENT_Relay1: currentDataPoint.valueRelay1 = dataPointPtr->valueRelay1; GIZWITS_LOG("Evt:EVENT_Relay1 %d \n", currentDataPoint.valueRelay1); if(0x01 ==currentDataPoint.valueRelay1) { HAL_GPIO_WritePin(Relay1_GPIO_Port, Relay1_Pin, GPIO_PIN_RESET);//继电器1,引脚拉低,继电器低电平有效 } else { HAL_GPIO_WritePin(Relay1_GPIO_Port, Relay1_Pin, GPIO_PIN_SET); } break; case EVENT_Relay2: currentDataPoint.valueRelay2 = dataPointPtr->valueRelay2; GIZWITS_LOG("Evt:EVENT_Relay2 %d \n", currentDataPoint.valueRelay2); if(0x01 == currentDataPoint.valueRelay2) { HAL_GPIO_WritePin(Relay2_GPIO_Port, Relay2_Pin, GPIO_PIN_RESET); } else { HAL_GPIO_WritePin(Relay2_GPIO_Port, Relay2_Pin, GPIO_PIN_SET); } break; case EVENT_Relay3: currentDataPoint.valueRelay3 = dataPointPtr->valueRelay3; GIZWITS_LOG("Evt:EVENT_Relay3 %d \n", currentDataPoint.valueRelay3); if(0x01 ==currentDataPoint.valueRelay3) { HAL_GPIO_WritePin(Relay3_GPIO_Port, Relay3_Pin, GPIO_PIN_RESET); } else { HAL_GPIO_WritePin(Relay3_GPIO_Port, Relay3_Pin, GPIO_PIN_SET); } break; case EVENT_Relay4: currentDataPoint.valueRelay4 = dataPointPtr->valueRelay4; GIZWITS_LOG("Evt:EVENT_Relay4 %d \n", currentDataPoint.valueRelay4); if(0x01 ==currentDataPoint.valueRelay4) { HAL_GPIO_WritePin(Relay4_GPIO_Port, Relay4_Pin, GPIO_PIN_RESET); } else { HAL_GPIO_WritePin(Relay4_GPIO_Port, Relay4_Pin, GPIO_PIN_SET); } break;在gizwits_product.c文件里面和main.c增加以下头文件#include "hal_temp_hum.h"#include "hal_rgb_led.h"#include "hal_motor.h"#include "Hal_infrared.h"#include "delay.h"#include "main.h" //main.c已有就无需添加,将此头文件添加到各个sensor的文件当中,否则编译引脚报错在gizwits_product.c文件的 userInit( ) 函数中添加各sensor的初始化void userInit(void){ memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t)); delay_init(72); // 延时 初始化rgbLedInit(); // RGB LED 初始化dht11Init(); // 温湿度初始化irInit(); // 红外初始化motorInit(); // 电机初始化motorStatus(0); // 电机转速初始化在gizwits_product.c 文件的 userHandle( ) 函数中添加只读型传感器数据点相关的代码void userHandle(void){ uint8_t ret = 0; static uint32_tthLastTimer = 0; ///< 新添加代码: 红外传感器数据获取 currentDataPoint.valueInfrared= irHandle(); ///< 新添加代码: 温湿度传感器数据获取 if((gizGetTimerCount()- thLastTimer) > 2000) //上报间隔2S { ret= dht11Read((uint8_t *)¤tDataPoint.valueTemperature,(uint8_t*)¤tDataPoint.valueHumidity); if(ret!= 0) { GIZWITS_LOG("Failedto read DHT11 [%d] \n", ret); } thLastTimer= gizGetTimerCount(); }在main.c 文件的 key2ShortPress( ) 函数与 key2LongPress( ) 函数中添加长/短按key2时的LED点亮代码。void key2ShortPress(void){GIZWITS_LOG("KEY2 PRESS ,Soft APmode\n");#if !MODULE_TYPE gizwitsSetMode(WIFI_SOFTAP_MODE); #endif //Soft AP mode,RGB 红灯 ledRgbControl(250,0, 0);}void key2LongPress(void){ //AirLink mode GIZWITS_LOG("KEY2PRESS LONG ,AirLink mode\n");#if !MODULE_TYPE gizwitsSetMode(WIFI_AIRLINK_MODE); #endif //AirLink mode,RGB 绿灯 ledRgbControl(0,250, 0);}在gizwits_product.c文件的 gizwitsEventProcess( ) 函数中进行配网成功关闭灯case WIFI_CON_ROUTER:ledRgbControl(0, 250, 0); break;接着继续在上面增加对RGB LED开启红色的控制。 if(0x01 == currentDataPoint.valueLED_OnOff) { ledRgbControl(254,0, 0); } else { ledRgbControl(0, 0,0); } break;接下来对其他颜色或者全彩已经电机等的修改 switch(currentDataPoint.valueLED_Color) { caseLED_Color_VALUE0: ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; caseLED_Color_VALUE1: ledRgbControl(254, 254, 0); break; caseLED_Color_VALUE2: ledRgbControl(254,0, 70); break; caseLED_Color_VALUE3: ledRgbControl(238,30, 30); break; default: break; } break;
case EVENT_LED_R: currentDataPoint.valueLED_R = dataPointPtr->valueLED_R; GIZWITS_LOG("Evt:EVENT_LED_R%d\n",currentDataPoint.valueLED_R); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_LED_G: currentDataPoint.valueLED_G = dataPointPtr->valueLED_G; GIZWITS_LOG("Evt:EVENT_LED_G %d\n",currentDataPoint.valueLED_G); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_LED_B: currentDataPoint.valueLED_B = dataPointPtr->valueLED_B; GIZWITS_LOG("Evt:EVENT_LED_B %d\n",currentDataPoint.valueLED_B); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_Motor_Speed: currentDataPoint.valueMotor_Speed = dataPointPtr->valueMotor_Speed; GIZWITS_LOG("Evt:EVENT_Motor_Speed%d\n",currentDataPoint.valueMotor_Speed); motorStatus(currentDataPoint.valueMotor_Speed); break;到此为止,所有的代码就修改结束了,将代码进行编译,下载到单片机,配网接入云就行了。http://club.gizwits.com/data/attachment/forum/201903/28/172534r5l55zqbrbcmalbx.png.thumb.jpg
页:
[1]