-
Notifications
You must be signed in to change notification settings - Fork 0
/
wattxPublisher.py
76 lines (53 loc) · 1.72 KB
/
wattxPublisher.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
65
66
67
68
69
70
71
72
73
74
75
76
import random
import time
import paho.mqtt.client as mqtt
import json
import numpy
## Note: There are 2 ways of generating temperature data :##
## genTempCase = 0 generates uniform data with a specified interval :##
## genTempCase = 1 generates random temp values within a given interval :##
genTempCase = 0
minRange = 20.0 # min temp range for temp. simulation
maxRange = 27.0 # max temp range for temp. simulation
tempInterval = 0.5 # interval for genTempCase = 0 (uniform temp array)
indexCounter = 0
#create a uniform array for genTempCase = 0
tempCase1 = numpy.arange(minRange, maxRange, tempInterval).tolist()
def genTemperature(minRange,maxRange):
""" Returns a random temperature value within the given ranges
"""
temp = random.randint(minRange, maxRange)
return temp
def genTemperature2():
""" (Prototype) Returns a uniform temperature value within the given ranges
"""
global tempCase1
global indexCounter
output = ""
if indexCounter != len(tempCase1):
output = tempCase1[indexCounter]
indexCounter += 1
else:
indexCounter = 0
output = tempCase1[indexCounter]
indexCounter += 1
return output
def formatTemperature(sensorId):
""" Generates JSON string for sending to MQTT
"""
data = {}
data['sensorID'] = 'sensor-' + str(sensorId)
data['type'] = 'temperature'
if(genTempCase ==0):
data['value'] = genTemperature2()
else:
data['value'] = genTemperature(minRange,maxRange)
return data
def generateNodes(numNodes):
""" Generate temperature sensor objects
"""
objs = [mqtt.Client() for i in range(numNodes)]
for i in range(numNodes):
objs[i].connect("localhost", 1883, 60)
objs[i].loop_start()
return objs