|
| 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