Skip to content

Instantly share code, notes, and snippets.

@yoneken
Forked from rohinip/pnLED.py
Last active August 29, 2015 14:15
Show Gist options
  • Save yoneken/024f296f313453295098 to your computer and use it in GitHub Desktop.
Save yoneken/024f296f313453295098 to your computer and use it in GitHub Desktop.
##################
#### pnLED.py ####
##################
# This code is ported from Raspberry Pi to Intel Edison.
# LEDs are activated in active-low signal!
from Pubnub import Pubnub
import mraa ## Import libmraa library
G_LED = mraa.Gpio(19) ## Initialize GPIO Pin MRAA19 as Green LED
Y_LED = mraa.Gpio(20) ## Initialize GPIO Pin MRAA20 as Yellow LED
R_LED = mraa.Gpio(21) ## Initialize GPIO Pin MRAA21 as Red LED
G_LED.dir(mraa.DIR_OUT) ## Setup GPIO Pin MRAA19 to OUT - Green
Y_LED.dir(mraa.DIR_OUT) ## Setup GPIO Pin MRAA20 to OUT - Yellow
R_LED.dir(mraa.DIR_OUT) ## Setup GPIO Pin MRAA21 to OUT - Red
G_LED.write(1) ## Turn off GPIO pin MRAA19 - Green
Y_LED.write(1) ## Turn off GPIO pin MRAA20 - Yellow
R_LED.write(1) ## Turn off GPIO pin MRAA21 - Red
# Enter your PubNub Publish Key and use the Market Order Demo Subscribe Key
pubnub = Pubnub(publish_key="YOUR PUB KEY", subscribe_key="sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe", ssl_on=False)
# Listen for Messages on the Market Order Channel
channel = 'pubnub-market-orders'
def callback(message, channel):
print(message['order_quantity']) # Use the Order Quantity value as the LED trigger
# Reset LEDs to OFF
G_LED.write(1)
Y_LED.write(1)
R_LED.write(1)
if (message['order_quantity'] >= 100) and (message['order_quantity'] < 200):
G_LED.write(0) # Turn on GREEN
elif (message['order_quantity'] >= 200) and (message['order_quantity'] < 400):
Y_LED.write(0) # Turn on YELLOW
elif (message['order_quantity'] >= 400):
R_LED.write(0) # Turn on RED
def error(message):
print("ERROR : " + str(message))
def connect(message):
print("CONNECTED")
def reconnect(message):
print("RECONNECTED")
def disconnect(message):
print("DISCONNECTED")
pubnub.subscribe(channel, callback=callback, error=callback,
connect=connect, reconnect=reconnect, disconnect=disconnect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment