|
ZigBee選用CC2530射頻模塊,溫濕度、紫外線、空氣質量三種傳感器各使用一個CC2530模塊進行數據采集和傳輸,
要能實現與手機APP通訊,還需要設計ZigBee到藍牙或者WiFi網絡的轉換網關,選用WiFi通訊方式。為了節省成本,
把三個傳感器節點的其中一個設計為兼容網關功能,另外兩個節點的數據都匯聚到網關ZigBee處,ZigBee將數據發送到
WiFi模塊,最后通過WiFi信號上傳到APP,顯示各傳感器數據。LCD液晶顯示屏也設計到網關節點處,方便顯示各種傳感器數據。
主要硬件:
CC2530射頻模塊
溫濕度、紫外線、空氣質量三種傳感器
LCD液晶屏
WiFi模塊
溫濕度節點原理圖:
51hei截圖20210222172247.png (85.45 KB, 下載次數: 105)
下載附件
2021-2-22 17:24 上傳
空氣質量節點原理圖:
51hei截圖20210222172301.png (82.57 KB, 下載次數: 82)
下載附件
2021-2-22 17:26 上傳
紫外線+網關節點原理圖:
51hei截圖20210222172313.png (86.21 KB, 下載次數: 84)
下載附件
2021-2-22 17:27 上傳
PM2.5節點源碼:
- /*********************************************************************
- * INCLUDES
- */
- #include <stdio.h>
- #include <string.h>
- #include "AF.h"
- #include "OnBoard.h"
- #include "OSAL_Tasks.h"
- #include "SampleApp.h"
- #include "ZDApp.h"
- #include "ZDObject.h"
- #include "ZDProfile.h"
- #include "hal_drivers.h"
- #include "hal_key.h"
- #if defined ( LCD_SUPPORTED )
- #include "hal_lcd.h"
- #endif
- #include "hal_led.h"
- #include "hal_uart.h"
- #include "hal_adc.h"
- #include "MT_UART.h"
- #include "MT_APP.h"
- #include "MT.h"
- #include "pm2.5.h"
- #if !defined( SERIAL_APP_TX_MAX )
- #define SERIAL_APP_TX_MAX 80
- #endif
- /*********************************************************************
- * MACROS
- */
- /*********************************************************************
- * CONSTANTS
- */
- #if !defined( SAMPLE_APP_PORT )
- #define SAMPLE_APP_PORT 0
- #endif
- #if !defined( SAMPLE_APP_BAUD )
- #define SAMPLE_APP_BAUD HAL_UART_BR_115200
- #endif
- // When the Rx buf space is less than this threshold, invoke the Rx callback.
- #if !defined( SAMPLE_APP_THRESH )
- #define SAMPLE_APP_THRESH 64
- #endif
- #if !defined( SAMPLE_APP_RX_SZ )
- #define SAMPLE_APP_RX_SZ 128
- #endif
- #if !defined( SAMPLE_APP_TX_SZ )
- #define SAMPLE_APP_TX_SZ 128
- #endif
- // Millisecs of idle time after a byte is received before invoking Rx callback.
- #if !defined( SAMPLE_APP_IDLE )
- #define SAMPLE_APP_IDLE 6
- #endif
- // Loopback Rx bytes to Tx for throughput testing.
- #if !defined( SAMPLE_APP_LOOPBACK )
- #define SAMPLE_APP_LOOPBACK FALSE
- #endif
- // This is the max byte count per OTA message.
- #if !defined( SAMPLE_APP_TX_MAX )
- #define SAMPLE_APP_TX_MAX 80
- #endif
- #define SAMPLE_APP_RSP_CNT 4
- // This list should be filled with Application specific Cluster IDs.
- const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
- {
- SAMPLEAPP_P2P_CLUSTERID,
- SAMPLEAPP_PERIODIC_CLUSTERID,
- };
- const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
- {
- SAMPLEAPP_ENDPOINT, // int Endpoint;
- SAMPLEAPP_PROFID, // uint16 AppProfId[2];
- SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2];
- SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4;
- SAMPLEAPP_FLAGS, // int AppFlags:4;
- SAMPLE_MAX_CLUSTERS, // byte AppNumInClusters;
- (cId_t *)SampleApp_ClusterList, // byte *pAppInClusterList;
- SAMPLE_MAX_CLUSTERS, // byte AppNumOutClusters;
- (cId_t *)SampleApp_ClusterList // byte *pAppOutClusterList;
- };
- endPointDesc_t SampleApp_epDesc =
- {
- SAMPLEAPP_ENDPOINT,
- &SampleApp_TaskID,
- (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
- noLatencyReqs
- };
- /*********************************************************************
- * TYPEDEFS
- */
- /*********************************************************************
- * GLOBAL VARIABLES
- */
- devStates_t SampleApp_NwkState;
- uint8 SampleApp_TaskID; // Task ID for internal task/event processing.
- /*********************************************************************
- * EXTERNAL VARIABLES
- */
- /*********************************************************************
- * EXTERNAL FUNCTIONS
- */
- /*********************************************************************
- * LOCAL VARIABLES
- */
- uint16 PM25_TH=500;
- uint8 buff[20]={0};
- static uint8 SampleApp_MsgID;
- afAddrType_t SampleApp_Periodic_DstAddr; //廣播
- afAddrType_t SampleApp_Flash_DstAddr; //組播
- afAddrType_t SampleApp_P2P_DstAddr; //點播
- static afAddrType_t SampleApp_TxAddr;
- static uint8 SampleApp_TxSeq;
- static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
- uint8 SerialApp_TxBuf[SERIAL_APP_TX_MAX+1];
- uint8 SerialApp_TxLen;
- static uint8 SampleApp_TxLen;
- static afAddrType_t SampleApp_RxAddr;
- static uint8 SampleApp_RxSeq;
- static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
- /*********************************************************************
- * LOCAL FUNCTIONS
- */
- static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
- void SampleApp_CallBack(uint8 port, uint8 event);
- static void SampleApp_Send_P2P_Message( void );
- static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
- /*********************************************************************
- * @fn SampleApp_Init
- *
- * @brief This is called during OSAL tasks' initialization.
- *
- * @param task_id - the Task ID assigned by OSAL.
- *
- * @return none
- */
- void SampleApp_Init( uint8 task_id )
- {
- halUARTCfg_t uartConfig;
- SampleApp_TaskID = task_id;
- SampleApp_RxSeq = 0xC3;
- SampleApp_NwkState = DEV_INIT;
- MT_UartInit(); //串口初始化
- MT_UartRegisterTaskID(task_id); //注冊串口任務
- afRegister( (endPointDesc_t *)&SampleApp_epDesc );
- RegisterForKeys( task_id );
- #ifdef ZDO_COORDINATOR
- //協調器初始化
- //逢蜂鳴器初始化
- P0SEL &= ~0x80; //設置P07為普通IO口
- P0DIR |= 0x80; //P07定義為輸出口
- //默認蜂鳴器不響
- P0_7=1;
- #endif
- SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//廣播
- SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
- // Setup for the flash command's destination address - Group 1
- SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//組播
- SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;
- SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //點播
- SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000; //發給協調器
- }
- /*********************************************************************
- * @fn SampleApp_ProcessEvent
- *
- * @brief Generic Application Task event processor.
- *
- * @param task_id - The OSAL assigned task ID.
- * @param events - Bit map of events to process.
- *
- * @return Event flags of all unprocessed events.
- */
- UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
- {
- (void)task_id; // Intentionally unreferenced parameter
- if ( events & SYS_EVENT_MSG )
- {
- afIncomingMSGPacket_t *MSGpkt;
- while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
- {
- switch ( MSGpkt->hdr.event )
- {
- case AF_INCOMING_MSG_CMD:
- SampleApp_ProcessMSGCmd( MSGpkt );
- break;
- case ZDO_STATE_CHANGE:
- SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
- if (//(SampleApp_NwkState == DEV_ZB_COORD)||
- (SampleApp_NwkState == DEV_ROUTER)
- || (SampleApp_NwkState == DEV_END_DEVICE))
- {
- osal_start_timerEx( SampleApp_TaskID,
- SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );//采集PM2.5
- }
- break;
- default:
- break;
- }
- osal_msg_deallocate( (uint8 *)MSGpkt );
- }
- return ( events ^ SYS_EVENT_MSG );
- }
- //定時器時間到
- if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
- {
- // PM2.5采集
- SampleApp_Send_P2P_Message();
- // Setup to send message again in normal period (+ a little jitter)
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- 2000);
- // return unprocessed events
- return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
- }
- return ( 0 ); // Discard unknown events.
- }
- /*********************************************************************
- * @fn SerialApp_ProcessMSGCmd
- *
- * @brief Data message processor callback. This function processes
- * any incoming data - probably from other devices. Based
- * on the cluster ID, perform the intended action.
- *
- * @param pkt - pointer to the incoming message packet
- *
- * @return TRUE if the 'pkt' parameter is being used and will be freed later,
- * FALSE otherwise.
- */
- void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
- {
- switch ( pkt->clusterId )
- {
- // 接收終端上傳的溫度數據
- case SAMPLEAPP_P2P_CLUSTERID:
- break;
- case 2://報警閾值信息下發的簇
- break;
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn SampleApp_CallBack
- *
- * @brief Send data OTA.
- *
- * @param port - UART port.
- * @param event - the UART port event flag.
- *
- * @return none
- */
- void SampleApp_CallBack(uint8 port, uint8 event)
- {
- (void)port;
- if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
- #if SAMPLE_APP_LOOPBACK
- (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
- #else
- !SampleApp_TxLen)
- #endif
- {
- SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
- SAMPLE_APP_TX_MAX-SampleApp_TxLen);
- }
- }
- /*********************************************************************
- * @fn SampleApp_Send_P2P_Message
- *
- * @brief point to point.
- *
- * @param none
- *
- * @return none
- */
- void SampleApp_Send_P2P_Message( void )
- {
- uint16 Pm25;
- uint8 str[10]={0};
- int len=0;
- Pm25=GetPm25(); //取PM25數據
- str[0] = 3; //增加一個ID,如果是多個終端就增加這個值
- str[1] = LO_UINT16( Pm25 );
- str[2] = HI_UINT16( Pm25 );
- len=3;
- P1SEL &= ~0x13; //設置0、1、4為普通IO口
- P1DIR |= 0x13; //0、1、4定義為輸出口
- /******閾值報警處理*****/
- if( Pm25>PM25_TH )
- {
- P1_0=0;//板子上LED燈D1亮表示粉塵濃度報警
- }
- else
- {
- P1_0=1;
- }
- //無線發送到協調器
- if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
- SAMPLEAPP_P2P_CLUSTERID,
- len,
- str,
- &SampleApp_MsgID,
- AF_DISCV_ROUTE,
- AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
- {
- }
- else
- {
- // Error occurred in request to send.
- }
- }
- uint8 CheckSum(uint8 *pdata, uint8 len)
- {
- uint8 i;
- uint8 check_sum=0;
- for(i=0; i<len; i++)
- {
- check_sum += pdata[i];
- }
- return check_sum;
- }
- //數據打包發送
- /**
- *fc:功能碼
- *data:上傳的數據
- *len:數據長度
- 格式:len,校驗,fc,內容,$,@,
- */
- void packDataAndSend(uint8 fc, uint8* data, uint8 len)
- {
- osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
- //數據包長度
- SampleApp_TxBuf[0]=3+len;
- //功能碼
- SampleApp_TxBuf[2]=fc;
- //發送的數據
- if(len>0)
- {
- osal_memcpy(SampleApp_TxBuf+3, data, len);
- }
- //校驗和,從fc開始,
- SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
- //數據結尾
- SampleApp_TxBuf[3+len]='
- SampleApp_TxBuf[4+len]='@';
- SampleApp_TxBuf[5+len]='\r';
- SampleApp_TxBuf[6+len]='\n';
- //發送長度
- SampleApp_TxLen=7+len;
- //接著發數據包
- HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
- }
- 溫濕度節點源碼:
- /*********************************************************************
- * INCLUDES
- */
- #include <stdio.h>
- #include <string.h>
- #include "AF.h"
- #include "OnBoard.h"
- #include "OSAL_Tasks.h"
- #include "SampleApp.h"
- #include "ZDApp.h"
- #include "ZDObject.h"
- #include "ZDProfile.h"
- #include "hal_drivers.h"
- #include "hal_key.h"
- #if defined ( LCD_SUPPORTED )
- #include "hal_lcd.h"
- #endif
- #include "hal_led.h"
- #include "hal_uart.h"
- #include "hal_adc.h"
- #include "dht11.h"
- /*********************************************************************
- * MACROS
- */
- /*********************************************************************
- * CONSTANTS
- */
- #if !defined( SAMPLE_APP_PORT )
- #define SAMPLE_APP_PORT 0
- #endif
- #if !defined( SAMPLE_APP_BAUD )
- #define SAMPLE_APP_BAUD HAL_UART_BR_115200
- #endif
- // When the Rx buf space is less than this threshold, invoke the Rx callback.
- #if !defined( SAMPLE_APP_THRESH )
- #define SAMPLE_APP_THRESH 64
- #endif
- #if !defined( SAMPLE_APP_RX_SZ )
- #define SAMPLE_APP_RX_SZ 128
- #endif
- #if !defined( SAMPLE_APP_TX_SZ )
- #define SAMPLE_APP_TX_SZ 128
- #endif
- // Millisecs of idle time after a byte is received before invoking Rx callback.
- #if !defined( SAMPLE_APP_IDLE )
- #define SAMPLE_APP_IDLE 6
- #endif
- // Loopback Rx bytes to Tx for throughput testing.
- #if !defined( SAMPLE_APP_LOOPBACK )
- #define SAMPLE_APP_LOOPBACK FALSE
- #endif
- // This is the max byte count per OTA message.
- #if !defined( SAMPLE_APP_TX_MAX )
- #define SAMPLE_APP_TX_MAX 80
- #endif
- #define SAMPLE_APP_RSP_CNT 4
- // This list should be filled with Application specific Cluster IDs.
- const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
- {
- SAMPLEAPP_P2P_CLUSTERID,
- SAMPLEAPP_PERIODIC_CLUSTERID,
- };
- const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
- {
- SAMPLEAPP_ENDPOINT, // int Endpoint;
- SAMPLEAPP_PROFID, // uint16 AppProfId[2];
- SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2];
- SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4;
- SAMPLEAPP_FLAGS, // int AppFlags:4;
- SAMPLE_MAX_CLUSTERS, // byte AppNumInClusters;
- (cId_t *)SampleApp_ClusterList, // byte *pAppInClusterList;
- SAMPLE_MAX_CLUSTERS, // byte AppNumOutClusters;
- (cId_t *)SampleApp_ClusterList // byte *pAppOutClusterList;
- };
- endPointDesc_t SampleApp_epDesc =
- {
- SAMPLEAPP_ENDPOINT,
- &SampleApp_TaskID,
- (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
- noLatencyReqs
- };
- /*********************************************************************
- * TYPEDEFS
- */
- /*********************************************************************
- * GLOBAL VARIABLES
- */
- devStates_t SampleApp_NwkState;
- uint8 SampleApp_TaskID; // Task ID for internal task/event processing.
- /*********************************************************************
- * EXTERNAL VARIABLES
- */
- /*********************************************************************
- * EXTERNAL FUNCTIONS
- */
- /*********************************************************************
- * LOCAL VARIABLES
- */
- uint8 Temp_Hth=30,Temp_Lth=10;
- uint8 Humi_Hth=80,Humi_Lth=10;
- uint8 AO_Hth=70,AO_Lth=10;
- uint8 buff[20]={0};
- uint8 Motor_state=0x05,LED_state=1;
- static uint8 SampleApp_MsgID;
- afAddrType_t SampleApp_Periodic_DstAddr; //廣播
- afAddrType_t SampleApp_Flash_DstAddr; //組播
- afAddrType_t SampleApp_P2P_DstAddr; //點播
- static afAddrType_t SampleApp_TxAddr;
- static uint8 SampleApp_TxSeq;
- static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
- static uint8 SampleApp_TxLen;
- static afAddrType_t SampleApp_RxAddr;
- static uint8 SampleApp_RxSeq;
- static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
- /*********************************************************************
- * LOCAL FUNCTIONS
- */
- static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
- void SampleApp_CallBack(uint8 port, uint8 event);
- static void SampleApp_Send_P2P_Message( void );
- static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
- /*********************************************************************
- * @fn SampleApp_Init
- *
- * @brief This is called during OSAL tasks' initialization.
- *
- * @param task_id - the Task ID assigned by OSAL.
- *
- * @return none
- */
- void SampleApp_Init( uint8 task_id )
- {
- halUARTCfg_t uartConfig;
- SampleApp_TaskID = task_id;
- SampleApp_RxSeq = 0xC3;
- SampleApp_NwkState = DEV_INIT;
- MT_UartInit(); //串口初始化
- MT_UartRegisterTaskID(task_id); //注冊串口任務
- afRegister( (endPointDesc_t *)&SampleApp_epDesc );
- RegisterForKeys( task_id );
- #ifdef ZDO_COORDINATOR
- //協調器初始化
- //逢蜂鳴器初始化
- P0SEL &= ~0x80; //設置P07為普通IO口
- P0DIR |= 0x80; //P07定義為輸出口
- //默認蜂鳴器不響
- P0_7=1;
- #endif
- SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//廣播
- SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
- // Setup for the flash command's destination address - Group 1
- SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//組播
- SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;
- SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //點播
- SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000; //發給協調器
- }
- /*********************************************************************
- * @fn SampleApp_ProcessEvent
- *
- * @brief Generic Application Task event processor.
- *
- * @param task_id - The OSAL assigned task ID.
- * @param events - Bit map of events to process.
- *
- * @return Event flags of all unprocessed events.
- */
- UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
- {
- (void)task_id; // Intentionally unreferenced parameter
- if ( events & SYS_EVENT_MSG )
- {
- afIncomingMSGPacket_t *MSGpkt;
- while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
- {
- switch ( MSGpkt->hdr.event )
- {
- case AF_INCOMING_MSG_CMD:
- SampleApp_ProcessMSGCmd( MSGpkt );
- break;
- case ZDO_STATE_CHANGE:
- SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
- if ( //(SampleApp_NwkState == DEV_ZB_COORD)||
- (SampleApp_NwkState == DEV_ROUTER)
- || (SampleApp_NwkState == DEV_END_DEVICE) )
- {
- //連網成功后,啟動一個定時器
- osal_start_timerEx( SampleApp_TaskID,
- SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
- }
- else
- {
- // Device is no longer in the network
- }
- break;
- default:
- break;
- }
- osal_msg_deallocate( (uint8 *)MSGpkt );
- }
- return ( events ^ SYS_EVENT_MSG );
- }
- //定時器時間到
- if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
- {
- // DHT11采集
- SampleApp_Send_P2P_Message();
- // Setup to send message again in normal period (+ a little jitter)
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
- // return unprocessed events
- return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
- }
- return ( 0 ); // Discard unknown events.
- }
- /*********************************************************************
- * @fn SerialApp_ProcessMSGCmd
- *
- * @brief Data message processor callback. This function processes
- * any incoming data - probably from other devices. Based
- * on the cluster ID, perform the intended action.
- *
- * @param pkt - pointer to the incoming message packet
- *
- * @return TRUE if the 'pkt' parameter is being used and will be freed later,
- * FALSE otherwise.
- */
- void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
- {
- switch ( pkt->clusterId )
- {
- // 接收終端上傳的溫度數據
- case SAMPLEAPP_P2P_CLUSTERID:
- #ifdef ZDO_COORDINATOR
- {
- uint8 id=pkt->cmd.Data[0];//終端id
- uint8 t=pkt->cmd.Data[1]; //終端溫度
- uint8 h=pkt->cmd.Data[2]; //終端濕度
- if(id==1)
- {
- sprintf(buff, "%d T&H:%d %d", id, t, h);
- HalLcdWriteString(buff, HAL_LCD_LINE_2); //LCD顯示
- }
- else if(id==2)
- {
- sprintf(buff, "%d T&H:%d %d", id, t, h);
- HalLcdWriteString(buff, HAL_LCD_LINE_3); //LCD顯示
- }
- else if(id==3)
- {
- sprintf(buff, "%d T&H:%d %d", id, t, h);
- HalLcdWriteString(buff, HAL_LCD_LINE_4); //LCD顯示
- }
- //蜂鳴器報警
- if(t>35)//溫度高于35度報警
- {
- P0_7=0;
- }
- else if(h<30)//濕度低于30報警
- {
- P0_7=0;
- }
- else
- {
- P0_7=1;//不報警
- }
- //串口輸出可以看得見的字符,就把"#if 0"改為"#if 1"
- //默認是輸出16進制數據,使用我們寫的PC工具或者手機軟件查看
- #if 0
- HalUARTWrite(0, buff, osal_strlen(buff)); //串口輸出提示信息
- HalUARTWrite(0, "\r\n",2);
- #else
- //打包無線發送的數據
- buff[0]=id;//終端id
- buff[1]=t; //終端溫度
- buff[2]=h; //終端濕度
- //打包數據用于發送到wifi網關,或者串口
- packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);
- #endif
- }
- #endif
- break;
- case 2://報警閾值信息下發的簇
- Temp_Hth=pkt->cmd.Data[0]; //溫度上限
- Humi_Hth=pkt->cmd.Data[1]; //濕度上限
- AO_Hth=pkt->cmd.Data[2]; //光照上限
- Temp_Lth=pkt->cmd.Data[3]; //溫度下限
- Humi_Lth=pkt->cmd.Data[4]; //濕度下限
- AO_Lth=pkt->cmd.Data[5]; //光照下限
- break;
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn SampleApp_CallBack
- *
- * @brief Send data OTA.
- *
- * @param port - UART port.
- * @param event - the UART port event flag.
- *
- * @return none
- */
- void SampleApp_CallBack(uint8 port, uint8 event)
- {
- (void)port;
- if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
- #if SAMPLE_APP_LOOPBACK
- (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
- #else
- !SampleApp_TxLen)
- #endif
- {
- SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
- SAMPLE_APP_TX_MAX-SampleApp_TxLen);
- }
- }
- /*********************************************************************
- * @fn SampleApp_Send_P2P_Message
- *
- * @brief point to point.
- *
- * @param none
- *
- * @return none
- */
- void SampleApp_Send_P2P_Message( void )
- {
- uint8 str[10]={0};
- int len=0;
- DHT11(); //獲取溫濕度
- str[0] = 1; //增加一個ID,如果是多個終端就增加這個值
- str[1] = wendu; //溫度
- str[2] = shidu; //濕度
- len=3;
- P1SEL &= ~0x13; //設置0、1、4為普通IO口
- P1DIR |= 0x13; //0、1、4定義為輸出口
- /******閾值報警處理*****/
- if((str[1]>=Temp_Lth)&&(str[1]<=Temp_Hth))
- {
- P1_0=1;
- }
- else
- {
- P1_0=0;//板子上LED燈D1亮表示溫度報警
- }
- if((str[2]>=Humi_Lth)&&(str[2]<=Humi_Hth))
- {
- P1_1=1;
- }
- else
- {
- P1_1=0;//板子上LED燈D2亮表示濕度報警
- }
- //無線發送到協調器
- if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
- SAMPLEAPP_P2P_CLUSTERID,
- len,
- str,
- &SampleApp_MsgID,
- AF_DISCV_ROUTE,
- AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
- {
- }
- else
- {
- // Error occurred in request to send.
- }
- }
- uint8 CheckSum(uint8 *pdata, uint8 len)
- {
- uint8 i;
- uint8 check_sum=0;
- for(i=0; i<len; i++)
- {
- check_sum += pdata[i];
- }
- return check_sum;
- }
- //數據打包發送
- /**
- *fc:功能碼
- *data:上傳的數據
- *len:數據長度
- 格式:len,校驗,fc,內容,$,@,
- */
- void packDataAndSend(uint8 fc, uint8* data, uint8 len)
- {
- osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
- //數據包長度
- SampleApp_TxBuf[0]=3+len;
- //功能碼
- SampleApp_TxBuf[2]=fc;
- //發送的數據
- if(len>0)
- {
- osal_memcpy(SampleApp_TxBuf+3, data, len);
- }
- //校驗和,從fc開始,
- SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
- //數據結尾
- SampleApp_TxBuf[3+len]='
- SampleApp_TxBuf[4+len]='@';
- SampleApp_TxBuf[5+len]='\r';
- SampleApp_TxBuf[6+len]='\n';
- //發送長度
- SampleApp_TxLen=7+len;
- //接著發數據包
- HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
- }
- 紫外線+網關源碼:
- /*********************************************************************
- * INCLUDES
- */
- #include <stdio.h>
- #include <string.h>
- #include "AF.h"
- #include "OnBoard.h"
- #include "OSAL_Tasks.h"
- #include "SampleApp.h"
- #include "ZDApp.h"
- #include "ZDObject.h"
- #include "ZDProfile.h"
- #include "hal_drivers.h"
- #include "hal_key.h"
- #include "hal_lcd.h"
- #include "hal_led.h"
- #include "hal_uart.h"
- #include "hal_adc.h"
- /*********************************************************************
- * MACROS
- */
- /*********************************************************************
- * CONSTANTS
- */
- #if !defined( SAMPLE_APP_PORT )
- #define SAMPLE_APP_PORT 0
- #endif
- #if !defined( SAMPLE_APP_BAUD )
- #define SAMPLE_APP_BAUD HAL_UART_BR_115200
- #endif
- // When the Rx buf space is less than this threshold, invoke the Rx callback.
- #if !defined( SAMPLE_APP_THRESH )
- #define SAMPLE_APP_THRESH 64
- #endif
- #if !defined( SAMPLE_APP_RX_SZ )
- #define SAMPLE_APP_RX_SZ 128
- #endif
- #if !defined( SAMPLE_APP_TX_SZ )
- #define SAMPLE_APP_TX_SZ 128
- #endif
- // Millisecs of idle time after a byte is received before invoking Rx callback.
- #if !defined( SAMPLE_APP_IDLE )
- #define SAMPLE_APP_IDLE 6
- #endif
- // Loopback Rx bytes to Tx for throughput testing.
- #if !defined( SAMPLE_APP_LOOPBACK )
- #define SAMPLE_APP_LOOPBACK FALSE
- #endif
- // This is the max byte count per OTA message.
- #if !defined( SAMPLE_APP_TX_MAX )
- #define SAMPLE_APP_TX_MAX 80
- #endif
- #define SAMPLE_APP_RSP_CNT 4
- // This list should be filled with Application specific Cluster IDs.
- const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
- {
- SAMPLEAPP_P2P_CLUSTERID,
- SAMPLEAPP_PERIODIC_CLUSTERID,
- };
- const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
- {
- SAMPLEAPP_ENDPOINT, // int Endpoint;
- SAMPLEAPP_PROFID, // uint16 AppProfId[2];
- SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2];
- SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4;
- SAMPLEAPP_FLAGS, // int AppFlags:4;
- SAMPLE_MAX_CLUSTERS, // byte AppNumInClusters;
- (cId_t *)SampleApp_ClusterList, // byte *pAppInClusterList;
- SAMPLE_MAX_CLUSTERS, // byte AppNumOutClusters;
- (cId_t *)SampleApp_ClusterList // byte *pAppOutClusterList;
- };
- endPointDesc_t SampleApp_epDesc =
- {
- SAMPLEAPP_ENDPOINT,
- &SampleApp_TaskID,
- (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
- noLatencyReqs
- };
- /*********************************************************************
- * TYPEDEFS
- */
- /*********************************************************************
- * GLOBAL VARIABLES
- */
- devStates_t SampleApp_NwkState;
- uint8 SampleApp_TaskID; // Task ID for internal task/event processing.
- /*********************************************************************
- * EXTERNAL VARIABLES
- */
- /*********************************************************************
- * EXTERNAL FUNCTIONS
- */
- /*********************************************************************
- * LOCAL VARIABLES
- */
- uint8 Temp_Hth=30,Temp_Lth=10;
- uint8 Humi_Hth=80,Humi_Lth=10;
- uint8 AO_Hth=70,AO_Lth=10;
- uint8 buff[20]={0};
- uint8 header3[17]="AT+CIPSEND=0,28";
- uint8 Temperature=0,Humidity=0,AO=0;
- uint16 PM25;
- static uint8 SampleApp_MsgID;
- afAddrType_t SampleApp_Periodic_DstAddr; //廣播
- afAddrType_t SampleApp_Flash_DstAddr; //組播
- afAddrType_t SampleApp_P2P_DstAddr; //點播
- static afAddrType_t SampleApp_TxAddr;
- static uint8 SampleApp_TxSeq;
- static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
- static uint8 SampleApp_TxLen;
- static afAddrType_t SampleApp_RxAddr;
- static uint8 SampleApp_RxSeq;
- static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
- /*********************************************************************
- * LOCAL FUNCTIONS
- */
- static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
- void SampleApp_CallBack(uint8 port, uint8 event);
- static void SampleApp_Send_P2P_Message( void );
- static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
- //取光照值,接在P06上
- uint8 GetLight()
- {
- uint8 temp=0;//百分比的整數值
- float vol=0.0; //adc采樣電壓
- uint16 adc= HalAdcRead(HAL_ADC_CHANNEL_6, HAL_ADC_RESOLUTION_14); //ADC 采樣值 P06口
- //最大采樣值8192(因為最高位是符號位)
- if(adc>=8192)
- {
- return 0;
- }
- //轉化為百分比
- vol=(float)((float)adc)/8192.0;
- //取百分比兩位數字
- temp=vol*100;
- return temp;
- }
- /*********************************************************************
- * @fn SampleApp_Init
- *
- * @brief This is called during OSAL tasks' initialization.
- *
- * @param task_id - the Task ID assigned by OSAL.
- *
- * @return none
- */
- void SampleApp_Init( uint8 task_id )
- {
- halUARTCfg_t uartConfig;
- SampleApp_TaskID = task_id;
- SampleApp_RxSeq = 0xC3;
- SampleApp_NwkState = DEV_INIT;
- MT_UartInit(); //串口初始化
- MT_UartRegisterTaskID(task_id); //注冊串口任務
- afRegister( (endPointDesc_t *)&SampleApp_epDesc );
- RegisterForKeys( task_id );
- #ifdef ZDO_COORDINATOR
- //協調器初始化
- //逢蜂鳴器初始化
- P0SEL &= ~0x80; //設置P07為普通IO口
- P0DIR |= 0x80; //P07定義為輸出口
- //默認蜂鳴器不響
- P0_7=1;
- #endif
- SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//廣播
- SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
- // Setup for the flash command's destination address - Group 1
- SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//組播
- SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;
- SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //點播
- SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
- SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000; //發給協調器
- }
- /*********************************************************************
- * @fn SampleApp_ProcessEvent
- *
- * @brief Generic Application Task event processor.
- *
- * @param task_id - The OSAL assigned task ID.
- * @param events - Bit map of events to process.
- *
- * @return Event flags of all unprocessed events.
- */
- UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
- {
- (void)task_id; // Intentionally unreferenced parameter
- if ( events & SYS_EVENT_MSG )
- {
- afIncomingMSGPacket_t *MSGpkt;
- while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
- {
- switch ( MSGpkt->hdr.event )
- {
- case AF_INCOMING_MSG_CMD:
- SampleApp_ProcessMSGCmd( MSGpkt );
- break;
- case ZDO_STATE_CHANGE:
- SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
- if (SampleApp_NwkState == DEV_ZB_COORD)
- {
- uint8 header1[13]="AT+CIPMUX=1";
- header1[11]='\r';header1[12]='\n';
- header3[15]='\r';header3[16]='\n';
- HalUARTWrite(0,header1,13);
- osal_start_timerEx( SampleApp_TaskID,
- SAMPLEAPP_SEND_WIFI_MSG1_EVT,
- 2*1000 );
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
- }
- break;
- default:
- break;
- }
- osal_msg_deallocate( (uint8 *)MSGpkt );
- }
- return ( events ^ SYS_EVENT_MSG );
- }
- //定時器時間到
- if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
- {
- // 光照采集
- SampleApp_Send_P2P_Message();
- // Setup to send message again in normal period (+ a little jitter)
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
- // return unprocessed events
- return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
- }
- if ( events & SAMPLEAPP_SEND_WIFI_MSG1_EVT )
- {
- uint8 header2[21]="AT+CIPSERVER=1,2020";
- header2[19]='\r';header2[20]='\n';
- HalUARTWrite(0,header2,21);
- LCD_Fill(0x00); //初始清屏
- LCD_Set_Pos(0,0);
- HalLcdWriteString( "ZigBee環境監測", HAL_LCD_LINE_1 );
- return (events ^ SAMPLEAPP_SEND_WIFI_MSG1_EVT);
- }
- if ( events & SAMPLEAPP_UPDATE_EVT )
- {
- uint8 str[6]={0};
- str[0] = Temp_Hth;
- str[1] = Temp_Lth;
- str[2] = Humi_Hth;
- str[3] = Humi_Lth;
- str[4] = AO_Hth;
- str[5] = AO_Lth;
- if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
- 2,
- 6,
- str,
- &SampleApp_MsgID,
- AF_DISCV_ROUTE,
- AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
- {
- }
- return (events ^ SAMPLEAPP_UPDATE_EVT);
- }
- return ( 0 ); // Discard unknown events.
- }
- /*********************************************************************
- * @fn SerialApp_ProcessMSGCmd
- *
- * @brief Data message processor callback. This function processes
- * any incoming data - probably from other devices. Based
- * on the cluster ID, perform the intended action.
- *
- * @param pkt - pointer to the incoming message packet
- *
- * @return TRUE if the 'pkt' parameter is being used and will be freed later,
- * FALSE otherwise.
- */
- void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
- {
- uint8 id,PM25_L,PM25_H;
- switch ( pkt->clusterId )
- {
- // 接收終端上傳的溫度數據
- case SAMPLEAPP_P2P_CLUSTERID:
- #ifdef ZDO_COORDINATOR
- {
- id=pkt->cmd.Data[0];
- if(id == 1)//終端1,溫濕度
- {
- Temperature=pkt->cmd.Data[1]; //終端溫度
- Humidity=pkt->cmd.Data[2]; //終端濕度
- buff[0]=id;
- buff[1]=Temperature;
- buff[2]=Humidity;
- }
- else if(id == 3)//終端3,PM25
- {
- PM25_L=pkt->cmd.Data[1];
- PM25_H=pkt->cmd.Data[2];
- PM25=BUILD_UINT16(PM25_L, PM25_H);
- buff[0]=id;
- buff[1]=PM25_L;
- buff[2]=PM25_H;
- }
- #if 0
- HalUARTWrite(0, buff, osal_strlen(buff)); //串口輸出提示信息
- HalUARTWrite(0, "\r\n",2);
- #else
- //打包數據用于發送到wifi網關
- HalUARTWrite(0,header3,17);
- packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);
- #endif
- }
- #endif
- break;
- case 2://報警閾值信息下發的簇
- Temp_Hth=pkt->cmd.Data[0]; //溫度上限
- Temp_Lth=pkt->cmd.Data[1]; //溫度下限
- Humi_Hth=pkt->cmd.Data[2]; //濕度上限
- Humi_Lth=pkt->cmd.Data[3]; //濕度下限
- AO_Hth=pkt->cmd.Data[4]; //光照上限
- AO_Lth=pkt->cmd.Data[5]; //光照下限
- break;
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn SampleApp_CallBack
- *
- * @brief Send data OTA.
- *
- * @param port - UART port.
- * @param event - the UART port event flag.
- *
- * @return none
- */
- void SampleApp_CallBack(uint8 port, uint8 event)
- {
- (void)port;
- if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
- #if SAMPLE_APP_LOOPBACK
- (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
- #else
- !SampleApp_TxLen)
- #endif
- {
- SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
- SAMPLE_APP_TX_MAX-SampleApp_TxLen);
- }
- }
- /*********************************************************************
- * @fn SampleApp_Send_P2P_Message
- *
- * @brief point to point.
- *
- * @param none
- *
- * @return none
- */
- void SampleApp_Send_P2P_Message( void )
- {
- uint8 str[10]={0};
- uint8 strTemp[30]={0};
- AO = GetLight(); //光照AO口數據,然后轉成百分比0~100,數據越高,光照越強
- int len=0;
- str[0] = 2; //增加一個ID,如果是多個終端就增加這個值
- str[1] = AO;
- str[2] = 0;
- len=3;
- //LCD顯示pm2.5
- sprintf(strTemp, "PM2.5:%d ug/m3",PM25);
- HalLcdWriteString( strTemp, HAL_LCD_LINE_4 );
- //LCD顯示溫濕度
- sprintf(strTemp, "溫:%02d 濕:%02d",Temperature,Humidity);
- HalLcdWriteString( strTemp, HAL_LCD_LINE_3 );
- //LCD顯示光照強度
- sprintf(strTemp, "光照:%02d",AO);
- HalLcdWriteString( strTemp, HAL_LCD_LINE_2 );
- /******閾值報警處理*****/
- P1SEL &= ~0x13; //設置0、1、4為普通IO口
- P1DIR |= 0x13; //0、1、4定義為輸出口
- if((str[3]>=AO_Lth)&&(str[3]<=AO_Hth))
- {
- P1_4=1;
- }
- else
- {
- P1_4=0;//板子上LED燈D3亮表示光照報警
- }
- //串口發送到WiFi
- HalUARTWrite(0,header3,17);
- packDataAndSend(FUN_CODE_UPDATA_DATA, str, len);
- }
- uint8 CheckSum(uint8 *pdata, uint8 len)
- {
- uint8 i;
- uint8 check_sum=0;
- for(i=0; i<len; i++)
- {
- check_sum += pdata[i];
- }
- return check_sum;
- }
- //數據打包發送
- /**
- *fc:功能碼
- *data:上傳的數據
- *len:數據長度
- 格式:len,校驗,fc,內容,$,@,
- */
- void packDataAndSend(uint8 fc, uint8* data, uint8 len)
- {
- osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
- //數據包長度
- SampleApp_TxBuf[0]=3+len;
- //功能碼
- SampleApp_TxBuf[2]=fc;
- //發送的數據
- if(len>0)
- {
- osal_memcpy(SampleApp_TxBuf+3, data, len);
- }
- //校驗和,從fc開始,
- SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
- //數據結尾
- SampleApp_TxBuf[3+len]='
- SampleApp_TxBuf[4+len]='@';
- SampleApp_TxBuf[5+len]='\r';
- SampleApp_TxBuf[6+len]='\n';
- //發送長度
- SampleApp_TxLen=7+len;
- //接著發數據包
- HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
- }
復制代碼
全部資料51hei下載地址:
|
-
-
硬件原理圖和模塊資料.7z
2024-3-19 16:01 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
8.2 MB, 下載次數: 178, 下載積分: 黑幣 -5
-
-
android.zip
2021-2-22 17:38 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1.5 MB, 下載次數: 89, 下載積分: 黑幣 -5
-
-
Sunlight_Coord.7z
2021-2-23 02:05 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
8.49 MB, 下載次數: 89, 下載積分: 黑幣 -5
-
-
PM25.7z
2021-2-23 02:05 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
8.5 MB, 下載次數: 84, 下載積分: 黑幣 -5
-
-
Temp Humi.7z
2021-2-23 02:05 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
8.45 MB, 下載次數: 90, 下載積分: 黑幣 -5
評分
-
查看全部評分
|