Skip to content

Instantly share code, notes, and snippets.

@Kurt-E-Clothier
Last active October 18, 2017 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kurt-E-Clothier/60e05f204ba27c98a860 to your computer and use it in GitHub Desktop.
Save Kurt-E-Clothier/60e05f204ba27c98a860 to your computer and use it in GitHub Desktop.
Code chunks from project javamon
// Publish the value using PubNub
static void publishMsg (void)
{
// Check values for I2C line error...
if (TWI_msg[0] > 99 || TWI_msg[1] > 99) {
TWI_fullMsg = 10000;
}
// Ignore very small values
else if (TWI_msg[0] == 0 && TWI_msg[1] < 50) {
TWI_fullMsg = 0;
}
// Record full value and round to nearest 10
else {
int mod = TWI_msg[1] % 10;
TWI_msg[1] /= 10;
if (mod > 4)
++TWI_msg[1];
TWI_msg[1] *= 10;
TWI_fullMsg = (uint16_t)TWI_msg[1] + 100 * (uint16_t)TWI_msg[0];
}
// Only publish if this is a new value or it's been a while...
if((TWI_fullMsg != TWI_lastMsg) || TIME_TO_PUBLISH) {
char buf[40] = { 0, };
sprintf(buf, "{\"columns\":[[\"Coffee\",\"%d\"]]}", TWI_fullMsg);
pubnub_publish(channel, buf);
TWI_lastMsg = TWI_fullMsg;
stat_flag &= ~REQUEST_PUBLISH;
}
}
// Callback - Called when a connection to PubNub is made
static void IFA PN_connectedCB(void)
{
// Do Stuff once connected to PubNub
}
// Callback - Called when on PubNub connection error
static void IFA PN_connErrorCB(sint8 error)
{
// Do stuff on Connection error
// Maybe request a device reset!
}
// ...
// Called after connection to WiFi is established...
pubnub_connect(PN_connectedCB, PN_connErrorCB);
/**
* Creates a connection to Pubnub
* This should be called when a network connection is established!
* User can specify functions to be called on certain events.
*
* @param connCB callback for connection success event
* @param connErrCB callback for connection error events
*/
void IFA pubnub_connect(Pubnub_connectedCB connCB, Pubnub_connErrorCB connErrCB);
eon.chart({
pubnub:pubnub,
channel: channel,
connect: historyCall, // This function is called on initial connection
message: function(m){
// Do Stuff with Message "m"
}
generate: {
// Specify the chart characteristics
}
});
function historyCall(){
var unixTime = Date.now() / 1000; // current time in seconds
var endTime = (unixTime - (5 * 60)); // get the time five minutes ago
pubnub.history({
channel: 'javamon',
count: 1, // Only get 1 message from history
end: endTime * 10000000, // Only get history from the "end" timetoken and newer
reverse: false,
callback: function(m){
// If there is a message, publish it.
if (m[2] != 0) { // If there are no messages, this timetoken will be 0
pubnub.publish({
channel: 'javamon',
message: m[0][0]
});
}
// Otherwise, can't find the pot
else {
// Alert users to go check the pot!
}
}
});
};
var pubnub = PUBNUB.init({
publish_key: 'YOUR_PUBLISH_KEY',
subscribe_key: 'YOUR_SUBSCRIBE_KEY'
});
var channel = "javamon";
eon.chart({
pubnub:pubnub,
channel: channel,
message: function(m){
// Do cool stuff here!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment