Exemplo n.º 1
0
def on_message_callback(client, userdata, message):
    # parse messages in carCoordinates and publish any derived messages
    if message.topic == 'carCoordinates':
        payload = json.loads(message.payload)

        car_index = payload['carIndex']
        location = Location(payload['timestamp'], payload['location']['lat'], payload['location']['long'])

        topics_and_messages = data_store.add_new_car_coordinate(car_index, location)
        for topic, message in topics_and_messages:
            client.publish(topic, payload=json.dumps(message))


parser = argparse.ArgumentParser()
parser.add_argument('--host', default='127.0.0.1', help='hostname or IP address of the remote broker')
args = parser.parse_args()

# set lap start location for Silverstone Circuit
lap_start_location = Location(timestamp=None, latitude=52.067695316642116, longitude=-1.0241639614105225)

# initialize data store
data_store = CarDataStore(lap_start_location)

# initialize and start MQTT client
client = paho.mqtt.client.Client()
client.on_connect = connect_callback
client.on_message = on_message_callback
client.connect_async(args.host)
client.loop_forever()
Exemplo n.º 2
0
def on_message(client, userdata, message):
    print('-------- RECEIVED ------------------')
    print('topic: %s' % message.topic)
    print('payload: %s' % message.payload)
    print('qos: %d' % message.qos)    

def on_publish(client, userdata, message):
    print('------- PUB -----------------------')
    print('topic: %s' % message.topic)
    print('payload: %s' % message.payload)
    print('qos: %d' % message.qos)    



if (len(sys.argv) < 2):
    print("Falta nombre grabacion")
    sys.exit(12)

nombre=sys.argv[1]
print("GRabando %s" % nombre)


client = paho.mqtt.client.Client(client_id='GrabaIR-Mqtt', clean_session=True)
client.on_connect = on_connect
client.on_message = on_message
#client.on_publish = on_publish
client.connect_async(host='192.168.1.199', port=1883)

#client.publish("STAT/SwBot/Finger_Long/","OFF")
print("Switchbot MQTT Connected to MQTT Server. Starting loop")
client.loop_forever()
Exemplo n.º 3
0
    client.subscribe("cmnd/tasmota/light_D8DF25/power1")
    client.subscribe("#")


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))
    print(type(msg.payload))

    if msg.payload == b"Hello":
        print("Received message #1, do something")

    if msg.payload == b"World!":
        print("Received #2")


# Create an MQTT client and attach our routine to it.
mqtt = mqtt.Client(userdata="tom")
mqtt.username_pw_set("alto", "Ihm$2Te0T")
mqtt.on_connect = on_connect
mqtt.on_message = on_message
mqtt.connect_async(host="192.168.1.127", port=1883)

# Process network traffic and dispatch callbacks. This will also handle
# reconnecting. Check the documentation at
# https://github.com/eclipse/paho.mqtt.python
# for information on how to use other loop*() functions

# mqtt.loop_start()
mqtt.loop_forever()