Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Created April 30, 2015 05:14
Show Gist options
  • Save harrisonhjones/1bfc327f90e18ea17e62 to your computer and use it in GitHub Desktop.
Save harrisonhjones/1bfc327f90e18ea17e62 to your computer and use it in GitHub Desktop.
QueuedPublishEventsSpark
char eventMessages[10][65]; // 10 messages of a max 65 char length
char eventMessage[65]; // A buffer for outgoing publish events
int queueIndex = -1; // -1 = nothing to send, 0+ = a message is queued
// In your main loop();
// Your code here
// Step 1 - Build your publish string
sprintf(eventMessage, "The temp outside is %d C", tempValue);
// Step 2 - Attempt to send it
if(Spark.publish('eventName', eventMessage) != true)
{
// Step 3 - Queue it
if((queueIndex+1) >= 10)
{
// We've run our of queue spots. What to do now?
}
else
{
queueIndex++;
strncpy(eventMessages[queueIndex], eventMessage, 65);
eventMessages[queueIndex][64] = '\0'; // Prevent possible overflow condition
}
}
delay(1000);
// Step 4 - Send out the queue
while(queueIndex >= 0)
{
if(Spark.publish('queuedEventName', eventMessage[queueIndex]) == true)
{
queueIndex--; // Decrement the queue counter
}
delay(1000); // Wait a second so we don't exceed the publish limit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment