Circular Buffer publishing to MQTT

Hi all,
I am making a vibration monitor using a photon and an accelerometer and I’ve been having some trouble. I managed to get the photon to read the sensor and publish it to a MQTT broker, but I’d like to use a circular buffer as the data sampling rate is around 1khz. I have been searching the forums and googling what I want to do but I have not been able to find clear examples, just references to use a circular buffer. I’m very new to this (as in I had not heard of the photon till about 3 weeks ago) and I have no prior experience with microcontrollers so forgive me if this is an obvious question but does anyone have an example of a circular buffer that can be used with the photon? I have been trying to use the flashee library but everytime I try to write anything i get a hardware fault. I’m quite lost, but if I had an example I might be able to adapt it.

You could look at this library that uses a circular buffer approach for queuing events for asynchronously executing Particle.publish()

But circular buffers are nothing unique to the Particle ecosystem nor confined to microcontrolers. They do exist on any platform.
In a nutshell it’s nothing more than a single array with two variables that hold the head and tail index.
You add new items to at the head location and increment that index.
Once you reach the upper boundary of the array you wrap round to zero and continue till you approach the tail index. There you need to decide whether to overwrite old data or stop adding.
When pulling data out the buffer you do that at the tail index and increment that, till you reach the upper bound, wrap round till you reach the head index, which means you have read all data.
That’s it.

BTW, I found multiple threads with reference to that topic in this forum
e.g. this Circular Buffer - FIFO


Ah, I believe that has sent me down the right track. Thank you for your help.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.