-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy path05_set_property_notify.py
87 lines (74 loc) · 5.07 KB
/
05_set_property_notify.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
81
82
83
84
85
86
87
"""
[SET_PROPERTY/NOTIFY EXAMPLE] ==========================================================================================
Environment prepare:
In your Blynk App project:
- add "Slider" widget,
- bind it to Virtual Pin V5,
- set values range 0-255
- add "LED" widget and assign Virtual Pin V5 to it
- add "Notification" widget to be allowed receive notifications in App
- 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 "write_virtual_pin_handler".
In app you can move slider that will cause LED brightness change and will send virtual write event
to current running example. Handler will set random color for virtual pin and will send notification
event to App. Virtual pin property 'color' change will cause color changes for "Slider" and "LED" widgets
In App user will get notifications about color change event.
Schema:
=====================================================================================================================
+-----------+ +--------------+ +--------------+
| | | | | |
| blynk lib | | blynk server | | blynk app |
| | | virtual pin | | |
| | | | | |
+-----+-----+ +------+-------+ +-------+------+
| | |
| | write event from "Slider" widget |
| | |
| +<-----------------------------------+
| | |
| | |
| | |
| | |
event handler | write event to hw from server | |
(user function) | | |
+-----------<------------------------------------+ |
| | | |
| | pin set property | pin property changed msg |
+-----+--->------------------------------------->------------------------------------>+
| | | |
| | send notification | notification widget present? |
+--->------------------------------------->------------------------------------>+
| | |
| | yes |
| +<-----------------------------------+
| | |
| | |
| +----------------------------------->+
+ + notification delivery +
=====================================================================================================================
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'
blynk = blynklib.Blynk(BLYNK_AUTH)
NOTIFY_MSG = "['COLOR' = '{}']"
colors = {'#FF00FF': 'Magenta', '#00FF00': 'Lime'}
@blynk.handle_event('write V5')
def write_handler(pin, value):
current_color = random.choice(list(colors.keys()))
blynk.set_property(pin, 'color', current_color)
blynk.notify(NOTIFY_MSG.format(colors[current_color]))
print(NOTIFY_MSG.format(colors[current_color]))
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()