Commit e814ba94 by rakesh.pv

mqtt with python send subscribe topic

parents
# Default ignored files
/shelf/
/workspace.xml
mqtt_python
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N806" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mqtt_python.iml" filepath="$PROJECT_DIR$/.idea/mqtt_python.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import os
from scripts.core.services import app
from scripts.core.handlers import subscribe_mqttBrokr
import uvicorn
from fastapi import FastAPI
if __name__ == "__main__":
""
import random
mqttBroker = "mqtt.eclipseprojects.io"
broker = '192.168.0.220'
port = 1883
topic = "python/mqtt"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 100)}'
\ No newline at end of file
from paho.mqtt import client as mqtt_client
from scripts.config.mqtt_server_connect import client_id, broker, port
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)
# Set Connecting Client ID
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.connect(broker, port)
return client
#
#
# from random import randrange, uniform
# import time
#
# from scripts.core.services.app import client
#
# while True:
# r = uniform(20.0, 21.0)
#
# print(client.publish(" hello", r))
# print(" published " + str(r) + "to topic TESTTING MQTT FASTAPI")
# time.sleep(1)
#
#
import time
from scripts.config.mqtt_server_connect import topic
def publish(client):
msg_count = 0
while True:
time.sleep(1)
msg = f"messages: {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}")
msg_count += 1
# python3.6
import random
from paho.mqtt import client as mqtt_client
from scripts.config.mqtt_server_connect import client_id, broker, port, topic
from scripts.core.handlers.connect_mqttBroker import connect_mqtt
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
client.subscribe(topic)
client.on_message = on_message
client = connect_mqtt()
subscribe(client)
client.loop_forever()
from scripts.core.handlers.connect_mqttBroker import connect_mqtt
from scripts.core.handlers.send_message_mqtt import publish
from scripts.core.handlers.subscribe_mqttBrokr import subscribe
client = connect_mqtt()
client.loop_start()
publish(client)
subscribe(client)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment