欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

物联网小型演示

最编程 2024-05-06 09:05:37
...
/** ************************************************************ * @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 "oled.h" #include "dht11.h" #include "adc.h" static uint32_t timerMsCount; uint8_t aRxBuffer; uint8_t OLED_BUFF[30]; uint32_t ad_value; uint8_t uart3_send_buff[4]; uint8_t uart3_recv_buff[5]; uint8_t curtain_on_cmd = 0,curtain_off_cmd = 0,led_on_cmd = 0,led_off_cmd = 0; uint8_t zig_count = 81; /** User area the current device state structure*/ dataPoint_t currentDataPoint; extern TIM_HandleTypeDef htim2; extern TIM_HandleTypeDef htim4; extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; /**@} */ /**@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_led1: currentDataPoint.valueled1 = dataPointPtr->valueled1; GIZWITS_LOG("Evt: EVENT_led1 %d \n", currentDataPoint.valueled1); if(0x01 == currentDataPoint.valueled1) { HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,0); OLED_ShowString(0,6,"LED:ON ",16); //user handle } else { HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,1); OLED_ShowString(0,6,"LED:Off",16); //user handle } break; case EVENT_led2: currentDataPoint.valueled2 = dataPointPtr->valueled2; GIZWITS_LOG("Evt: EVENT_led2 %d \n", currentDataPoint.valueled2); if(0x01 == currentDataPoint.valueled2) { led_on_cmd = 1; //user handle } else { led_off_cmd = 1; //user handle } break; case EVENT_curtain: currentDataPoint.valuecurtain = dataPointPtr->valuecurtain; GIZWITS_LOG("Evt: EVENT_curtain %d \n", currentDataPoint.valuecurtain); if(0x01 == currentDataPoint.valuecurtain) { curtain_on_cmd = 1; //user handle } else { curtain_off_cmd = 1; //user handle } 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; } /** * 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) { DHT_Read(); HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1,10); ad_value=HAL_ADC_GetValue(&hadc1); currentDataPoint.valuesmoke = HAL_GPIO_ReadPin(SOMKE_GPIO_Port, SOMKE_Pin); currentDataPoint.valuetemperature1 = Data[0]; currentDataPoint.valuewet1 = Data[2]; currentDataPoint.valuesun1 = 100-(ad_value/4095.0*100); sprintf(OLED_BUFF,"H:%2d%% T:%2dC",currentDataPoint.valuetemperature1,currentDataPoint.valuewet1); OLED_ShowString(35,2,OLED_BUFF,16); if(HAL_GPIO_ReadPin(SOMKE_GPIO_Port, SOMKE_Pin)) { OLED_ShowString(0,4,"smog:Y",16); } else { OLED_ShowString(0,4,"smog:N",16); } sprintf(OLED_BUFF,"sun:%2d%%",currentDataPoint.valuesun1); OLED_ShowString(64,4,OLED_BUFF,16); if(zig_count>80)//没收到心跳信号 { OLED_ShowString(64,6,"Zig:ERR",16); } else { OLED_ShowString(64,6,"Zig:OK ",16); } /* currentDataPoint.valuesmoke = ;//Add Sensor Data Collection currentDataPoint.valuetemperature1 = ;//Add Sensor Data Collection currentDataPoint.valuewet1 = ;//Add Sensor Data Collection currentDataPoint.valuesun1 = ;//Add Sensor Data Collection currentDataPoint.valuetemperature2 = ;//Add Sensor Data Collection currentDataPoint.valuewet2 = ;//Add Sensor Data Collection */ if(led_on_cmd)//打开卧室灯 { uart3_send_buff[0]= 0x55;//帧头 uart3_send_buff[1]= 0x1;//数据指令 uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1];//CRC uart3_send_buff[3]= 0xAA;//帧尾 HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100); } if(led_off_cmd)//关闭卧室灯 { uart3_send_buff[0]= 0x55; uart3_send_buff[1]= 0x2; uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1]; uart3_send_buff[3]= 0xAA; HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100); } if(curtain_on_cmd)//打开窗帘 { uart3_send_buff[0]= 0x55; uart3_send_buff[1]= 0x3; uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1]; uart3_send_buff[3]= 0xAA; HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100); } if(curtain_off_cmd)//关闭窗帘 { uart3_send_buff[0]= 0x55; uart3_send_buff[1]= 0x4; uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1]; uart3_send_buff[3]= 0xAA; HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100); } } /** * 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)); OLED_Init(); OLED_Clear(); OLED_ShowCHinese(16,0,0);//一个字16个像素点 从中间开始 两头剩余32 OLED_ShowCHinese(32,0,1); OLED_ShowCHinese(48,0,2); OLED_ShowCHinese(64,0,3); OLED_ShowCHinese(80,0,4); OLED_ShowCHinese(96,0,5); OLED_ShowCHinese(0,2,6);//2代表第二行 OLED_ShowCHinese(16,2,7); OLED_ShowString(0,6,"LED:Off",16); HAL_ADCEx_Calibration_Start(&hadc1);//adc的采集函数 需要事先使能 /** Warning !!! DataPoint Variables Init , Must Within The Data Range **/ /* currentDataPoint.valuesmoke = ; currentDataPoint.valueled1 = ; currentDataPoint.valueled2 = ; currentDataPoint.valuecurtain = ; currentDataPoint.valuetemperature1 = ; currentDataPoint.valuewet1 = ; currentDataPoint.valuesun1 = ; currentDataPoint.valuetemperature2 = ; currentDataPoint.valuewet2 = ; */ } /** * @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(); } /**@} */ #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(); gizTimerMs(); } if(htim==&htim4) { zig_count++; if(zig_count>100) zig_count=90; } } /** * @brief Timer TIM3 init function * @param none * @return none */ void timerInit(void)//定时器初始化 { HAL_TIM_Base_Start_IT(&htim2); HAL_TIM_Base_Start_IT(&htim4); } /** * @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);//开启下一次接收中断 } if(UartHandle->Instance == USART3)//判断是否是串口3收到的数据 { if(uart3_recv_buff[0] == 0x55&&uart3_recv_buff[0]+uart3_recv_buff[1]+uart3_recv_buff[2] == uart3_recv_buff[3]&&uart3_recv_buff[4] == 0xAA) { switch(uart3_recv_buff[1]) { case 0x5: led_on_cmd=0; break; case 0x6: led_off_cmd=0; break; case 0x7: curtain_on_cmd=0; break; case 0x8: curtain_off_cmd=0; break; case 0x9: currentDataPoint.valuetemperature2 = uart3_recv_buff[2];//Add Sensor Data Collection 卧室温度 break; case 0xa: currentDataPoint.valuewet2 = uart3_recv_buff[2];//Add Sensor Data Collection 卧室湿度 break; case 0xb: zig_count=0; break; default:break; } } HAL_UART_Receive_IT(&huart3,uart3_recv_buff, 5); } } /** * @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);//开启下一次接收中断 HAL_UART_Receive_IT(&huart3,uart3_recv_buff,5); } /** * @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 } } #ifdef PROTOCOL_DEBUG GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len); for(i=0; i<