sdt-mqtt-app-version1/main.py

76 lines
2.1 KiB
Python
Raw Normal View History

2024-03-14 13:06:35 +00:00
import sdtcloudpubsub
import time
import uuid
import random
# call sdtcloudpubsub
sdtcloudMqttClient = sdtcloudpubsub.sdtcloudpubsub()
mqttClient = sdtcloudMqttClient.setClient(f"device-app-{uuid.uuid1()}") # parameter is client ID(string)
def runAction():
while True:
index = random.randint(0, 10) # float
if index < 5:
openValue = "ture"
else:
openValue = "false"
timedata = int(time.time()*1000)
data = {
"rssi": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"gas_o2": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"gas_h2s": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"rsrp": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"battery": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"sinr": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"rsrq": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"temperature": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"ph": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"humidity": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"water_level": {
"value": random.uniform(0, 30), # float,
"timestamp": timedata
},
"open": {
"value": openValue,
"timestamp": timedata
}
}
sdtcloudMqttClient.pubMessage(mqttClient, data)
time.sleep(10)
if __name__ == "__main__":
runAction()