Commit 6344b2a0 by arjun.b

MQTT Receiver

parents
# Default ignored files
/shelf/
/workspace.xml
<?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
<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="N801" />
</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 (MQTT_reciever)" 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_reciever.iml" filepath="$PROJECT_DIR$/.idea/MQTT_reciever.iml" />
</modules>
</component>
</project>
\ 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 uvicorn
from scripts.config.application_config import server_path, port_no
if __name__ == "__main__":
try:
uvicorn.run(server_path, port=int(port_no), reload=True)
except Exception as e:
print(str(e))
[server]
server_path=scripts.services.main:app
port_no=8467
[MQTT]
host=192.168.0.220
port=1883
File added
import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
# server
server_path = config.get("server", "server_path")
port_no = config.get("server", "port_no")
# mqtt host and port
hostname = config.get("MQTT", "host")
port_num = config.get("MQTT", "port")
except configparser.NoOptionError:
print("could not find the conf fle")
import json
import sqlite3
def on_connect(client, userdata, flags, rc):
try:
print("Connected with result code " + str(rc))
client.subscribe("mqtt_json/test")
except Exception as e:
print(str(e))
def on_message(client, userdata, msg):
try:
data = json.loads(msg.payload.decode())
# print(data)
# Connect to SQLite database
conn = sqlite3.connect("mqtt_db.db")
cursor = conn.cursor()
# create table
if cursor.fetchall():
pass
else:
table = "create table if not exists mqtt_table " \
"(first_name varchar(20),last_name varchar(20),age varchar(20))"
cursor.execute(table)
print("table mqtt is created")
# Insert json data into SQLite table
for item in range(len(data["personal_details"])):
values = tuple(data["personal_details"][item].values())
cursor.execute("insert into mqtt_table(first_name,last_name,age) values(?,?,?)", values)
print("data insert into database")
conn.commit()
conn.close()
except Exception as e:
print(str(e))
from fastapi import FastAPI
import json
import sqlite3
import paho.mqtt.client as mqtt
from scripts.config.application_config import port_num, hostname
from scripts.core.handlers.mqtt_listener import on_connect, on_message
app = FastAPI()
@app.get("/", tags=["root"])
async def root():
return {"data": "MQTT receiver"}
@app.get("/get_payload_and store", tags=["load and store"])
async def load_store():
try:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(hostname, int(port_num))
client.loop_forever()
return {"data": "load data from MQTT and store to sqlite"}
except Exception as e:
print(str(e))
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