📄 simplesensor.c
字号:
/******************************************************************************
Filename: SimpleSwitch.c
Revised: $Date: 2007-05-18 18:14:21 -0700 (Fri, 18 May 2007) $
Revision: $Revision: 14347 $
Description:
Sample application for a simple light switch utilizing the Simple API.
Copyright (c) 2006 by Texas Instruments, Inc.
All Rights Reserved. Permission to use, reproduce, copy, prepare
derivative works, modify, distribute, perform, display or sell this
software and/or its documentation for any purpose is prohibited
without the express written consent of Texas Instruments, Inc.
******************************************************************************/
/******************************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "OSAL.h"
#include "sapi.h"
#include "OSAL_Memory.h"
#include "OSAL_Nv.h"
#include "hal_key.h"
#include "hal_led.h"
#include "hal_adc.h"
#include "hal_mcu.h"
#include "SimpleApp.h"
#include "wxl_uart.h"
#include "ds18b20.h"
#include <ioCC2430.h>
/*********************************************************************
* CONSTANTS
*/
// Application States
#define APP_INIT 0 // Initial state
#define APP_START 1 // Sensor has joined network
#define APP_BOUND 2 // Sensor is bound to collector
// Application osal event identifiers
// Bit mask of events ( from 0x0000 to 0x00FF )
#define MY_START_EVT 0x0001
#define MY_REPORT_TEMP_EVT 0x0002
#define MY_REPORT_ADD_EVT 0x0004
#define MY_FIND_COLLECTOR_EVT 0x0008
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
static uint8 myAppState = APP_INIT;
static uint16 myStartRetryDelay = 10000; // milliseconds
static uint16 myAddressCheckPeriod = 1000; // milliseconds
static uint16 myBindRetryDelay = 2000; // milliseconds
//uint8 nsensors=1;
//uint8 SensorAdd[MAX_NUM_SENSORS][8];
//uint8 Temp[MAX_NUM_SENSORS][2];
/*********************************************************************
* GLOBAL VARIABLES
*/
uint32 myTempReportPeriod = 10000; // milliseconds
uint32 myTempOrignalPeriod = 10000; // milliseconds
// Inputs and Outputs for Switch device
#define NUM_OUT_CMD_SENSOR 1
#define NUM_IN_CMD_SENSOR 1
// List of output and input commands for Switch device
const cId_t zb_OutCmdList[NUM_OUT_CMD_SENSOR] =
{
ENDDEV_REPORT_CMD_ID
};
const cId_t zb_InCmdList[NUM_IN_CMD_SENSOR] =
{
ENDDEV_REQUEST_CMD_ID
};
#define TEMP_REPORT 0x01
#define ADD_REPORT 0x02
#define TIME_REQUEST 0x03
#define TEMP_REQUEST 0x04
#define ADD_REQUEST 0x05
#define EV P0_1
#define SetEVWriteDire {P0DIR=0x03;}
// Define SimpleDescriptor for sensor device
const SimpleDescriptionFormat_t zb_SimpleDesc =
{
MY_ENDPOINT_ID, // Endpoint
MY_PROFILE_ID, // Profile ID
DEV_ID_SENSOR, // Device ID
DEVICE_VERSION_SENSOR, // Device Version
0, // Reserved
NUM_IN_CMD_SENSOR, // Number of Input Commands
(cId_t *) zb_InCmdList, // Input Command List
NUM_OUT_CMD_SENSOR, // Number of Output Commands
(cId_t *) zb_OutCmdList // Output Command List
};
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void myApp_StartReporting( void );
static void myApp_StopReporting( void );
static uint8 myApp_ReadTemperature( void );
static uint8 myApp_ReadBattery( void );
static void ZDApp_Set_Reporttime( uint16 Reporttime );
//static void Init_SLEEP_TIMER(void);
//static void addToSleepTimer(UINT16 sec);
//#define ST_VECTOR VECT( 5, 0x2B ) /* Sleep Timer Compare */
//__interrupt void ST_ISR(void);//void ST_ISR(void);
/*
void Init_SLEEP_TIMER(void)
{
EA = 1; //开中断
STIE = 1;
STIF = 0;
}
*/
/*****************************************
//设置Sleep Timer唤醒时间
//sec :间隔时间,单位为秒
//无返回
*****************************************/
/*void addToSleepTimer(UINT16 sec)
{
uint32 sleepTimer = 0;
sleepTimer |= ST0;
sleepTimer |= (uint32)ST1 << 8;
sleepTimer |= (uint32)ST2 << 16;
sleepTimer += ((uint32)sec * (uint32)32768);
ST2 = (uint8)(sleepTimer >> 16);
ST1 = (uint8)(sleepTimer >> 8);
ST0 = (uint8) sleepTimer;
}*/
CONST uint8 strDevice[] = "Device:0x";
CONST uint8 strTemp[] = "Temp: ";
CONST uint8 strBattery[] = "Battery: ";
/*****************************************************************************
* @fn zb_HandleOsalEvent
*
* @brief The zb_HandleOsalEvent function is called by the operating
* system when a task event is set
*
* @param event - Bitmask containing the events that have been set
*
* @return none
*/
//static int count=0;
// static int bflag=0;
void zb_HandleOsalEvent( uint16 event )
{
///////////////////////////
uint8 buf[128];
uint8 i;
uint8 saddr[2];
uint8 *ieeeAddr;
int cmdlen;
//uint8 tempbatter[5];
///////////////////////////
if ( event & MY_START_EVT )
{
zb_StartRequest();
}
if(event & MY_REPORT_ADD_EVT)
{
// SetEVWriteDire;
// EV=0;
//发送节点和传感器地址
cmdlen=2+(owb_nCntDevice+1)*8+5;
osal_memset(buf,0,sizeof(buf));
buf[0]=PRESTR_DATA;
buf[1]=ADD_REPORT;
buf[2]=(owb_nCntDevice+1)*8+2;// 2个字节的短地址
buf[3]=owb_nCntDevice+1; //节点地址和传感器地址个数
ieeeAddr = NLME_GetExtAddr();
osal_cpyExtAddr( buf+4, ieeeAddr );
// osal_memcpy(buf+4,(void *)NLME_GetExtAddr(),8);
saddr[0]=(NLME_GetShortAddr()>>8)&0x00ff;
saddr[1]=(NLME_GetShortAddr())&0x00ff;
osal_memcpy(buf+12,saddr,2);
for(i=0;i<owb_nCntDevice;i++){
owb_CacheDeviceROM=owb_DeviceROMAdd[i];
osal_memcpy(buf+14+SENSOR_ADDRESS_LEN*i,(void *)&owb_CacheDeviceROM,SENSOR_ADDRESS_LEN);
}
buf[cmdlen-1]=ENDSTR_DATA;
// SendData(buf, 0x0000, cmdlen);
osal_stop_timer( MY_REPORT_ADD_EVT );
zb_SendDataRequest( 0xFFFE, ENDDEV_REPORT_CMD_ID, cmdlen, buf, 0, AF_ACK_REQUEST, 0 );
}
/* if(event & MY_REPORT_TIME_EVT)
{
if(myTempReportPeriod<=60000){//1 //minutes
myTempReportPeriod=myTempReportPeriod-1500; //1500ms 为采集数据所消耗的时间//最少时间为5s
if((myTempReportPeriod-1500)<0)myTempReportPeriod=10;
osal_start_timer( MY_REPORT_TEMP_EVT, myTempReportPeriod);
myTempReportPeriod= myTempOrignalPeriod;
}
else
{
myTempReportPeriod=myTempReportPeriod-60000;//1
osal_start_timer( MY_REPORT_TIME_EVT, 60000);
}
}
*/
if ( event & MY_REPORT_TEMP_EVT )
{
// Read and report temperature and battery value
TEMPE pt;
int j;
if(myTempReportPeriod==0)
{
SetEVWriteDire;
EV=0;
for(j=0;j<10;j++)
MicroWait (50000);
osal_memset(buf,0,sizeof(buf));
cmdlen=8+1+owb_nCntDevice*2+5;
buf[0]=PRESTR_DATA;
buf[1]=TEMP_REPORT;
buf[2]=owb_nCntDevice*2+1;// 2个字节的传感器数据和1个字节的温度数据
buf[3]=owb_nCntDevice+1; //传感器数据和温度数据
//osal_memcpy(buf+4,(void*)NLME_GetExtAddr(),8);
ieeeAddr = NLME_GetExtAddr();
osal_cpyExtAddr( buf+4, ieeeAddr );
/////////////电池电量滤波///////////////
// tempbatter[count]=myApp_ReadBattery();
buf[12]=myApp_ReadBattery();
/*count=(count+1)%5;
if(count==0) bflag=1;
if(bflag==0)
{
vbatter=0;
for(i=0;i<count;i++)
vbatter=vbatter+tempbatter[i];
buf[12]=(uint8)(vbatter/count);
}
else if(bflag==1)
{
vbatter=0;
for(i=0;i<5;i++)
vbatter=vbatter+tempbatter[i];
buf[12]=(uint8)(vbatter/5);
}*/
/////////////////////////////////////
owbMesureConvertBegin();
for(i=0;i<owb_nCntDevice;i++){
owb_CacheDeviceROM=owb_DeviceROMAdd[i];
owbReadMeasure(&pt,0x00);
buf[13+2*i]=pt.th;
buf[14+2*i]=pt.tl;
}
buf[cmdlen-1]=ENDSTR_DATA;
// SendData(buf, 0x0000,cmdlen);
zb_SendDataRequest( 0xFFFE, ENDDEV_REPORT_CMD_ID, cmdlen, buf, 0, AF_ACK_REQUEST, 0 );
osal_start_timer( MY_REPORT_TEMP_EVT, 10 );
myTempReportPeriod=myTempOrignalPeriod;
SetEVWriteDire;
EV=1;
return;
}
if(myTempReportPeriod<=60000){//1 //minutes
myTempReportPeriod=myTempReportPeriod-1300; //1300ms 为采集数据所消耗的时间//最少时间为5s
osal_start_timer( MY_REPORT_TEMP_EVT, myTempReportPeriod);
myTempReportPeriod=0;
}
else
{
myTempReportPeriod=myTempReportPeriod-60000;//1
osal_start_timer( MY_REPORT_TEMP_EVT, 60000);
}
}
if ( event & MY_FIND_COLLECTOR_EVT )
{
// Find and bind to a collector device
zb_BindDevice( TRUE, ENDDEV_REPORT_CMD_ID, (uint8 *)NULL );
//UartTX_Send_String("I am finding a collector to bind!\n");
}
/* if (( event & MY_REPORT_BATT_EVT )||( event & MY_REPORT_TEMP_EVT ))
{
//////////////////////////////////////////////
sensorReading = pData[1];
// If tool available, write to serial port
if ( pData[0] == BATTERY_REPORT )
{
tmpLen = (uint8)osal_strlen( (char*)strBattery );
pBuf = osal_memcpy( buf, strBattery, tmpLen );
*pBuf++ = (sensorReading / 10 ) + '0'; // convent msb to ascii
*pBuf++ = '.'; // decimal point ( battery reading is in units of 0.1 V
*pBuf++ = (sensorReading % 10 ) + '0'; // convert lsb to ascii
*pBuf++ = ' ';
*pBuf++ = 'V';
}
else
{
tmpLen = (uint8)osal_strlen( (char*)strTemp );
pBuf = osal_memcpy( buf, strTemp, tmpLen );
*pBuf++ = (sensorReading / 10 ) + '0'; // convent msb to ascii
*pBuf++ = (sensorReading % 10 ) + '0'; // convert lsb to ascii
*pBuf++ = ' ';
*pBuf++ = 'C';
}
*pBuf++ = '\r';
*pBuf++ = '\n';
*pBuf = '\0';
// can also write directly to uart
UartTX_Send_String(buf);*/
///////////////////////////////////////////////
}
/*********************************************************************
* @fn zb_HandleKeys
*
* @brief Handles all key events for this device.
*
* @param shift - true if in shift/alt.
* @param keys - bit field for key events. Valid entries:
* EVAL_SW4
* EVAL_SW3
* EVAL_SW2
* EVAL_SW1
*
* @return none
*/
void zb_HandleKeys( uint8 shift, uint8 keys )
{
}
/******************************************************************************
* @fn zb_StartConfirm
*
* @brief The zb_StartConfirm callback is called by the ZigBee stack
* after a start request operation completes
*
* @param status - The status of the start operation. Status of
* ZB_SUCCESS indicates the start operation completed
* successfully. Else the status is an error code.
*
* @return none
*/
void zb_StartConfirm( uint8 status )
{
if ( status == ZB_SUCCESS )
{
myAppState = APP_START;
// Set event to bind to a collector
osal_start_timer( MY_FIND_COLLECTOR_EVT, myBindRetryDelay );
}
else
{
// Try joining again later with a delay
osal_start_timer( MY_START_EVT, myStartRetryDelay );
}
}
/******************************************************************************
* @fn zb_SendDataConfirm
*
* @brief The zb_SendDataConfirm callback function is called by the
* ZigBee after a send data operation completes
*
* @param handle - The handle identifying the data transmission.
* status - The status of the operation.
*
* @return none
*/
void zb_SendDataConfirm( uint8 handle, uint8 status )
{
if ( status != ZSuccess )
{
// Remove bindings to the existing collector
zb_BindDevice( FALSE, ENDDEV_REPORT_CMD_ID, (uint8 *)NULL );
myAppState = APP_START;
myApp_StopReporting();
// Start process of finding new collector with minimal delay
osal_start_timer( MY_FIND_COLLECTOR_EVT, 1 );
// UartTX_Send_String("send data failed!\n");
}
}
/******************************************************************************
* @fn zb_BindConfirm
*
* @brief The zb_BindConfirm callback is called by the ZigBee stack
* after a bind operation completes.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -