-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy path03_connect_disconnect.py
84 lines (67 loc) · 3.41 KB
/
03_connect_disconnect.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
"""
[CONNECT/DISCONNECT EVENTS EXAMPLE] =================================================================
Environment prepare:
- define your auth token for current example and run it
This started program after successful connect operation will call and execute "connect event handler"
Within handler after short sleep delay blynk disconnect call will be performed that will trigger
"disconnect event handler" execution.
Schema:
=====================================================================================================
+-----------+ +--------------+
| | | |
| blynk lib | | blynk server |
| | | virtual pin |
| | | |
+-----+-----+ +------+-------+
| |
| connect/authenticate request |
+------------------------------------>+
connect handler | |
(user function) | connected successfully |
+-----------<------------------------------------+
| | |
| | disconnect request |
+--------->------------------------------------->+
| |
| |
disconnect handler | |
(user function) | disconnected successfully |
+-----------<------------------------------------+
| | |
| | |
+--------->+ |
| reconnect request |
| performed by lib automatically |
+------------------------------------>+
+ +
====================================================================================================
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 time
BLYNK_AUTH = 'YourAuthToken'
blynk = blynklib.Blynk(BLYNK_AUTH)
CONNECT_PRINT_MSG = '[CONNECT_EVENT]'
DISCONNECT_PRINT_MSG = '[DISCONNECT_EVENT]'
@blynk.handle_event("connect")
def connect_handler():
print(CONNECT_PRINT_MSG)
print('Sleeping 2 sec in connect handler...')
time.sleep(2)
blynk.disconnect()
@blynk.handle_event("disconnect")
def disconnect_handler():
print(DISCONNECT_PRINT_MSG)
print('Sleeping 4 sec in disconnect handler...')
time.sleep(4)
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()