第一篇内容:总体设计/系统功能介绍/机智云自助开发平台-开发利器GAgent等等
点击下载:【Io开发笔记】机智云智能浇花器实战(1)-基础Demo实现
第二篇内容:
继电器实现/功能测试/DHT11驱动代码实现/OLED屏幕显示传感器数据/中文字模制作等等
点击下载:机智云智能浇花器实战(2)-基础Demo实现
一,BH1750光照传感器原理图
二,BH1750传感器代码
- #include "bh1750.h"
- #include "delay.h"
- uint8_t BUF[8]; //接收数据缓存区
- int mcy; //进位标志
- / 开始信号 /
- void BH1750_Start()
- {
- BH1750_SDA_H; //拉高数据线
- BH1750_SCL_H; //拉高时钟线
- Delay_nus(5); //延时
- GPIO_ResetBits(BH1750_PORT, BH1750_SDA_PIN); //产生下降沿
- Delay_nus(5); //延时
- GPIO_ResetBits(BH1750_PORT, BH1750_SCL_PIN); //拉低时钟线
- }
- / 停止信号 /
- void BH1750_Stop()
- {
-
BH1750_SDA_L;
-
BH1750_SCL_H; //拉高时钟线
Delay_nus(5);
-
GPIO_SetBits(BH1750_PORT, BH1750_SDA_PIN); //产生上升沿
Delay_nus(5);
- }
- /******************
- 发送应答信号
- 入口参数:ack (0:ACK 1:NAK)
- ******************/
- void BH1750_SendACK(int ack)
- {
-
GPIO_InitTypeDef GPIO_InitStruct;
35.
36. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
37. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
38. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
39. GPIO_Init(BH1750_PORT, &GPIO_InitStruct);
40.
41. ```
```
if(ack == 1) //写应答信号
-
BH1750_SDA_H;
else if(ack == 0)
-
BH1750_SDA_L;
else
-
return;
47. BH1750_SCL_H; //拉高时钟线
48. Delay_nus(5); //延时
49. BH1750_SCL_L; //拉低时钟线
50. Delay_nus(5); //延时
51. }
52.
53. /******************
54. 接收应答信号
55. ******************/
56. int BH1750_RecvACK()
57. {
58. GPIO_InitTypeDef GPIO_InitStruct;
59.
60. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; / *这里一定要设成输入上拉,否则不能读出数据* /
61. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
62. GPIO_InitStruct.GPIO_Pin=BH1750_SDA_PIN;
63. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
64.
65. BH1750_SCL_H; //拉高时钟线
66. Delay_nus(5); //延时
67. ```
```
if(GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN)==1)//读应答信号
-
mcy = 1 ;
69. else
70. ```
```
mcy = 0 ;
- BH1750_SCL_L; //拉低时钟线
- Delay_nus(5); //延时
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
-
return mcy;
76. }
77.
78. /******************
79. 向IIC总线发送一个字节数据
80. ******************/
81. void BH1750_SendByte(uint8_t dat)
82. {
83. uint8_t i;
84. for (i=0; i<8; i++) //8位计数器
85. {
86. ```
```
if( 0X80 & dat )
-
GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN);
else
-
GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN);
dat <<= 1;
-
BH1750_SCL_H; //拉高时钟线
Delay_nus(5);
-
BH1750_SCL_L; //拉低时钟线
Delay_nus(5);
- }
- BH1750_RecvACK();
- }
- uint8_t BH1750_RecvByte()
- {
- uint8_t i;
- uint8_t dat = 0;
- uint8_t bit;
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; /这里一定要设成输入上拉,否则不能读出数据/
- GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct );
- GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN); //使能内部上拉,准备读取数据,
- for (i=0; i<8; i++) //8位计数器
- {
-
dat <<= 1;
BH1750_SCL_H;
-
Delay_nus(5); //延时
if( SET == GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN))
-
bit = 0X01;
else
-
bit = 0x00;
dat |= bit;
-
BH1750_SCL_L; //拉低时钟线
Delay_nus(5);
- }
-
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
128. GPIO_Init(BH1750_PORT, &GPIO_InitStruct );
129. return dat;
130. }
131.
132. void Single_Write_BH1750(uint8_t REG_Address)
133. {
134. BH1750_Start();
135. BH1750_SendByte(SlaveAddress);
136. BH1750_SendByte(REG_Address);
137.
138. BH1750_Stop();
139. }
140.
141.
142. void BH1750_Init()
143. {
144. GPIO_InitTypeDef GPIO_InitStruct;
145. ```
```
/ *开启GPIOB的外设时钟* /
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN | BH1750_SCL_PIN ;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
- Single_Write_BH1750(0x01);
- Delay_nms(180); //延时180ms
- }
- //连续读出BH1750内部数据
- void mread(void)
- {
- uint8_t i;
- BH1750_Start(); //起始信号
- BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号
-
for (i=0; i<3; i++) //连续读取6个地址数据,存储中BUF
164. {
165. ```
```
BUF[i] = BH1750_RecvByte(); //BUF[0]存储0x32地址中的数据
-
if (i == 3)
{
-
BH1750_SendACK(1); //最后一个数据需要回NOACK
}
-
else
{
-
BH1750_SendACK(0); //回应ACK
}
- }
- BH1750_Stop(); //停止信号
- Delay_nms(5);
- }
- floatRead_BH1750(void)
- {
- int dis_data; //变量
-
float temp1;
float temp2;
-
Single_Write_BH1750(0x01); // power on
Single_Write_BH1750(0x10);
-
Delay_nms(180); //延时180ms
mread();
-
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1]
-
temp1=dis_data/1.2;
temp2=10*dis_data/1.2
-
temp2=(int)temp2%10;
return temp1;
- }
复制代码
- #ifndef BH1750_H
- #define BH1750_H
- #include "STM32f10x.h"
- /*
-
定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改
8. ```
ALT ADDRESS引脚接地时地址为0x46, 接电源时地址为0xB8
- */
- #define SlaveAddress 0x46
- #define BH1750_PORT GPIOB
- #define BH1750_SCL_PIN GPIO_Pin_1
- #define BH1750_SDA_PIN GPIO_Pin_0
- #define BH1750_SCL_H GPIO_SetBits(BH1750_PORT,BH1750_SCL_PIN)
- #define BH1750_SCL_L GPIO_ResetBits(BH1750_PORT,BH1750_SCL_PIN)
- #define BH1750_SDA_H GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN)
- #define BH1750_SDA_L GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN)
- extern uint8_t BUF[8]; //接收数据缓存区
- extern int dis_data; //变量
- extern int mcy; //表示进位标志位
- void BH1750_Init(void);
- void conversion(uint32_t temp_data);
- void Single_Write_BH1750(uint8_t REG_Address); //单个写入数据
- uint8_t Single_Read_BH1750(uint8_t REG_Address); //单个读取内部寄存器数据
- void mread(void); //连续的读取内部寄存器数据
- float Read_BH1750(void);
- #endif
复制代码
BH1750传感器代码说明
核心板单独测试程序在PB0PB1管脚是完全正常,不知道是不是核心板的PB2上接了什么暂时还未排查出来问题,如果你是用开发板或者是自己设计的项目板的话,那么程序是直接可以使用的程序依然按照PB0PB1保留。
三,机智云自助开发平台数据点创建
机智云官方网站:https://www.gizwits.com/
步骤1,创建产品
创建好后就会有基本信息
步骤2,填写机智云产品ProductKey
这两个信息比较重要最好是保存下来
- Product Key :9c8a5a8e38344fb4af14b6db0f5b1df7
- Product Secret :45c86d8c6a2a4b1dac7d68df54f6e4f0
复制代码
步骤3,定义自己的数据点
只读:就是只允许赋值数据传感器赋值给平台平台只能读取
可写:就是数据可以被修改继电器的开关状态平台可以修改
四,MCU开发
mcu开发注意事项平台选Common其实就是STM32F103x平台
1,生成代码包
2,下载自动生成的代码包
3,机智云Gizwits协议移植
这两个文件夹要添加到自己的工程
这是添加的文件夹以及文件的目录
4,修改gizwits_product.c
- #include <stdio.h>
- #include <string.h>
- #include "gizwits_product.h"
- #include "usart3.h"
- static uint32_t timerMsCount;
- uint8_t aRxBuffer;
- dataPoint_t currentDataPoint;
- uint8_t wifi_flag;
- //存放事件处理API接口函数
- 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; inum; i++)
- {
-
switch(info->event[i])
{
-
case EVENT_Relay_1:
currentDataPoint.valueRelay_1 = dataPointPtr->valueRelay_1
-
GIZWITS_LOG("Evt: EVENT_Relay_1 %d
", currentDataPoint.valueRelay_1);
if(0x01 == currentDataPoint.valueRelay_1)
-
{
currentDataPoint.valueRelay_1 = 1
-
}
else
-
{
currentDataPoint.valueRelay_1 = 0
-
}
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:
wifi_flag = 1
-
break;
case WIFI_DISCON_M2M:
-
wifi_flag = 0; //WiFi断开标志
break;
-
case WIFI_RSSI:
GIZWITS_LOG("RSSI %d
", wifiData->rssi);
-
break;
case TRANSPARENT_DATA:
-
GIZWITS_LOG("TRANSPARENT_DATA
");
//user handle , Fetch data from [data] , size is [len]
-
break;
case WIFI_NTP:
-
GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d]
",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
break;
-
case MODULE_INFO:
GIZWITS_LOG("MODULE INFO ...
")
-
#if MODULE_TYPE
GIZWITS_LOG("GPRS MODULE ...
")
-
//Format By gprsInfo_t
#else
-
GIZWITS_LOG("WIF MODULE ...
");
//Format By moduleInfo_t
-
GIZWITS_LOG("moduleType : [%d]
",ptModuleInfo->moduleType);
#endif
-
break;
default:
-
break;
}
- }
- return 0;
- }
- void userHandle(void)
- {
- /*
-
currentDataPoint.valueTemp = ;//Add Sensor Data Collection
currentDataPoint.valueHumi =
-
currentDataPoint.valueLight_Intensity = ;//Add Sensor Data Collection
102. */
103. }
104. void userInit(void)
105. {
106. ```
```
memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));
-
currentDataPoint.valueRelay_1 = 0;
currentDataPoint.valueTemp = 0
-
currentDataPoint.valueHumi = 0;
currentDataPoint.valueLight_Intensity = 0
- }
- void gizTimerMs(void)
- {
-
timerMsCount++;
116. }
117.
118. uint32_t gizGetTimerCount(void)
119. {
120. ```
```
return timerMsCount;
- }
- void mcuRestart(void)
- {
-
__set_FAULTMASK(1);
NVIC_SystemReset();
- }
- void TIMER_IRQ_FUN(void)
- {
- gizTimerMs();
- }
- void UART_IRQ_FUN(void)
- {
- uint8_t value = 0;
- gizPutData(&value, 1);
- }
- int32_t uartWrite(uint8_t *buf, uint32_t len)
- {
-
uint32_t i = 0;
if(NULL == buf)
-
{
return -1;
-
}
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)
-
{
USART_SendData(USART3, 0x55);
-
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕
}
-
}
return len;
- }
复制代码
5,修改
- #ifndef _GIZWITS_PRODUCT_H
- #define _GIZWITS_PRODUCT_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include<stdint.h>
- //#include "stm32f1xx.h"
- #include"gizwits_protocol.h"
- /**
-
- */
- #define SOFTWARE_VERSION "03030000"
- /**
-
- */
- #define HARDWARE_VERSION "03010100"
- /**
-
- Communication module model
- */
- #define MODULE_TYPE 0 //0,WIFI ;1,GPRS
- /**[url=home.php?mod=space&uid=830701]@name[/url] TIM3 related macro definition
-
- */
- #define TIMER TIM3
- #define TIMER_IRQ TIM3_IRQn
- #define TIMER_RCC RCC_APB1Periph_TIM3
- #define TIMER_IRQ_FUN TIM3_IRQHandler
- /**@} */
- /**@name USART related macro definition
-
- */
- #define UART_BAUDRATE 9600
- #define UART_PORT 2
- #define UART USART2
- #define UART_IRQ USART2_IRQn
- #define UART_IRQ_FUN USART2_IRQHandler
- #if (UART_PORT == 1)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB2PeriphClockCmd
- #define UART_CLK RCC_APB2Periph_USART1
- #define UART_GPIO_PORT GPIOA
- #define UART_RxPin GPIO_Pin_10
- #define UART_TxPin GPIO_Pin_9
- #endif
- #if (UART_PORT == 2)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
- #define UART_CLK RCC_APB1Periph_USART2
- #define UART_GPIO_PORT GPIOA
- #define UART_RxPin GPIO_Pin_3
- #define UART_TxPin GPIO_Pin_2
- #endif
- #if (UART_PORT == 3)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOC
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
- #define UART_CLK RCC_APB1Periph_USART3
- #define UART_GPIO_PORT GPIOC
- #define UART_RxPin GPIO_Pin_11
- #define UART_TxPin GPIO_Pin_10
- #endif
- /**@} */
- /** User area the current device state structure*/
- extern dataPoint_t currentDataPoint;
- voidgizTimerMs(void);
- uint32_tgizGetTimerCount(void);
- voidtimerInit(void);
- voiduartInit(void);
- voiduserInit(void);
- voiduserHandle(void);
- voidmcuRestart(void);
- int32_tuartWrite(uint8_t *buf, uint32_t len);
- int8_tgizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len);
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码
5,修改gizwits_product.h
Listitem
Listitem
Listitem
Listitem
Listitem
- /**
-
-
- [url=home.php?mod=space&uid=1455510]@file[/url] gizwits_protocol.c
-
- [url=home.php?mod=space&uid=2666770]@Brief[/url] Corresponding gizwits_product.c header file (including product hardware and software version definition)
-
- [url=home.php?mod=space&uid=40524]@author[/url] Gizwits
-
-
- [url=home.php?mod=space&uid=644434]@version[/url] V03030000
-
- [url=home.php?mod=space&uid=855824]@copyright[/url] Gizwits
-
-
- [url=home.php?mod=space&uid=1902110]@NOTE[/url] 机智云.只为智能硬件而生
-
Gizwits Smart Cloud for Smart Products
12. * ```
```
链接|增值ֵ|开放|中立|安全|自有|自由|生态
-
www.gizwits.com
14. *
15. ***************************/
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
28. rb_t pRb;
29. static uint8_t rbBuf[RB_MAX_LEN];
30.
31.
32.
33.
34.
- */
- int32_t gizPutData(uint8_t *buf, uint32_t len)
- {
-
int32_t count = 0;
44.
45. ```
```
if(NULL == buf)
{
47. ```
```
GIZWITS_LOG("ERR: gizPutData buf is empty
");
return -1;
49. ```
```
}
count = rbWrite(&pRb, buf, len);
52. ```
```
if(count != len)
{
54. ```
```
GIZWITS_LOG("ERR: Failed to rbWrite
");
return -1;
56. ```
```
}
return count;
59. }
60.
61.
62.
63. /**
64. * @brief Protocol header initialization
65. *
66. * @param [out] head : Protocol header pointer
67. *
68. * @return 0, success; other, failure
69. */
70. static int8_t gizProtocolHeadInit(protocolHead_t *head)
71. {
72. ```
```
if(NULL == head)
{
74. ```
```
GIZWITS_LOG("ERR: gizProtocolHeadInit head is empty
");
return -1;
76. ```
```
}
memset((uint8_t *)head, 0, sizeof(protocolHead_t));
79. ```
```
head->head[0] = 0xFF;
head->head[1] = 0xFF;
81.
82. ```
```
return 0;
- }
- /**
-
- @brief Protocol ACK check processing function
-
- @param [in] data : data adress
-
- @param [in] len : data length
-
- @return 0, suceess; other, failure
- */
- static int8_t gizProtocolWaitAck(uint8_t *gizdata, uint32_t len)
- {
-
if(NULL == gizdata)
96. ```
```
{
GIZWITS_LOG("ERR: data is empty
");
98. ```
```
return -1;
}
100.
101. ```
```
memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
memcpy((uint8_t *)gizwitsProtocol.waitAck.buf, gizdata, len);
103. ```
```
gizwitsProtocol.waitAck.dataLen = (uint16_t)len;
gizwitsProtocol.waitAck.flag = 1;
106. ```
```
gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();
return 0;
109. }
110.
118. static int8_t ICACHE_FLASH_ATTR gizDataPoint2Event(gizwitsIssued_t *issuedData, eventInfo_t *info, dataPoint_t *dataPoints)
119. {
120. ```
```
if((NULL == issuedData) || (NULL == info) ||(NULL == dataPoints))
{
122. ```
```
GIZWITS_LOG("gizDataPoint2Event Error , Illegal Param
");
return -1;
124. ```
```
}
/** Greater than 1 byte to do bit conversion **/
127. ```
```
if(sizeof(issuedData->attrFlags) > 1)
{
129. ```
```
if(-1 == gizByteOrderExchange((uint8_t *)&issuedData->attrFlags,sizeof(attrFlags_t)))
{
131. ```
```
GIZWITS_LOG("gizByteOrderExchange Error
");
return -1;
133. ```
```
}
}
135.
136. ```
```
if(0x01 == issuedData->attrFlags.flagRelay_1)
{
138. ```
```
info->event[info->num] = EVENT_Relay_1;
info->num++;
140. ```
```
dataPoints->valueRelay_1 = gizStandardDecompressionValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)&issuedData->attrVals.wBitBuf,sizeof(issuedData->attrVals.wBitBuf));
}
142.
143.
144. ```
```
return 0;
- }
- /**
-
- @brief contrasts the current data with the last data
-
- @param [in] cur: current data point data
-
- @param [in] last: last data point data
-
- @return: 0, no change in data; 1, data changes
- */
- static int8_t ICACHE_FLASH_ATTR gizCheckReport(dataPoint_t *cur, dataPoint_t *last)
- {
-
int8_t ret = 0;
158. ```
```
static uint32_t lastReportTime = 0;
uint32_t currentTime = 0;
160.
161. ```
```
if((NULL == cur) || (NULL == last))
{
163. ```
```
GIZWITS_LOG("gizCheckReport Error , Illegal Param
");
return -1;
165. ```
```
}
currentTime = gizGetTimerCount();
167. ```
```
if(last->valueRelay_1 != cur->valueRelay_1)
{
169. ```
```
GIZWITS_LOG("valueRelay_1 Changed
");
ret = 1;
171. ```
```
}
if(last->valueTemp != cur->valueTemp)
174. ```
```
{
if(currentTime - lastReportTime >= REPORT_TIME_MAX)
176. ```
```
{
GIZWITS_LOG("valueTemp Changed
");
178. ```
```
ret = 1;
}
180. ```
```
}
if(last->valueHumi != cur->valueHumi)
182. ```
```
{
if(currentTime - lastReportTime >= REPORT_TIME_MAX)
184. ```
```
{
GIZWITS_LOG("valueHumi Changed
");
186. ```
```
ret = 1;
}
188. ```
```
}
if(last->valueLight_Intensity != cur->valueLight_Intensity)
190. ```
```
{
if(currentTime - lastReportTime >= REPORT_TIME_MAX)
192. ```
```
{
GIZWITS_LOG("valueLight_Intensity Changed
");
194. ```
```
ret = 1;
}
196. ```
```
}
if(1 == ret)
199. ```
```
{
lastReportTime = gizGetTimerCount();
201. ```
```
}
return ret;
203. }
204.
205. /**
206. * @brief User data point data is converted to wit the cloud to report data point data
207. *
208. * @param [in] dataPoints: user data point data address
209. * @param [out] devStatusPtr: wit the cloud data point data address
210. *
211. * @return 0, the correct return; -1, the error returned
212. */
213. static int8_t ICACHE_FLASH_ATTR gizDataPoints2ReportData(dataPoint_t *dataPoints , devStatus_t *devStatusPtr)
214. {
215. ```
```
if((NULL == dataPoints) || (NULL == devStatusPtr))
{
217. ```
```
GIZWITS_LOG("gizDataPoints2ReportData Error , Illegal Param
");
return -1;
219. ```
```
}
gizMemset((uint8_t *)devStatusPtr->wBitBuf,0,sizeof(devStatusPtr->wBitBuf));
222.
223. ```
```
gizStandardCompressValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)devStatusPtr,dataPoints->valueRelay_1);
gizByteOrderExchange((uint8_t *)devStatusPtr->wBitBuf,sizeof(devStatusPtr->wBitBuf));
225.
226. ```
```
devStatusPtr->valueTemp = gizY2X(Temp_RATIO, Temp_ADDITION, dataPoints->valueTemp);
devStatusPtr->valueHumi = gizY2X(Humi_RATIO, Humi_ADDITION, dataPoints->valueHumi);
228. ```
```
devStatusPtr->valueLight_Intensity = gizY2X(Light_Intensity_RATIO, Light_Intensity_ADDITION, dataPoints->valueLight_Intensity);
return 0;
234. }
235.
236.
237.
245. static int8_t gizProtocolIssuedProcess(char *did, uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen)
246. {
247. ```
```
uint8_t issuedAction = inData[0];
if((NULL == inData)||(NULL == outData)||(NULL == outLen))
250. ```
```
{
GIZWITS_LOG("gizProtocolIssuedProcess Error , Illegal Param
");
252. ```
```
return -1;
}
254.
255. ```
```
if(NULL == did)
{
257. ```
```
memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent, 0, sizeof(eventInfo_t));
switch(issuedAction)
259. ```
```
{
case ACTION_CONTROL_DEVICE:
261. ```
```
gizDataPoint2Event((gizwitsIssued_t *)&inData[1], &gizwitsProtocol.issuedProcessEvent,&gizwitsProtocol.gizCurrentDataPoint);
gizwitsProtocol.issuedFlag = ACTION_CONTROL_TYPE;
263. ```
```
outData = NULL;
*outLen = 0;
265. ```
```
break;
case ACTION_READ_DEV_STATUS:
268. ```
```
if(0 == gizDataPoints2ReportData(&gizwitsProtocol.gizLastDataPoint,&gizwitsProtocol.reportData.devStatus))
{
270. ```
```
memcpy(outData+1, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(gizwitsReport_t));
outData[0] = ACTION_READ_DEV_STATUS_ACK;
272. ```
```
*outLen = sizeof(gizwitsReport_t)+1;
}
274. ```
```
else
{
276. ```
```
return -1;
}
278. ```
```
break;
case ACTION_W2D_TRANSPARENT_DATA:
280. ```
```
memcpy(gizwitsProtocol.transparentBuff, &inData[1], inLen-1);
gizwitsProtocol.transparentLen = inLen - 1;
282.
283. ```
```
gizwitsProtocol.issuedProcessEvent.event[gizwitsProtocol.issuedProcessEvent.num] = TRANSPARENT_DATA;
gizwitsProtocol.issuedProcessEvent.num++;
285. ```
```
gizwitsProtocol.issuedFlag = ACTION_W2D_TRANSPARENT_TYPE;
outData = NULL;
287. ```
```
*outLen = 0;
break;
289.
290. ```
```
default:
break;
292. ```
```
}
}
294.
295. ```
```
return 0;
- }
- /**
-
- @brief The protocol sends data back , P0 ACK
-
- @param [in] head : Protocol head pointer
-
- @param [in] data : Payload data
-
- @param [in] len : Payload data length
-
- @param [in] proFlag : DID flag ,1 for Virtual sub device did ,0 for single product or gateway
-
- -1,Input Param Illegal
- -2,Serial send faild
- */
- static int32_t gizProtocolIssuedDataAck(protocolHead_t *head, uint8_t *gizdata, uint32_t len, uint8_t proFlag)
- {
-
int32_t ret = 0;
312. ```
```
uint8_t tx_buf[RB_MAX_LEN];
uint32_t offset = 0;
314. ```
```
uint8_t sDidLen = 0;
uint16_t data_len = 0;
316. ```
```
uint8_t *pTxBuf = tx_buf;
if(NULL == gizdata)
318. ```
```
{
GIZWITS_LOG("[ERR] data Is Null
");
320. ```
```
return -1;
}
322.
323.
324. ```
```
if(0x1 == proFlag)
{
326. ```
```
sDidLen = *((uint8_t *)head + sizeof(protocolHead_t));
data_len = 5 + 1 + sDidLen + len;
328. ```
```
}
else
330. ```
```
{
data_len = 5 + len;
332. ```
```
}
GIZWITS_LOG("len = %d , sDidLen = %d ,data_len = %d
", len,sDidLen,data_len);
334. ```
```
*pTxBuf ++= 0xFF;
*pTxBuf ++= 0xFF;
336. ```
```
*pTxBuf ++= (uint8_t)(data_len>>8);
*pTxBuf ++= (uint8_t)(data_len);
338. ```
```
*pTxBuf ++= head->cmd + 1;
*pTxBuf ++= head->sn;
340. ```
```
*pTxBuf ++= 0x00;
*pTxBuf ++= proFlag;
342. ```
```
offset = 8;
if(0x1 == proFlag)
344. ```
```
{
*pTxBuf ++= sDidLen;
346. ```
```
offset += 1;
memcpy(&tx_buf[offset],(uint8_t *)head+sizeof(protocolHead_t)+1,sDidLen);
348. ```
```
offset += sDidLen;
pTxBuf += sDidLen;
350.
351. ```
```
}
if(0 != len)
353. ```
```
{
memcpy(&tx_buf[offset],gizdata,len);
355. ```
```
}
tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));
357.
358. ```
```
ret = uartWrite(tx_buf, data_len+4);
if(ret < 0)
360. ```
```
{
GIZWITS_LOG("uart write error %d
", ret);
362. ```
```
return -2;
}
364.
365. ```
```
return 0;
- }
- /**
-
- @brief Report data interface
-
- @param [in] action : PO action
-
- @param [in] data : Payload data
-
- @param [in] len : Payload data length
-
- -1,Input Param Illegal
- -2,Serial send faild
- */
- static int32_t gizReportData(uint8_t action, uint8_t *gizdata, uint32_t len)
- {
-
int32_t ret = 0;
382. ```
```
protocolReport_t protocolReport;
if(NULL == gizdata)
385. ```
```
{
GIZWITS_LOG("gizReportData Error , Illegal Param
");
387. ```
```
return -1;
}
389. ```
```
gizProtocolHeadInit((protocolHead_t *)&protocolReport);
protocolReport.head.cmd = CMD_REPORT_P0;
391. ```
```
protocolReport.head.sn = gizwitsProtocol.sn++;
protocolReport.action = action;
393. ```
```
protocolReport.head.len = exchangeBytes(sizeof(protocolReport_t)-4);
memcpy((gizwitsReport_t *)&protocolReport.reportData, (gizwitsReport_t *)gizdata,len);
395. ```
```
protocolReport.sum = gizProtocolSum((uint8_t *)&protocolReport, sizeof(protocolReport_t));
ret = uartWrite((uint8_t *)&protocolReport, sizeof(protocolReport_t));
398. ```
```
if(ret < 0)
{
400. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
return -2;
402. ```
```
}
gizProtocolWaitAck((uint8_t *)&protocolReport, sizeof(protocolReport_t));
405.
406. ```
```
return ret;
- }/**
-
- @brief Datapoints reporting mechanism
-
-
- Changes are reported immediately
-
-
- Data timing report , 600000 Millisecond
- *@param [in] currentData : Current datapoints value
-
- */
- static void gizDevReportPolicy(dataPoint_t *currentData)
- {
-
static uint32_t lastRepTime = 0;
420. ```
```
uint32_t timeNow = gizGetTimerCount();
if((1 == gizCheckReport(currentData, (dataPoint_t *)&gizwitsProtocol.gizLastDataPoint)))
423. ```
```
{
GIZWITS_LOG("changed, report data
");
425. ```
```
if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))
{
427. ```
```
gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t)); }
memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));
429. ```
```
}
if((0 == (timeNow % (600000))) && (lastRepTime != timeNow))
432. ```
```
{
GIZWITS_LOG("Info: 600S report data
");
434. ```
```
if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))
{
436. ```
```
gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t));
}
438. ```
```
memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));
lastRepTime = timeNow;
441. ```
```
}
- }
- /**
-
- @brief Get a packet of data from the ring buffer
-
- @param [in] rb : Input data address
-
- @param [out] data : Output data address
-
- @param [out] len : Output data length
-
- @return : 0,Return correct ;-1,Return failure;-2,Data check failure
- */
- static int8_t gizProtocolGetOnePacket(rb_t *rb, uint8_t *gizdata, uint16_t *len)
- {
-
int32_t ret = 0;
456. ```
```
uint8_t sum = 0;
int32_t i = 0;
458. ```
```
uint8_t tmpData;
uint8_t tmpLen = 0;
460. ```
```
uint16_t tmpCount = 0;
static uint8_t protocolFlag = 0;
462. ```
```
static uint16_t protocolCount = 0;
static uint8_t lastData = 0;
464. ```
```
static uint8_t debugCount = 0;
uint8_t *protocolBuff = gizdata;
466. ```
```
protocolHead_t *head = NULL;
if((NULL == rb) || (NULL == gizdata) ||(NULL == len))
469. ```
```
{
GIZWITS_LOG("gizProtocolGetOnePacket Error , Illegal Param
");
471. ```
```
return -1;
}
473.
474. ```
```
tmpLen = rbCanRead(rb);
if(0 == tmpLen)
476. ```
```
{
return -1;
478. ```
```
}
for(i=0; i<tmpLen; i++)
481. ```
```
{
ret = rbRead(rb, &tmpData, 1);
483. ```
```
if(0 != ret)
{
485. ```
```
if((0xFF == lastData) && (0xFF == tmpData))
{
487. ```
```
if(0 == protocolFlag)
{
489. ```
```
protocolBuff[0] = 0xFF;
protocolBuff[1] = 0xFF;
491. ```
```
protocolCount = 2;
protocolFlag = 1;
493. ```
```
}
else
495. ```
```
{
if((protocolCount > 4) && (protocolCount != tmpCount))
497. ```
```
{
protocolBuff[0] = 0xFF;
499. ```
```
protocolBuff[1] = 0xFF;
protocolCount = 2;
501. ```
```
}
}
503. ```
```
}
else if((0xFF == lastData) && (0x55 == tmpData))
505. ```
```
{
}
507. ```
```
else
{
509. ```
```
if(1 == protocolFlag)
{
511. ```
```
protocolBuff[protocolCount] = tmpData;
protocolCount++;
513.
514. ```
```
if(protocolCount > 4)
{
516. ```
```
head = (protocolHead_t *)protocolBuff;
tmpCount = exchangeBytes(head->len)+4;
518. ```
```
if(protocolCount == tmpCount)
{
520. ```
```
break;
}
522. ```
```
}
}
524. ```
```
}
lastData = tmpData;
527. ```
```
debugCount++;
}
529. ```
```
}
if((protocolCount > 4) && (protocolCount == tmpCount))
532. ```
```
{
sum = gizProtocolSum(protocolBuff, protocolCount);
534.
535. ```
```
if(protocolBuff[protocolCount-1] == sum)
{
537. ```
```
memcpy(gizdata, protocolBuff, tmpCount);
*len = tmpCount;
539. ```
```
protocolFlag = 0;
protocolCount = 0;
542. ```
```
debugCount = 0;
lastData = 0;
544.
545. ```
```
return 0;
}
547. ```
```
else
{
549. ```
```
return -2;
}
551. ```
```
}
return 1;
554. }
555.
556.
557.
558.
567. static void gizProtocolResendData(void)
568. {
569. ```
```
int32_t ret = 0;
if(0 == gizwitsProtocol.waitAck.flag)
572. ```
```
{
return;
574. ```
```
}
GIZWITS_LOG("Warning: timeout, resend data
");
577.
578. ```
```
ret = uartWrite(gizwitsProtocol.waitAck.buf, gizwitsProtocol.waitAck.dataLen);
if(ret != gizwitsProtocol.waitAck.dataLen)
580. ```
```
{
GIZWITS_LOG("ERR: resend data error
");
582. ```
```
}
gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();
585. }
586.
587.
594. static int8_t gizProtocolWaitAckCheck(protocolHead_t *head)
595. {
596. ```
```
protocolHead_t *waitAckHead = (protocolHead_t *)gizwitsProtocol.waitAck.buf;
if(NULL == head)
599. ```
```
{
GIZWITS_LOG("ERR: data is empty
");
601. ```
```
return -1;
}
603.
604. ```
```
if(waitAckHead->cmd+1 == head->cmd)
{
606. ```
```
memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
}
608.
609. ```
```
return 0;
- }
- /**
-
- @brief Send general protocol message data
-
- @param [in] head : Protocol header address
-
- @return : Return effective data length;-1,return failure
- */
- static int32_t gizProtocolCommonAck(protocolHead_t *head)
- {
-
int32_t ret = 0;
622. ```
```
protocolCommon_t ack;
if(NULL == head)
625. ```
```
{
GIZWITS_LOG("ERR: gizProtocolCommonAck data is empty
");
627. ```
```
return -1;
}
629. ```
```
memcpy((uint8_t *)&ack, (uint8_t *)head, sizeof(protocolHead_t));
ack.head.cmd = ack.head.cmd+1;
631. ```
```
ack.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
ack.sum = gizProtocolSum((uint8_t *)&ack, sizeof(protocolCommon_t));
633.
634. ```
```
ret = uartWrite((uint8_t *)&ack, sizeof(protocolCommon_t));
if(ret < 0)
636. ```
```
{
GIZWITS_LOG("ERR: uart write error %d
", ret);
638. ```
```
}
return ret;
641. }
642.
643.
652. static void gizProtocolAckHandle(void)
653. {
654. ```
```
if(1 == gizwitsProtocol.waitAck.flag)
{
656. ```
```
if(SEND_MAX_NUM > gizwitsProtocol.waitAck.num)
{
658. ```
```
if(SEND_MAX_TIME < (gizGetTimerCount() - gizwitsProtocol.waitAck.sendTime))
660. ```
```
{
GIZWITS_LOG("Warning:gizProtocolResendData %d %d %d
", gizGetTimerCount(), gizwitsProtocol.waitAck.sendTime, gizwitsProtocol.waitAck.num);
662. ```
```
gizProtocolResendData();
gizwitsProtocol.waitAck.num++;
664. ```
```
}
}
666. ```
```
else
{
668. ```
```
memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
}
670. ```
```
}
- }
- /**
-
- @brief Protocol 4.1 WiFi module requests device information
-
- @param[in] head : Protocol header address
-
- @return Return effective data length;-1,return failure
- */
- static int32_t gizProtocolGetDeviceInfo(protocolHead_t * head)
- {
-
int32_t ret = 0;
683. ```
```
protocolDeviceInfo_t deviceInfo;
if(NULL == head)
686. ```
```
{
GIZWITS_LOG("gizProtocolGetDeviceInfo Error , Illegal Param
");
688. ```
```
return -1;
}
690.
691. ```
```
gizProtocolHeadInit((protocolHead_t *)&deviceInfo);
deviceInfo.head.cmd = ACK_GET_DEVICE_INFO;
693. ```
```
deviceInfo.head.sn = head->sn;
memcpy((uint8_t *)deviceInfo.protocolVer, protocol_VERSION, 8);
695. ```
```
memcpy((uint8_t *)deviceInfo.p0Ver, P0_VERSION, 8);
memcpy((uint8_t *)deviceInfo.softVer, SOFTWARE_VERSION, 8);
697. ```
```
memcpy((uint8_t *)deviceInfo.hardVer, HARDWARE_VERSION, 8);
memcpy((uint8_t *)deviceInfo.productKey, PRODUCT_KEY, strlen(PRODUCT_KEY));
699. ```
```
memcpy((uint8_t *)deviceInfo.productSecret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
memset((uint8_t *)deviceInfo.devAttr, 0, 8);
701. ```
```
deviceInfo.devAttr[7] |= DEV_IS_GATEWAY<<0;
deviceInfo.devAttr[7] |= (0x01<<1);
703. ```
```
deviceInfo.ninableTime = exchangeBytes(NINABLETIME);
deviceInfo.head.len = exchangeBytes(sizeof(protocolDeviceInfo_t)-4);
705. ```
```
deviceInfo.sum = gizProtocolSum((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));
ret = uartWrite((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));
708. ```
```
if(ret < 0)
{
710. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
}
712.
713. ```
```
return ret;
- }
- /**
-
- @brief Protocol 4.7 Handling of illegal message notification
-
- @param[in] head : Protocol header address
-
- @param[in] errno : Illegal message notification type
-
- @return 0, success; other, failure
- */
- static int32_t gizProtocolErrorCmd(protocolHead_t *head,errorPacketsType_t errno)
- {
-
int32_t ret = 0;
726. ```
```
protocolErrorType_t errorType;
if(NULL == head)
729. ```
```
{
GIZWITS_LOG("gizProtocolErrorCmd Error , Illegal Param
");
731. ```
```
return -1;
}
733. ```
```
gizProtocolHeadInit((protocolHead_t *)&errorType);
errorType.head.cmd = ACK_ERROR_PACKAGE;
735. ```
```
errorType.head.sn = head->sn;
errorType.head.len = exchangeBytes(sizeof(protocolErrorType_t)-4);
738. ```
```
errorType.error = errno;
errorType.sum = gizProtocolSum((uint8_t *)&errorType, sizeof(protocolErrorType_t));
740.
741. ```
```
ret = uartWrite((uint8_t *)&errorType, sizeof(protocolErrorType_t));
if(ret < 0)
743. ```
```
{
GIZWITS_LOG("ERR: uart write error %d
", ret);
745. ```
```
}
return ret;
748. }
749.
750.
757. static int8_t gizProtocolNTP(protocolHead_t *head)
758. {
759. ```
```
protocolUTT_t *UTTInfo = (protocolUTT_t *)head;
if(NULL == head)
762. ```
```
{
GIZWITS_LOG("ERR: NTP is empty
");
764. ```
```
return -1;
}
766.
767. ```
```
memcpy((uint8_t *)&gizwitsProtocol.TimeNTP,(uint8_t *)UTTInfo->time, (7 + 4));
gizwitsProtocol.TimeNTP.year = exchangeBytes(gizwitsProtocol.TimeNTP.year);
769. ```
```
gizwitsProtocol.TimeNTP.ntp =exchangeWord(gizwitsProtocol.TimeNTP.ntp);
gizwitsProtocol.NTPEvent.event[gizwitsProtocol.NTPEvent.num] = WIFI_NTP;
772. ```
```
gizwitsProtocol.NTPEvent.num++;
gizwitsProtocol.issuedFlag = GET_NTP_TYPE;
775.
776.
777. ```
```
return 0;
- }
- /**
-
- @brief Protocol 4.4 Device MCU restarts function
-
-
- */
- static void gizProtocolReboot(void)
- {
-
uint32_t timeDelay = gizGetTimerCount();
789.
790. ```
```
while((gizGetTimerCount() - timeDelay) <= 600);
792. ```
```
mcuRestart();
- }
- /**
-
- @brief Protocol 4.5 :The WiFi module informs the device MCU of working status about the WiFi module
-
- @param[in] status WiFi module working status
-
- */
- static int8_t gizProtocolModuleStatus(protocolWifiStatus_t *status)
- {
-
static wifiStatus_t lastStatus;
804.
805. ```
```
if(NULL == status)
{
807. ```
```
GIZWITS_LOG("gizProtocolModuleStatus Error , Illegal Param
");
return -1;
809. ```
```
}
status->ststus.value = exchangeBytes(status->ststus.value);
812.
813. ```
```
if(lastStatus.types.onboarding != status->ststus.types.onboarding)
815. ```
```
{
if(1 == status->ststus.types.onboarding)
817. ```
```
{
if(1 == status->ststus.types.softap)
819. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;
821. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("OnBoarding: SoftAP or Web mode
");
823. ```
```
}
if(1 == status->ststus.types.station)
826. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_AIRLINK;
828. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("OnBoarding: AirLink mode
");
830. ```
```
}
}
832. ```
```
else
{
834. ```
```
if(1 == status->ststus.types.softap)
{
836. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;
gizwitsProtocol.wifiStatusEvent.num++;
838. ```
```
GIZWITS_LOG("OnBoarding: SoftAP or Web mode
");
}
840.
841. ```
```
if(1 == status->ststus.types.station)
{
843. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_STATION;
gizwitsProtocol.wifiStatusEvent.num++;
845. ```
```
GIZWITS_LOG("OnBoarding: Station mode
");
}
847. ```
```
}
}
849.
850. ```
```
if(lastStatus.types.binding != status->ststus.types.binding)
852. ```
```
{
lastStatus.types.binding = status->ststus.types.binding;
854. ```
```
if(1 == status->ststus.types.binding)
{
856. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_BINDING;
gizwitsProtocol.wifiStatusEvent.num++;
858. ```
```
GIZWITS_LOG("WiFi status: in binding mode
");
}
860. ```
```
else
{
862. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_BINDING;
gizwitsProtocol.wifiStatusEvent.num++;
864. ```
```
GIZWITS_LOG("WiFi status: out binding mode
");
}
866. ```
```
}
//router status
869. ```
```
if(lastStatus.types.con_route != status->ststus.types.con_route)
{
871. ```
```
lastStatus.types.con_route = status->ststus.types.con_route;
if(1 == status->ststus.types.con_route)
873. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_ROUTER;
875. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("WiFi status: connected router
");
877. ```
```
}
else
879. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_ROUTER;
881. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("WiFi status: disconnected router
");
883. ```
```
}
}
885.
886. ```
```
if(lastStatus.types.con_m2m != status->ststus.types.con_m2m)
888. ```
```
{
lastStatus.types.con_m2m = status->ststus.types.con_m2m;
890. ```
```
if(1 == status->ststus.types.con_m2m)
{
892. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_M2M;
gizwitsProtocol.wifiStatusEvent.num++;
894. ```
```
GIZWITS_LOG("WiFi status: connected m2m
");
}
896. ```
```
else
{
898. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_M2M;
gizwitsProtocol.wifiStatusEvent.num++;
900. ```
```
GIZWITS_LOG("WiFi status: disconnected m2m
");
}
902. ```
```
}
//APP status
905. ```
```
if(lastStatus.types.app != status->ststus.types.app)
{
907. ```
```
lastStatus.types.app = status->ststus.types.app;
if(1 == status->ststus.types.app)
909. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_APP;
911. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("WiFi status: app connect
");
913. ```
```
}
else
915. ```
```
{
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_APP;
917. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
GIZWITS_LOG("WiFi status: no app connect
");
919. ```
```
}
}
921.
922. ```
```
if(lastStatus.types.test != status->ststus.types.test)
924. ```
```
{
lastStatus.types.test = status->ststus.types.test;
926. ```
```
if(1 == status->ststus.types.test)
{
928. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_TESTMODE;
gizwitsProtocol.wifiStatusEvent.num++;
930. ```
```
GIZWITS_LOG("WiFi status: in test mode
");
}
932. ```
```
else
{
934. ```
```
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_TESTMODE;
gizwitsProtocol.wifiStatusEvent.num++;
936. ```
```
GIZWITS_LOG("WiFi status: out test mode
");
}
938. ```
```
}
gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_RSSI;
941. ```
```
gizwitsProtocol.wifiStatusEvent.num++;
gizwitsProtocol.wifiStatusData.rssi = status->ststus.types.rssi;
943. ```
```
GIZWITS_LOG("RSSI is %d
", gizwitsProtocol.wifiStatusData.rssi);
gizwitsProtocol.issuedFlag = WIFI_STATUS_TYPE;
946.
947. ```
```
return 0;
- }
- /**@name Gizwits User API interface
-
- */
- /**
-
- @brief gizwits Protocol initialization interface
-
- Protocol-related timer, serial port initialization
-
-
-
- */
- void gizwitsInit(void)
- {
-
pRb.rbCapacity = RB_MAX_LEN;
968. ```
```
pRb.rbBuff = rbBuf;
if(0 == rbCreate(&pRb))
970. ```
```
{
GIZWITS_LOG("rbCreate Success
");
972. ```
```
}
else
974. ```
```
{
GIZWITS_LOG("rbCreate Faild
");
976. ```
```
}
memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));
979. }
980.
981.
990. int32_t gizwitsSetMode(uint8_t mode)
991. {
992. ```
```
int32_t ret = 0;
protocolCfgMode_t cfgMode;
994. ```
```
protocolCommon_t setDefault;
switch(mode)
997. ```
```
{
case WIFI_RESET_MODE:
999. ```
```
gizProtocolHeadInit((protocolHead_t *)&setDefault);
setDefault.head.cmd = CMD_SET_DEFAULT;
1001. ```
```
setDefault.head.sn = gizwitsProtocol.sn++;
setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
1003. ```
```
setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1005. ```
```
if(ret < 0)
{
1007. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
}
1009.
1010. ```
```
gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
break;
1012. ```
```
case WIFI_SOFTAP_MODE:
gizProtocolHeadInit((protocolHead_t *)&cfgMode);
1014. ```
```
cfgMode.head.cmd = CMD_WIFI_CONFIG;
cfgMode.head.sn = gizwitsProtocol.sn++;
1016. ```
```
cfgMode.cfgMode = mode;
cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
1018. ```
```
cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
1020. ```
```
if(ret < 0)
{
1022. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
}
1024. ```
```
gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
break;
1026. ```
```
case WIFI_AIRLINK_MODE:
gizProtocolHeadInit((protocolHead_t *)&cfgMode);
1028. ```
```
cfgMode.head.cmd = CMD_WIFI_CONFIG;
cfgMode.head.sn = gizwitsProtocol.sn++;
1030. ```
```
cfgMode.cfgMode = mode;
cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
1032. ```
```
cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
1034. ```
```
if(ret < 0)
{
1036. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
}
1038. ```
```
gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
break;
1040. ```
```
case WIFI_PRODUCTION_TEST:
gizProtocolHeadInit((protocolHead_t *)&setDefault);
1042. ```
```
setDefault.head.cmd = CMD_PRODUCTION_TEST;
setDefault.head.sn = gizwitsProtocol.sn++;
1044. ```
```
setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1046. ```
```
ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
if(ret < 0)
1048. ```
```
{
GIZWITS_LOG("ERR: uart write error %d
", ret);
1050. ```
```
}
gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1053. ```
```
break;
case WIFI_NINABLE_MODE:
1055. ```
```
gizProtocolHeadInit((protocolHead_t *)&setDefault);
setDefault.head.cmd = CMD_NINABLE_MODE;
1057. ```
```
setDefault.head.sn = gizwitsProtocol.sn++;
setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
1059. ```
```
setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1061. ```
```
if(ret < 0)
{
1063. ```
```
GIZWITS_LOG("ERR: uart write error %d
", ret);
}
1065.
1066. ```
```
gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
break;
1068. ```
```
case WIFI_REBOOT_MODE:
gizProtocolHeadInit((protocolHead_t *)&setDefault);
1070. ```
```
setDefault.head.cmd = CMD_REBOOT_MODULE;
setDefault.head.sn = gizwitsProtocol.sn++;
1072. ```
```
setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1074. ```
```
ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
if(ret < 0)
1076. ```
```
{
GIZWITS_LOG("ERR: uart write error %d
", ret);
1078. ```
```
}
gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
1081. ```
```
break;
default:
1083. ```
```
GIZWITS_LOG("ERR: CfgMode error!
");
break;
1085. ```
```
}
return ret;
1088. }
1089.
1090. /**
1091. * @brief Get the the network time
1092.
1093. * Protocol 4.13:"Device MCU send" of "the MCU requests access to the network time"
1094.
1095. * @param[in] none
1096. * @return none
1097. */
1098. void gizwitsGetNTP(void)
1099. {
1100. ```
```
int32_t ret = 0;
protocolCommon_t getNTP;
1102.
1103. ```
```
gizProtocolHeadInit((protocolHead_t *)&getNTP);
getNTP.head.cmd = CMD_GET_NTP;
1105. ```
```
getNTP.head.sn = gizwitsProtocol.sn++;
getNTP.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
1107. ```
```
getNTP.sum = gizProtocolSum((uint8_t *)&getNTP, sizeof(protocolCommon_t));
ret = uartWrite((uint8_t *)&getNTP, sizeof(protocolCommon_t));
1109. ```
```
if(ret < 0)
{
1111. ```
```
GIZWITS_LOG("ERR[NTP]: uart write error %d
", ret);
}
1113.
1114. ```
```
gizProtocolWaitAck((uint8_t *)&getNTP, sizeof(protocolCommon_t));
- }
- /**
-
-
-
- */
- void gizwitsGetModuleInfo(void)
- {
-
int32_t ret = 0;
1129. ```
```
protocolGetModuleInfo_t getModuleInfo;
gizProtocolHeadInit((protocolHead_t *)&getModuleInfo);
1132. ```
```
getModuleInfo.head.cmd = CMD_ASK_MODULE_INFO;
getModuleInfo.head.sn = gizwitsProtocol.sn++;
1134. ```
```
getModuleInfo.type = 0x0;
getModuleInfo.head.len = exchangeBytes(sizeof(protocolGetModuleInfo_t)-4);
1136. ```
```
getModuleInfo.sum = gizProtocolSum((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
ret = uartWrite((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
1138. ```
```
if(ret < 0)
{
1140. ```
```
GIZWITS_LOG("ERR[NTP]: uart write error %d
", ret);
}
1142.
1143. ```
```
gizProtocolWaitAck((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
- }
- /**
-
- @brief Module Info Analyse
-
-
- @return 0, Success, , other,Faild
- */
- static int8_t gizProtocolModuleInfoHandle(protocolHead_t *head)
- {
-
protocolModuleInfo_t *moduleInfo = (protocolModuleInfo_t *)head;
1157.
1158. ```
```
if(NULL == head)
{
1160. ```
```
GIZWITS_LOG("NTP is empty
");
return -1;
1162. ```
```
}
memcpy((uint8_t *)&gizwitsProtocol.wifiModuleNews,(uint8_t *)&moduleInfo->wifiModuleInfo, sizeof(moduleInfo_t));
1165.
1166. ```
```
gizwitsProtocol.moduleInfoEvent.event[gizwitsProtocol.moduleInfoEvent.num] = MODULE_INFO;
gizwitsProtocol.moduleInfoEvent.num++;
1168.
1169. ```
```
gizwitsProtocol.issuedFlag = GET_MODULEINFO_TYPE;
return 0;
1173. }
1174.
1175. /**
1176. * @brief Protocol handling function
1177.
1178. *
1179.
1180. * @param [in] currentData :The protocol data pointer
1181. * @return none
1182. */
1183. int32_t gizwitsHandle(dataPoint_t *currentData)
1184. {
1185. ```
```
int8_t ret = 0;
- #ifdef PROTOCOL_DEBUG
-
uint16_t i = 0;
1188.
1189. ```
```
uint8_t ackData[RB_MAX_LEN];
uint16_t protocolLen = 0;
1191. ```
```
uint32_t ackLen = 0;
protocolHead_t *recvHead = NULL;
1193. ```
```
char *didPtr = NULL;
uint16_t offset = 0;
1195.
1196.
1197. ```
```
if(NULL == currentData)
{
1199. ```
```
GIZWITS_LOG("GizwitsHandle Error , Illegal Param
");
return -1;
1201. ```
```
}
/ resend strategy /
1204. ```
```
gizProtocolAckHandle();
ret = gizProtocolGetOnePacket(&pRb, gizwitsProtocol.protocolBuf, &protocolLen);
1206.
1207. ```
```
if(0 == ret)
{
1209. ```
```
GIZWITS_LOG("Get One Packet!
");
- #ifdef PROTOCOL_DEBUG
-
GIZWITS_LOG("WiFi2MCU[%4d:%4d]: ", gizGetTimerCount(), protocolLen);
1213. ```
```
for(i=0; i<protocolLen;i++)
{
1215. ```
```
GIZWITS_LOG("%02x ", gizwitsProtocol.protocolBuf[i]);
}
1217. ```
```
GIZWITS_LOG("
");
- #endif
-
recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;
1221. ```
```
switch (recvHead->cmd)
{
1223. ```
```
case CMD_GET_DEVICE_INTO:
gizProtocolGetDeviceInfo(recvHead);
1225. ```
```
break;
case CMD_ISSUED_P0:
1227. ```
```
GIZWITS_LOG("flag %x %x
", recvHead->flags[0], recvHead->flags[1]);
//offset = 1;
1229.
1230. ```
```
if(0 == gizProtocolIssuedProcess(didPtr, gizwitsProtocol.protocolBuf+sizeof(protocolHead_t)+offset, protocolLen-(sizeof(protocolHead_t)+offset+1), ackData, &ackLen))
{
1232. ```
```
gizProtocolIssuedDataAck(recvHead, ackData, ackLen,recvHead->flags[1]);
GIZWITS_LOG("AckData :
");
1234. ```
```
}
break;
1236. ```
```
case CMD_HEARTBEAT:
gizProtocolCommonAck(recvHead);
1238. ```
```
break;
case CMD_WIFISTATUS:
1240. ```
```
gizProtocolCommonAck(recvHead);
gizProtocolModuleStatus((protocolWifiStatus_t *)recvHead);
1242. ```
```
break;
case ACK_REPORT_P0:
1244. ```
```
case ACK_WIFI_CONFIG:
case ACK_SET_DEFAULT:
1246. ```
```
case ACK_NINABLE_MODE:
case ACK_REBOOT_MODULE:
1248. ```
```
gizProtocolWaitAckCheck(recvHead);
break;
1250. ```
```
case CMD_MCU_REBOOT:
gizProtocolCommonAck(recvHead);
1252. ```
```
GIZWITS_LOG("report:MCU reboot!
");
gizProtocolReboot();
1255. ```
```
break;
case CMD_ERROR_PACKAGE:
1257. ```
```
break;
case ACK_PRODUCTION_TEST:
1259. ```
```
gizProtocolWaitAckCheck(recvHead);
GIZWITS_LOG("Ack PRODUCTION_MODE success
");
1261. ```
```
break;
case ACK_GET_NTP:
1263. ```
```
gizProtocolWaitAckCheck(recvHead);
gizProtocolNTP(recvHead);
1265. ```
```
GIZWITS_LOG("Ack GET_UTT success
");
break;
1267. ```
```
case ACK_ASK_MODULE_INFO:
gizProtocolWaitAckCheck(recvHead);
1269. ```
```
gizProtocolModuleInfoHandle(recvHead);
GIZWITS_LOG("Ack GET_Module success
");
1271. ```
```
break;
default:
1274. ```
```
gizProtocolErrorCmd(recvHead,ERROR_CMD);
GIZWITS_LOG("ERR: cmd code error!
");
1276. ```
```
break;
}
1278. ```
```
}
else if(-2 == ret)
1280. ```
```
{
//Check failed, report exception
1282. ```
```
recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;
gizProtocolErrorCmd(recvHead,ERROR_ACK_SUM);
1284. ```
```
GIZWITS_LOG("ERR: check sum error!
");
return -2;
1286. ```
```
}
switch(gizwitsProtocol.issuedFlag)
1289. ```
```
{
case ACTION_CONTROL_TYPE:
1291. ```
```
gizwitsProtocol.issuedFlag = STATELESS_TYPE;
gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)&gizwitsProtocol.gizCurrentDataPoint, sizeof(dataPoint_t));
1293. ```
```
memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent,0x0,sizeof(gizwitsProtocol.issuedProcessEvent));
break;
1295. ```
```
case WIFI_STATUS_TYPE:
gizwitsProtocol.issuedFlag = STATELESS_TYPE;
1297. ```
```
gizwitsEventProcess(&gizwitsProtocol.wifiStatusEvent, (uint8_t *)&gizwitsProtocol.wifiStatusData, sizeof(moduleStatusInfo_t));
memset((uint8_t *)&gizwitsProtocol.wifiStatusEvent,0x0,sizeof(gizwitsProtocol.wifiStatusEvent));
1299. ```
```
break;
case ACTION_W2D_TRANSPARENT_TYPE:
1301. ```
```
gizwitsProtocol.issuedFlag = STATELESS_TYPE;
gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)gizwitsProtocol.transparentBuff, gizwitsProtocol.transparentLen);
1303. ```
```
break;
case GET_NTP_TYPE:
1305. ```
```
gizwitsProtocol.issuedFlag = STATELESS_TYPE;
gizwitsEventProcess(&gizwitsProtocol.NTPEvent, (uint8_t *)&gizwitsProtocol.TimeNTP, sizeof(protocolTime_t));
1307. ```
```
memset((uint8_t *)&gizwitsProtocol.NTPEvent,0x0,sizeof(gizwitsProtocol.NTPEvent));
break;
1309. ```
```
case GET_MODULEINFO_TYPE:
gizwitsProtocol.issuedFlag = STATELESS_TYPE;
1311. ```
```
gizwitsEventProcess(&gizwitsProtocol.moduleInfoEvent, (uint8_t *)&gizwitsProtocol.wifiModuleNews, sizeof(moduleInfo_t));
memset((uint8_t *)&gizwitsProtocol.moduleInfoEvent,0x0,sizeof(moduleInfo_t));
1313. ```
```
break;
default:
1315. ```
```
break;
}
1317.
1318. ```
```
gizDevReportPolicy(currentData);
return 0;
1321. }
1322.
1323. /**
1324. * @brief gizwits report transparent data interface
1325.
1326. * The user can call the interface to complete the reporting of private protocol data
1327.
1328. * @param [in] data :Private protocol data
1329. * @param [in] len :Private protocol data length
1330. * @return 0,success ;other,failure
1331. */
1332. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len)
1333. {
1334. ```
```
int32_t ret = 0;
uint8_t tx_buf[MAX_PACKAGE_LEN];
1336. ```
```
uint8_t *pTxBuf = tx_buf;
uint16_t data_len = 6+len;
1338. ```
```
if(NULL == gizdata)
{
1340. ```
```
GIZWITS_LOG("[ERR] gizwitsPassthroughData Error
");
return (-1);
1342. ```
```
}
*pTxBuf ++= 0xFF;
1345. ```
```
*pTxBuf ++= 0xFF;
*pTxBuf ++= (uint8_t)(data_len>>8);//len
1347. ```
```
*pTxBuf ++= (uint8_t)(data_len);
*pTxBuf ++= CMD_REPORT_P0;//0x1b cmd
1349. ```
```
*pTxBuf ++= gizwitsProtocol.sn++;//sn
*pTxBuf ++= 0x00;//flag
1351. ```
```
*pTxBuf ++= 0x00;//flag
*pTxBuf ++= ACTION_D2W_TRANSPARENT_DATA;//P0_Cmd
1353.
1354. ```
```
memcpy(&tx_buf[9],gizdata,len);
tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));
1356.
1357. ```
```
ret = uartWrite(tx_buf, data_len+4);
if(ret < 0)
1359. ```
```
{
GIZWITS_LOG("ERR: uart write error %d
", ret);
1361. ```
```
}
gizProtocolWaitAck(tx_buf, data_len+4);
1364.
1365. ```
```
return 0;
- }
- void gziwits_Task(dataPoint_t * currentDataPoint)
- {
-
static uint32_t Timer=0;
1372. ```
```
if(SoftTimer(Timer,5000))
{
1374. ```
```
gizwitsHandle(currentDataPoint);
Timer=GetSoftTimer();
1376. ```
```
}
- }
- /**@} */
复制代码
修改gizwits_protocol.h
- #ifndef _GIZWITS_PROTOCOL_H
- #define _GIZWITS_PROTOCOL_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "common.h"
- #define SEND_MAX_TIME 200 ///< 200ms resend
- #define SEND_MAX_NUM 2 ///< resend times
- #define protocol_VERSION "00000004" ///< protocol version
- #define P0_VERSION "00000002" ///< P0 protocol version
- /**@name Product Key
-
- */
- #define PRODUCT_KEY "9c8a5a8e38344fb4af14b6db0f5b1df7"
- /**@} */
- /**@name Product Secret
-
- */
- #define PRODUCT_SECRET "45c86d8c6a2a4b1dac7d68df54f6e4f0"
- /**@name Device status data reporting interval
-
- */
- #define REPORT_TIME_MAX 6000 //6S
- /**@} */
- #define CELLNUMMAX 7
- /**@name Whether the device is in the control class, 0 means no, 1 means yes
-
- */
- #define DEV_IS_GATEWAY 0
- /**@} */
- /**@name Binding time
-
- */
- #define NINABLETIME 0
- /**@} */
- #define MAX_PACKAGE_LEN (sizeof(devStatus_t)+sizeof(attrFlags_t)+20) ///< Data buffer maximum length
- #define RB_MAX_LEN (MAX_PACKAGE_LEN*2) ///< Maximum length of ring buffer
- /**@name Data point related definition
-
- */
- #define Relay_1_BYTEOFFSET 0
- #define Relay_1_BITOFFSET 0
- #define Relay_1_LEN 1
- #define Temp_RATIO 1
- #define Temp_ADDITION 0
- #define Temp_MIN 0
- #define Temp_MAX 100
- #define Humi_RATIO 1
- #define Humi_ADDITION 0
- #define Humi_MIN 0
- #define Humi_MAX 100
- #define Light_Intensity_RATIO 1
- #define Light_Intensity_ADDITION 0
- #define Light_Intensity_MIN 0
- #define Light_Intensity_MAX 100
- /**@} */
- /** Writable data points Boolean and enumerated variables occupy byte size */
- #define COUNT_W_BIT 1
- /** Event enumeration */
- typedef enum
- {
- WIFI_SOFTAP = 0x00, ///< WiFi SOFTAP configuration event
- WIFI_AIRLINK, ///< WiFi module AIRLINK configuration event
- WIFI_STATION, ///< WiFi module STATION configuration event
- WIFI_OPEN_BINDING, ///< The WiFi module opens the binding event
- WIFI_CLOSE_BINDING, ///< The WiFi module closes the binding event
- WIFI_CON_ROUTER, ///< The WiFi module is connected to a routing event
- WIFI_DISCON_ROUTER, ///< The WiFi module has been disconnected from the routing event
- WIFI_CON_M2M, ///< The WiFi module has a server M2M event
- WIFI_DISCON_M2M, ///< The WiFi module has been disconnected from the server M2M event
- WIFI_OPEN_TESTMODE, ///< The WiFi module turns on the test mode event
- WIFI_CLOSE_TESTMODE, ///< The WiFi module turns off the test mode event
- WIFI_CON_APP, ///< The WiFi module connects to the APP event
- WIFI_DISCON_APP, ///< The WiFi module disconnects the APP event
- WIFI_RSSI, ///< WiFi module RSSI event
- WIFI_NTP, ///< Network time event
- MODULE_INFO, ///< Module information event
- TRANSPARENT_DATA, ///< Transparency events
- EVENT_Relay_1,
- EVENT_TYPE_MAX ///< Enumerate the number of members to calculate (user accidentally deleted)
- } EVENT_TYPE_T;
- /** P0 Command code */
- typedef enum
- {
-
ACTION_CONTROL_DEVICE = 0x01, ///< Protocol 4.10 WiFi Module Control Device WiFi Module Send
-
ACTION_READ_DEV_STATUS = 0x02, ///< Protocol 4.8 WiFi Module Reads the current status of the device WiFi module sent
ACTION_READ_DEV_STATUS_ACK = 0x03, ///< Protocol 4.8 WiFi Module Read Device Current Status Device MCU Reply
-
ACTION_REPORT_DEV_STATUS = 0x04, ///< Protocol 4.9 device MCU to the WiFi module to actively report the current status of the device to send the MCU
ACTION_W2D_TRANSPARENT_DATA = 0x05, ///< WiFi to device MCU transparent
-
ACTION_D2W_TRANSPARENT_DATA = 0x06, ///< Device MCU to WiFi
121. } actionType_t;
122.
123.
124. typedef struct
125. {
126. ```
```
uint16_t year;
-
uint8_t month;
uint8_t day;
-
uint8_t hour;
uint8_t minute;
-
uint8_t second;
uint32_t ntp;
- }protocolTime_t;
- /** WiFi Module configuration parameters*/
- typedef enum
- {
- WIFI_RESET_MODE = 0x00, ///< WIFI module reset
- WIFI_SOFTAP_MODE, ///< WIFI module softAP modeF
- WIFI_AIRLINK_MODE, ///< WIFI module AirLink mode
- WIFI_PRODUCTION_TEST, ///< MCU request WiFi module into production test mode
- WIFI_NINABLE_MODE, ///< MCU request module to enter binding mode
- WIFI_REBOOT_MODE, ///< MCU request module reboot
- }WIFI_MODE_TYPE_T;
- /** The protocol event type*/
- typedef enum
- {
- STATELESS_TYPE = 0x00, ///< Stateless type
- ACTION_CONTROL_TYPE, ///< Protocol 4.10 :WiFi module control device event
- WIFI_STATUS_TYPE, ///< Protocol 4.5 :WiFi module inform the device MCU of the change event of the WiFi module status
- ACTION_W2D_TRANSPARENT_TYPE, ///< Protocol WiFi to device MCU transparent event
- GET_NTP_TYPE, ///< Protocol 4.13 :The MCU requests access to the network time event
- GET_MODULEINFO_TYPE, ///< Protocol 4.9 :The MCU get module information event
- PROTOCOL_EVENT_TYPE_MAX ///< Count enumerated member (User donot delete)
- } PROTOCOL_EVENT_TYPE_T;
- /** Protocol command code */
- typedef enum
- {
-
CMD_GET_DEVICE_INTO = 0x01, ///< Protocol:3.1
ACK_GET_DEVICE_INFO = 0x02, ///< Protocol:3.1
-
CMD_ISSUED_P0 = 0x03, ///< Protocol:3.2 3.3
ACK_ISSUED_P0 = 0x04, ///< Protocol:3.2 3.3
-
CMD_REPORT_P0 = 0x05, ///< Protocol:3.4
ACK_REPORT_P0 = 0x06, ///< Protocol:3.4
-
CMD_HEARTBEAT = 0x07, ///< Protocol:3.5
ACK_HEARTBEAT = 0x08, ///< Protocol:3.5
-
CMD_WIFI_CONFIG = 0x09, ///< Protocol:3.6
ACK_WIFI_CONFIG = 0x0A, ///< Protocol:3.6
-
CMD_SET_DEFAULT = 0x0B, ///< Protocol:3.7
ACK_SET_DEFAULT = 0x0C, ///< Protocol:3.7
-
CMD_WIFISTATUS = 0x0D, ///< Protocol:3.8
ACK_WIFISTATUS = 0x0E, ///< Protocol:3.8
-
CMD_MCU_REBOOT = 0x0F, ///< Protocol:4.1
ACK_MCU_REBOOT = 0x10, ///< Protocol:4.1
-
CMD_ERROR_PACKAGE = 0x11, ///< Protocol:3.9
ACK_ERROR_PACKAGE = 0x12, ///< Protocol:3.9
-
CMD_PRODUCTION_TEST = 0x13, ///< Protocol:
ACK_PRODUCTION_TEST = 0x14, ///< Protocol:
-
CMD_NINABLE_MODE = 0x15, ///< Protocol:3.10
ACK_NINABLE_MODE = 0x16, ///< Protocol:3.10
-
CMD_GET_NTP = 0x17, ///< Protocol:4.3
ACK_GET_NTP = 0x18, ///< Protocol:4.3
-
CMD_ASK_BIGDATA = 0x19, ///< Protocol:4.4
ACK_ASK_BIGDATA = 0x1A, ///< Protocol:4.4
-
CMD_BIGDATA_READY = 0x1B, ///< Protocol:4.5
ACK_BIGDATA_READY = 0x1C, ///< Protocol:4.5
-
CMD_BIGDATA_SEND = 0x1D, ///< Protocol:4.6
ACK_BIGDATA_SEND = 0x1E, ///< Protocol:4.6
-
CMD_S_STOP_BIGDATA_SEND = 0x1F, ///< Protocol:4.7
ACK_S_STOP_BIGDATA_SEND = 0x20, ///< Protocol:4.7
-
CMD_D_STOP_BIGDATA_SEND = 0x27, ///< Protocol:4.8
ACK_D_STOP_BIGDATA_SEND = 0x28, ///< Protocol:4.8
-
CMD_ASK_MODULE_INFO = 0x21, ///< Protocol:4.9
ACK_ASK_MODULE_INFO = 0x22, ///< Protocol:4.9
-
CMD_ASK_AFFAIR_HANDLE = 0x23, ///< Protocol:4.10
ACK_ASK_AFFAIR_HANDLE = 0x24, ///< Protocol:4.10
-
CMD_AFFAIR_RESULT = 0x25, ///< Protocol:4.10
ACK_AFFAIR_RESULT = 0x26, ///< Protocol:4.10
-
CMD_REBOOT_MODULE = 0x29, ///< Protocol:3.11
ACK_REBOOT_MODULE = 0x2A, ///< Protocol:3.11
-
CMD_CONNECT_M2M = 0x2D, ///< Protocol:for Virtualization
ACK_CONNECT_M2M = 0x2E, ///< Protocol:for Virtualization
-
CMD_CONNECT_M2M_BACK = 0x2F, ///< Protocol:for Virtualization
ACK_CONNECT_M2M_BACK = 0x30, ///< Protocol:for Virtualization
-
CMD_UPLOAD_DATA = 0x31, ///< Protocol:for Virtualization
ACK_UPLOAD_DATA = 0x32, ///< Protocol:for Virtualization
-
CMD_UPLOAD_DATA_BACK = 0x33, ///< Protocol:for Virtualization
ACK_UPLOAD_DATA_BACK = 0x34, ///< Protocol:for Virtualization
-
CMD_DISCONNECT_M2M = 0x35, ///< Protocol:for Virtualization
ACK_DISCONNECT_M2M = 0x36, ///< Protocol:for Virtualization
-
CMD_DISCONNECT_M2M_BACK = 0x37, ///< Protocol:for Virtualization
ACK_DISCONNECT_M2M_BACK = 0x38, ///< Protocol:for Virtualization
-
CMD_RESET_SIMULATOR = 0x39, ///< Protocol:for Virtualization
ACK_RESET_SIMULATOR = 0x3A, ///< Protocol:for Virtualization
-
CMD_RESET_SIMULATOR_BACK = 0x3B, ///< Protocol:for Virtualization
ACK_RESET_SIMULATOR_BACK = 0x3C, ///< Protocol:for Virtualization
- } PROTOCOL_CMDTYPE;
- /** Illegal message type*/
- typedef enum
- {
-
ERROR_ACK_SUM = 0x01, ///< check error
ERROR_CMD = 0x02, ///< Command code error
-
ERROR_OTHER = 0x03, ///< other
257. } errorPacketsType_t;
258.
259. typedef enum
260. {
261. ```
```
EXE_SUCESS = 0x00,
-
EXE_FAILE = 0x01,
263. } execute_result;
264.
265. #pragma pack(1)
266.
267.
268. typedef struct {
269. bool valueRelay_1;
270. uint32_t valueTemp;
271. uint32_t valueHumi;
272. uint32_t valueLight_Intensity;
273. } dataPoint_t;
274.
275.
276.
277. typedef struct {
278. uint8_t flagRelay_1:1;
279. } attrFlags_t;
280.
281.
282.
283.
284. typedef struct {
285. uint8_t wBitBuf[COUNT_W_BIT];
286. } attrVals_t;
287.
288.
289. typedef struct {
290. ```
```
attrFlags_t attrFlags;
-
attrVals_t attrVals;
292. }gizwitsIssued_t;
293.
294.
295.
296. typedef struct {
297. uint8_t wBitBuf[COUNT_W_BIT];
298. uint8_t valueTemp;
299. uint8_t valueHumi;
300. uint8_t valueLight_Intensity;
301. } devStatus_t;
302.
303.
304.
305.
306. typedef struct {
307. ```
```
uint8_t num;
-
uint8_t event[EVENT_TYPE_MAX]; ///< Queue member event content
309. }eventInfo_t;
310.
311.
312.
313.
314. typedef struct {
315. ```
```
uint8_t rssi;
- }moduleStatusInfo_t;
- /** Protocol standard header structure */
- typedef struct
- {
-
uint8_t head[2]; ///< The head is 0xFFFF
uint16_t len;
-
uint8_t cmd; ///< command
uint8_t sn;
-
uint8_t flags[2]; ///< flag,default is 0
326. } protocolHead_t;
327.
328.
329. typedef struct
330. {
331. ```
```
protocolHead_t head;
-
uint8_t protocolVer[8]; ///< Protocol version
uint8_t p0Ver[8]; ///< p0 Protocol version
-
uint8_t hardVer[8]; ///< Hardware version
uint8_t softVer[8];
-
uint8_t productKey[32]; ///< Product key
uint16_t ninableTime; ///< Binding time(second)
-
uint8_t devAttr[8]; ///< Device attribute
uint8_t productSecret[32];
-
uint8_t sum; ///< checksum
341. } protocolDeviceInfo_t;
342.
343.
344. typedef struct
345. {
346. ```
```
protocolHead_t head;
-
uint8_t sum; ///< checksum
348. } protocolCommon_t;
349.
350.
351. typedef struct
352. {
353. ```
```
protocolHead_t head;
-
uint8_t cfgMode; ///< Configuration parameters
uint8_t sum;
- } protocolCfgMode_t;
- /** 4.13 The MCU requests the network time protocol structure */
- typedef struct
- {
-
protocolHead_t head; ///< Protocol standard header structure
uint8_t time[7];
-
uint8_t ntp_time[4]; ///< Software version
uint8_t sum;
- } protocolUTT_t;
- /** WiFi module working status*/
- typedef union
- {
-
uint16_t value;
struct
-
{
uint16_t softap:1;
-
uint16_t station:1;
uint16_t onboarding:1;
-
uint16_t binding:1;
uint16_t con_route:1;
-
uint16_t con_m2m:1;
uint16_t reserve1:2;
-
uint16_t rssi:3;
uint16_t app:1;
-
uint16_t test:1;
uint16_t reserve2:3;
-
}types;
385.
386. } wifiStatus_t;
387.
388.
389. typedef struct
390. {
391. ```
```
protocolHead_t head;
-
wifiStatus_t ststus; ///< WIFI status
uint8_t sum;
- } protocolWifiStatus_t;
- /** Protocol common data frame(4.9) :protocol structure */
- typedef struct
- {
-
protocolHead_t head; ///< Protocol standard header structure
uint8_t type; ///< Information Type
-
uint8_t sum; ///< checksum
402. } protocolGetModuleInfo_t;
403.
404. typedef struct
405. {
406. ```
```
uint8_t moduleType;
-
uint8_t serialVer[8]; ///< Serial port protocol version
uint8_t hardVer[8];
-
uint8_t softVer[8]; ///< Software version
uint8_t mac[16];
-
uint8_t ip[16]; ///< ip
uint8_t devAttr[8];
- } moduleInfo_t;
- /** Protocol common data frame(4.9) :protocol structure */
- typedef struct
- {
-
protocolHead_t head; ///< Protocol standard header structure
moduleInfo_t wifiModuleInfo;
-
uint8_t sum; ///< checksum
421. } protocolModuleInfo_t;
422.
423.
424.
425. typedef struct
426. {
427. ```
```
uint16_t LAC_ID;
-
uint16_t CellID; ///<Base station ID
uint8_t RSSI;
- } gprsCellInfo_t;
- /** 3.19 The basic information of the GPRS communication module */
- typedef struct
- {
-
uint8_t Type;//2G/3g/4g
uint8_t Pro_ver[8];
-
uint8_t Hard_ver[8];//Hardware version
uint8_t Soft_ver[8];
-
uint8_t Device_attribute[8];//Device attribute
uint8_t IMEI[16];
-
uint8_t IMSI[16];//string
uint8_t MCC[8];
-
uint8_t MNC[8];//Mobile network code
uint8_t CellNum;
-
uint8_t CellInfoLen;//Information length of base station
gprsCellInfo_t GPRS_CellINFO[CELLNUMMAX];
- }gprsInfo_t;
- /** 4.7 Illegal message notification :protocol structure*/
- typedef struct
- {
-
protocolHead_t head; ///< Protocol standard header structure
uint8_t error;
-
uint8_t sum; ///< checksum
456. } protocolErrorType_t;
457.
458.
459.
460. typedef struct
461. {
462. ```
```
protocolHead_t head;
-
uint8_t action; ///< p0 command
464. } protocolP0Head_t;
465.
466.
467.
468. typedef struct {
469.
470. ```
```
devStatus_t devStatus;
- }gizwitsReport_t;
- /** resend strategy structure */
- typedef struct {
-
uint8_t num; ///< resend times
uint8_t flag; ///< 1,Indicates that there is a need to wait for the ACK;0,Indicates that there is no need to wait for the ACK
-
uint8_t buf[MAX_PACKAGE_LEN]; ///< resend data buffer
uint16_t dataLen; ///< resend data length
-
uint32_t sendTime; ///< resend time
480. } protocolWaitAck_t;
481.
482.
483. typedef struct
484. {
485. ```
```
protocolHead_t head;
-
uint8_t action; ///< p0 action
gizwitsReport_t reportData; ///< p0 data
-
uint8_t sum; ///< Checksum
489. } protocolReport_t;
490.
491.
492.
493. typedef struct
494. {
495. ```
```
uint8_t issuedFlag;
-
uint8_t protocolBuf[MAX_PACKAGE_LEN]; ///< Protocol data handle buffer
uint8_t transparentBuff[MAX_PACKAGE_LEN];
-
uint32_t transparentLen; ///< Transmission data length
uint32_t sn;
-
uint32_t timerMsCount; ///< Timer Count
protocolWaitAck_t waitAck; ///< Protocol wait ACK data structure
-
eventInfo_t issuedProcessEvent; ///< Control events
eventInfo_t wifiStatusEvent;
-
eventInfo_t NTPEvent; ///< NTP events
eventInfo_t moduleInfoEvent;
-
gizwitsReport_t reportData; ///< The protocol reports data for standard product
dataPoint_t gizCurrentDataPoint;
-
dataPoint_t gizLastDataPoint; ///< Last device datapoints status
moduleStatusInfo_t wifiStatusData;
-
protocolTime_t TimeNTP; ///< Network time information
514.
515. ```
```
gprsInfo_t gprsInfoNews;
- #else
-
moduleInfo_t wifiModuleNews; ///< WIFI module Info
518. #endif
519.
520.
521. }gizwitsProtocol_t;
522.
523. #pragma pack()
524.
525.
528.
529. extern uint32_t gizGetTimerCount(void);
530.
531. void gizwitsInit(void);
532. int32_t gizwitsSetMode(uint8_t mode);
533. void gizwitsGetNTP(void);
534. int32_t gizwitsHandle(dataPoint_t *currentData);
535. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len);
536. void gizwitsGetModuleInfo(void);
537. int32_t gizPutData(uint8_t *buf, uint32_t len);
538.
539.
540. / *添加用户自定义的函数* */
541. void gziwits_Task(dataPoint_t * currentDataPoint);
542.
543.
544. #ifdef __cplusplus
545. }
546. #endif
547.
548. #endif
549.
550.
*复制代码*
6,Utils直接移植无需修改
![](
7,添加TIM3定时器代码驱动
1. #include "tim3.h"
2. #include "gizwits_product.h"
3.
4. void TIM3_Config(uint16_t psc,uint16_t arr)
5. {
6. ```
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
-
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
TIM_TimeBaseStructure.TIM_Period = arr
-
TIM_TimeBaseStructure.TIM_Prescaler = psc
-
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up
-
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
- NVIC_InitTypeDef NVIC_InitStructure;
-
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0
-
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE
-
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);
- }
- /用户实现的定时器接口/
- void TIM3_IRQHandler(void)
- {
-
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
-
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
gizTimerMs();
-
}
33. }
34. /*******************/
35. //void TIMER_IRQ_FUN(void)
36. //{
37. // gizTimerMs();
38. //}
39. /*******************/
40.
41.
42. ## 添加UART3串口通信代码驱动
43.
44. ```c
```
- #include"usart3.h"
- #include"gizwits_product.h"
- voidUSART3_Init(uint32_t BaudRate)
- {
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口3时钟使能
-
USART_DeInit(USART3); //复位串口3
GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //USART3_TX PB10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP
-
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure)
-
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = BaudRate
-
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1
-
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None
-
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART3, &USART_InitStructure);
-
USART_Cmd(USART3, ENABLE); //使能串口
79.
80.
81. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //开启中断
82.
83. NVIC_InitTypeDef NVIC_InitStructure;
84. ```
```
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3
-
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
- }
- /用户实现的中断服务函数接口/
- void USART3_IRQHandler(void)
- {
-
uint8_t Recv_Data;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
-
{
Recv_Data = USART_ReceiveData(USART3)
-
gizPutData(&Recv_Data, 1);
}
- }
- /*********************/
- //void UART_IRQ_FUN(void)
- //{
- // uint8_t value = 0;
- // gizPutData(&value, 1);
- //}
- /*********************/
复制代码
8,修改关键函数的函数体,封装各模块的初始化
五,机智云初始化函数封装
成功联网
设备上线显示
效果展示