Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » MQTT(mosquitto-paho) does not published temperature data
MQTT(mosquitto-paho) does not published temperature data [message #1744919] Sun, 02 October 2016 19:16
Jwaad Malik is currently offline Jwaad MalikFriend
Messages: 1
Registered: October 2016
Junior Member
Hi all
• I started to learn MQTT(mosquitto) with python program (paho). I have 2 raspberry pi3 and temperature device sensor (DS18B20 sensor -the waterproof version). I tested the subscribe and publish messages by commands on 2 terminals and it's work fine.
• So, I configured temperature sensor on( let's name it Pi_1) I supposed Pi_1 as the client. And the mosquito broker is installed on Pi_2.
• I tried to run the code attached to published data temperature from Pi_1 to broker wherein Pi_2, but unfortunately, I failed to connect to the broker.
•could you please help me why this function "publish.single(localTopic, payload=payload, localBroker=localBroker)" does not published the data which is in "def send_message(localTopic, payload): "method?
Please see the code and the result.
import os
import time
import logging
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')

logger = logging.getLogger(os.path.basename(__file__))

""" hostname and topic of MQTT """
localBroker = "" # Local MQTT broker
localPort = 1883 # Local MQTT port
localTopic = "/office_temperature" # Local MQTT topic to monitor

# Define on_publish event function
def on_publish(client, userdata, mid):
print "Message Published..."

# Initiate MQTT Client
client = mqtt.Client("temp_client1")

# Register publish callback function
client.on_publish = on_publish

# Connect with MQTT Broker
client.connect(localBroker, localPort, keepalive)

# Publish message to MQTT Broker

# Disconnect from MQTT_Broker

""" Optional one wire device name """
w1_device = os.getenv('W1_DEVICE', None)

base_dir = '/sys/bus/w1/devices/'
if w1_device:
device_folder = base_dir + w1_device
logger.debug("Using device from environment: %s" % device_folder)
import glob
device_folder = glob.glob(base_dir + '28*')[0]
logger.debug("Autodetected device: %s" % device_folder)

device_file = device_folder + '/w1_slave'

def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
return temp_c

def send_message(localTopic, payload):
logger.debug("sending localTopic=%s payload=%s" % (localTopic, payload))
publish.single(localTopic, payload=payload, localBroker=localBroker)
logger.error("Failed to publish message, details follow")
logger.error("localBroker=%s localTopic=%s payload=%s" % (localBroker, localTopic, payload))

if __name__ == "__main__":

while True:
send_message(localTopic, read_temp())
pi@raspberrypi:~/Desktop/MQTT $ python
09-24 02:55 DEBUG Starting...
09-24 02:55 DEBUG Autodetected device: /sys/bus/w1/devices/28-00000802298c
09-24 02:55 DEBUG sending localTopic=/office_temperature payload=21.937
09-24 02:55 ERROR Failed to publish message, details follow
09-24 02:55 ERROR localBroker= localTopic=/office_temperature payload=21.937

Previous Topic:Mqtt websocket using c++ release plan
Next Topic:Client is not publishing in paho C
Goto Forum:

Current Time: Wed Oct 28 20:55:19 GMT 2020

Powered by FUDForum. Page generated in 0.01213 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top