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

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

logger = logging.getLogger(os.path.basename(__file__))
logger.debug('Starting...')

""" hostname and topic of MQTT """
localBroker = "130.159.179.161" # Local MQTT broker
localPort = 1883 # Local MQTT port
keepalive=60
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
#client.publish(localTopic,read_temp())

# Disconnect from MQTT_Broker
client.disconnect()



""" 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)
else:
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()
f.close()
return lines


def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
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))
try:
publish.single(localTopic, payload=payload, localBroker=localBroker)
except:
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())
time.sleep(2)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
pi@raspberrypi:~/Desktop/MQTT $ python pub_temp_mqtt2.py
09-24 02:55 pub_temp_mqtt2.py DEBUG Starting...
09-24 02:55 pub_temp_mqtt2.py DEBUG Autodetected device: /sys/bus/w1/devices/28-00000802298c
09-24 02:55 pub_temp_mqtt2.py DEBUG sending localTopic=/office_temperature payload=21.937
09-24 02:55 pub_temp_mqtt2.py ERROR Failed to publish message, details follow
09-24 02:55 pub_temp_mqtt2.py ERROR localBroker=130.159.179.161 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 Apr 24 17:27:10 GMT 2024

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

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

Back to the top