Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import paho.mqtt.client as mqtt
import json
from datetime import datetime
import time
def logging(client_id): # logs
f = open("systemlog.txt", "a")
x = datetime.now()
string = f"{x} | {client_id}\n" # Added newline character for better formatting
f.write(string)
f.close()
# Open and read the file after appending:
f = open("demofile2.txt", "r")
print(f.read())
f.close() # Close the file after reading
def on_log(client, userdata, level, buf): # Log the subscriber considering the buffer
print("Log: ", buf)
def on_publish(client, userdata, mid):
print("Data published \n")
print("Published ", mid) # Print the message id of the publisher
def on_message(client, userdata, msg): # Callback function for message received
print(f"Received message: {msg.payload.decode()} from topic: {msg.topic}")
def on_connect(client, userdata, flags, rc):
if rc == 0:
client.connected_flag = True # Set the flag to true once connected
print("Connected with result code " + str(rc)) # Check to know if we are connected to broker
client.subscribe("cartrax/location/data") # Subscribe to desired topics upon connection
client.subscribe("cartrax/moisture_sensor/data")
client.subscribe("cartrax/temperature_sensor/data")
client.subscribe("cartrax/security/data")
client.subscribe("cartrax/speedometer/data")
else:
print("Bad connection, returned code= ", rc) # If connection failed
print("Client disconnected ok")
def on_disconnect(client, userdata, rc):
if rc == 0:
client.connected_flag = True # Set flag to true once connected
print("Connected with result code " + str(rc)) # Check for connection
client.subscribe("cartrax/location/data") # Subscribe to desired topics
client.subscribe("cartrax/moisture_sensor/data")
client.subscribe("cartrax/temperature_sensor/data")
client.subscribe("cartrax/security/data")
client.subscribe("cartrax/speedometer/data")
else:
print("Bad connection, returned code= ", rc) # If connection failed
print("Client disconnected ok")
def on_disconnect(client, userdata, rc):
print("Client disconnected")
client.connected_flag = False # Set flag to false if disconnected
time.sleep(1) # Wait before retrying connection
client.reconnect() # Attempt reconnection
client1 = mqtt.Client("Thermometer") # Create an MQTT client object
client1.connect("broker.hivemq.com", 1883)
client1.on_publish = on_publish
client1.on_log = on_log # Set log function to print the messages
client1.on_message = on_message # Assign the on_message function to the callback attribute
client1.on_connect = on_connect # Assign the on_connect function to the callback attribute
client1.on_disconnect = on_disconnect
client1.loop_start() # Start MQTT loop
# Example of publishing a message
while True:
data = {"temperature": 25.5, "humidity": 60} # Sample data to be published
client1.publish("cartrax/temperature_sensor/data", json.dumps(data)) # Publish data to specified topic
time.sleep(5) # Publish data every 5 seconds (for demonstration purposes)