【发布时间】:2021-07-21 08:24:09
【问题描述】:
我编写了一个 python 模拟器脚本来将数据发送到多个 python 主题,但是当我发送数据时,它只向第一个主题发布数据,而不是下面的其他主题是代码。
from time import sleep
from datetime import datetime
import time
from random import randint
from random import seed
#from random import random
import random,json
import paho.mqtt.publish as publish
energy_utilization=''
energy_utilization=''
asset_workdone=''
asset_tracking=''
gateway_id1=1
gateway_id2=2
gateway_id3=3
currentTime_timestamp = time.time() - 36000
time_incr=900
incr_range=900
seed(3)
for _ in range(1200):
#helth
engineoiltemp_c_1234=randint(44, 50)
hydraulicoiltemp_c_1234= randint(50, 58)
#util
fuelconsumed_ltr_1234= randint(1, 6)
#workdone:
load_kg_1234= randint(1000, 1400)
cyclecount_1234= randint(1,3)
# # for tracking
weather_c_1234= randint(30, 45)
##########for Asset:
#seed(2)
engineoiltemp_c_5618=randint(44, 55)
hydraulicoiltemp_c_5618= randint(48, 58)
fuelconsumed_ltr_5618= randint(1, 5)
#workdone:
load_kg_5618= randint(1200, 1600)
cyclecount_5618= randint(1,3)
# # for tracking
weather_c_5618= randint(35, 42)
time_incr+=incr_range
ct_ts = currentTime_timestamp
nextTime = ct_ts + time_incr
timestamp= str(datetime.fromtimestamp(nextTime).strftime("%Y-%m-%d %H:%M:%S"))
energy_health = [ \
{"gateway_id": gateway_id2,"engineoiltemp_c":engineoiltemp_c_5618,"hydraulicoiltemp_c":hydraulicoiltemp_c_5618 ,"timestamp": timestamp},\
\
{"gateway_id":gateway_id1,"engineoiltemp_c":engineoiltemp_c_1234,"hydraulicoiltemp_c":hydraulicoiltemp_c_1234 ,"timestamp": timestamp}, \
\
{"gateway_id":gateway_id3, "engineoiltemp_c":0,"hydraulicoiltemp_c":0, "timestamp":timestamp} \
]
energy_utilization = [ \
{"gateway_id": gateway_id2,"fuelconsumed_ltr":fuelconsumed_ltr_5618,"timestamp": timestamp },\
\
{"gateway_id":gateway_id1,"fuelconsumed_ltr":fuelconsumed_ltr_1234,"timestamp":timestamp } ]
asset_workdone =[ \
{"gateway_id":gateway_id1, \
"enginestatus":1,"timestamp": timestamp,"load_kg":load_kg_1234,"cyclecount":cyclecount_1234 },\
\
{"gateway_id":gateway_id2,"timestamp":timestamp,"load_kg":load_kg_5618,"cyclecount":cyclecount_5618}]
asset_tracking =[ \
{"gateway_id":gateway_id2,"weather_c":weather_c_5618,"timestamp":timestamp},\
\
{"gateway_id":gateway_id1,"weather_c":weather_c_1234,"timestamp": timestamp}, \
\
{"gateway_id":gateway_id3,"weather_c":35.5,"timestamp":timestamp} ]
print(energy_health)
print(energy_utilization)
print(asset_workdone)
print(asset_tracking)
import paho.mqtt.client as mqtt #import the client1
broker_address="XXXXXXX"
client = mqtt.Client("P1") #create new instance
client.connect(broker_address) #connect to broker
for i in energy_health:
client.publish("UB/RA/Ex/EnergyHealth", json.dumps(i).encode('utf-8'))#publish
print("msg publish to energyhealth")
for j in energy_utilization:
client.publish("UB/RA/Ex/EnergyUtilization", json.dumps(j).encode('utf-8'))#publish
print("msg publish to energyutili")
for k in asset_workdone:
client.publish("UB/RA/Ex/Asset_WD_Productivity", json.dumps(k).encode('utf-8'))
print("msg publish to workdone")
for l in asset_tracking:
client.publish("UB/RA/Ex/AssetTracking", json.dumps(l).encode('utf-8'))
print("msg publish to tracking")
sleep(1)
print('***************************************************************************************************************')
print('\n \n')
请帮助我在此代码中的错误之处。以及为什么它只将数据写入能量健康主题而不是其他主题
【问题讨论】:
-
您从哪里开始了 MQTT 客户端循环?
-
如果我遗漏了什么,请建议我需要在哪里添加循环,因为使用该代码我只能将数据推送到第一个主题。