LED colour using Requests (Python)

I am using a Raspberry pi running Python 2.7 I have managed to use the LED widget and changing the intensity using Requests but can’t find any examples of how to change the colour using Requests.

Anybody with examples or ideas?

Similar process but using properties command instead…

http://docs.blynk.cc/#blynk-main-operations-change-widget-properties

Thanks for the reply. I’m making my connections from Python using the Requests lib. I have a two more questions?

1 Do I have to use Requests in Python to connect to Blynk? Can I call another type of function?

2 Are there examples of changing color of an LED using Requests?

1- OK, I honestly don’t know what you are referring to with the the term “Requests” :blush: Just getting my feet wet with Python.

I tried my sugestion above, however it appears that Blynk.setProperty() is not setup in the Alpha version of Blynk Python Library anyhow… and no, I have no idea how to add it :stuck_out_tongue_winking_eye:

2 - No, see above.

As far as I know (advised by a group of Pi users) using the Requests library allows me to send to Blynk server direct from Pyton code. I am successfully sending pin update information to the app and sending emails.

This is the line which turns the LED on the app green the intensity depending on the value of put_data using Request:

r = requests.put(BLYNK_URL+BLYNK_AUTH+’/update/V8’, data=put_body, headers=put_header)

I am looking at changing the color from the default green and understand why it happens. I think the color change has to added to the instruction line above, tried many combinations but failing.

I see… so Requests are akin to Blynks HTTP RESTfull API??

I have never used it… but here is the “instructions” for such…

You might be able to extrapolate from that.

I had thought you meant via a script…

As for python examples, such as it is… This shows Uptime; Turns on and off a physical and virtual LED with a virtual button; Flashes another virtual LED every two seconds; And updates a Value display with a number every five seconds.

BLYNK_AUTH ='xxxxxxxxxx'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

# Register Virtual Pins
@blynk.VIRTUAL_WRITE(1)

def my_write_handler(value):
    if int(value) == 1:
	print('Value: {}'.format(value))
        led.on()
	blynk.virtual_write(0,255)
    else:
	print('Value: {}'.format(value))
        led.off()
	blynk.virtual_write(0,0)

@blynk.VIRTUAL_READ(2)

def my_read_handler():
    # this widget will show some time in seconds..
    blynk.virtual_write(2, time.ticks_ms() // 1000)


    
def LED_ON_task():
    #print('Flash ON')
    blynk.virtual_write(3, 255)
    time.sleep(0.5)
    #print('Flash OFF')    	
    blynk.virtual_write(3, 0)

blynk.set_user_task(LED_ON_task, 2000)



def counter_task(cntr):
    while cntr > 0:
    	blynk.virtual_write(4,cntr)
    	print cntr
    	cntr += 1
    	time.sleep(5)

thread.start_new_thread(counter_task, (1,))


# Start Blynk (this call should never return)
blynk.run()

This makes an web request in Python:

import Adafruit_DHT
import urllib2
#import urllib.request

# Domoticz
sensor_idx  = 1
url_json    = "http://192.168.178.103:8099/json.htm?type=command&param=udevice&i                                                                    dx="

humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)

humidity = float(round(humidity, 2))
temperature = float(round(temperature, 2))

if humidity is not None and temperature is not None:

  url_humidity = 'http://192.168.178.103:8080/<token here>/update/V1?value=%s' % (format(humidity))
  url_temperature = 'http://192.168.178.103:8080/<token here>/update/V2?value=%s' % (format(temperature))

#  urllib.request.urlopen(url_humidity)
#  urllib.request.urlopen(url_temperature)

  urllib2.urlopen(url_humidity)
  urllib2.urlopen(url_temperature)

  # Domoticz
  cmd = url_json  + str(sensor_idx) + "&nvalue=0&svalue=" + str(temperature) + "                                                                    ;" + str(humidity) + ";0"
#  urllib.request.urlopen(cmd)
  urllib2.urlopen(cmd)
else:

  print ('Geen data')

It’s for my DHT sensor, but you’ll get the idea :slight_smile:

Little sidenote: request in python2 and python3 work differently, they changed that lib! urllib2 is for python2 and the other (commented in my script) is for python3.

So haven’t tried this from a python script yet… but the command to change a widgets colour should be something like this

http://xxx.xxx.xxx.xxx:pppp/***AUTH***/update/Vpin?color=%23FFFFFF (Sets the widget colour white - Note: the %23 represents the # normally used for the HEX colour code)

Thanks for all the ideas, back home on Monday, loads to try, I’ll report back

Back on my LED colour discussion.

Before I give up and try a different method can anybody please explain why the following Python Code works to turn on LED V12 with 200 brightness? This is part of my Python 2.7 code that drives the app LED’s with these lines working when Pin = 7. Somebody else wrote this piece of code.

Any idea’s how I add the color attribute to the code below?

import time
import requests
import json

    BLYNK_AUTH = '273d9020516144038ad5331xxxxx'
    BLYNK_URL = 'http://blynk-cloud.com/'
    
    put_header={"Content-Type": "application/json"}
    put_body = json.dumps([val])
        
    if pin ==7:
        
        val = 200
        put_body = json.dumps([val])
        r = requests.put(BLYNK_URL+BLYNK_AUTH+'/update/V12', data=put_body, headers=put_header)

Solved the problem with help from my Pi group last night.

By entering the line:

r = requests.get(BLYNK_URL+BLYNK_AUTH+’/update/V11?color=%230000FF’ (use %23 instead of #)
the above line turns the LED Blue.

Not sure why you send a Brightness as a “put” and a color change as a “get” but it works.

Don’t forget that from the App LED widget you can set the colour of the led by pressing the coloured ring, this will change the colour continuously and then onlyy requires Off or Brightness.

A post was split to a new topic: Please help me with writing pin values of http parsing using blynk api