Zenoh subscriber not picking up payload

109 views Asked by At

Win 10 x64, Python 3.10

I'm working from Your first Zenoh app

The publisher code,

# z_sensor.py

import zenoh, random, time

random.seed()

def read_temp():
    return random.randint(15, 30)

if __name__ == "__main__":
    session = zenoh.open()
    key = 'myhome/kitchen/temp'
    pub = session.declare_publisher(key)
    while True:
        t = read_temp()
        buf = f"{t}"
        print(f"Putting Data ('{key}': '{buf}')...")
        pub.put(buf)
        time.sleep(1)

The subscriber code,

# z_subsciber.py

import zenoh, time

def listener(sample):
    print(f"Received {sample.kind} ('{sample.key_expr}': '{sample.payload.decode('utf-8')}')")

if __name__ == "__main__":
    session = zenoh.open()
    sub = session.declare_subscriber('myhome/kitchen/temp', listener)
    time.sleep(60)

I start up 2 Anaconda command windows & activate my Zenoh environment in both.

I run the subscriber code in the first window followed by the publisher in the second window.

The publisher window is pushing out the data & printing to the window as expected,

Putting Data ('myhome/kitchen/temp': '28')...
Putting Data ('myhome/kitchen/temp': '27')...
Putting Data ('myhome/kitchen/temp': '25')...
Putting Data ('myhome/kitchen/temp': '16')...
Putting Data ('myhome/kitchen/temp': '19')...
Putting Data ('myhome/kitchen/temp': '25')...
Putting Data ('myhome/kitchen/temp': '28')...
.....

But the subscriber window does nothing. There are no print outs from the listener function.

I installed using pip after installing the Rust toolchain.

Anyone know what's going on here?

1

There are 1 answers

0
Gabriele On BEST ANSWER

If you stated the example without any parameter they will try to discover via multicast scouting, thus multicast needs to be enabled. By default in Windows, it is not allowed by Windows firewall.

You can try to use locators to be sure that the multicast is indeed the problem.

Change your subscriber to listen on a specific port:

import zenoh, time, json

def listener(sample):
    print(f"Received {sample.kind} ('{sample.key_expr}': '{sample.payload.decode('utf-8')}')")

if __name__ == "__main__":
    cfg = zenoh.Config()
    cfg.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(["tcp/127.0.0.1:7447"]))
    session = zenoh.open(cfg)
    sub = session.declare_subscriber('myhome/kitchen/temp', listener)
    while True:
        time.sleep(60)

And your sensor to connect to this subscriber:

import zenoh, random, time, json

random.seed()

def read_temp():
    return random.randint(15, 30)

if __name__ == "__main__":
    cfg = zenoh.Config()
    cfg.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(["tcp/127.0.0.1:7447"]))
    session = zenoh.open()
    key = 'myhome/kitchen/temp'
    pub = session.declare_publisher(key)
    while True:
        t = read_temp()
        buf = f"{t}"
        print(f"Putting Data ('{key}': '{buf}')...")
        pub.put(buf)
        time.sleep(1)

If that works, then you need to enable multicast in your Windows firewall.