Things Bus: Difference between revisions

From Pumping Station One
 
(10 intermediate revisions by 2 users not shown)
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.


    #!/usr/bin/env python
<syntaxhighlight lang="python">
    import zmq
#!/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:
      
      
    context = zmq.Context.instance()
     # Read messages from the doors
   
    topic, message = door_socket.recv_multipart()
     # Create a zmq socket that will SUBscribe to door nodes.
 
    door_socket = context.socket(zmq.SUB)
     # Send the message to the irc channel
    door_socket.connect("tcp://frontdoor.pumpingstationone.org:5556")
    zirc_socket.send(message)
    door_socket.connect("tcp://backdoor.pumpingstationone.org:5556")
</syntaxhighlight>
   
    # 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.send(message)


== Hardware ==
== Hardware ==
Line 86: Line 88:
For details of this system, see https://github.com/eastein/thingsbus
For details of this system, see https://github.com/eastein/thingsbus


== Communication ==
== Inter-Node Communication ==
 
{{ambox
|type=content
|text=The code snippits contain errors
}}


The software for a node can be written in any language that supports zmq and json.
The software for a node can be written in any language that supports zmq and json.


Nodes send messages using json data. Sensors use zmq PUB/SUB. Actuators use zmq PUSH/PULL. Neurons use whatever they have to do get the job done, which means they SUB to sensors, and PUSH to actuators.
=== Code Snippits ===
Sensors send data like this:
Sensors send data like this:


    import zmq
    context = zmq.Context.instance()
     socket = context.socket(zmq.PUB)
     socket = context.socket(zmq.PUB)
     socket.bind('tcp://*:5556')
     socket.bind('tcp://*:5556')
Line 98: Line 110:
Neurons receive data like this:
Neurons receive data like this:


    import zmq
    context = zmq.Context.instance()
     sensor = context.socket(zmq.SUB)
     sensor = context.socket(zmq.SUB)
     sensor.connect("tcp://sensor.tld:5556")
     sensor.connect("tcp://sensor.tld:5556")
    sensor.setsockopt(zmq.SUBSCRIBE, b"dot.delimited.filter")
     topic, message = sensor.recv_multipart()
     topic, message = sensor.recv_multipart()
     data = json.loads(message)
     data = json.loads(message)
Line 105: Line 120:
Neurons send data like this:
Neurons send data like this:


    import zmq
    context = zmq.Context.instance()
     actuator = context.socket(zmq.PUSH)
     actuator = context.socket(zmq.PUSH)
     actuator.connect('tcp://actuator.tld:5558')
     actuator.connect('tcp://actuator.tld:5558')
Line 111: Line 128:
Actuators receive data like this:
Actuators receive data like this:


    import zmq
    context = zmq.Context.instance()
     socket = context.socket(zmq.PULL)
     socket = context.socket(zmq.PULL)
     socket.bind('tcp://*:5558')
     socket.bind('tcp://*:5558')