Skip to content

Instantly share code, notes, and snippets.

@girliemac
Last active January 2, 2017 00:42
Show Gist options
  • Save girliemac/99bfac315be93d3f65ba to your computer and use it in GitHub Desktop.
Save girliemac/99bfac315be93d3f65ba to your computer and use it in GitHub Desktop.
[Tut] Controlling Lights with Pulse Width Modulations
import RPi.GPIO as GPIO
import sys
GPIO.setmode(GPIO.BCM)
PIN_LIVING = 4
GPIO.setup(PIN_LIVING, GPIO.OUT)
living = GPIO.PWM(PIN_LIVING, 100)
living.start(0)
dc = 50
living.ChangeDutyCycle(dc)
PIN_FIREPLACE = 27
GPIO.setup(PIN_FIREPLACE, GPIO.OUT)
fire = GPIO.PWM(PIN_FIREPLACE, 30)
fire.start(0)
try:
while 1:
pass
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit(1)
<script src="http://cdn.pubnub.com/pubnub-3.7.11.min.js"></script>
<input id="lightliving" type="range" min="0" max="100" step="1" value="0">
var pubnub = PUBNUB({
publish_key: 'demo',
subscribe_key: 'demo'
});
function publishUpdate(data) {
pubnub.publish({
channel: 'pi-house',
message: data
});
}
var lightLiving = document.getElementById('lightLiving');
lightLiving.addEventListener('change', function(e){
publishUpdate({item: 'light-living', brightness: + this.value});
}, false);
from pubnub import Pubnub
pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
pubnub.subscribe(channels='pi-house', callback=_callback, error=_error)
def _callback(m, channel):
print(m)
dc = m['brightness']
if m['item'] == 'light-living':
living.ChangeDutyCycle(dc)
def _error(m):
print(m)
@anazhd
Copy link

anazhd commented Jan 2, 2017

Hi, pubnub api v4 is quite different and I'm having trouble with the callback. It's now using listener which I don't really know where to start. Can you help me with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment