Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/publishMsg.c
Created November 15, 2017 21:06
Show Gist options
  • Save stephenlb/028f74e2f6c205c82675c23d8a747080 to your computer and use it in GitHub Desktop.
Save stephenlb/028f74e2f6c205c82675c23d8a747080 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,
channels: ['eon-chart'],
limit: 20,
generate: {
bindto: '#body-spline'
}
});
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, // how many items to fetch
reverse: false, // Setting to true will traverse the time line in reverse starting with the oldest message first.
stringifiedTimeToken: true, // false is the default
start: '123123123123', // start time token to fetch
end: '123123123133' // end timetoken to fetch
},
function (status, response) {
if (response[2] != 0) { // If there are no messages, this timetoken will be 0
var publishConfig = {
channel : "javamon",
message : response[0][0]}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
}
else {
}
}
});
};
var pubnub = new PubNub({
publishKey: 'demo',
subscribeKey: 'demo'
});
var channel = "javamon";
eon.chart({
pubnub: pubnub,
channels: [channel]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment