Regarding waking up of nrf52832 from sleep mode

I am a beginner I want to advertise for some time then go to sleep mode and wake up do some tasks like advertising and go to sleep mode again. Do all this process recursively please suggest to me some way of doing this.

Thanks in advance

Parents
  • Hi,

    Which SDK are you using (nRF5 SDK or nRFConnect SDK)? Regardless, most most application samples go to system on sleep mode whenever possible, and wake up on any events for tasks that should be done, and go back to sleep as soon as possible again. The exact methods differ between the SDK's, though.

    If what you mean is send an advertising packet, then sleep, send an advertising packet again, this is the default behavior or virtually all Bluetooth peripheral examples. If you mean advertise (including the sleep) for a good duration of time, then stop advertising and wait a good period of time before you start advertising again you would need to time this (using for instance app_timer in nRF5 SDK or for instance a timer expiery function or similar in the nRF Connect SDK.

    PS: You will normally get a reply from Nordic staff within the next Norwegian business day, but should not expect faster replies then that. 

  • I am using nrfsdk 17.1.0. I have also done by using a timer actually, first of all, I had created a timer for getting value from the sensor in that timeout handler I had enabled i2c to get data from the sensor and stored it in the variable then disable it Then use another timer to start advertising but my devkit (nrf52832) is not advertising properly Suggest me a way to do this. I want the data to be taken from the sensor to advertise for some time then go to sleep wake up advertise data then go to sleep repeat this process. The below-mentioned code is for starting advertising in timeout handler

    static void repeated_timer_handler(void * p_context)
    {
                
    
    	ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    	
    	
    }

  • Basically what you want do do is this:

    1. Read a sensor at a regular interval
    2. Update the advertising data when the sensor is read
    3. Advertise at a regular interval
    4. Sleep in between

    Is that correct? This is a common use case.

    For 4, the default behavior of most SDK examples is to sleep (sleep mode is entered when a interrupt/event has been processed by doing so in the main loop.

    Regarding 3, advertising is by default periodic, and the device sleeps in between (unless it does something else). You just need to configure the advertising interval to whatever fits your application. That means that you should not call ble_advertising_start() from an application timer. Just call this once.

    For 2, updating advertising data I suggest you refer to How to update advertising data dynamically using BLE Advertising library.

    For 1, you need to handle the specific sensor yourself. There are TWI drivers and libraries in the SDK you can use, though. As you care about current consumption it makes sense to enable TWI, then read the data from the sensor, and disable TWI to save current.

    This means that after you have configured everything and started to advertise (perhaps after getting the initial sensor reading), you just need a single repeated application timer where you:

    1. Enable TWI/sensor driver
    2. Read sensor data
    3. Disable TWI/sensor driver
    4. Update advertising data
  • I had used two app timers one for getting data from the sensor another for start advertising but my timer corresponding to the sensor is working perfect but the timer for start advertising is not advertising properly it works once or two times then got stuck what could be a possible mistake can you suggest me something.

    I am using ble_app_template code as a base in that it advertises once then I had to press the reset button for advertising but my requirement is such that it should advertise itself in a periodic fashion for that I had used the timer so that it will call and start advertising but if I use only timer corresponding to advertise then it works perfect but with timer corresponding to sensor data it stops after advertising once or twice although times for both timers are different. 

    Thanks for replying

Reply
  • I had used two app timers one for getting data from the sensor another for start advertising but my timer corresponding to the sensor is working perfect but the timer for start advertising is not advertising properly it works once or two times then got stuck what could be a possible mistake can you suggest me something.

    I am using ble_app_template code as a base in that it advertises once then I had to press the reset button for advertising but my requirement is such that it should advertise itself in a periodic fashion for that I had used the timer so that it will call and start advertising but if I use only timer corresponding to advertise then it works perfect but with timer corresponding to sensor data it stops after advertising once or twice although times for both timers are different. 

    Thanks for replying

Children
  • Hi,

    Piyush_9536 said:
    I had used two app timers one for getting data from the sensor another for start advertising but my timer corresponding to the sensor is working perfec

    Good. Then you have most of what you need working.

    Piyush_9536 said:

    but the timer for start advertising is not advertising properly it works once or two times then got stuck what could be a possible mistake can you suggest me something.

    I am using ble_app_template code as a base in that it advertises once then I had to press the reset button for advertising but my requirement is such that it should advertise itself in a periodic fashion for that I had used the timer so that it will call and start advertising

    I am not sure exactly what you want to do. Do you want "normal" advertising, where an advertising packet is sent every x unit of time? Or do you want to wait a relatively long amount of time, then advertise many packets, and then wait a long time a gain? Please explain.

    Piyush_9536 said:
    but if I use only timer corresponding to advertise then it works perfect but with timer corresponding to sensor data it stops after advertising once or twice although times for both timers are different. 

    I cannot say what happens without knowing more about your application. If you explain what you want to do, show your code and explain how it fails I will be in a better position to suggest what you should change. (Also, I don't understand why you have separate timers for advertising and reading sensor data, but perhaps that makes sense - if so, please explain).

  • Yes, I want it to advertise for some duration then wait again most probably for a longer time period then again start advertising and repeat this whole process again and again.

    During the wait, I want to take some reading from sensor store them in an array or variable, and advertise these reading when advertising starts

    But when I had tried to implement it using two timers then if I use one timer at a time then they both work perfectly but when I try to use both the timer simultaneously then timer corresponding to advertising works only once or twice depending upon the ticks time taken after that my code gets stuck there 

    #define ADV_DATA_UPDATE_INTERVAL        APP_TIMER_TICKS(120000)                        //5000L    
    #define ADV_DATA_UPDATE        APP_TIMER_TICKS(80000)            
    
    //timer handler for advertising
    
    static void repeated_timer_handler(void * p_context)
    {
                
    //   bool erase_bonds;
    	  advertising_init();
    //   advertising_start(erase_bonds);                              
    	NRF_LOG_INFO("Advertising start");
    	
        ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    
        APP_ERROR_CHECK(err_code);
    	
    		
    }
    
    // timer handler for taking data from sensor
    
    static void sensor_timer_handler(void *p_context)
    {
    
    	UNUSED_PARAMETER(p_context);
      nrf_delay_ms(50);	
    	sensirion_i2c_init();
    			while (sht4x_probe() != STATUS_OK) 
    				{
    						NRF_LOG_INFO("SHT sensor probing failed\n");
    						nrf_delay_ms(200);
    				}
            NRF_LOG_INFO("SHT sensor probing successful\n");
            int32_t temperature, humidity;
            /* Measure temperature and relative humidity and store into variables
             * temperature, humidity (each output multiplied by 1000).
             */
            int8_t ret = sht4x_measure_blocking_read(&temperature, &humidity);
            if (ret == STATUS_OK)
    				{
    						 NRF_LOG_INFO("measured temperature: %3.2f degreeCelsius,"
    													"measured humidity: %3.2f percentRH\n",temperature / 1000.0f, humidity / 1000.0f);
    													 temp =temperature / 1000.0f;
    													 hum =humidity / 1000.0f;
    			
    	//					sprintf((char *)data_to_send, "%3.2f,%3.2f,%s",temp,hum,nrf_cal_get_time_string(true));
    	//					NRF_LOG_INFO("data_to_send %s",data_to_send);
    						NRF_LOG_INFO("temp=%f,hum=%f",temp,hum);
    				} 
    			else 
    			 {
    						NRF_LOG_INFO("error reading measurement\n");
    			 }
    			 	nrf_delay_ms(100);
    			 		// disable and uninitialize twi
    		nrf_drv_twi_disable(&i2c1_instance);
    		nrf_drv_twi_uninit(&i2c1_instance);
    	
      	sprintf((char *)data,"%f %f",temp,hum);
    			 	
    	
    }
    

  • Hi,

    OK, now I understand better what you want to do.

    Piyush_9536 said:
    But when I had tried to implement it using two timers then if I use one timer at a time then they both work perfectly but when I try to use both the timer simultaneously then timer corresponding to advertising works only once or twice depending upon the ticks time taken after that my code gets stuck there 

    Where exactly "is it stuck"? What did you find by debugging? Please refer to the code. (Example: "in this file, I see from a debugger that execution is "stuck" in this while loop waiting for a flag that is never set").

  • Actually, I had printed some comments on the serial port to get assurance that whether my code is entering in the timeout handler properly or not. Therefore from there I get to know as ticks for getting reading from the sensor is less its timeout handler will be called first it is working properly after that timeout handler of advertising will be called that is also working fine then again sensor reading gets updated after that when advertising timeout handler called second time it got stuck there as not advertising is occurring properly moreover comment does not gets printed on my com(serial) port which shows that it does not get entered in its timeout handler or it got stuck somewhere in its middle.

  • I see. I am not able to relate this to what could actually be going on though. If you want more input, please share your code and explain what you found when debugging with reference to your code (at this point, I saw with a debugger that this function returned that error, etc..)

Related