Commit 7f6e9fee by rakesh.pv

send : recive : store mqtt mssg

parents
# Default ignored files
/shelf/
/workspace.xml
<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.11 (mqtt_send_stor)" 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_send_stor.iml" filepath="$PROJECT_DIR$/.idea/mqtt_send_stor.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="inheritedJdk" />
<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
from scripts.core.services.app import main
main()
\ No newline at end of file
File added
import paho.mqtt.client as mqtt
import sqlite3
from time import time
MQTT_HOST = '192.168.0.220'
MQTT_PORT = 1883
MQTT_CLIENT_ID = 'Python MQTT client'
TOPIC = 'home/#'
DATABASE_FILE = 'mqtt.db'
import paho.mqtt.client as mqtt
import sqlite3
from time import time
from scripts.core.config.mqtt_connetion import TOPIC, DATABASE_FILE, MQTT_CLIENT_ID, MQTT_PORT, MQTT_HOST
def on_connect(mqtt_client, user_data, flags, conn_result):
mqtt_client.subscribe(TOPIC)
def on_message(mqtt_client, user_data, message):
payload = message.payload.decode('utf-8')
db_conn = user_data['db_conn']
sql = 'INSERT INTO sensors_data (topic, payload, created_at) VALUES (?, ?, ?)'
cursor = db_conn.cursor()
cursor.execute(sql, (message.topic, payload, int(time())))
db_conn.commit()
cursor.close()
def main():
db_conn = sqlite3.connect(DATABASE_FILE)
sql = """
CREATE TABLE IF NOT EXISTS sensors_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT NOT NULL,
payload TEXT NOT NULL,
created_at INTEGER NOT NULL
)
"""
cursor = db_conn.cursor()
cursor.execute(sql)
cursor.close()
mqtt_client = mqtt.Client(MQTT_CLIENT_ID)
mqtt_client.user_data_set({'db_conn': db_conn})
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect(MQTT_HOST, MQTT_PORT)
mqtt_client.loop_forever()
main()
\ No newline at end of file
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