def doctor(debug=False): sensor = UARTSensor(port=SENSOR_PORT, rate=SENSOR_RATE, mode=SENSOR_MODE) client = MQTTClient(host=SERVER_HOST, port=SERVER_PORT, name=APPKEY, debug=debug) print('[+] UART checking ...', end=" ") print('ok') if sensor.connect() else print('no') # print('[+] MODE checking ...', end=" ") # print('ok') if sensor.verify('mode') else print('no') print('[+] MQTT checking ...', end=" ") print('ok') if client.connect() else print('no')
def client(debug=False, tls=False): print('APPKEY: ', APPKEY) print('SECRET: ', SECRET) print('') print('SERVER_HOST: ', SERVER_HOST) print('SERVER_PORT: ', SERVER_PORT) print('') client = MQTTClient(host=SERVER_HOST, port=SERVER_PORT, name=APPKEY, debug=debug, tls=tls) client.connect(APPKEY, SECRET) client.publish('console', {'st': 'up', 'id': APPKEY}) client.loop() while True: client.publish('console', json.dumps({'st': '00, 01, 02, 03', 'id': APPKEY})) time.sleep(1)
def run(client_id, topic, topics, qos, log, ws): mqtt_client = MQTTClient( client_id=client_id, enable_log=log, enable_ws=ws, ).connect() if topics: # Topics by comma for topic in [x.strip() for x in topics.split(',')]: mqtt_client.subscribe( topic, qos ) # Once the client has connected to the broker, subscribe to the topic else: mqtt_client.subscribe(topic, qos) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. # Continue the network loop, exit when an error occurs rc = 0 while rc == 0: rc = mqtt_client.loop_forever() print("rc: " + str(rc))
def run(client_id, topic, msg, qos, retain, log, ws, auto, loop): logging.basicConfig( level={ 0: logging.WARN, 1: logging.INFO, }.get(log, logging.DEBUG), format="%(asctime)-15s %(levelname)-8s %(message)s", ) mqtt_client = MQTTClient( client_id=client_id, enable_log=log, enable_ws=ws, ).connect() if auto and not loop: auto_topic, auto_msg = get_topic_message() # Publish a message mqtt_client.publish( topic=auto_topic, payload=auto_msg, qos=qos, retain=retain, ) elif auto and loop: while True: auto_topic, auto_msg = get_topic_message() # Publish a message mqtt_client.publish( topic=auto_topic, payload=auto_msg, qos=qos, retain=retain, ) time.sleep(5) else: # Publish a message mqtt_client.publish( topic=topic, payload=msg, qos=qos, retain=retain, )
def _create_new_session(self, client_id, keep_alive, flags): client = MQTTClient(client_id, keep_alive, flags, self._client_disconnect) self._sessions[client_id] = client client.start_session() return client
from client import MQTTClient import rfid_reader as wg import configparser as cp import mqttBrokerInfo as mbi import local_file as ll import logger import os logger = logger.Logger(1) # Leaving it blank will use local host, host port 1883 mbi = mbi.getmqttinfo() mqtt = MQTTClient(mbi[0], mbi[1], mbi[2], mbi[3], logger) # host adress, port, brokername,brokerpass mqtt.createClient() mqtt.startConnection() verifier = wg.ParkingVerifier([]) local_file = ll.LocalFile() receive_data = wg.Wiegand() def give_directions(path): length = '' destination = [] directions = [] direct = ['V', 'R', 'B', 'L'] # Directions/ Forward, Right, Reverse, left for i in path: for j in direct: if j in i: length = i.split(j) destination.append(length[0])
print("Callback", topic, message) with app.app_context(): db.session.add( Message(type="received", topic=topic, message=message, created=datetime.now())) parse_message(topic, message) db.session.commit() print("Saved") with app.app_context(): # db.create_all() topics = [topic.name for topic in db.session.query(Topic).all()] mqtt_client = MQTTClient(MQTT_HOST, topics, on_message_callback) mqtt_client.start() @app.route("/topics/list") def topics(): return jsonify({"topics": sorted(list(mqtt_client.topics))}) @app.route("/topics/subscribe") def subscribe_topic(): topic = request.args["topic"] if mqtt_client.subscribe_topic(topic): db.session.add(Topic(name=topic, created=datetime.now())) db.session.commit() return jsonify({"topics": sorted(list(mqtt_client.topics))})