Hi
Below is a working script.
The script is execute about 8 seconds. Can this time be shortened?
Code: Select all
#!/usr/bin/python
import json
import urllib2
import requests
import ast
import base64
# Settings for the domoticz server
domoticzserver="192.168.1.2:8080"
domoticzusername = "pi"
domoticzpassword = "raspi"
domoticzpasscode = ""
base64string = base64.encodestring('%s:%s'% (domoticzusername, domoticzpassword)).replace('\n', '')
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=devices&rid=37'
def domoticzrequest (url):
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
return response.read()
def domoticzstatus ():
json_object = json.loads(domoticzrequest(domoticzurl))
status = 1
return status
def show_data(type):
data = json.loads(domoticzrequest(domoticzurl))
show = data["result"][0][type]
return show
#json_object = json.loads(domoticzrequest(domoticzurl))
#print(json_object)
#print(show_data("Color"))
#print(show_data("Level"))
color= show_data("Color")
print " JSON - Color: ", color
colour_data = ast.literal_eval(color)
red = int(colour_data["r"])
green = int(colour_data["g"])
blue = int(colour_data["b"])
print "RED: ",red
print "GREEN: ",green
print "BLUE: ",blue
level_data = show_data("Level")
level = int(level_data)
print "LEVEL: ",level
redl = int((red /100) * level)
greenl = int((green /100) * level)
bluel = int((blue /100) * level)
print redl
print greenl
print bluel
# send to led strip
from bootstrap import *
led.fill(Color(redl,bluel,greenl),0,10)
led.update()