Commit 34b60c6b authored by vidya.m's avatar vidya.m

firstcommit

parents
mongo_uri=mongodb://intern_23:intern%40123@192.168.0.220:2717/?authSource=interns_b2_23&authMechanism=SCRAM-SHA-256
\ No newline at end of file
# 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.10 (mqtt)" 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.iml" filepath="$PROJECT_DIR$/.idea/mqtt.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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
[MQTT_SER]
MQTT_BROKER = 192.168.0.220
MQTT_PORT = 1883
[MONGO-DB]
mongo_uri = $MONGO_URI
\ No newline at end of file
import paho.mqtt.client as mqtt
import json
import random
from datetime import datetime
from scripts.core.engine.subscriber import MONGODB_COLLECTION, insert_to_mongodb
from scripts.constants import MQTT_BROKER , MQTT_PORT,MQTT_TOPIC
# Create an MQTT client object
client = mqtt.Client()
# Connect to the MQTT broker
client.connect(MQTT_BROKER, MQTT_PORT)
# Get message from user and publish it
while True:
message = input("Enter message to publish: ")
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Create payload with humidity, temperature, and windspeed values
payload = {
"humidity": random.randint(1,10),
"temperature": random.randint(20,50),
"windspeed": random.randint(5,100)
}
# Add payload to message
message_with_payload = {
"message": message,
"timestamp": timestamp,
"payload": payload
}
# Convert message to JSON string
message_json = json.dumps(message_with_payload)
# Publish message
client.publish(MQTT_TOPIC, message_json)
print(f"Published message: {message_json}")
# Create document to insert into MongoDB
document = {
"message": message ,
"timestamp": datetime.utcnow(),
"payload": payload,
"topic": MQTT_TOPIC
}
# Insert the document into MongoDB
insert_to_mongodb(document, MONGODB_COLLECTION)
print("Message inserted into MongoDB")
\ No newline at end of file
import os
from dotenv import load_dotenv
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('conf/application.conf')
load_dotenv()
class Service:
port = int(config.get("SERVICE", "port"))
host = config.get("SERVICE", "host")
MQTT_BROKER = "192.168.0.220"
MQTT_PORT = 1883
MQTT_TOPIC = "vidnew"
\ No newline at end of file
import pymongo
import json
from datetime import datetime
from scripts.core.interns.connection import MONGODB_COLLECTION,MONGODB_DATABASE,MONGODB_URI
# Define function to insert a document into MongoDB
def insert_to_mongodb(document, collection_name):
# Connect to MongoDB
mongo_client = pymongo.MongoClient(MONGODB_URI)
db = mongo_client[MONGODB_DATABASE]
collection = db[collection_name]
# Insert the document into MongoDB
collection.insert_one(document)
print("Message inserted into MongoDB")
# Define callback function for when a message is received
def on_message(client, userdata, message):
received_message = message.payload.decode()
# Extract humidity, temperature, and windspeed values from the message payload
payload = json.loads(received_message)
humidity = payload["humidity"]
temperature = payload["temperature"]
windspeed = payload["windspeed"]
# Print the received message and extracted values
print("Received message:", received_message)
print("Humidity:", humidity)
print("Temperature:", temperature)
print("Windspeed:", windspeed)
# Create document to insert into MongoDB
document = {
"message": received_message,
"timestamp": datetime.utcnow(),
"topic": message.topic,
"humidity": humidity,
"temperature": temperature,
"windspeed": windspeed
}
\ No newline at end of file
MONGODB_URI = "mongodb://intern_23:intern%40123@192.168.0.220:2717/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=interns_b2_23&authMechanism=SCRAM-SHA-256"
MONGODB_DATABASE = "interns_b2_23"
MONGODB_COLLECTION = "vid_data"
\ 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