Пример #1
0
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
Пример #2
0
# 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)

# Run the Hardwire server.
server.run()

while True:
    try:
        # Poll the input pin and update the corresponding Hardwire signal.
        # For a more advanced approach, use the BBIO event callback
        # functionality.
        sleep(0.1)
        if GPIO.input("P8_14"):
            status.value = True
        else:
            status.value = False
    except KeyboardInterrupt:
        server.stop()
        GPIO.cleanup()
        sys.exit(0)