Hello Hiren,
first my application from back then: I used the XDK as MQTT client and a computer as MQTT broker. The broker was implemented with Visual Studio.
Then the buffer algorithm. Mostly I used the buffer algorithm from the example program "DemoDataLogger" with some modifications (see pseudocode below).
The MQTT stuff i looked in the example project "SendDataOverMqtt". In my application i used the API Serval_Mqtt.h directly because then you have more flexibility.
In the pseudocode below you can see the rough structure. In reality for sure you have to do some additional stuff. Especially FreeRTOS operations. You have to run two different tasks. One of the is collecting your sensor data, the other one publishes to collected data over MQTT. Both tasks need to be synchronised via Semaphores.
I hope this was helpfull. If you have any further questens don't hesitate to ask.
Best regards,
Christian
typedef struct {
char data[512];
uint16_t length;
}
// Declare Buffers
Buffer pingBuffer;
Buffer pongBuffer;
Buffer *activeBuffer;
Buffer *backBuffer;
void InitBuffer(void){
activeBuffer = &pingBuffer;
backBuffer = &pongBuffer;
}
void ResetBuffer(void){
activeBuffer->length = 0;
activeBuffer->data[0] = 0;
backBuffer->length = 0;
backBuffer->data[0] = 0;
}
void SwitchBuffer(void){
backBuffer->length = 0;
backBuffer->data[0] = 0;
if(activeBuffer == &pingBuffer){
activeBuffer = &pongBuffer;
backBuffer = &pingBuffer;
} else {
activeBuffer = &pingBuffer;
backBuffer = &pongBuffer;
}
activeBuffer->length = 0;
activeBuffer->data[0] = 0;
}
}
void SomeCallbackWithYourData(uint16_t data){
activeBuffer->length += sprintf(activeBuffer->data + activeBuffer->length, "%u", (unsigned short int) data);
if(activeBuffer->length >= 490){
SwitchBuffer();
PublishMqtt();
}
}
void PublishMqtt(void){
MQttPublish("yourtopic", "yourqos", backbuffer->data);
}
void main(void){
InitBuffer();
ResetBuffer();
}