This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Separate event for reading Sensor value

Hello,

               I have created two endpoints and trying to read the sensor with same gpio pin. Since this sensor is giving both humidity and temperature. If i read the sensor with each humidity and temperature(separate Endpoints) events. I am unable to get the data, Since while reading one, other getting distracted my making pin as output. How can avoid this?. I have created separate event for reading Sensor. But it doesn't work.

  • Hello,

                   I have created two endpoints and trying to read the sensor with same gpio pin. Since this sensor is giving both humidity and temperature. If i read the sensor with each humidity and temperature(separate Endpoints) events. I am unable to get the data, Since while reading one, other getting distracted my making pin as output. How can avoid this?. I have created separate event for reading Sensor. But it doesn't work.

    in the task Arr[]                                                                                                                                                                                                 :

    zcl_event_loop,

    zcl_ReadSensor_loop,

    zcl_Sensor1_loop,

    zcl_Sensor2_loop.

    -------------------------------------------------------

    zcl_ReadSensor_loop(byte task_id, UINT16 events)

    {

    if(events & TIME_OUT_EVENT) //TIME_OUT = 0x0001;

    {

             read_the_sensor( );

    osal_start_timerEx( task_id, TIME_OUT_EVENT, 2000 );

    }

    I need to read the Data periodically with 2 sec.

    This was i have done. It doesn't work. Is i need to any thing in the App_Init function? Can some body suggest me.

  • I would suggest you to read both temperature and humidity in a read event which comes every two second. Then, create another two event, one is dedicated for sending the read temperature value and another is for sending humidity.

  • That only i have done. I read both values in zcl_ReadSensor_loop, i have mentioned in previous post. if do this, It's going to next event.It's in that event only.

        One more problem i noticed, If i declare Global variable,my data segment get corrupted. I am not getting expected result, some times it's hangs also.

  • 1)That only i have done. I read both values in zcl_ReadSensor_loop, i have mentioned in previous post. if do this, It's not going to next event.It's in that event only.

        2)One more problem i noticed, If i declare Global variable,my data segment get corrupted. I am not getting expected result, some times it's hangs also. I have seen, in IAR general options/Stack/heap, is i need to change any thing in that, to get more memory for global variable

  • Refer to your post, you don't clear event when you leave the event processing. You should add the red line in the following example to your code.

    if(events & TIME_OUT_EVENT) //TIME_OUT = 0x0001;
    {
        read_the_sensor( );
        osal_start_timerEx( task_id, TIME_OUT_EVENT, 2000 );
        return (events ^ TIME_OUT_EVENT);
    }

  • I have Done that in Code, Sorry for not posting it. 

             Can you tell me, if i declare variable as global, why my data segment corrupted and respective Device get hangs?. If that same variable i declared as local, then my app is working fine. Is there any option to increase Data segment memory in IAR?

  • Would you show me how you declare the global variable?

  • I have changes the Code as below

    osalInitTasks(void)
    {
    ---------
    ---------
    zcl_Init(taskID++);
    zcl_SensorInit(taskID++);
    zcl_SampleLight(taskID++);
    zcl_SampleOcc(taskID++);
    }

    const pTaskEventHandlerFn tasksArr[] = {
    ----------------
    -----------------
    zcl_event_loop,
    zclSensor_event_loop,
    zcl_Samplelight_event_loop,
    zcl_SampleOcc_event_loop
    };
    --------------------------------------------------------
    Sensor.c

    /*global var*/
    byte zclSensor TaskID;
    byte buffer[50] = {0};

    zcl_SensorInit(uint8 taskID)
    {

    HalLcdWriteString ("entering here", 1)
    zclSensor TaskID = taskID;
    }

    zclSensor_event_loop(uint8 task_id, uint16 events)
    {
    (void)task_id;
    if(events & SENSOR_STATUS_EVENT) //defined event as 0x0001
    {
    Sensor_reading(buffer, 40);
    osal_start_timerEx(zclSensor TaskID, SENSOR_STATUS_EVENT, SENSOR_STATUS_TIME); //time 3000
    }
    }

    but it's entering in any loop(zcl_SensorInit, zclSensor_event_loop), is i need to register any test endpoints? any suggestion.

  • Can any body suggest me, is my code is ok? or i need to do syncronize(mutex) to read same gpio?


  • zclSensor_event_loop(uint8 task_id, uint16 events)
    {
    (void)task_id;
    if(events & SENSOR_STATUS_EVENT) //defined event as 0x0001
    {
    Sensor_reading(buffer, 40);
    osal_start_timerEx(zclSensor TaskID, SENSOR_STATUS_EVENT, SENSOR_STATUS_TIME); //time 3000

    return(events & SENSOR_STATUS_EVENT);
    }

    }

    please can some help me. I stuck in this.

  • Your source code is incomplete and erratic. Could you attach your whole C file?

  • files are 

    /**************************************************************************************************
    	Filename:       OSAL_Common.c
      
    	Description:    This file contains all the settings and other functions
                      that the user should set and change.
    				  
    	Copyright 2013, Unizen Technologies Pvt Ltd.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "zcl_HumiSensor.h"
    #include "zcl_TempSensor.h"
    #include "zcl_Common_data.h"
    #include "Sensor.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      macEventLoop,
      nwk_event_loop,
      Hal_ProcessEvent,
    #if defined( MT_TASK )
      MT_ProcessEvent,
    #endif
      APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_ProcessEvent,
    #endif
      ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_event_loop,
    #endif
      zcl_event_loop,
    zclSensor_event_loop,
        zclHumiSensor_event_loop,
      zclTempSensor_event_loop
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[INIT_VAL] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
    	uint8 taskID = INIT_VAL;
    
    	tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
    	osal_memset( tasksEvents, INIT_VAL, (sizeof( uint16 ) * tasksCnt));
    	macTaskInit( taskID++ );
    	nwk_init( taskID++ );
    	Hal_Init( taskID++ );
    	#if defined( MT_TASK )
    	MT_TaskInit( taskID++ );
    	#endif
    	APS_Init( taskID++ );
    	#if defined ( ZIGBEE_FRAGMENTATION )
    	APSF_Init( taskID++ );
    	#endif
    	ZDApp_Init( taskID++ );
    	#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
    	ZDNwkMgr_Init( taskID++ );
    	#endif
    	zcl_Init( taskID++ );
    	zcl_SensorInit( taskID++ );
    	zclHumiSensor_Init(taskID++);
    	zclTempSensor_Init(taskID);
    }
    
    /*********************************************************************
    *********************************************************************/
    
    /**************************************************************************************************
    	Filename:       Sensor.c
    
    	Description:    Functions to read Temperature and Humidity Sensor
    					Data
    					
    	Copyright 2013, Unizen Technologies Pvt Ltd.
    **************************************************************************************************/
    
    /************************************************************************************
     * INCLUDES
     */
    #include <stdio.h>
    #include <string.h>
    #include "ZComDef.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "onboard.h" 
    #include "Sensor.h"
    #include "zcl_Common_data.h"
    #include "hal_lcd.h"
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * CONSTANTS
     */
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSensor_TaskID;
    byte buff[MAX_SEN_BIT] = {INIT_VAL};
    
    /*********************************************************************
     * @fn	zcl_SensorInit
     *
     * @brief	Initialization function for the zcl layer.
     *
     * @param task_id 	[IN]	 ZCL task id
     *
     * @return      none
     */
    void zcl_SensorInit( uint8 task_id )
    {
        zclSensor_TaskID = task_id;
    }
    
    /*********************************************************************
     * @fn	zclHumiSensor_event_loop
     *
     * @brief	Event Loop Processor for Reading Sensor Data.
     *
     * @param task_id [IN]	Task id for Processing  Event
     * @param event   [IN]	Events to do for Reading Sensor value
     
     * @return		SUCCESS
     */
    uint16 zclSensor_event_loop( uint8 task_id, uint16 events )
    {
    	(void)task_id; 
    	
    	if ( events & SENSOR_STATUS_EVT )
    	{
    		Sensor_reading(buff, MAX_SEN_BIT);
    		osal_start_timerEx( zclSensor_TaskID,
    								SENSOR_STATUS_EVT,
    								SENSOR_STATUS_TIME );
    		return (events ^ SENSOR_STATUS_EVT);
    	}
        return SUCCESS;
    }								
    
    /** **************************************************************************
     *  @brief Process for reading Sensor
     *
     *  @param buff		[IN] 	buffer to store Sensor 40 Bits 
     * 	@param size		[IN]	Size of the Buffer   
     *
     *  @return		 None
     *      
     */
    void Sensor_reading(uint8 buff[], uint8 size)
    {
        uint8 portData;
    	
    	P0SEL &= ~(BIT_ONE << MAX_BIT);//make gpio
    	P0DIR |= (BIT_ONE << MAX_BIT);//make output port
    	SENSOR_PORT_PIN = LOW; 
    	MicroWait(OUTPUT_LOW_DELAY);//18000
    	SENSOR_PORT_PIN = HIGH;
    	MicroWait(OUTPUT_HIGH_DELAY);//40
    	P0DIR &= ~(BIT_ONE << MAX_BIT); //make input port
    	MicroWait(INIT_INPUT_DELAY);
    	//Check the response
    	portData = SENSOR_PORT_PIN;
    	if(portData == HIGH)	//If it line is High it is an Error
    	{
    		HalLcdWriteString("Condition nt met", HAL_LCD_LINE_1);
    		HalLcdWriteString("Error", HAL_LCD_LINE_2);
    	}
    	MicroWait(SENSOR_RESPONSE_LOW_DELAY);//80
    	portData = SENSOR_PORT_PIN;
    	if(portData != HIGH) //If it is Low, it is an Error
    	{
    		HalLcdWriteString("Condition nt met", HAL_LCD_LINE_1);
    		HalLcdWriteString("Error 2nd", HAL_LCD_LINE_2);
    	}
    	MicroWait(SENSOR_RESPONSE_HIGH_DELAY);//80
        read_data(buff, size);
    }
    
    /******************************************************************************
     * @fn	readSensor Data
     *
     * @brief	Read a bit by bit of Sensor
     *
     * @param buff	[IN] 	buffer to store Sensor 40 Bits 
     * @param size	[IN]	Size of the Buffer 
     * @return      none
     */
    void read_data(uint8 buff[], uint8 size)
    { 
    	uint8 i = INIT_VAL;
    	uint8 port_data;
    	uint32 count = INIT_VAL;
    	HalLcdWriteString("entering here",3);/*TBR*/
    	memset(buff, INIT_VAL, sizeof(buff));
    	for(i=LOW; i<size; i++)
    	{  
    		port_data = SENSOR_PORT_PIN; 
    		count = INIT_VAL;
    		while(port_data != HIGH && count++ < MAX_COUNT)//Waiting for HIGH
    			port_data = SENSOR_PORT_PIN;
    		// Port Line is High Now.
    		MicroWait(TRANS_DELAY);
    		port_data = SENSOR_PORT_PIN;
    		if(port_data == HIGH)
    		{
    			buff[i] = HIGH;
    			while(port_data == HIGH)
    				port_data = SENSOR_PORT_PIN;
    		} 
    	}      
    }

  • I think the problem is your zcl_SensorInit. You only set zclSensor_TaskID = task_id;  You need to fill all things like in SampleLight 

    void zclSampleLight_Init( byte task_id )
    {
    zclSampleLight_TaskID = task_id;

    // Set destination address to indirect
    //zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
    //zclSampleLight_DstAddr.endPoint = 0;
    //zclSampleLight_DstAddr.addr.shortAddr = 0;

    // This app is part of the Home Automation Profile
    zclHA_Init( &zclSampleLight_SimpleDesc );

    // Register the ZCL General Cluster Library callback functions
    zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );

    // Register the application's attribute list
    zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, SAMPLELIGHT_MAX_ATTRIBUTES, zclSampleLight_Attrs );

    // Register the Application to receive the unprocessed Foundation command/response messages
    zcl_registerForMsg( zclSampleLight_TaskID );

    // Register for all key events - This app will handle all key events
    RegisterForKeys( zclSampleLight_TaskID );

    // Register for a test endpoint
    afRegister( &sampleLight_TestEp );
    }