python mqtt script on raspberry pi to send and receive messages

前端 未结 1 625
南方客
南方客 2020-12-30 15:33

MQTT question:

Hi, I’m trying to set up a MQTT network between multiple Raspberry Pis (starting with two). I have one raspberry pi (RPi-A), MQTT client, with a therm

相关标签:
1条回答
  • 2020-12-30 16:27

    The simplest way is to start the network loop on a separate thread using the client.loop_start() function, then use the normal client.publish method

    from sense_hat import SenseHat
    import time
    import paho.mqtt.client as mqtt
    import paho.mqtt.publish as publish
    sense = SenseHat()
    
    Broker = "192.168.1.252"
    
    sub_topic = "sensor/instructions"    # receive messages on this topic
    
    pub_topic = "sensor/data"       # send messages to this topic
    
    
    ############### sensehat inputs ##################
    
    def read_temp():
        t = sense.get_temperature()
        t = round(t)
        return t
    
    def read_humidity():
        h = sense.get_humidity()
        h = round(h)
        return h
    
    def read_pressure():
        p = sense.get_pressure()
        p = round(p)
        return p
    
    def display_sensehat(message):
        sense.show_message(message)
        time.sleep(10)
    
    ############### MQTT section ##################
    
    # when connecting to mqtt do this;
    
    def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))
        client.subscribe(sub_topic)
    
    # when receiving a mqtt message do this;
    
    def on_message(client, userdata, msg):
        message = str(msg.payload)
        print(msg.topic+" "+message)
        display_sensehat(message)
    
    def on_publish(mosq, obj, mid):
        print("mid: " + str(mid))
    
    
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(Broker, 1883, 60)
    client.loop_start()
    
    while True:
        sensor_data = [read_temp(), read_humidity(), read_pressure()]
        client.publish("monto/solar/sensors", str(sensor_data))
        time.sleep(1*60)
    
    0 讨论(0)
提交回复
热议问题