2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 机智云esp8266stm32 控制LED灯

机智云esp8266stm32 控制LED灯

时间:2021-02-19 23:59:34

相关推荐

机智云esp8266stm32 控制LED灯

一、准备模块

1.esp8266

2.stm32f103

3.TTL下载器

4.杜邦线若干

二、esp8266下载机智云固件

1.打开机智云官网

2.注册机智云

3.如下图下载固件

下载解压后就是下图这个文件夹

三、烧录固件

1.烧录接线,IO0接地才能进入下载模式

2.打开烧录软件,如下图烧录进去

四、创建产品,下载单片机代码

1.创建产品

一定要记得点击应用

2.下载单片机代码

硬件平台选择你的单片机型号

下面的密钥在基本信息中

然后点击生成代码包

下载

解压后的文件夹

我们只需要用到这两个软件

把他加入我们的工程,加入工程我就不演示了

五、修改代码

1.usart3.c

连接esp8266的串口

添加如下代码

添加头文件gizwits_product.h

2.timer.c

定时器添加如下代码

添加头文件gizwits_product.h

3.gizwits_product.c

有一些要注释掉的,可以参考一下下面的代码

/*************************************************************** @file gizwits_product.c* @brief Gizwits control protocol processing, and platform-related hardware initialization * @author Gizwits* @date -07-19* @versionV03030000* @copyright Gizwits* * @note 机智云.只为智能硬件而生*Gizwits Smart Cloud for Smart Products*链接|增值ֵ|开放|中立|安全|自有|自由|生态*************************************************************/#include <stdio.h>#include <string.h>//#include "hal_key.h"#include "gizwits_product.h"#include "common.h"#include "led.h"#include "gizwits_protocol.h"static uint32_t timerMsCount;//uint8_t aRxBuffer;/** User area the current device state structure*///dataPoint_t currentDataPoint;//extern keysTypedef_t keys;//extern TIM_HandleTypeDef htim2;//extern UART_HandleTypeDef huart1;//extern UART_HandleTypeDef huart2;/**@} *//**@name Gizwits User Interface* @{*//*** @brief Event handling interface* Description:* 1. Users can customize the changes in WiFi module status* 2. Users can add data points in the function of event processing logic, such as calling the relevant hardware peripherals operating interface* @param [in] info: event queue* @param [in] data: protocol data* @param [in] len: protocol data length* @return NULL* @ref gizwits_protocol.h*/int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len){uint8_t i = 0;dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;protocolTime_t *ptime = (protocolTime_t *)gizdata;#if MODULE_TYPEgprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;#elsemoduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;#endifif((NULL == info) || (NULL == gizdata)){return -1;}for(i=0; i<info->num; i++){switch(info->event[i]){case EVENT_LEDonoff:currentDataPoint.valueLEDonoff = dataPointPtr->valueLEDonoff;GIZWITS_LOG("Evt: EVENT_LEDonoff %d \n", currentDataPoint.valueLEDonoff);if(0x01 == currentDataPoint.valueLEDonoff){LED1=0;}else{LED1=1; }break;case WIFI_SOFTAP:break;case WIFI_AIRLINK:break;case WIFI_STATION:break;case WIFI_CON_ROUTER:break;case WIFI_DISCON_ROUTER:break;case WIFI_CON_M2M:break;case WIFI_DISCON_M2M:break;case WIFI_RSSI:GIZWITS_LOG("RSSI %d\n", wifiData->rssi);break;case TRANSPARENT_DATA:GIZWITS_LOG("TRANSPARENT_DATA \n");//user handle , Fetch data from [data] , size is [len]break;case WIFI_NTP:GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);break;case MODULE_INFO:GIZWITS_LOG("MODULE INFO ...\n");#if MODULE_TYPEGIZWITS_LOG("GPRS MODULE ...\n");//Format By gprsInfo_t#elseGIZWITS_LOG("WIF MODULE ...\n");//Format By moduleInfo_tGIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);#endifbreak;default:break;}}return 0;}/*** User data acquisition* Here users need to achieve in addition to data points other than the collection of data collection, can be self-defined acquisition frequency and design data filtering algorithm* @param none* @return none*///void userHandle(void)//{// /*// */// //}/*** Data point initialization function* In the function to complete the initial user-related data* @param none* @return none* @note The developer can add a data point state initialization value within this function*///void userInit(void)//{// memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));// // /** Warning !!! DataPoint Variables Init , Must Within The Data Range **/ // /*//currentDataPoint.valueLEDonoff = ;// *///}/*** @brief Millisecond timing maintenance function, milliseconds increment, overflow to zero* @param none* @return none*/void gizTimerMs(void){timerMsCount++;}/*** @brief Read millisecond count* @param none* @return millisecond count*/uint32_t gizGetTimerCount(void){return timerMsCount;}/*** @brief MCU reset function* @param none* @return none*/void mcuRestart(void){__set_FAULTMASK(1);NVIC_SystemReset();}/**@} *///#ifdef __GNUC__// /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf//set to 'Yes') calls __io_putchar() */// #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)//#else// #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)//#endif /* __GNUC__ *////**// * @brief Retargets the C library printf function to the USART.// * @param None// * @retval None// *///PUTCHAR_PROTOTYPE//{// /* Place your implementation of fputc here */// /* e.g. write a character to the USART1 and Loop until the end of transmission */// HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);// // return ch;//}///**// * @brief Period elapsed callback in non blocking mode // * @param htim : TIM handle// * @retval None// *///void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//{//if(htim==&htim2)//{//keyHandle((keysTypedef_t *)&keys);//gizTimerMs();//}//}///**//* @brief Timer TIM3 init function//* @param none//* @return none//*///void timerInit(void)//{//HAL_TIM_Base_Start_IT(&htim2);//}///**// * @brief This function handles USART IDLE interrupt.// *///void HAL_UART_RxCpltCallback(UART_HandleTypeDef*UartHandle) //{ // if(UartHandle->Instance == USART2) // { //gizPutData((uint8_t *)&aRxBuffer, 1);// HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);//开启下一次接收中断 // } //} ///**//* @brief USART init function//* Serial communication between WiFi modules and device MCU//* @param none//* @return none//*///void uartInit(void)//{//HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);//开启下一次接收中断 //}/*** @brief Serial port write operation, send data to WiFi module** @param buf: buf address* @param len: buf length** @return : Return effective data length;-1,return failure*/int32_t uartWrite(uint8_t *buf, uint32_t len){uint8_t crc[1] = {0x55};uint32_t i = 0;if(NULL == buf){return -1;}for(i=0; i<len; i++){//USART_SendData(UART, buf[i]);//STM32 test demo//实现串口发送函数,将buf[i]发送到模组USART_SendData(USART3,buf[i]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕 #ifdef PROTOCOL_DEBUGGIZWITS_LOG("%02x ", buf[i]);#endifif(i >=2 && buf[i] == 0xFF){//实现串口发送函数,将0x55发送到模组USART_SendData(USART3,0x55);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕#ifdef PROTOCOL_DEBUGGIZWITS_LOG("%02x ", 0x55);#endif}}#ifdef PROTOCOL_DEBUGGIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);for(i=0; i<len; i++){GIZWITS_LOG("%02x ", buf[i]);if(i >=2 && buf[i] == 0xFF){GIZWITS_LOG("%02x ", 0x55);}}GIZWITS_LOG("\n");#endifreturn len;}

这里是控制LED亮灭的

这里在按照下图改一下

mian.c

#include "led.h"#include "delay.h"#include "key.h"#include "sys.h"#include "usart.h"#include "timer.h"#include "usart3.h"#include "gizwits_product.h"/* 用户区当前设备状态结构体*/dataPoint_t currentDataPoint;//协议初始化void Gizwits_Init(void){TIM3_Int_Init(9,7199);//1MS系统定时usart3_init(9600);//WIFI初始化memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));//设备状态结构体初始化gizwitsInit();//缓冲区初始化}//数据采集void userHandle(void){}//主函数 int main(void){int key;delay_init();//延时函数初始化 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级uart_init(115200); //串口初始化为115200LED_Init(); //LED端口初始化KEY_Init(); //按键初始化 Gizwits_Init(); //协议初始化printf("KEY1:AirLink接入模式\t KEY_UP:复位\r\n\r\n");while(1){userHandle();//用户采集gizwitsHandle((dataPoint_t *)&currentDataPoint);//协议处理key = KEY_Scan(0);if(key==KEY1_PRES)//KEY1按键{printf("WIFI进入AirLink接入模式\r\n");gizwitsSetMode(WIFI_AIRLINK_MODE);//Air-link模式接入}if(key==WKUP_PRES)//KEY_UP按键{ printf("WIFI复位,请重新配置连接\r\n");gizwitsSetMode(WIFI_RESET_MODE);//WIFI复位}delay_ms(200);LED0=!LED0;} }

烧录到单片机后,就可以下载机智云APP进行配网,控制LED灯的亮灭啦

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。