前言:
大四上学期的一门课程设计,主要用到CC2530单片机、WiFi模块:ESP826607、光照强度传感器:BH1750、步进电机等… 现分享给大家,希望对大家有所帮助。
实现功能:
数据采集:
- 采集光照强度的数据到ONENET云平台
- 采集终端设备的工作状态
本地控制:
- 控制照明设备的开关
- 控制照明设备是否进入自动模式
- 控制照明设备是否进入手动模式
自动控制:
- 根据当前的光照强度和设定的光照阈值自动控制照明设备的亮灭
- 自动调节照明设备的亮度
远程控制:
- 远程控制照明设备的亮灭
- 远程调节照明设备的亮度
- 远程控制窗帘的开关
- 远程控制照明设备的自动模式
- 远程设定光照阈值
原理图:
PCB图:
3D模型图:
实物图:
手机应用界面:
自动控制代码:
if(flag1==0&&flag2==1)
{
if(end_temp < LIGHT )
{
P1_4 = 0;
}
else
{
P1_4 = 1;
}
}
if(flag1==0&&Remote_self_dimming==1)
{
pwmConfig(100-light2);
}
else
pwmConfig(Remote_dimming_control);
}
本地控制代码:
extern unsigned char flag = 0;
extern unsigned char flag1 = 0;
void SampleApp_HandleKeys( uint8 shift, uint8 keys )
{
(void)shift;
if ( keys & HAL_KEY_SW_6 )
{
if(flag1 == 0)
{
flag1 = 1;
}
else
{
flag1 = 0;
}
}
if ( keys & HAL_KEY_SW_1 )
{
if(flag == 0)
{
flag = 1;
HalLedSet( HAL_LED_3, HAL_LED_MODE_OFF);
}
else
{
flag = 0;
HalLedSet( HAL_LED_3, HAL_LED_MODE_ON);
}
}
}
远程控制代码:
void Ctrl(uint8* cmd)
{
if(strstr((char *)cmd, "D31"))
{
P1_4 = 0;
}
else if(strstr((char *)cmd, "D30"))
{
P1_4 = 1;
}
else if(strstr((char *)cmd, "flag21"))
{
flag2 = 1;
}
else if(strstr((char *)cmd, "flag20"))
{
flag2 = 0;
}
else if(strstr((char *)cmd, "Remote_self_dimming1"))
{
Remote_self_dimming = 1;
}
else if(strstr((char *)cmd, "Remote_self_dimming0"))
{
Remote_self_dimming = 0;
}
else if(cmd[0]== 'L')
{
if(osal_strlen(cmd)==6)
LIGHT = (cmd[1]-0X30)*10000+(cmd[2]-0X30)*1000+(cmd[3]-0X30)*100+(cmd[4]-0X30)*10+(cmd[5]-0X30)*1;
else if(osal_strlen(cmd)==5)
LIGHT = (cmd[1]-0X30)*1000+(cmd[2]-0X30)*100+(cmd[3]-0X30)*10+(cmd[4]-0X30)*1;
else if(osal_strlen(cmd)==4)
LIGHT = (cmd[1]-0X30)*100+(cmd[2]-0X30)*10+(cmd[3]-0X30)*1;
else if(osal_strlen(cmd)==3)
LIGHT = (cmd[1]-0X30)*10+(cmd[2]-0X30)*1;
else if(osal_strlen(cmd)==2)
LIGHT = (cmd[1]-0X30)*1;
}
else if(cmd[0]== 'A')
{
if(osal_strlen(cmd)==4)
Remote_dimming_control = (cmd[1]-0X30)*100+(cmd[2]-0X30)*10+(cmd[3]-0X30)*1;
else if(osal_strlen(cmd)==3)
Remote_dimming_control = (cmd[1]-0X30)*10+(cmd[2]-0X30)*1;
else if(osal_strlen(cmd)==2)
Remote_dimming_control = (cmd[1]-0X30)*1;
}
else if(cmd[0]== 'M')
{
if(cmd[1]== '0')
{
uint8 on=0;
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
1,
&on,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
else if(cmd[1]== 'F')
{
uint8 buff[3];
buff[0]=cmd[1];
buff[1]=cmd[2];
buff[2]=cmd[4];
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
4,
buff,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
else if(cmd[1]== 'P')
{
uint8 buff[3];
buff[0]=cmd[1];
buff[1]=cmd[2];
buff[2]=cmd[4];
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
4,
buff,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
}
}
终端发送数据到协调器代码:
void SampleApp_Send_P2P_Message( void )
{
byte str[6]={0};
byte str1[5]={0};
int len=0;
unsigned short light=0;
light=get_light();
wan = light/10000+0x30;
qian = (light/1000)%10+0x30;
bai = (light/100)%10+0x30;
shi = (light/10)%10+0x30;
ge = light %10+0x30;
str[4]=wan;
str[3]=qian;
str[2]=bai;
str[1]=shi;
str[0]=ge;
str[5]=mflag+0x30;
sprintf(str1, "%d", light);
len=osal_strlen(str1);
HalUARTWrite(0, "light: ",7);
HalUARTWrite(0, str1,len );
HalUARTWrite(0, "\r\n",2);
AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_P2P_CLUSTERID,
6,
str,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
应用层部分代码:
关于ZigBee协议栈的代码太多,故只能放一部分。
#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 "MT_UART.h"
#include "string.h"
#include "stdio.h"
#ifdef ZDO_COORDINATOR
#include "stdlib.h"
#include "LocalControl.h"
#include "onenet.h"
#include "edpkit.h"
#include "pwm.h"
#else
#include "bh1750.h"
#include "motor.h"
#endif
#if !defined( SAMPLE_APP_PORT )
#define SAMPLE_APP_PORT 0
#endif
#if !defined( SAMPLE_APP_TX_MAX )
#define SAMPLE_APP_TX_MAX 80
#endif
#define SAMPLE_APP_RSP_CNT 4
#define TO_ONENET_TIME 5000
#define TO_COOR_TIME 5000
uint32 LIGHT = 100;
uint8 Remote_dimming_control = 0;
uint8 Remote_self_dimming=1 ;
const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
{
SAMPLEAPP_P2P_CLUSTERID,
SAMPLEAPP_PERIODIC_CLUSTERID,
};
const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
{
SAMPLEAPP_ENDPOINT,
SAMPLEAPP_PROFID,
SAMPLEAPP_DEVICEID,
SAMPLEAPP_DEVICE_VERSION,
SAMPLEAPP_FLAGS,
SAMPLE_MAX_CLUSTERS,
(cId_t *)SampleApp_ClusterList,
SAMPLE_MAX_CLUSTERS,
(cId_t *)SampleApp_ClusterList
};
endPointDesc_t SampleApp_epDesc =
{
SAMPLEAPP_ENDPOINT,
&SampleApp_TaskID,
(SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
noLatencyReqs
};
devStates_t SampleApp_NwkState;
uint8 SampleApp_TaskID;
static uint8 SampleApp_MsgID;
afAddrType_t SampleApp_P2P_DstAddr;
afAddrType_t SampleApp_Periodic_DstAddr;
static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1]={0};
static uint8 SampleApp_TxLen=0;
uint8 onenet_login_ok=0;
unsigned short end_temp;
uint8 ge,shi,bai,qian,wan;
static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
void SampleApp_CallBack(uint8 port, uint8 event);
static void SampleApp_Send_P2P_Message( void );
void motor(void);
uint8 mflag1 = 0;
void SampleApp_Init( uint8 task_id )
{
SampleApp_TaskID = task_id;
SampleApp_NwkState = DEV_INIT;
MT_UartInit();
MT_UartRegisterTaskID(task_id);
afRegister( (endPointDesc_t *)&SampleApp_epDesc );
RegisterForKeys( task_id );
#ifdef ZDO_COORDINATOR
start_pwm();
#else
lightInit();
initSensorPort();
#endif
SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
}
UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
{
(void)task_id;
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 KEY_CHANGE:
SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
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
{
}
break;
default:
break;
}
osal_msg_deallocate( (uint8 *)MSGpkt );
}
return ( events ^ SYS_EVENT_MSG );
}
#ifdef ZDO_COORDINATOR
if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
{
if(onenet_login_ok==2)
{
P1_1 = 0;
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_ONENET_HEART_BEAT_EVT,15000 );
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_ONENET_SEND_DATA_EVT,TO_ONENET_TIME );
}
else
{
OneNet_DevLink();
onenet_login_ok=1;
HalLedBlink (HAL_LED_2, 5, 50, 500);
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,5000 );
}
return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
}
if ( events & SAMPLEAPP_ONENET_HEART_BEAT_EVT )
{
if(onenet_login_ok==2)
{
EDP_PacketPing();
}
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_ONENET_HEART_BEAT_EVT,15000 );
return (events ^ SAMPLEAPP_ONENET_HEART_BEAT_EVT);
}
if ( events & SAMPLEAPP_ONENET_SEND_DATA_EVT )
{
if(onenet_login_ok==2)
{
OneNet_SendTempData(end_temp);
OneNet_SendLedAndBuzzerData(LIGHT, flag1, P1_4,mflag1,Remote_dimming_control,Remote_self_dimming);
}
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_ONENET_SEND_DATA_EVT,TO_ONENET_TIME );
return (events ^ SAMPLEAPP_ONENET_SEND_DATA_EVT);
}
#else
if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
{
static uint16 msCount=0;
if(msCount++>TO_COOR_TIME)
{
SampleApp_Send_P2P_Message();
msCount=0;
}
motorCtrl();
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,1 );
return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
}
#endif
return ( 0 );
}
void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
{
switch ( pkt->clusterId )
{
case SAMPLEAPP_P2P_CLUSTERID:
#ifdef ZDO_COORDINATOR
{
ge = pkt->cmd.Data[0]-0x30;
shi = pkt->cmd.Data[1]-0x30;
bai = pkt->cmd.Data[2]-0x30;
qian = pkt->cmd.Data[3]-0x30;
wan = pkt->cmd.Data[4]-0x30;
mflag1 = pkt->cmd.Data[5]-0x30;
end_temp = wan*10000+qian*1000+bai*100+shi*10+ge;
uint16 light2=end_temp;
if(flag1==0&&flag2==1)
{
if(end_temp < LIGHT )
{
P1_4 = 0;
}
else
{
P1_4 = 1;
}
}
if(flag1==0&&Remote_self_dimming==1)
{
pwmConfig(100-light2);
}
else
pwmConfig(Remote_dimming_control);
}
break;
#else
case SAMPLEAPP_PERIODIC_CLUSTERID:
{
uint8 buff[3];
osal_memcpy(buff,pkt->cmd.Data,sizeof(buff));
motorFlag1=buff[0];
uint8 a=buff[1]-0x30;
uint8 b=buff[2]-0x30;
float c=a+(float)b/10;
motorFlag = c;
}
break;
#endif
default:
break;
}
}
#ifdef ZDO_COORDINATOR
void Ctrl(uint8* cmd)
{
if(strstr((char *)cmd, "D31"))
{
P1_4 = 0;
}
else if(strstr((char *)cmd, "D30"))
{
P1_4 = 1;
}
else if(strstr((char *)cmd, "flag21"))
{
flag2 = 1;
}
else if(strstr((char *)cmd, "flag20"))
{
flag2 = 0;
}
else if(strstr((char *)cmd, "Remote_self_dimming1"))
{
Remote_self_dimming = 1;
}
else if(strstr((char *)cmd, "Remote_self_dimming0"))
{
Remote_self_dimming = 0;
}
else if(cmd[0]== 'L')
{
if(osal_strlen(cmd)==6)
LIGHT = (cmd[1]-0X30)*10000+(cmd[2]-0X30)*1000+(cmd[3]-0X30)*100+(cmd[4]-0X30)*10+(cmd[5]-0X30)*1;
else if(osal_strlen(cmd)==5)
LIGHT = (cmd[1]-0X30)*1000+(cmd[2]-0X30)*100+(cmd[3]-0X30)*10+(cmd[4]-0X30)*1;
else if(osal_strlen(cmd)==4)
LIGHT = (cmd[1]-0X30)*100+(cmd[2]-0X30)*10+(cmd[3]-0X30)*1;
else if(osal_strlen(cmd)==3)
LIGHT = (cmd[1]-0X30)*10+(cmd[2]-0X30)*1;
else if(osal_strlen(cmd)==2)
LIGHT = (cmd[1]-0X30)*1;
}
else if(cmd[0]== 'A')
{
if(osal_strlen(cmd)==4)
Remote_dimming_control = (cmd[1]-0X30)*100+(cmd[2]-0X30)*10+(cmd[3]-0X30)*1;
else if(osal_strlen(cmd)==3)
Remote_dimming_control = (cmd[1]-0X30)*10+(cmd[2]-0X30)*1;
else if(osal_strlen(cmd)==2)
Remote_dimming_control = (cmd[1]-0X30)*1;
}
else if(cmd[0]== 'M')
{
if(cmd[1]== '0')
{
uint8 on=0;
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
1,
&on,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
else if(cmd[1]== 'F')
{
uint8 buff[3];
buff[0]=cmd[1];
buff[1]=cmd[2];
buff[2]=cmd[4];
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
4,
buff,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
else if(cmd[1]== 'P')
{
uint8 buff[3];
buff[0]=cmd[1];
buff[1]=cmd[2];
buff[2]=cmd[4];
AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
4,
buff,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
}
}
}
int parseOnenetCmd(uint8* cmd)
{
EDP_PACKET_STRUCTURE edpPacket = {NULL, 0, 0, 0};
char *cmdid_devid = NULL;
unsigned short cmdid_len = 0;
char *req = NULL;
uint32 req_len = 0;
unsigned char type = 0;
short result = -1;
char *dataPtr = NULL;
char numBuf[10];
int num = 0;
type = EDP_UnPacketRecv(cmd);
switch(type)
{
case CMDREQ:
result = EDP_UnPacketCmd(cmd, (int8 **)&cmdid_devid, &cmdid_len, (int8 **)&req, &req_len);
if(result == 0)
{
EDP_PacketCmdResp(cmdid_devid, cmdid_len, req, req_len, &edpPacket);
HalLcdWriteString(req, HAL_LCD_LINE_2);
Ctrl(req);
}
break;
default:break;
}
if(type == CMDREQ && result == 0)
{
EDP_FreeBuffer(cmdid_devid);
EDP_FreeBuffer(req);
ESP8266_SendData(edpPacket._data, edpPacket._len);
EDP_DeleteBuffer(&edpPacket);
}
return result;
}
void SampleApp_CallBack(uint8 port, uint8 event)
{
(void)port;
if(0==onenet_login_ok)
{
HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf, SAMPLE_APP_TX_MAX);
SampleApp_TxLen=0;
osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX);
}
else if(1==onenet_login_ok)
{
SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen, 1);
if(SampleApp_TxLen>=4)
{
if(SampleApp_TxBuf[SampleApp_TxLen-4]==0x20&&SampleApp_TxBuf[SampleApp_TxLen-3]==0x02&&SampleApp_TxBuf[SampleApp_TxLen-2]==0x00&&SampleApp_TxBuf[SampleApp_TxLen-1]==0x00)
{
onenet_login_ok=2;
SampleApp_TxLen=0;
}
}
if(SampleApp_TxLen>=SAMPLE_APP_TX_MAX)
{
SampleApp_TxLen=0;
}
}
else if(2==onenet_login_ok)
{
SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen, SAMPLE_APP_TX_MAX-SampleApp_TxLen);
if(SampleApp_TxLen>0 && CMDREQ==SampleApp_TxBuf[0])
{
if(0==parseOnenetCmd((uint8*)SampleApp_TxBuf))
{
SampleApp_TxLen=0;
osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX);
}
}
else
{
SampleApp_TxLen=0;
osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX);
}
if(SampleApp_TxLen>=SAMPLE_APP_TX_MAX)
{
SampleApp_TxLen=0;
osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX);
}
}
}
#else
void SampleApp_Send_P2P_Message( void )
{
byte str[6]={0};
byte str1[5]={0};
int len=0;
unsigned short light=0;
light=get_light();
wan = light/10000+0x30;
qian = (light/1000)%10+0x30;
bai = (light/100)%10+0x30;
shi = (light/10)%10+0x30;
ge = light %10+0x30;
str[4]=wan;
str[3]=qian;
str[2]=bai;
str[1]=shi;
str[0]=ge;
str[5]=mflag+0x30;
sprintf(str1, "%d", light);
len=osal_strlen(str1);
HalUARTWrite(0, "light: ",7);
HalUARTWrite(0, str1,len );
HalUARTWrite(0, "\r\n",2);
if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_P2P_CLUSTERID,
6,
str,
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
else
{
}
}
void SampleApp_CallBack(uint8 port, uint8 event)
{
(void)port;
HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf, 1);
}
#endif
void ESP8266_SendData(char* buff, int len)
{
if(len==0) return;
HalUARTWrite(0,buff, len);
}
|