I'm new on Python and also on MQTT. I was able to write some Python working code to publish connected sensors data to a broker, now I'd like to subscribe to more than one topic and write all the payloads on an LCD 20x4 connected to a raspberry.
Actually I wrote a Python to subscribe on one topic and write the payload on the LCD; no problem, it's working. No way to include other topics in the same Python and write the payload on the LCD.
Following you can find my working Python code for "just" ONE topic.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# RPi-S-LCD.py
# Created on 8/1/2017
#
# Python program that write payloads on LCD 20x4
#
import time
import sys
import os
import paho.mqtt.client as paho
sys.path.append("/home/pi/Adafruit_Python_CharLCD")
import Adafruit_CharLCD as LCD
import Adafruit_GPIO.MCP230xx as MCP
### Define MCP pins connected to LCD
lcd_rs = 6
lcd_en = 4
lcd_d4 = 3
lcd_d5 = 2
lcd_d6 = 1
lcd_d7 = 0
lcd_backlight = None
### Define LCD type
lcd_columns = 20
lcd_rows = 4
### Initialize MCP23017 for LCD
gpiomcp = MCP.MCP23017(0x20, busnum=1)
### Initialize LCD panel parameters
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight, gpio=gpiomcp)
lcd.show_cursor(False)
lcd.blink(False)
lcd.clear()
### Read data from MQTT broker
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed to the broker "+str(mid)+" "+str(granted_qos))
### Print topic data to LCD
def on_message1(client, userdata, msg):
lcd.set_cursor(0,0)
lcd.message(msg.payload)
client = paho.Client()
client.on_subscribe = on_subscribe
client.on_message = on_message1
client.connect("10.0.2.10", 1883, 30)
client.subscribe("iDomus/Time",2)
client.loop_forever()
client.subscribe("...",2)
line with the required topic and and the message will be handled by the sameon_message1
function. – hardillb