-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtempsensor.py
52 lines (46 loc) · 1.92 KB
/
tempsensor.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
import zmq
import random
import sys
import time
class TemperatureSensor:
def toJSON(self,d):
j = { 'sala': d[0],
'temperatura': d[1],
'humidity': d[2],
'timestamp': d[3]
}
return j
def __init__(self,roomNumber,host,port):
#self.port = port
self.topic = roomNumber
#self.context = zmq.Context()
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUB)
#socket.bind("tcp://*:%d" % port)
self.socket.connect("tcp://%s:%d" % (host,port))
#self.socket = self.context.socket(zmq.PUB)
#self.socket = socket
#random.seed(str(self))
self.roomtemp = random.randrange(200,280)/10 # temperatura inicial da sala
def broadcastTemperature(self):
self.roomtemp += (random.randrange(0,200)-100)/10000 * self.roomtemp
self.humidity = random.randrange(600,920)/10
self.a = self.toJSON([self.topic,self.roomtemp,self.humidity,1])
print(self.a)
print("%d %f" % (self.topic,self.roomtemp))
self.socket.send("%d %f %d".encode('ascii') % (self.topic,self.roomtemp,self.humidity))
#socket.send("%d %d" % (topic, messagedata))
def broadcastTemperatureForever(self):
t = 1
while True:
self.roomtemp += (random.randrange(0,200)-100)/10000 * self.roomtemp
self.humidity = random.randrange(600,920)/10
t += 1
self.a = self.toJSON([self.topic,self.roomtemp,self.humidity,t])
print( str(self.a))
sj = str(self.topic)+' '+str(self.a)
#print("%d %f" % (self.topic,self.roomtemp))
#self.socket.send("%d %f %d %d".encode('ascii') % (self.topic,self.roomtemp,self.humidity,t))
self.socket.send(sj.encode('ascii') )
#socket.send("%d %d" % (topic, messagedata))
time.sleep(0.1)