вот сам код
import random
import time
import snap7
from snap7 import util
from paho.mqtt import client as mqtt_client
broker = ''
port = 1
topic = "/P"
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = 'A'
password = 'A'
def connect_mqtt() -> mqtt_client:
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.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
global client1
client1 = snap7.client.Client()
client1.connect('176.16.11.110', 0, 2)
client1.get_connected()
client1.db_read(10, 0, 2)
bytearray(b'\xc4\x9d\xc0\x00\xc2\xfcff\xff~\xc3\x02\x00\x00\xc3\x01\x00\x00')
return client
def publish(client):
msg_count = 0
while True:
time.sleep(1)
msg = f"{msg_count}"
result = client.publish(topic, msg)
#result: [0, 1]
status = result[0]
if status == 0:
print(f"Send`{msg}`to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
db = client1.db_read(10, 0, 2)
t = util.get_int(db, 0)
msg_count = t
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
if __name__ == '__main__':
run()
Вопрос задан
19 июн.
63 просмотра