Things Bus: Difference between revisions
From Pumping Station One
| Line 49: | Line 49: | ||
Before running the example, you must have a recent python, recent libzmq installed, and must have pyzmq installed. | Before running the example, you must have a recent python, recent libzmq installed, and must have pyzmq installed. | ||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python | |||
import zmq | |||
context = zmq.Context.instance() | |||
# Create a zmq socket that will SUBscribe to door nodes. | |||
door_socket = context.socket(zmq.SUB) | |||
door_socket.connect("tcp://frontdoor.pumpingstationone.org:5556") | |||
door_socket.connect("tcp://backdoor.pumpingstationone.org:5556") | |||
# The doors send a lot of types of messages. We only care about "door.state.unlock" messages | |||
door_socket.setsockopt(zmq.SUBSCRIBE, b"door.state.unlock") | |||
# create a zmq socket that will PUSH data to our IRC actuator node. | |||
zirc_socket = context.socket(zmq.PUSH) | |||
zirc_socket.connect('tcp://sally.ad.pumpingstationone.org:5558') | |||
# Loop forever | |||
while True: | |||
# Read messages from the doors | |||
topic, message = door_socket.recv_multipart() | |||
# Send the message to the irc channel | |||
zirc_socket.send(message) | |||
<syntaxhighlight> | |||
== Hardware == | == Hardware == | ||