def on_message(mosq, obj, msg): signal = msg.topic.split('/')[1] value = msg.payload print "MQTT received " + signal + ": " + str(value) # Create the Hardwire Signal if it doesn't exist yet if signal in signals: pass else: signals[signal] = Signal(signal, value) # Update signal value, converting to boolean if necessary if value in ["True", "true", "T", "t"]: signals[signal].value = True elif value in ["False", "false", "F", "f"]: signals[signal].value = False else: signals[signal].value = value
# Add Hardwire to the path to import. Only needed if not installed. import sys, os hardwire_dir = os.path.abspath(os.path.join(__file__, '..', '..', '..')) sys.path.append(os.path.abspath(hardwire_dir)) from hardwire import Hardwire, Signal import Adafruit_BBIO.GPIO as GPIO # Create and initialize the Hardwire server. server = Hardwire(port=8080, debug=True) # Create Signal objects. Updating the value property of these objects will # automatically update any bound values in the HTML document, and vice versa. checkbox = Signal('checkbox', 0) status = Signal('status', 0) # Initialize GPIO pins GPIO.setup("P8_10", GPIO.OUT) GPIO.setup("P8_14", GPIO.IN) # Register a signal handler for checkbox. Every time the checkbox is clicked, # this function executes and sets the GPIO output value to the checkbox value. @checkbox.on_change() def set_ouput(name, value): """Handle changes to the value of output_pin.""" print "Setting P8_10 to " + str(value) if value: GPIO.output("P8_10", GPIO.HIGH) else: GPIO.output("P8_10", GPIO.LOW)