Last active
October 17, 2017 22:14
-
-
Save girliemac/1ac1d6354bbca31438da to your computer and use it in GitHub Desktop.
Code samples for IoT 101 with Raspberry Pi Blog: http://www.pubnub.com/blog/internet-of-things-101-getting-started-w-raspberry-pi/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from pubnub import Pubnub | |
pubnub = Pubnub(publish_key='<your-pub-key>', subscribe_key='<your-sub-key>') | |
channel = 'hello-pi' | |
data = { | |
'username': 'Your name', | |
'message': 'Hello World from Pi!' | |
} | |
def callback(m): | |
print(m) | |
pubnub.publish(channel, data, callback=callback, error=callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode (GPIO.BCM) | |
LIGHT = 4 | |
GPIO.setup(LIGHT,GPIO.OUT) | |
while True: | |
GPIO.output(LIGHT,True) | |
time.sleep(0.5) | |
GPIO.output(LIGHT,False) | |
time.sleep(0.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var channel = 'disco'; | |
var p = PUBNUB.init({ | |
subscribe_key: 'your-sub-key', | |
publish_key: 'your-pub-key' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button>Disco on!</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.querySelector('button').addEventListener('click', function() { | |
p.publish({ | |
channel : channel, | |
message : {led: 1} // trigger | |
}); | |
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pubnub import Pubnub | |
... | |
pubnub.subscribe(channels='disco', callback=_callback, error=_error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _callback(m, channel): | |
if m['led'] == 1: | |
for i in range(6): | |
GPIO.output(LED_PIN,True) | |
time.sleep(0.5) | |
GPIO.output(LED_PIN,False) | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment