1

So I am using the python bindings for ZeroMQ in an attempt to send messages to several raspberry pis on a network for an art project. The issue is that I am not receiving any messages on the raspberry pi. To make matters worse, I can:

  • Publish messages from the raspberry pi and receive them on my laptop(osx)
  • A REQ/REP model works in both directions (osx -> RPI and RPI -> osx)

I was thinking that it has something to do with the firewall on either device, which I disabled on both. No luck.

Anyone have any ideas? Here is my code:

ZMQ Publisher

import time
import zmq


def main():
    port = 5563

    # Prepare our context and publisher
    context = zmq.Context()
    publisher = context.socket(zmq.PUB)
    publisher.bind("tcp://*:" + str(port))

    while True:
        # Write two messages, each with an envelope and content
        publisher.send_multipart([b"A", b"We don't want to see this"])
        publisher.send_multipart([b"B", b"We would like to see this"])
        time.sleep(1)

    # We never get here but clean up anyhow
    publisher.close()
    context.term()

if __name__ == "__main__":
    main()

ZMQ Subscriber

import zmq
import sys


def main():
    ip = sys.argv[1]
    port = 5563
    string = "tcp://" + ip + ":" + str(port)

    # Prepare our context and publisher
    context = zmq.Context()
    subscriber = context.socket(zmq.SUB)
    subscriber.connect(string)
    subscriber.setsockopt(zmq.SUBSCRIBE, b"B")

    while True:
        # Read envelope with address
        [address, contents] = subscriber.recv_multipart()
        print("[%s] %s" % (address, contents))

    # We never get here but clean up anyhow
    subscriber.close()
    context.term()

if __name__ == "__main__":
    main()
  • Inevitably network programming will lead you to the necessity of using a packet sniffer such as wireshark. It may be hard to understand at first if you aren't familiar with some low level details (e.g., what a "packet" is), but if you can set things up so the only traffic on your LAN is the computers you are concerned with, it will probably make sense with a bit of experimentation. This isn't a solution to your problem, but it is an immediate way to confirm who is actually receiving what. – goldilocks May 18 '15 at 7:40
  • Does ZMQ require a daemon to be running on the Pi? Or does it just attach itself to the nominated socket? Do any of the Python calls return an error status which you are not checking? – joan May 18 '15 at 7:44
  • 1
    I've just tried the code and it works between a laptop and a Pi. I added #!/usr/bin/env python as the first line of each script. Perhaps you gave the wrong IP address for the publisher in the subscriber. – joan May 18 '15 at 8:00
1

Solved. There were two issues:

First- Since I am using python3, I had to make sure I was using pip-3.2 commands when updating/installing python modules

Second- On my laptop I was using pyzmq 14.6.0 and I was using 2.2.0 on the PI. I had updated it, but used pip instead of pip-3.2. Once I properly updated pyzmq it worked as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.