MQTT Publish from Button(ISR)
Answer
3/12/19 7:45 AM

In the main task I periodically publish messages via MQTT. that works well.

Now I would like to do the following:

button is pressed -> publish a message

I don't think the Mqtt libraries are safe to use from within an ISR though. That doesn't seem to work.

Does anybody have a solution for this?

Perhaps:

in the Button callback put a small 'event' together (number, event, tick count), put these on a queue, done.

Have a separate task that takes events from that queue and publishes it over MQTT.

But I'd like the button events to 'overtake' the scheduled messages. 

Ricardo.

 

 

0 (0 Votes)
RE: MQTT Publish from Button(ISR)
Answer
3/13/19 9:17 AM as a reply to Ricardo Gomez-Ulmke.

hi,

I just read about RTOS Message Buffers. This seems to be what I'm looking for.

Can somebody with experience comment on this?

Are they actually supported in the XDK libraries?

Many thanks for the help,

Ricardo. 

0 (0 Votes)
RE: MQTT Publish from Button(ISR)
Answer
3/13/19 9:45 AM as a reply to Ricardo Gomez-Ulmke.

Hi @Ricardo Gomez-Ulmke,

1. Register button callback

	retcode = BSP_Button_Enable((uint32_t) BSP_XDK_BUTTON_1, ButtonOne_OnEvent);

2. Enqueue the button function to publish the mqtt message. myTask is the main system task

	void ButtonOne_OnEvent(uint32_t data)
	{
		if(data == BSP_XDK_BUTTON_PRESS) {
			Retcode_T retcode = CmdProcessor_enqueueFromIsr(&myTask, HandleButtonPressed, NULL, data);
			if(retcode != RETCODE_OK)
			{
				Retcode_RaiseErrorFromIsr(retcode);
			}
		}
}

3. Your enqueued button function can now publish the mqtt

	Retcode_T HandleButtonPressed(void)
	{
		Retcode_T retcode= RETCODE_OK;
		char* value = "sensor data";
		static StringDescr_T publishTopicDescription;
		static char *topic = "BCDS/XDK110/example/out";
		StringDescr_wrap(&publishTopicDescription, topic);
		
		retcode = Mqtt_publish(&mqttSession, publishTopicDescription, *value, strlen(*value), (uint8_t) MQTT_QOS_AT_MOST_ONE, false)
		return retcode;
	}

Best regards,

Padmapriya.

0 (0 Votes)
RE: MQTT Publish from Button(ISR)
Answer
3/15/19 12:23 PM as a reply to Padmapriya G.

Thanks Padmapriva.

I have implemented your suggestion and it works fine until I start pressing the button really quickly.

after about 10 times, I get the following error:

asserted at Filename 3rd-party/FreeRTOS/portable/GCC/ARM_CM3/port.c , line no  373

it is thrown in the following function in port.c:

void vPortEnterCritical( void )
{
	portDISABLE_INTERRUPTS();
	uxCriticalNesting++;

	/* This is not the interrupt safe version of the enter critical function so
	assert() if it is being called from an interrupt context.  Only API
	functions that end in "FromISR" can be used in an interrupt.  Only assert if
	the critical nesting count is 1 to protect against recursive calls if the
	assert function also uses a critical section. */
	if( uxCriticalNesting == 1 )
	{
		configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
	}
}

I can't figure out where this is called from. Any ideas?

Ricardo.

0 (0 Votes)
RE: MQTT Publish from Button(ISR)
Answer
3/15/19 2:40 PM as a reply to Ricardo Gomez-Ulmke.

actually, my mistake. It works as per your suggestion.

Only issue now is that when pressing the button really fast, the 'enqueue' call fails after a while. I suppose, there is a queue limit and if that is reached, it just doesn't enqueue any more handlers.

not sure if / how to increase the queue size for a command processor.

 

 

0 (0 Votes)