I need to read two sensors and get the result on a pygame window. I have a pi zero running as publisher of the sensors and as broker. It creates two topics, "house" and "heat". On a second pi (jessie on virtualbox) I run the following python script and I get the results expected. Therefore I would ask if my choice of creating two different clients (with two different on_message) is a right choice, in order to get two values from two different topics. Please forgive me for errors in the code or inaccuracies, I'm not expert and I knew Mosquitto only a month ago. Most of this code is a raw code only for testing mqtt. Surfing the web I didn't found practical examples on how to read sensor values and put them in a window (like pygame or Tkinter), without using cloud apps and using python and pi's; does anybody know of such a tutorial? Thanks
import time
import paho.mqtt.client as paho
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((640,480),0,32)
background = pygame.Surface(screen.get_size())
background.fill((0,0,0))
font = pygame.font.SysFont("comicsansms", 72)
broker = "192.168.100.21"
ss=''
tt=''
def omessage(client, userdata, message):
if message.topic=="house":
global ss
ss=(str(message.payload.decode("utf-8")+message.topic))
if message.topic=="heat":
global tt
tt=(str(message.payload.decode("utf-8")+message.topic))
client=paho.Client("cliente-001")
client.on_message=omessage
client.connect(broker)
client.loop_start()
client.subscribe("house")
client.subscribe("heat")
while True:
screen.blit(background, (0,0))
text=font.render(" %s" %ss, True, (0,255,0))
textRect = text.get_rect()
screen.blit(text,textRect)
text1=font.render(" %s" %tt, True, (0,255,0))
text1Rect = text1.get_rect(center=(150,150))
screen.blit(text1,text1Rect)
time.sleep(0.2)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.quit()
sys.exit()
on_message
function – hardillb