Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Commit 973b7ed

Browse files
committed
added example to match website
1 parent 493e1d5 commit 973b7ed

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

example/z_temp_pub.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from zenoh import Zenoh
2+
import random
3+
import time
4+
5+
random.seed()
6+
7+
def read_temp():
8+
return random.randint(15, 30)
9+
10+
def run_sensor_loop(z, pub):
11+
# read and produce e temperature every half a second
12+
while True:
13+
t = read_temp()
14+
z.stream_data(pub, str(t).encode())
15+
time.sleep(0.5)
16+
17+
if __name__ == "__main__":
18+
z = Zenoh.open()
19+
pub = z.declare_publisher('/myhome/kitcken/temp')
20+
run_sensor_loop(z, pub)

example/z_temp_sub.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from zenoh import Zenoh, SubscriberMode
2+
import time
3+
4+
def listener(rname, data, info):
5+
print("{}: {}".format(rname, data.decode()))
6+
7+
8+
if __name__ == "__main__":
9+
z = Zenoh.open()
10+
sub = z.declare_subscriber('/myhome/kitcken/temp', SubscriberMode.push(), listener)
11+
12+
# Listen for one minute and then exit
13+
time.sleep(10)
14+
z.undeclare_subscriber(sub)
15+
z.close()

0 commit comments

Comments
 (0)