Advertisement
Guest User

MQTT

a guest
Nov 12th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. ################### pub.py
  2.  
  3. import time
  4. import paho.mqtt.client as mqtt
  5. from sense_emu import SenseHat
  6.  
  7. #Publikant wysyla wiadomosc do sieci
  8.  
  9. localhost = '127.0.0.1'
  10. port = 8888
  11. timeout = 60
  12.  
  13. topic_c = "sense/cisnienie"
  14. topic_t = "sense/temperatura"
  15. topic_w = "sense/wilgotnosc"
  16.  
  17. Qos = 0
  18. message_payload = "Wiadomosc testowa"
  19.  
  20. sense = SenseHat()
  21.  
  22. temp = sense.temp
  23. wilg = sense.humidity
  24. ci = sense.pressure
  25.  
  26. temp_str = str(temp)
  27. wilg_str = str(wilg)
  28. ci_str = str(ci)
  29.  
  30. def on_connect(client, userdata, flags, rc):
  31. print("Polaczona; Result code: "+str(rc))
  32.  
  33. client = mqtt.Client()
  34. client.on_connect = on_connect
  35.  
  36. client.connect(localhost, port, timeout)
  37. client.publish(topic_c,ci_str.encode('utf-8'),0)
  38. client.publish(topic_t,temp_str.encode('utf-8'),0)
  39. client.publish(topic_w,wilg_str.encode('utf-8'),0)
  40. #temp_str.encode('utf-8')
  41.  
  42. time.sleep(4)
  43. #client.disconnect()
  44.  
  45. #######
  46. #######
  47. ############## sub_cis.py
  48.  
  49. import paho.mqtt.client as mqtt
  50.  
  51. # Sybskrybent nasluchuje wiadomosci o okreslonym temacie
  52.  
  53.  
  54. localhost = '127.0.0.1'
  55. port = 8888
  56. timeout = 60
  57. topic = "sense/cisnienie"
  58.  
  59. def on_connect(client, userdata, flags, rc):
  60. print("Polaczona; Result code: "+str(rc))
  61. client.subscribe(topic)
  62.  
  63. def on_message(client, userdata, msg):
  64. print(msg.topic+": "+str(msg.payload))
  65.  
  66. client = mqtt.Client()
  67.  
  68. client.connect(localhost, port, timeout)
  69.  
  70. client.on_connect = on_connect
  71. client.on_message = on_message
  72.  
  73. client.loop_forever()
  74.  
  75.  
  76.  
  77. ####
  78. ####
  79. ####
  80. #### sub_temp.py
  81.  
  82. import paho.mqtt.client as mqtt
  83.  
  84. # Sybskrybent nasluchuje wiadomosci o okreslonym temacie
  85.  
  86.  
  87. localhost = '127.0.0.1'
  88. port = 8888
  89. timeout = 60
  90. topic = "sense/temperatura"
  91.  
  92. def on_connect(client, userdata, flags, rc):
  93. print("Polaczona; Result code: "+str(rc))
  94. client.subscribe(topic)
  95.  
  96. def on_message(client, userdata, msg):
  97. print(msg.topic+": "+str(msg.payload))
  98.  
  99. client = mqtt.Client()
  100.  
  101. client.connect(localhost, port, timeout)
  102.  
  103. client.on_connect = on_connect
  104. client.on_message = on_message
  105.  
  106. client.loop_forever()
  107.  
  108.  
  109. ####
  110. ####
  111. ###
  112. ### sub_wilgot.py
  113.  
  114.  
  115. import paho.mqtt.client as mqtt
  116.  
  117. # Sybskrybent nasluchuje wiadomosci o okreslonym temacie
  118.  
  119.  
  120. localhost = '127.0.0.1'
  121. port = 8888
  122. timeout = 60
  123. topic = "sense/wilgotnosc"
  124.  
  125. def on_connect(client, userdata, flags, rc):
  126. print("Polaczona; Result code: "+str(rc))
  127. client.subscribe(topic)
  128.  
  129. def on_message(client, userdata, msg):
  130. print(msg.topic+": "+str(msg.payload))
  131.  
  132. client = mqtt.Client()
  133.  
  134. client.connect(localhost, port, timeout)
  135.  
  136. client.on_connect = on_connect
  137. client.on_message = on_message
  138.  
  139. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement