-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_pub.py
64 lines (50 loc) · 1.73 KB
/
Data_pub.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Imports
import time
import paho.mqtt.client as mqtt
from random import randint
from string import ascii_uppercase
import random, string
from itertools import islice
from datetime import datetime, timedelta
# For connection with mosquitto
server = "localhost"
port = 1881
vhost = "yourvhost"
username = "username"
password = "password"
topic = "sensor/"
# Unique topic
# connecting to mosquitto
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
# For number of data to generate randomly
msgNum = int(input("Quantity of data: "))
for i in range(msgNum):
# For generation of random floating point to treat as data
random_gen = random.uniform(4.5, 42.7)
print(random_gen)
# I had already set 10 sensors so chossing a random sensor to put data in that sensor's table
sensor_id = randint(1,10)
topic = "sensor/"
topic += str(sensor_id) + "/data/"
# For setting a random DOC.
var = randint(0,365)
doc = datetime.now()-timedelta(365-var)
# Joining data and Sensor_id with "/" so we can break it and get data and sensor id seperate to write data
message = str(random_gen) + "/" + str(doc)
# timestamp 1072392329
# data 1239002382
#publish
client.publish(topic, payload=message, qos=0, retain=False)
# Sleeping for .0001 sec
time.sleep(.0001)
# stop loop
client.loop_stop()
# disconnecting
client.disconnect()
# Printing in case of errors
except Exception, e:
print e