-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy path07_tweet_and_logging.py
94 lines (79 loc) · 5.4 KB
/
07_tweet_and_logging.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
88
89
90
91
92
93
94
"""
[TWEET AND LOGGING EXAMPLE] =========================================================================================
Environment prepare:
In your Blynk App project:
- add "Slider" widget,
- bind it to Virtual Pin V7,
- set values range 0-255
- add "Twitter Settings" widget
- connect via this widget to your twitter account.
- 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 send virtual write event to current running example.
Handler will log to console this event and additionally will send tweet message with pin number and it's updated value.
Schema:
=====================================================================================================================
+-----------+ +--------------+ +--------------+ +---------+
| | | | | | | |
| blynk lib | | blynk server | | blynk app | | twitter |
| | | virtual pin | | | | |
| | | | | | | |
+-----+-----+ +------+-------+ +-------+------+ +----+----+
| | | tweet widget |
| | | auth |
| | +--------------->+
| | | ok |
| | write event from "Slider" widget +<---------------+
| | | |
| +<-----------------------------------+ |
| | | |
event handler | write event to hw from server | | |
(user function) | | | |
+-----------<------------------------------------+ | |
| | | | |
| | msg for tweet widget | | |
+--------->-------------------------------------------------------------------------->+ |
| | | real tweet msg |
| | +--------------->+
| | | |
+ + + +
=====================================================================================================================
Additional blynk info 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
=====================================================================================================================
"""
# uncomment line below if simple printing will be used instead of logging
# from __future__ import print_function
import blynklib
import logging
# tune console logging
_log = logging.getLogger('BlynkLog')
logFormatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
_log.addHandler(consoleHandler)
_log.setLevel(logging.DEBUG)
# uncomment line below if simple printing will be used instead of logging
# _log.info = print
BLYNK_AUTH = 'YourAuthToken'
blynk = blynklib.Blynk(BLYNK_AUTH, log=_log.info)
# uncomment line below if simple printing will be used instead of logging
# blynk = blynklib.Blynk(BLYNK_AUTH, log=print)
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
TWEET_MSG = "New value='{}' on VPIN({})"
# register handler for all virtual pins (including V7).
# So in one block here we can handle evens for vpins 0-32 (32 - Max allowed VPIN number in lib)
@blynk.handle_event('write V*')
def write_virtual_pin_handler(pin, value):
_log.info(WRITE_EVENT_PRINT_MSG.format(pin, value))
blynk.tweet(TWEET_MSG.format(pin, value))
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()