#申请原创#
@21小跑堂
1、目的
最近在学习各个厂家的物联网云平台,体验各个厂家的物联网云平台接入方案和设备端开发过程。本帖以超声波传感器和烟雾传感器数据接入机智云平台为例进行分析,实现在手机APP上监测超声波传感器测距数据和烟雾传感器数据2个参数。
2、开发过程
通过官方文档学习了机智云平台功能和设备接入原理后,只需要3步即可将设备接入云平台:MCU端开发,云端配置,设备调试。
硬件模块:机智云gokit开发板(扩展板带有esp8266无线WIFI模块、底板为STM32F103C8T6)。
stm32端开发
stm32端工作是编写ADC采集程序读取烟雾传感器数据,编写超声波传感器驱动程序读取超声波传感器测距数据,与云端交互的逻辑代码。
stm32cubemx配置如下:
云端配置
在机智云开发者中心创建一个产品,然后编辑数据点。数据点是设备产品的功能的抽象,用于描述产品功能及其参数。创建数据点后,设备与云端通讯的数据格式即可确定,设备、机智云可以相互识别设备与机智云互联互通的数据。
本次项目需要把stm32获取的烟雾数据、测距数据2个参数上传到服务器,这2个参数为数值类型,对于云端来讲只读,数据点可从excel导入或者导入机智云提供的范例后再修改。
自动生成MCU SDK
自动生成的MCUSDK代码实现了机智云通信协议的解析与封包、传感器数据与通信数据的转换逻辑,并封装成了简单的 API。自动生成的代码包基于keil软件,在自动生成的代码包里面添加自己需要的传感器驱动后目录结构如下:
当设备收到云端或 APP 端的数据后,程序会将数据转换成对应的事件并通知到应用层,开发者只需要在对应的事件处理逻辑中添加传感器的控制函数,便可完成产品的开发。
MCU通过烧录好GAgent固件的WIFI/GPRS模组与服务器交互的逻辑,可以看出整个过程全部被封装,留给用户的接口很简单,对于资源足够的MCU,用户只需要在MCU实现3个接口函数即串口发送、串口中断接收、定时器1ms中断即可通过无线通信模组联网。而数据点相关、云端交互相关全部由MCU代码自动生成工具生成,只需在Gizwits_product.c文件填充业务逻辑。前面提到的三个接口是串口收发、定时器1ms中断,本次stm32配置了串口2(根据机智云的协议,串口参数为:波特率9600、无校验、停止位1)作为与WIFI模组通信的接口;定时器10为1ms中断,作为MCU与WIFI模组的“心跳”信号;下面就是如何在具体位置填充这3个接口:
串口接收数据回调写入环形缓存:
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);//开启下一次接收中断
}
}
串口发送数据写入环形缓存接口接口:
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
}
}
#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;
}
用户驱动初始化接口:
void userInit(void)
{
memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));
delay_init(); // 延时 初始化
rgbLedInit(); // RGB LED 初始化
dht11Init(); // 温湿度初始化
irInit(); // 红外初始化
/** Warning !!! DataPoint Variables Init , Must Within The Data Range **/
/*
currentDataPoint.valueLED_OnOff = ;
currentDataPoint.valueLED_Color = ;
currentDataPoint.valueLED_R = ;
currentDataPoint.valueLED_G = ;
currentDataPoint.valueLED_B = ;
currentDataPoint.valueInfrared = ;
currentDataPoint.valueTemperature = ;
currentDataPoint.valueHumidity = ;
currentDataPoint.valueDistance = ;
currentDataPoint.valueAirQuality = ;
currentDataPoint.valueAlert_1 = ;
currentDataPoint.valueAlert_2 = ;
currentDataPoint.valueFault_LED = ;
currentDataPoint.valueFault_TemHum = ;
currentDataPoint.valueFault_IR = ;
*/
}
数据上报到云平台逻辑代码:
void userHandle(void)
{
uint8_t ret = 0;
static uint32_t thLastTimer = 0;
static uint32_t thLastTimer2 = 0;
if((gizGetTimerCount() - thLastTimer) > 2000)
{
ret = dht11Read((uint8_t *)¤tDataPoint.valueTemperature,(uint8_t *)¤tDataPoint.valueHumidity);
if(ret != 0)
{
GIZWITS_LOG("Failed to read DHT11 [%d] \n", ret);
}
//printf("Temp=%d Hum=%d\r\n",currentDataPoint.valueTemperature,currentDataPoint.valueHumidity);
thLastTimer = gizGetTimerCount();
}
if((gizGetTimerCount() - thLastTimer2) > 200)
{
currentDataPoint.valueInfrared = irHandle();
if(HAL_TIM_Base_Start(&htim4)!=HAL_OK)
{
printf("HAL_TIM_Base_Start Error!\r\n");
}
HAL_GPIO_WritePin(Trig_GPIO_Port,Trig_Pin,GPIO_PIN_SET);
delay_us(10);
HAL_GPIO_WritePin(Trig_GPIO_Port,Trig_Pin,GPIO_PIN_RESET);
while(!HAL_GPIO_ReadPin(Echo_GPIO_Port, Echo_Pin));
v1=htim4.Instance->CNT;
while(HAL_GPIO_ReadPin(Echo_GPIO_Port, Echo_Pin));
v2=htim4.Instance->CNT;
htim4.Instance->CNT=0;
if(HAL_TIM_Base_Stop(&htim4)!=HAL_OK)
{
printf("HAL_TIM_Base_Stop Error!\r\n");
}
printf("v2=%d v1=%d\r\n",v2,v1);
Distance=(v2-v1)*17/(float)1000;
AirQuality = ((float)((float)ConvertedValue * VREF)/MAX_CONVERTED_VALUE);
printf("distance=%0.1fcm AirQuality=%d\r\n",Distance,AirQuality);
thLastTimer2 = gizGetTimerCount();
currentDataPoint.valueDistance = Distance;
currentDataPoint.valueAirQuality = AirQuality;
}
/*
currentDataPoint.valueInfrared = ;//Add Sensor Data Collection
currentDataPoint.valueTemperature = ;//Add Sensor Data Collection
currentDataPoint.valueHumidity = ;//Add Sensor Data Collection
currentDataPoint.valueDistance = ;//Add Sensor Data Collection
currentDataPoint.valueAirQuality = ;//Add Sensor Data Collection
currentDataPoint.valueAlert_1 = ;//Add Sensor Data Collection
currentDataPoint.valueAlert_2 = ;//Add Sensor Data Collection
currentDataPoint.valueFault_LED = ;//Add Sensor Data Collection
currentDataPoint.valueFault_TemHum = ;//Add Sensor Data Collection
currentDataPoint.valueFault_IR = ;//Add Sensor Data Collection
*/
}
云平台下发控制接口代码:
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_LED_OnOff:
currentDataPoint.valueLED_OnOff = dataPointPtr->valueLED_OnOff;
GIZWITS_LOG("Evt: EVENT_LED_OnOff %d \n", currentDataPoint.valueLED_OnOff);
if(0x01 == currentDataPoint.valueLED_OnOff)
{
//user handle
ledRgbControl(254, 0, 0);
}
else
{
//user handle
ledRgbControl(0, 0, 0);
}
break;
case EVENT_LED_Color:
currentDataPoint.valueLED_Color = dataPointPtr->valueLED_Color;
GIZWITS_LOG("Evt: EVENT_LED_Color %d\n", currentDataPoint.valueLED_Color);
switch(currentDataPoint.valueLED_Color)
{
case LED_Color_VALUE0:
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case LED_Color_VALUE1:
ledRgbControl(254, 254, 0);
break;
case LED_Color_VALUE2:
ledRgbControl(254, 0, 70);
break;
case LED_Color_VALUE3:
ledRgbControl(238, 30, 30);
break;
default:
break;
}
break;
case EVENT_LED_R:
currentDataPoint.valueLED_R = dataPointPtr->valueLED_R;
GIZWITS_LOG("Evt:EVENT_LED_R %d\n",currentDataPoint.valueLED_R);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case EVENT_LED_G:
currentDataPoint.valueLED_G = dataPointPtr->valueLED_G;
GIZWITS_LOG("Evt:EVENT_LED_G %d\n",currentDataPoint.valueLED_G);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case EVENT_LED_B:
currentDataPoint.valueLED_B = dataPointPtr->valueLED_B;
GIZWITS_LOG("Evt:EVENT_LED_B %d\n",currentDataPoint.valueLED_B);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case WIFI_SOFTAP:
break;
case WIFI_AIRLINK:
break;
case WIFI_STATION:
break;
case WIFI_CON_ROUTER:
ledRgbControl(0, 0, 0);
break;
case WIFI_DISCON_ROUTER:
ledRgbControl(250, 0, 0);
break;
case WIFI_CON_M2M:
ledRgbControl(0, 0, 0);
break;
case WIFI_DISCON_M2M:
ledRgbControl(0, 0, 250);
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;
}
实现效果:
控制LED显示蓝色
控制LED显示红色
控制LED显示绿色
超声波和烟雾传感器数据采集上报效果:
总结:
机智云平台的接入还是十分简单的,基本上可以通过图形化简单的进行配置一下参数,然后生成代码包,基于代码包二次开发十分简单方便,只需要看懂从哪个入口填充上行或者下行处理代码就行;对于个人用户,目前只体验了传感器类数据接入开发,通过官网资料来看,目前还没看到音视频类设备接入方法,如果能够体验音视频类产品接入开发,可玩性增加不少。
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
打赏榜单
21小跑堂 打赏了 80.00 元 2023-01-12 理由:恭喜通过原创审!期待您更多的原创作品~
共1人点赞
|
通过机智云,轻松部署建议的云平台,并可将单片机采集的传感器数据进行上传,同时手机APP进行显示。比较不错的主题,但是作者写作方向偏重于效果展示,对于实现过程和代码解释篇幅较短,希望后续可以稍加改进,获得更高的打赏。