Communicating with running Python Script

  • Platform information:
    • Hardware: Raspberrypi 2 B+
    • OS: Raspbian
    • Java Runtime Environment: 1.8.0_65
    • openHAB version: 2.3.0-1
  • Issue of the topic:
    Hello I want to control my WS2812 with openhab. At the moment I use the SPI pin to send the data to the LEDs. For this I use a Python script wich is triggerd by a Switch and the Colorpicker to change color and dim the LEDs. My first solution was without dimming and just execute the Pythonscript with arguments for the RGB data. For this I use the neopixel library for the rpi. Now comes my problem: When I have to start the pythonscript with every Trigger event, I have to initialize the LEDs and the current state of the LED data get lost. This is really anoying when I just want to dim the LEDs. Now was my idea to make a pythonscript wich takes commands while running. But I don’t know how to get the commands to the running script when an event happens.
    So Basicly the idea was the Switch trigger ON starts the Pythonscript which keeps running. When I change the color a command is send with the new RGB data. When I want to dim a command is send with the direction.
    Is there any possibility to get Commands to a running Pythonscript with rules?
  • Please post configurations (if applicable):
    • Items
      1 Switch WS2812On “ON/OFF”
      2 Color WS2812Color “Color”

    • Sitemap
      1 sitemap ws2812 label=“WS2812 RGB LED Strup”
      2 {
      3 Switch item=WS2812On label=“led on/off”
      4 Colorpicker item=WS2812Color label=“LED Color”
      5 }

    • Rules
      1 rule “WS2812OFF”
      2 when Item WS2812On changed from OFF to ON
      3 then
      4 executeCommandLine(‘python /etc/openhab2/scripts/LED_Control.py’)
      5 end
      6
      7 rule “WS2812 Change”
      8 when
      9 Item WS2812Color received command
      10 then
      11 if (receivedCommand instanceof HSBType)
      12 {
      13 val red = ((WS2812Color.state as HSBType).red * 2.55)
      14 val green = ((WS2812Color.state as HSBType).green * 2.55)
      15 val blue = ((WS2812Color.state as HSBType).blue *2.55)
      16
      17 print(“changeColor \n”)
      18 print(red.toString + “\n”)
      19 print(green.toString + “\n”)
      20 print(blue.toString + “\n”)
      21 } else if (receivedCommand == ON) {
      22 executeCommandLine("python /etc/openhab2/scripts/LED_Control.py " + “dim” + " " + 0 + " " + “up”)
      23 } else if (receivedCommand == OFF) {
      24 executeCommandLine("python /etc/openhab2/scripts/LED_Control.py " + “dim” + " " + 0 + " " + “down”)
      25 }
      26 end

Yes, you can use MQTT

See https://github.com/rkoshak/sensorReporter for an example of using MQTT with a python script.

The key is that python runs as a service external to oh and you send messages to it.

I run my python apps as docker images, and communicate them using the HTTP binding.