Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/pubnub-js-publish
Created November 15, 2017 21:05
Show Gist options
  • Save stephenlb/ff21edc453c75bcb7f1b72dd65b33dd9 to your computer and use it in GitHub Desktop.
Save stephenlb/ff21edc453c75bcb7f1b72dd65b33dd9 to your computer and use it in GitHub Desktop.
pubnub_iot_tech_uc_1
$('#toggle').click(function(e){
pubmsg = { "req" : "toggle" };
var publishConfig = {
channel : 'gpio-raspberry-control',
message : pubmsg
}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
});
pubnub.addListener({
status: function(statusEvent) {
if (statusEvent.category === "PNConnectedCategory") {
publishSampleMessage();
}
},
message: function(message) {
if('resp' in message) {
if('on' == message['resp']){
$('#led').removeClass('dim');
$('#led').addClass('glow');
} else {
$('#led').removeClass('glow');
$('#led').addClass('dim');
}
}
},
presence: function(presenceEvent) {
// handle presence
}
})
console.log("Subscribing..");
pubnub.subscribe({
channels: ['gpio-raspberry-control']
});
glow = False
#PubNub Channel Subscribe Callback
def gpioCallback(msg,channel):
global glow
respstring = ''
command = msg
print "Command is : " + str(command)
if('req' in command):
if(command['req'] == 'toggle'):
if(glow):
glow = False;
respstring = 'off'
else:
glow = True
respstring = 'on'
GPIO.output(16, glow)
respmsg = {"resp" : respstring }
pubnub.publish().channel("pubnubChannelName").message(respmsg).async(my_publish_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment