8000 Added blynktimer example · dfromeror/lib-python@96b7dad · GitHub
[go: up one dir, main page]

Skip to content

Commit 96b7dad

Browse files
author
amorozenko
committed
Added blynktimer example
1 parent d6f8b40 commit 96b7dad

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Examples can be found **[here][blynk-py-examples]** Check them all to get famili
159159
- [05_set_property_notify.py](https://github.com/blynkkk/lib-python/blob/master/examples/05_set_property_notify.py): How to change some of widget UI properties like colors, labels, etc
160160
- [06_terminal_widget.py](https://github.com/blynkkk/lib-python/blob/master/examples/06_terminal_widget.py): Communication between hardware and app through Terminal widget)
161161
- [07_tweet_and_logging.py](https://github.com/blynkkk/lib-python/blob/master/examples/07_tweet_and_logging.py): How to post to Twitter and log events from your hardware
162+
- [08_blynk_timer.py](https://github.com/blynkkk/lib-python/blob/master/examples/08_blynk_timer.py): How send data periodically from hardware by using **[Blynk Timer][blynktimer-doc]**
162163

163164
##### Raspberry Pi (any):
164165
Read [Raspberry Pi guide](https://github.com/blynkkk/lib-python/tree/master/examples/raspberry) first.
@@ -244,4 +245,4 @@ This project is released under The MIT License (MIT)
244245
[micropython-pkg]: https://github.com/micropython/micropython/wiki/Getting-Started
245246
[virtual-env]: https://virtualenv.pypa.io/en/latest/installation/
246247
[esp8266-readme]: https://github.com/blynkkk/lib-python/blob/master/examples/esp8266/README.md
247-
[blynktimer-doc]: https://github.com/blynkkk/lib-python/blob/master/blynktimer.py
248+
[blynktimer-doc]: https://github.com/blynkkk/lib-python/blob/master/TIMERS.md

examples/08_blynk_timer.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
[BLYNK TIMER EXAMPLE] ============================================================================================
3+
4+
Environment prepare:
5+
In your Blynk App project:
6+
- add "SuperChart" widget
7+
- add two data streams for it ( stream #1 - Virtual Pin 8, stream #2 - Virtual Pin 9)
8+
- set different colors for data stream (ex. red and orange)
9+
- Run the App (green triangle in the upper right corner).
10+
- define your auth token for current example and run it
11+
12+
13+
This started program will register two timers that will update Virtual Pins data after defined intervals.
14+
In app you can see on graph both pins data change. Additionally timers will print new pin values info to stdout.
15+
16+
Schema:
17+
=================================================================================================================
18+
+-----------+ +--------------+ +--------------+
19+
| | | | | |
20+
| blynk lib | | blynk server | | blynk app |
21+
| | | virtual pin | | |
22+
| | | | | |
23+
+-----+-----+ +------+-------+ +-------+------+
24+
| | |
25+
| | |
26+
| | |
27+
| | |
28+
+------------+ | |
29+
| +---------+ | |
30+
| | | update virtual pin 8 value | |
31+
| | | | notify app about vpin 8 update |
32+
| +-------->------------------------------------->+ |
33+
| | +----------------------------------->+
34+
+----------->------------------------------------->+ |
35+
| +----------------------------------->+
36+
| update virtual pin 9 value | |
37+
| | notify app about vpin 9 update |
38+
| | |
39+
| | |
40+
| | |
41+
+ + +
42+
43+
================================================================================================================
44+
Additional blynk info you can find by examining such resources:
45+
46+
Downloads, docs, tutorials: https://blynk.io
47+
Sketch generator: http://examples.blynk.cc
48+
Blynk community: http://community.blynk.cc
49+
Social networks: http://www.fb.com/blynkapp
50+
http://twitter.com/blynk_app
51+
====================================================================================================
52+
"""
53+
54+
import blynklib
55+
import blynktimer
56+
import random
57+
58+
BLYNK_AUTH = 'YourAuthToken'
59+
blynk = blynklib.Blynk(BLYNK_AUTH)
60+
timer = blynktimer.Timer()
61+
62+
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_WRITE] Pin: V{} Value: '{}'"
63+
64+
65+
# register two timers for different pins with different intervals
66+
# first num is delay interval in seconds
67+
# second num is function argument
68+
# run_once flag that allows run timers once or periodically
69+
@timer.register(4, 8, run_once=False)
70+
@timer.register(10, 9, run_once=False)
71+
def write_to_virtual_pin(pin_num):
72+
value = random.randint(0, 20)
73+
print(WRITE_EVENT_PRINT_MSG.format(pin_num, value))
74+
blynk.virtual_write(pin_num, value)
75+
76+
77+
while True:
78+
blynk.run()
79+
timer.run()

0 commit comments

Comments
 (0)
0