-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestUniSensor1.py
115 lines (88 loc) · 3.29 KB
/
testUniSensor1.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'''
uni sensor:
- can generate value
- generation +/- value from vector with randomize value
- generate extreme value - random
- coomunicate via mqtt
another test mqtt: test.mosquitto.org,broker.hivemq.com,iot.eclipse.org.
Used: flespi.io
'''
import random
import time
import paho.mqtt.client as mqtt
class UniSensor(object):
# attributes
Position = 0
def __init__ (self, StartValue=0, MaxValue=100, MinValue=-100, VectorMax=15, VectorExtreme=55, ExtremeFreq=23, Name='UniSensor'):
# set default value
self.StartValue = StartValue
self.MaxValue = MaxValue
self.MinValue = MinValue
self.VectorMax = VectorMax
self.VectorExtreme = VectorExtreme
self.ExtremeFreq = ExtremeFreq
self.Value = self.StartValue
self.ExtremePos = random.randint(1,self.ExtremeFreq)
self.Name = Name
# actions
def read_data(self):
if self.Position==self.ExtremePos:
ExtremeDirection = random.randrange(-1,2,1)
self.Value=ExtremeDirection*self.VectorExtreme
self.ExtremePos = self.Position+random.randint(1,self.ExtremeFreq)
else:
#AdditionValue = random.randrange(-self.VectorMax, self.VectorMax)
AdditionValue = random.triangular(-self.VectorMax, self.VectorMax,0)
self.Value+=AdditionValue
self.Value = self.MaxValue if self.Value>self.MaxValue else self.Value
self.Value = self.MinValue if self.Value<self.MinValue else self.Value
self.Position+=1
return self.Value
class MQTT_comm(object):
def __init__ (self, MQQTConfig, HeaderM='booomy'):
self.HeaderMsg = HeaderM
self.MQTTServer = MQQTConfig["Server"]
self.MQTTPort = MQQTConfig["Port"]
self.MQTTUser = MQQTConfig["User"]
self.MQTTPasswd = MQQTConfig["Passwd"]
self.MQTTClientID = "MyMQTTClient1"
# cls.mqqt_connect()
pass
# act
def on_connect(self,client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# client.subscribe("$SYS/#")
def mqtt_connect(self):
self.client = mqtt.Client(client_id=self.MQTTClientID)
self.client.on_connect = self.on_connect
#client.on_message = on_message
self.client.username_pw_set(self.MQTTUser, password=None)
self.client.connect(self.MQTTServer, self.MQTTPort, 60)
pass
def send_data(self,name,data):
headerMsg=self.HeaderMsg+"/"+name
self.client.publish(headerMsg, data)
# ........................................
#create my sensor and generator
Sensor1 = UniSensor()
# config MQTT client
mqttConfig={}
mqttConfig["Server"]='mqtt.flespi.io'
mqttConfig["Port"]=1883
flespi_token='C5mgGM7FtPBRtPM7aOHeHYr1CvDx8uSyq2DUFAqWdRDwwduwR1ymNObd9EB7Zhud'
mqttConfig["User"]=flespi_token
mqttConfig["Passwd"]=''
# create mqtt connection
MyMQTT = MQTT_comm(mqttConfig)
MyMQTT.mqtt_connect()
#MyMQTT.send_data(Sensor1.Name,10)
# generate data
Freq=1
while True:
payloadS=Sensor1.read_data()
print(payloadS)
time.sleep(Freq)
MyMQTT.send_data(Sensor1.Name,payloadS)
pass