|
| 1 | +""" |
| 2 | +[VIRTUAL PIN SYNC EXAMPLE] ========================================================================================== |
| 3 | +
|
| 4 | +Environment prepare: |
| 5 | +In your Blynk App project: |
| 6 | + - add 3 "Button" widgets, |
| 7 | + - bind then to Virtual Pins V0, V1, v2 |
| 8 | + - set mode "SWITCH" for all of them |
| 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 restore on connect write_virtual_pin states and colors. |
| 14 | +Buttons states and colors can be modified during script run. |
| 15 | +If script was interrupted (KeyboardInterrupt) buttons colors will be changed to red. |
| 16 | +During next connect event ( script re-run) previous buttons states and colors will be restored. |
| 17 | +
|
| 18 | +Schema: |
| 19 | +===================================================================================================================== |
| 20 | + +-----------+ +--------------+ +--------------+ |
| 21 | + | | | | | | |
| 22 | + | blynk lib | | blynk server | | blynk app | |
| 23 | + | | | virtual pin | | | |
| 24 | + | | | | | | |
| 25 | + +-----+-----+ +------+-------+ +-------+------+ |
| 26 | +connect handler | | | |
| 27 | + +--------+ | | |
| 28 | + | +------------------------------------>+ | |
| 29 | + | | virtual pin sync | | |
| 30 | + | +<------------------------------------+----------------------------------->+ |
| 31 | + | | virtual pin write stored value | send pin value to app | |
| 32 | + | +<------------------------------------+----------------------------------->+ |
| 33 | + | | virtual pin apply stored properties | send pin properties to app | |
| 34 | + +------->+ | | |
| 35 | + write handler | | | |
| 36 | + +--------+ +<-----------------------------------+ |
| 37 | + | +<------------------------------------+ write event from button | |
| 38 | + +>------>+ write event form server | | |
| 39 | + | | | |
| 40 | + | | | |
| 41 | + disconnect | | | |
| 42 | + handler | | | |
| 43 | + +-------+ | | |
| 44 | + | +------------------------------------>+ | |
| 45 | + +------>+ set new virtual pin property | | |
| 46 | + | | | |
| 47 | + | | | |
| 48 | + | | | |
| 49 | + + + + |
| 50 | +
|
| 51 | +===================================================================================================================== |
| 52 | +Additional info about blynk you can find by examining such resources: |
| 53 | +
|
| 54 | + Downloads, docs, tutorials: https://blynk.io |
| 55 | + Sketch generator: http://examples.blynk.cc |
| 56 | + Blynk community: http://community.blynk.cc |
| 57 | + Social networks: http://www.fb.com/blynkapp |
| 58 | + http://twitter.com/blynk_app |
| 59 | +===================================================================================================================== |
| 60 | +""" |
| 61 | +import logging |
| 62 | +import blynklib |
| 63 | + |
| 64 | +# tune console logging |
| 65 | +_log = logging.getLogger('BlynkLog') |
| 66 | +logFormatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s") |
| 67 | +consoleHandler = logging.StreamHandler() |
| 68 | +consoleHandler.setFormatter(logFormatter) |
| 69 | +_log.addHandler(consoleHandler) |
| 70 | +_log.setLevel(logging.DEBUG) |
| 71 | + |
| 72 | +colors = {'1': '#FFC300', '0': '#CCCCCC', 'OFFLINE': '#FF0000'} |
| 73 | + |
| 74 | +BLYNK_AUTH = 'YourAuthToken' |
| 75 | +blynk = blynklib.Blynk(BLYNK_AUTH, log=_log.info) |
| 76 | + |
| 77 | + |
| 78 | +@blynk.handle_event("connect") |
| 79 | +def connect_handler(): |
| 80 | + _log.info('SCRIPT_START') |
| 81 | + for pin in range(3): |
| 82 | + _log.info('Syncing virtual pin {}'.format(pin)) |
| 83 | + blynk.virtual_sync(pin) |
| 84 | + |
| 85 | + # within connect handler after each server send operation forced socket reading is required cause: |
| 86 | + # - we are not in script listening state yet |
| 87 | + # - without forced reading some portion of blynk server messages can be not delivered to HW |
| 88 | + blynk.read_response(timeout=0.5) |
| 89 | + |
| 90 | + |
| 91 | +@blynk.handle_event('write V*') |
| 92 | +def write_handler(pin, value): |
| 93 | + button_state = value[0] |
| 94 | + blynk.set_property(pin, 'color', colors[button_state]) |
| 95 | + |
| 96 | + |
| 97 | +@blynk.handle_event("disconnect") |
| 98 | +def connect_handler(): |
| 99 | + for pin in range(3): |
| 100 | + _log.info("Set 'OFFLINE' color for pin {}".format(pin)) |
| 101 | + blynk.set_property(pin, 'color', colors['OFFLINE']) |
| 102 | + |
| 103 | + |
| 104 | +########################################################### |
| 105 | +# infinite loop that waits for event |
| 106 | +########################################################### |
| 107 | +try: |
| 108 | + while True: |
| 109 | + blynk.run() |
| 110 | +except KeyboardInterrupt: |
| 111 | + blynk.disconnect() |
| 112 | + _log.info('SCRIPT WAS INTERRUPTED') |
0 commit comments