-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensor_sub.py
34 lines (30 loc) · 1 KB
/
Sensor_sub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
import paho.mqtt.client as mqtt
from random import randint
from string import ascii_uppercase
import random, string
from itertools import islice
def randomword(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
server = "localhost"
port = 1881
vhost = "yourvhost"
username = "username"
password = "password"
topic = "123"
try:
client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol="MQTTv31")
client.username_pw_set(vhost + ":" + username, password)
client.connect(server, port, keepalive=60, bind_address="") #connect
client.loop_start() #start loop
msgNum = int(input("Quantity of Sensor added to node randomly: "))
for i in range(msgNum):
random_gen = randomword(12)
message = random_gen +"/1"
print(message)
client.publish(topic, payload=message, qos=0, retain=False) #publish
time.sleep(.01)
client.loop_stop() #stop loop
client.disconnect()
except Exception, e:
print e