Publish MQTT temp from Raspberry Pi to Domoticz

Python and python framework

Moderator: leecollings

Post Reply
atatistcheff
Posts: 17
Joined: Sunday 19 June 2016 18:16
Target OS: Linux
Domoticz version: 3.8153
Contact:

Publish MQTT temp from Raspberry Pi to Domoticz

Post by atatistcheff »

I am attempting to use a temperature sensor with a Raspberry Pi to send via MQTT to Mosquitto on another Pi where Domoticz is running.

First of all, this works fine when using a hacked Sonoff (tasmota) switch. Attaching the temp sensor (DHT21) and configuring MQTT the temp works perfectly. So I know my MQTT setup is correct. The issue seems to be syntax in the MQTT message. I've attached the Domoticz log with maximum debugging and you can see two messages. The second is the invalid message from my Python script. The first is the valid message (with an empty temperature) from the Sonoff.

Right off the bat I see three things:
1. The invalid message has a space between the colon and the parameter.
2. The invalid message has the items in a different order.
3. The invalid message does not put the temp reading in quotes.

This is a two part question. The first part is - what is causing Domoticz not to like this MQTT message? For the second part I'm going to post the section of my Python script that generates this message. I'm using the paho_mqtt library to generate the message.

The second part of my question is how do I change the way my message is constructed to make Domoticz happy?

Here is the Python

Code: Select all

        
        temp_data = {'idx':deviceIDX, 'nvalue':0, 'svalue':round(temperature, decim_digits)}
        client.publish(topic, json.dumps(temp_data))
Let me know if I need to clarify further.

Thanks!
Attachments
domotics-log.png
domotics-log.png (70.84 KiB) Viewed 3683 times
Last edited by atatistcheff on Saturday 24 March 2018 22:39, edited 1 time in total.
atatistcheff
Posts: 17
Joined: Sunday 19 June 2016 18:16
Target OS: Linux
Domoticz version: 3.8153
Contact:

Re: Help with Python MQTT error

Post by atatistcheff »

Well I was going to give up and wait for a response to this question but I thought I'd try one more thing. The most likely issue seemed to be the lack of quotes around the temperature. So I changed the data type in the script from float or whatever the Adafruit library returns to a string. This added quotes into the MQTT message automagically and it now works. So here's the modified Python if anyone is interested.

Code: Select all

        temperature = round(temperature, decim_digits)
        str_temp = str(temperature)
        temp_data = {'idx':deviceIDX, 'nvalue':0, 'svalue':str_temp}
        client.publish(topic, json.dumps(temp_data))
atatistcheff
Posts: 17
Joined: Sunday 19 June 2016 18:16
Target OS: Linux
Domoticz version: 3.8153
Contact:

Re: Help with Python MQTT error

Post by atatistcheff »

Here is the finished script. It is a one shot script which is scheduled from cron to run every 10 minutes.

Code: Select all

#!/usr/bin/env python2

import paho.mqtt.client as mqtt
import time
import Adafruit_DHT
import json

sensor = Adafruit_DHT.DHT22
pin = 10
topic = 'domoticz/in'
decim_digits = 2
sleep_time = 10
deviceIDX = 62
mqtt_svr = '10.0.0.9'


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected to " + mqtt_svr + " with result code {}".format(rc))

client = mqtt.Client()
client.on_connect = on_connect
client.connect(mqtt_svr,1883,60)
client.loop_start()

print('Publishing temperature and humidity')
while True:

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        temperature = round(temperature, decim_digits)
        humidity = round(humidity, decim_digits)
        str_temp = str(temperature)
        str_humidity = str(humidity)
        temp_hum = str_temp + ';' + str_humidity + ';' + '0;'
        publish_data = {'idx':deviceIDX, 'nvalue':0, 'svalue':temp_hum}
        client.publish(topic, json.dumps(publish_data))

        print('Published!')
        break
    else:
        print('Failed to get temperature reading. Pause and retry.')
        time.sleep(sleep_time)


print('Finished!')
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest