Subscribing to more than one topic

69 views
Skip to first unread message

Mickey

unread,
May 17, 2017, 9:01:10 AM5/17/17
to MQTT
I'm having trouble getting a Raspberry Pi Zero W to subscribe to more than one topic. I'm reading from 2 temperature sensors connected to a Pi Zero W, for now it reads the same sensor twice as I'd need to buy another ADC chip which has a different part number because the I2C address is fixed in each one! Anyway the publisher side works fine, the main point is in the subscriber end where it doesn't appear to try and make a connection to the broker to subscribe to the 1st topic (or top temperature sensor in my application, after successfully printing the 2nd topic (bottom temperature sensor).

I've tried changing the order of the statements at the bottom of the subscribe code, but will only report back either the 1st or 2nd topic.

I don't even know if more than one topic can be subscribed to, but I would think that's fundamental to MQTT, as it's supposed to give a network of 'points' or 'nodes' to relay data around! I guess I don't know enough about using python for MQTT, as it seems the problem is in the way I'm trying to program the last set of lines in the subscribe code.

All the code for the sensor, publisher and subscriber is below:

Sensor ADC:
import smbus
import time
#from py7seg import Py7Seg # xxx

class MCP3021():
   
VINmax = 3.3
    bus
= smbus.SMBus(1)
   
   
def __init__(self, address):
       
self.address = address
   
   
def setVINmax(self, v):
       
self.VINmax = v
   
   
def readRaw(self):
       
# Reads word (16 bits) as int
        rd
= self.bus.read_word_data(self.address, 0)
       
# Exchanges high and low bytes
        data
= ((rd & 0xFF) << 8) | ((rd & 0xFF00) >> 8)
       
# Ignores two least significiant bits
       
return data >> 2
   
   
def getValue(self):
       
return float(self.VINmax) * self.readRaw() / 1023.0ode here...

Publish:
import time
import paho.mqtt.client as mqtt
from MCP3021A5_2_1 import MCP3021

topTank1address
= 0x4D
topTank1sensor
= MCP3021(topTank1address)
bottomTank1address
= 0x4D
bottomTank1sensor
= MCP3021(bottomTank1address)
client
= mqtt.Client()
client
.connect("192.168.1.102", 1883, 60)
while True:
    vTop
= topTank1sensor.getValue()
   
#w = "%4.3f" %v
    tempTopTank1
= round((vTop / 1.65) * 25, 0)
    hall
= tempTopTank1
    client
.publish("topTemperature",hall)
   
print(hall)
    vBottom
= bottomTank1sensor.getValue()
   
#w = "%4.3f" %v
    tempBottomTank1
= round((vBottom / 1.65) * 25, 0)
    hall
= tempBottomTank1
    client
.publish("bottomTemperature",hall)
   
print(hall)
    time
.sleep(10)

Subscribe:
import paho.mqtt.client as mqtt

class StorageTank():
        temperature_top_tank
= 0
        temperature_bottom_tank
= 0

       
def on_connect_top(self, client, userdata, flags, rc):  
               
print("Connected with result code "+str(rc))
                client
.subscribe("topTemperature")

       
def on_message_top(self, client, userdata, msg):  
               
self.temperature_top_tank = str(msg.payload)
               
self.temperature_top_tank = self.temperature_top_tank[2:(len(self.temperature_top_tank)-1)]
               
self.temperature_top_tank = float(self.temperature_top_tank)
               
print("Top",self,"is: ",self.temperature_top_tank)
       
       
def on_connect_bottom(self, client, userdata, flags, rc):  
               
print("Connected with result code "+str(rc))
                client
.subscribe("bottomTemperature")

       
def on_message_bottom(self, client, userdata, msg):  
               
self.temperature_bottom_tank = str(msg.payload)
               
self.temperature_bottom_tank = self.temperature_bottom_tank[2:(len(self.temperature_bottom_tank)-1)]
               
self.temperature_bottom_tank = float(self.temperature_bottom_tank)
               
print("Bottom",self,"is: ",self.temperature_bottom_tank)

tank_1
= StorageTank()

client
= mqtt.Client()
client
.on_connect = tank_1.on_connect_top
client
.on_message = tank_1.on_message_top
client
.connect("192.168.1.102", 1883, 60)
client
.on_connect = tank_1.on_connect_bottom
client
.on_message = tank_1.on_message_bottom
client
.connect("192.168.1.102", 1883, 60)
client
.loop_forever()

Thanks in advance for your help!

Reply all
Reply to author
Forward
0 new messages