机智云,全球领先的智能硬件软件自助开发及物联网(iot)云服务平台。机智云平台为开发者提供了自助式智能硬件开发工具与开放的云端服务。通过傻瓜化的自助工具、完善的SDK与API服务能力最大限度降低了物联网硬件开发的技术门槛,降低开发者的研发成本,提升开发者的产品投产速度,帮助开发者进行硬件智能化升级,更好的连接、服务最终消费者。使用机智云作为云平台可以轻松实现物联网功能。首先,我们需要注册并进入机智云官网。
一、进入开发者中心
如图,这是我的机智云。
创建一个自定义产品,如下
添加好数据节点,如下,这是一个开关一个led灯:
复制密码,以备用
二、生成代码
1、选择MCU开发其他平台,输入密钥,生成代码包。
2、生成成功,下载到本地
3、移植代码
将 Gizwits 和 Utils 一直到我们的工程中,如下图:
将它们添加进工程
修改 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 is only for smart hardware
* Gizwits Smart Cloud for Smart Products
* Links | Value Added | Open | Neutral | Safety | Own | Free | Ecology
* www.gizwits.com
*
***********************************************************/
#include <stdio.h>
#include <string.h>
#include "stm32f10x.h" // Device header
#include "gizwits_product.h"
#include "Serial3.h"
#include "LED.h"
static uint32_t timerMsCount;
/** Current datapoint */
dataPoint_t currentDataPoint;
/**@} */
/**@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_btn:
currentDataPoint.valuebtn = dataPointPtr->valuebtn;
GIZWITS_LOG("Evt: EVENT_btn %d \n", currentDataPoint.valuebtn);
if(0x01 == currentDataPoint.valuebtn)
{
//user handle
LED1_ON();
}
else
{
//user handle
LED1_OFF();
}
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
GIZWITS_LOG("moduleType : [%d] \n",gprsInfoData->Type);
#else
GIZWITS_LOG("WIF MODULE ...\n");
//Format By moduleInfo_t
GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
#endif
break;
default:
break;
}
}
return 0;
}
/**
* 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*)¤tDataPoint, 0, sizeof(dataPoint_t));
currentDataPoint.valuebtn = 0;
currentDataPoint.valuenum = 0;
}
/**
* @brief gizTimerMs
* millisecond timer maintenance function ,Millisecond increment , Overflow to zero
* @param none
* @return none
*/
void gizTimerMs(void)
{
timerMsCount++;
}
/**
* @brief gizGetTimerCount
* Read system time, millisecond timer
* @param none
* @return System time millisecond
*/
uint32_t gizGetTimerCount(void)
{
return timerMsCount;
}
/**
* @brief mcuRestart
* MCU Reset function
* @param none
* @return none
*/
void mcuRestart(void)
{
__set_FAULTMASK(1);
NVIC_SystemReset();//修改
}
/**
* @brief uartWrite
* Serial write operation, send data to the WiFi module
* @param buf : Data address
* @param len : Data length
*
* @return : Not 0,Serial send success;
* -1,Input Param Illegal
*/
int32_t uartWrite(uint8_t *buf, uint32_t len)
{
uint32_t i = 0;
if(NULL == buf)
{
return -1;
}
#ifdef PROTOCOL_DEBUG
GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
for(i=0; i<len; i++)
{
GIZWITS_LOG("%02x ", buf[i]);
}
GIZWITS_LOG("\n");
#endif
for(i=0; i<len; i++)
{
//USART_SendData(UART, buf[i]);//STM32 test demo
//Serial port to achieve the function, the buf[i] sent to the module
USART_SendData(USART3, buf[i]);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
if(i >=2 && buf[i] == 0xFF)
{
//Serial port to achieve the function, the 0x55 sent to the module
//USART_SendData(UART, 0x55);//STM32 test demo
USART_SendData(USART3, 0x55);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
return len;
}
准备一个定时器程序,定时大约1ms,在定时器中断中调用 gizTimerMs(); 为机智云提供时钟。
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
{
gizTimerMs(); //为机智云提供时钟
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
准备一个UART连接ESP01,并在改UART的中断函数中调用 gizPutData(&value, 1); 函数,用于接受ESP01串口中断发来的数据。
void USART3_IRQHandler(void)
{
uint8_t value = 0;
if(USART_GetITStatus(Serial3_UARTx, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(Serial3_UARTx, USART_IT_RXNE);
value = USART_ReceiveData(USART3); //读取接收到的数据
gizPutData(&value, 1);
}
}
同时配置好按键,在主函数中按键获取函数,并通过按键值选择调用联网和重新配置,如下图:
#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "Timer.h"
#include "gizwits_product.h"
#include "Serial.h"
#include "Serial3.h"
#include "LED.h"
#include "Key.h"
#include "Delay.h"
extern dataPoint_t currentDataPoint;
//协议初始化
void Gizwits_Init(void)
{
Timer_Init();//1ms
Serial_Init();
Serial3_Init();//gizPutData((uint8_t *)&aRxBuffer,1);
userInit();//用户信息初始化,目前只是把结构体信息复位
gizwitsInit();//机智云的初始化
printf("gizwitsInit智能云初始化\r\n");
}
//数据采集
void userHandle(void)
{
currentDataPoint.valuenum = 50;
}
int main()
{
uint8_t keyNum=0;
LED_Init();
Key_Init();
Gizwits_Init();
while(1)
{
keyNum = Key_GetNum();
if(keyNum==1)//按键1按下
{
printf("WIFI进入AirLink连接模式\r\n");
gizwitsSetMode(WIFI_AIRLINK_MODE);//Air-link模式接入
}
if(keyNum==2)//按键2按下99
{
printf("WIFI复位,请重新配置连接\r\n");
gizwitsSetMode(WIFI_RESET_MODE);//WIFI复位
}
userHandle();//数据上行
gizwitsHandle((dataPoint_t *)¤tDataPoint);
}
}
至此,STM32代码移植已经完毕
三、ESP01烧入机智云固件
1、进入机智云固件下载地址,下载如下固件,地址如下
Gizwits
2、本次使用STM32F103C8T6,于是选择8M带combine的
3、打开 flash_download_tool,进行固件下载,如图:
四、APP配置机智云
1、按下按键1,进入配网模式,注意网络wifi网关不能是5GHZ的频段,建议2.4GHz,如图:
2、 点击机智云app左上角的加号,选择Airlink,输入我们的wifi名和密码,点击下一步:
选择乐鑫,然后下一步。
最后等待连接成功即可:
3、连接成功,APP出现一个叫做测试的设备,如下:
五、测试功能
1、显示测试数50,本次采用固定数字测试数字上传情况,代码如下
//数据采集
void userHandle(void)
{
currentDataPoint.valuenum = 50;
}
2、按键开关灯
按下开,此时led会亮起,如下:
再次按下,led会关闭,如下: