- Platform information:
- Hardware: RPi2
- OS: Openhabian
- Java Runtime Environment: Whatever comes with Openhabian
- openHAB version: 2.0.0, 2017-12-18T00:35:32
- Issue of the topic: Two issues, please be kind to the new guy
I’ve setup the MQTT Binding and can publish/subscribe via an extra terminal session to many topics. I’ve created three things in my items file. One should be a simple switch, a Temperature sensor and an Item with multiple states.
First problem: The “simple” switch:
I am able to send the ON/OFF state from Openhab to the topic and see the message, however, If I publish from a console to the same topic, the dashboard (Basic or Classic UI) does not update the state. I’ve tried several things such as setting the variable to command or state.
Second problem: Item with multiple states:
I purchased an HDMI switch with a serial/RS232 port on it. My thought is to have OpenHAB be able to set the right input on the HDMI switch based on what input I wish to use. The serial communication is pretty simple, I just need to send "swi + " to the console. I have that part working and publishing to an MQTT topic. OpenHAB can see the port it’s on whether I publish to the topic, or I’ve also used the RPi’s GPIO to wire up a few buttons to essentially publish a message that the HDMI switch is listening for. See my buggy python script below.
I’ll attach my Items, Sitemap, Map and Python file for review. I figure the “simple switch” problem is something I’m doing wrong in the items file. For the HDMI switch, I don’t even know how to approach how to send to the MQTT topic one of eight items. Essentially is there an item that permits a drop-down list to set something?
I have not yet defined any Rules, but I know there is an Alexa binding. Ultimately I want to be able to walk into the room and say something to the sort of “Alexa, let’s play PS4” and it will turn on my Roku TV, set the right HDMI input on the TV and set the right output of my HDMI switcher.
I found a python library for Roku to turn on and set inputs, volume and all that stuff, I did not see a binding for it yet, but I think I can probably get it to leverage MQTT as well.
Items:
Switch SmartPlug "Lamp [%s]" <poweroutlet> {mqtt=">[mqtt:sunroom/plug1:command:*:default],<[mqtt:/sunroom/plug1:command:default]"}
Number Temperature "Room Temperature [%.1f]" {mqtt="<[mqtt:sunroom/temp:state:default]"}
String Gaming "Playing [%s]" <sofa> {mqtt="<[mqtt:sunroom/switch:state:MAP(gaming.map)]"}
Sitemap:
sitemap default label="Elder Home"
{
Switch item=SmartPlug label="Lamp"
Text item=Temperature
Text item=Gaming
}
Map:
swi01=PS3
swi02=PS4
swi03=XBOX 360
swi04=Nintendo Wii
swi05=Nintendo Switch
swi06=Super Nintendo Classic
swi07=Nintendo Classic
swi08=RCA Jacks
Still working out the bugs Python file:
import time
import serial
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
SWI01 = [17, 1]
SWI02 = [23, 2]
# SWI03 = [22, 3]
# SWI04 = [23, 4]
SWI05 = [24, 5]
# SWI06 = [25, 6]
# SWI07 = [26, 7]
# SWI08 = [27, 8]
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
topic = "sunroom/switch"
# GPIO Callbacks
def port1_callback(channel):
client.publish(topic, "swi01")
def port2_callback(channel):
client.publish(topic, "swi02")
def port5_callback(channel):
client.publish(topic, "swi05")
# GPIO Button Interrupts
GPIO.add_event_detect(SWI01[0], GPIO.FALLING, callback=port1_callback, bouncetime=300)
GPIO.add_event_detect(SWI02[0], GPIO.FALLING, callback=port2_callback, bouncetime=300)
GPIO.add_event_detect(SWI05[0], GPIO.FALLING, callback=port5_callback, bouncetime=300)
# MQTT Callbacks
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker")
global Connected
Connected = True
else:
print("Connection failed")
def on_message(client, userdata, message):
output = str(message.payload.decode("utf-8"))
print("received message = {}".format(output))
data = "{}\r".format(output)
ser.write(data.encode())
Connected = False
client = mqtt.Client("P1")
client.connect("localhost")
client.on_connect = on_connect
client.on_message = on_message
client.loop_start()
ser = serial.Serial("/dev/ttyUSB0")
ser.baudrate = 19200
while Connected != True: #Wait for MQTT connection
time.sleep(0.1)
client.subscribe(topic)
print("Subcribed to {}".format(topic))
try:
print("Program is Running")
time.sleep(1000)
except KeyboardInterrupt:
GPIO.cleanup()
client.disconnect()
client.loop_stop()
finally:
GPIO.cleanup()