Last active
January 2, 2017 00:42
-
-
Save girliemac/99bfac315be93d3f65ba to your computer and use it in GitHub Desktop.
[Tut] Controlling Lights with Pulse Width Modulations
This file contains hidden or 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 sys | |
| GPIO.setmode(GPIO.BCM) | |
| PIN_LIVING = 4 | |
| GPIO.setup(PIN_LIVING, GPIO.OUT) |
This file contains hidden or 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
| living = GPIO.PWM(PIN_LIVING, 100) | |
| living.start(0) |
This file contains hidden or 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
| dc = 50 | |
| living.ChangeDutyCycle(dc) |
This file contains hidden or 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
| PIN_FIREPLACE = 27 | |
| GPIO.setup(PIN_FIREPLACE, GPIO.OUT) |
This file contains hidden or 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
| fire = GPIO.PWM(PIN_FIREPLACE, 30) | |
| fire.start(0) |
This file contains hidden or 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
| try: | |
| while 1: | |
| pass | |
| except KeyboardInterrupt: | |
| GPIO.cleanup() | |
| sys.exit(1) |
This file contains hidden or 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.11.min.js"></script> |
This file contains hidden or 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
| <input id="lightliving" type="range" min="0" max="100" step="1" value="0"> |
This file contains hidden or 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 pubnub = PUBNUB({ | |
| publish_key: 'demo', | |
| subscribe_key: 'demo' | |
| }); |
This file contains hidden or 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
| 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); |
This file contains hidden or 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 = Pubnub(publish_key='demo', subscribe_key='demo') |
This file contains hidden or 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
| pubnub.subscribe(channels='pi-house', callback=_callback, error=_error) |
This file contains hidden or 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): | |
| print(m) | |
| dc = m['brightness'] | |
| if m['item'] == 'light-living': | |
| living.ChangeDutyCycle(dc) | |
| def _error(m): | |
| print(m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?