import csv
import os
import random
import time
from paho.mqtt import client as mqtt_client
broker = '175.24.64.90'
port = 1883
topic = "/api/ems_cloud/v1_1/operate_time_command/{HZ01}"
client_id = f'python-mqtt-{random.randint(1000, 2000)}'
file = open("output.csv", 'w').close()
def write_csv(mPayload, mTopic):
with open("output.csv", 'a', encoding='utf-8', newline='') as f:
f.seek(0)
writer = csv.writer(f)
writer.writerow([mPayload, mTopic])
import random
import time
import datetime
from paho.mqtt import client as mqtt_client
broker = '175.24.64.90'
port = 1883
topic = "/v1_1/down/remote_control/EMS1"
client_id = f'python-mqtt-{random.randint(0, 1000)}'
pub_data = {
"cmdid": 1024,
"ts": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"identify": "simulator",
"data": [
{
"identity": "cmd_bool",
"value": 1,
},
{
"identity": "cmd_ushort",
"value": 10,
},
{
"identity": "cmd_float",
"value": -11.98,
}
]
}
msg = str(pub_data)
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.username_pw_set("emsuser","emsuser")
client.connect(broker, port, 60)
return client
def publish(client):
msg_count = 0
while True:
time.sleep(1)
result = client.publish(topic, msg)
status = result[0]
if status == 0:
print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
msg_count += 1
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
if __name__ == '__main__':
run()