STM32连接机智云(踩雷日记)

目录

一、前期准备

二、固件烧写

 三、创建机智云产品

​四、MCU开发


好记性不如赖笔头,为了自己以后开发物联网项目,同时方便大家学习利用机智云进行物联网开发,我觉得有必要把自己的学习经历写下来。

一、前期准备

STM32核心板或者开发板一个(板子上带有LED)

网上这种便宜的ESP01S一个

USB转TTL模块一个

二、固件烧写

ESP8266烧写机智云的官方固件,才能与机智云进行通信。

固件可以去机智云官网下载,也可以直接用我提供的压缩包里的固件,我的固件也是从官网下载的,后面我以我的这个固件进行讲解。

工具包

 GAgent文件下载解压后包含7个bin文件

这里我得说下我踩的第一大坑了,一开始我找网上资料说的是让烧写32Mbit的。完事了死活连接不上(我还以为是我程序的问题,我改了一下午程序,离谱的是他竟然能烧写进去)。后来我换成了8Mbit,很快就连接上了。ESP01和ESP01S应该都是8Mbit的。

准备好GAgent固件后,那么就需要一个固件的下载工具。固件下载工具,我们用的是机智云官方推荐的乐鑫原厂固件烧写工具,在我提供的压缩包里也有。

8266固件烧写接线方式

3.33.3
RXTX
IO0GND
TXRX
GNDGND

接下来打开软件,选择第一个。

下面就开始进行烧写 (记得把你要烧写的固件勾选上)

如果一直卡在等待上电同步就把你的RST引脚接下地,在拔出来。 

等待下载

 下载完成

 三、创建机智云产品

 首先在机智云官网注册一个开发者账号,然后进入到开发者中心,然后开始创建新产品,如下图所示

在个人项目页面左侧栏上,点击数据点,然后点击新建数据点,会弹出一个配置小页面 我们可以看到数据点定义基本内容可分为标识名,读写类型,数据类型及备 注。

读写类型:定义数据点的作用范围,包含有 4 种类型,分别是:只读、可写、报警、故障, 以下是官方对读写类型的介绍:

① 只读:表示该数据点为非控制,数据只支持从设备上报。

② 可写:表示该数据点可控制。设备端可上报数据点数据,云端/客户端(移动端)可对该数 据点数据做出下发控制。

③ 报警:表示该数据点非控制,数据只支持从设备(设备端)上报,数据类型需为布尔值。

④ 故障:表示该数据点非控制,数据只支持从设备(设备端)上报,数据类型需为布尔值。 云端对设备上报的该数据点做统计,可在“运行状态”查看。

虚拟设备能模拟 真实设备的上传数据行为,快速的验证接口功能的开发。终端设备没做好,但是想验证功能逻 辑,那么虚拟设备功能就可以派上用场了,在产品开发前会显得尤其重要。

虽说没有终端设备,我们可以用虚拟设备代替,但是手机上没有 APP 还是没有用,因为控制不到设备,也显示不到设备上传云端的服务数据,那应该怎么办呢?Don’t worry 机智云已经为开发者想好了,提供一个非常便利调试的 Demo App,还有针对不同移动端平台 Android、IOS 的 SDK APP 开发资源,在上侧栏,下载中心->通信产品调试app,就可以下载了

 点击启动虚拟设备,页面跳转后点击显示二维码(虚拟设备二维码)

 

二维码生成后,在手机 App 上点击右上角- -然后点击扫描绑定设备

我们点击 APP LEDkey按钮,开关的数据也同步上传到服务器了

四、MCU开发

到此为止一切正常,然后我们开始写程序,首先先让机智云自动生成数据点和设备接入等的代码,然后移植到工程当中,先到基本信息这把Product Secret复制下来,接下来要用

 

 生成代码包,并下载。

 压缩包内包含以下文件,我们只使用Gizwits和Utils两个文件夹,并复制

 

 接下来我用正点原子定时器中断实验的代码来进行移植

 把刚才那两个文件夹复制到工程里

然后复制一个usart3工程 到工程里

接下来就是把这些代码和头文件添加到工程里,我就不多说了。 

 

接下来就是修改代码了

 

 gizwits_product.c文件修改如下:

/**
************************************************************
* @file         gizwits_product.c
* @brief        Gizwits control protocol processing, and platform-related       hardware initialization 
* @author       Gizwits
* @date         2017-07-19
* @version      V03030000
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/

#include <stdio.h>
#include <string.h>
//#include "hal_key.h"//注释掉
#include "gizwits_product.h"
#include "common.h"

#include "usart3.h"//添加上
#include "led.h"//添加上

static uint32_t timerMsCount;
uint8_t aRxBuffer;

/** User area the current device state structure*/
extern 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_TYPE
  gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
#else
  moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
#endif

  if((NULL == info) || (NULL == gizdata))
  {
    return -1;
  }

  for(i=0; i<info->num; i++)
  {
    switch(info->event[i])
    {
      case EVENT_LEDkey:
        currentDataPoint.valueLEDkey = dataPointPtr->valueLEDkey;
        GIZWITS_LOG("Evt: EVENT_LEDkey %d \n", currentDataPoint.valueLEDkey);
        if(0x01 == currentDataPoint.valueLEDkey)
        {
          //user handle
					LED1=0;//添加上
        }
        else
        {
          //user handle   
					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_TYPE
            GIZWITS_LOG("GPRS MODULE ...\n");
            //Format By gprsInfo_t
      #else
            GIZWITS_LOG("WIF MODULE ...\n");
            //Format By moduleInfo_t
            GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
      #endif
    break;
      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.valueLEDkey = ;
    */

}


/**
* @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);
//    HAL_NVIC_SystemReset();//注释
			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++)//注释
//    {
//        HAL_UART_Transmit_IT(&huart2, (uint8_t *)&buf[i], 1);
//				while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission

//        if(i >=2 && buf[i] == 0xFF)
//        {
//						HAL_UART_Transmit_IT(&huart2, (uint8_t *)&crc, 1);
//						while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission
//        }
//    }
    for(i=0; i<len; i++)//添加
    {
			USART_SendData(USART3,buf[i]);
			while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕 
			if(i >=2 && buf[i] == 0xFF)
       {
          //实现串口发送函数,将0x55发送到模组
					USART_SendData(USART3,0x55);
          while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕 	
			 }				 
    }
#ifdef PROTOCOL_DEBUG
    GIZWITS_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");
#endif
		
		return len;
}  

 main.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"  

/************************************************
 ALIENTEK战舰STM32开发板实验8
 定时器中断实验
 技术支持:www.openedv.com
 淘宝店铺:http://eboard.taobao.com 
 关注微信公众平台微信号:"正点原子",免费获取STM32资料。
 广州市星翼电子科技有限公司  
 作者:正点原子 @ALIENTEK
************************************************/

/* 用户区当前设备状态结构体*/
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)
{

    //判断当前LED1开关量
	   if(LED1==0)
	    currentDataPoint.valueLEDkey = 1;
      else 
	    currentDataPoint.valueLEDkey = 0;
	
}

 int main(void)
 {		
  int key;
	delay_init();	    	 //延时函数初始化	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 //串口初始化为115200
 	LED_Init();			     //LED端口初始化
	KEY_Init();             //按键初始化
	Gizwits_Init();            
	printf("--------机智云-协议移植LED测试实验----------\r\n");
	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;
	}	

 
}	 
 

Timer.c修改:

编译没有错误后,下载到开发板上,按KEY1进入AirLink连接模式,返回下面的数据。

 打开手机app 右上角+号“一键配置”

 选择工作WiFi(你的手机也要连接上这个WiFi)

 

 开始连接后你的开发板的串口1会打印以下数据

 最后,你就可以用手机控制你开发板上电的LED了

附上修改后的代码:STM32_机智云

评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值