Python script for full rgb + brightness control of easyESP neopixel

Python and python framework

Moderator: leecollings

Post Reply
stephenmhall
Posts: 13
Joined: Saturday 10 June 2017 22:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Python script for full rgb + brightness control of easyESP neopixel

Post by stephenmhall »

I was trying to control an easyESP neopixel from an RGB switch but the best I could do was switch on a pre defined colour and brightness by sending the Neopixel command from the on/off action command as below.

Code: Select all

http://espeasyIPaddress/control?cmd=NeoPixelAll,255,255,0
http://espeasyIP/control?cmd=NeoPixelAll
I tried some blocky code but could not find out how to get the current colour and brightness levels.

So after some googling I wrote a python script that is run on the on/off command. And it works even better than I hoped, as whenever you make a change on the colour wheel or brightness it also runs the script, it must count as an "on" command on a change.

Here is my script. It is saved in domoticz/scripts/python. Make sure you chmod +x the file so it runs. my file is called owl2.py as it is a nightlight that sits in a printed owl body. The script queries Domoticz to get the current state of the switch by ID (97 here). Parses out the json to get the colours, brightness and the on/off state. Then builds a url string to change colours on the eastESP device. I also have a HA bridge running so this will all work with Alexa.

Code: Select all

#!/usr/bin/env python

import requests
import ast

ID = '97'

domo_url = "http://<username:password>@domoticzIP:port/json.htm?type=devices&rid={}".format(ID)
owl_control_url = 'http://easyESPIPaddress/control?cmd=NeoPixelAll,{},{},{}'

def app():
    data = requests.get(url=domo_url)
    colour_data = ast.literal_eval(data.json()['result'][0]['Color'])
    level_data = data.json()['result'][0]['Level']
    state = data.json()['result'][0]['Data']
    red = int(colour_data["r"])
    green = int(colour_data["g"])
    blue = int(colour_data["b"])
    level = int(level_data)

    redl = int((red/100) * level)
    greenl = int((green/100) * level)
    bluel = int((blue/100) * level)

    if state == 'Off':
        redl = 0
        greenl = 0
        bluel = 0

    #send to light
    _i = requests.get(url=owl_control_url.format(redl,bluel,greenl))


if __name__ == "__main__":
    app()
The onAction command is

Code: Select all

script:///volume1/@appstore/domoticz/scripts/python/owl2.py
stephenmhall
Posts: 13
Joined: Saturday 10 June 2017 22:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Python script for full rgb + brightness control of easyESP neopixel

Post by stephenmhall »

Still playing with this, have updated it to accept sys args so you only need one script.

Code: Select all

#!/usr/bin/env python

import sys
import getopt
import requests
import ast


def app(argv):
    ID = ''
    IP = ''
    try:
        opts, args = getopt.getopt(argv, "hi:e:", ["ID=", "IP="])
    except getopt.GetoptError:
        print("night_light.py -i <Domoticz IDX> -e <easyESP IP last octet>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("night_light.py -i <Domoticz IDX> -e <easyESP IP last octet>")
            sys.exit()
        elif opt in ("-i", "--id"):
            ID = arg
        elif opt in ("-e", "eIP"):
            IP = arg

    try:
        domo_url = "http://<username:password>@domoticzIP:port/json.htm?type=devices&rid={}".format(ID)
        data = requests.get(url=domo_url)
        colour_data = ast.literal_eval(data.json()['result'][0]['Color'])
        level_data = data.json()['result'][0]['Level']
        state = data.json()['result'][0]['Data']
        red = int(colour_data["r"])
        green = int(colour_data["g"])
        blue = int(colour_data["b"])
        level = int(level_data)

        redl = int((red/100) * level)
        greenl = int((green/100) * level)
        bluel = int((blue/100) * level)

        if state == 'Off':
            redl = 0
            greenl = 0
            bluel = 0

        #send to light
        owl_control_url = 'http://easyESPipaddress.{}/control?cmd=NeoPixelAll,{},{},{}'.format(IP, redl, bluel, greenl)
        _i = requests.get(url=owl_control_url)

    except Exception as err:
        print(err)
        sys.exit()


if __name__ == "__main__":
    app(sys.argv[1:])
run the script with IDX and final octet of the easyESP ip address

Code: Select all

script:///volume1/@appstore/domoticz/scripts/python/night_light.py -i 97 -e 124
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: Python script for full rgb + brightness control of easyESP neopixel

Post by vrega »

Hi,i've tried your script.
In the log i've this error:
Error: Error executing script command (/opt/domoticz/scripts/python/ledstrip.py). returned: 256

If i try to launch the script from cli using

python ledstrip.py -i 97 -e 233

it gives me:
Traceback (most recent call last):
File "ledstrip.py", line 5, in <module>
import requests
ImportError: No module named requests

Can you help me to find why it gives me this message?
Thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest