-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy path02_read_virtual_pin.py
80 lines (66 loc) · 4.31 KB
/
02_read_virtual_pin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
[READ VIRTUAL PIN EXAMPLE] ========================================================================
Environment prepare:
In your Blynk App project:
- add "Value Display" widget,
- bind it to Virtual Pin V11,
- set values range 0-255
- set the read frequency to 5 second.
- optionally to have more visibility you can add "LED" widget and assign Virtual Pin V11 to it
- Run the App (green triangle in the upper right corner).
- define your auth token for current example and run it
This started program will periodically call and execute event handler "read_virtual_pin_handler".
Calling virtual_write operation inside handler updates widget value.
In app you can see updated values and optionally LED brightness change.
Schema:
=====================================================================================================
+-----------+ +--------------+ +--------------+
| | | | | |
| blynk lib | | blynk server | | blynk app |
| | | virtual pin | | |
| | | | | |
+-----------+ +--------------+ +--------------+
| | |
| | |
| | widget read frequency = 5 sec |
| +<-----------------------------------+
| | |
| | |
| | send virtual pin value to widget |
| | |
event handler | read event to hw from server +----------------------------------->+
(user function) | | |
+-----------<------------------------------------+ |
| | | |
| | write random 0-255 value to pin | |
+--------->------------------------------------->+ next widget read event |
| | |
| +<-----------------------------------+
| | |
| | |
+ + +
====================================================================================================
Additional info about blynk you can find by examining such resources:
Downloads, docs, tutorials: https://blynk.io
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Social networks: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
=====================================================================================================
"""
import blynklib
import random
BLYNK_AUTH = 'YourAuthToken'
# initialize blynk
blynk = blynklib.Blynk(BLYNK_AUTH)
READ_PRINT_MSG = "[READ_VIRTUAL_PIN_EVENT] Pin: V{}"
# register handler for virtual pin V11 reading
@blynk.handle_event('read V11')
def read_virtual_pin_handler(pin):
print(READ_PRINT_MSG.format(pin))
blynk.virtual_write(pin, random.randint(0, 255))
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()