|
| 1 | +""" |
| 2 | +[TERMINAL WIDGET ESP32 EXAMPLE] ===================================================================================== |
| 3 | +
|
| 4 | +Environment prepare: |
| 5 | +In your Blynk App project: |
| 6 | + - add "Terminal" widget, |
| 7 | + - bind it to Virtual Pin V2, |
| 8 | + - add input line option in widget settings |
| 9 | + - Run the App (green triangle in the upper right corner). |
| 10 | + - define your auth token for current example |
| 11 | + - define SSID and WiFi password that will be used by ESP32 board |
| 12 | + - run current example |
| 13 | +
|
| 14 | +
|
| 15 | +This started program will call and execute event handler "write_virtual_pin_handler" if new |
| 16 | +event comes from terminal app widget. |
| 17 | +In App Terminal widget you can type commands. |
| 18 | +'help' - info about available commnds |
| 19 | +'logo' - prints blynk library ascii logo |
| 20 | +'version' - prints blynk library version |
| 21 | +'sysinfo' - prints board system info |
| 22 | +'ls' - list target board dir. Example: 'ls /lib'. |
| 23 | + root dir will be listed if no arguments provided |
| 24 | +
|
| 25 | +For any other terminal inputs will be printed error info that provided command is not supported. |
| 26 | +===================================================================================================================== |
| 27 | +Additional info about blynk you can find by examining such resources: |
| 28 | +
|
| 29 | + Downloads, docs, tutorials: http://www.blynk.cc |
| 30 | + Sketch generator: http://examples.blynk.cc |
| 31 | + Blynk community: http://community.blynk.cc |
| 32 | + Social networks: http://www.fb.com/blynkapp |
| 33 | + http://twitter.com/blynk_app |
| 34 | +===================================================================================================================== |
| 35 | +""" |
| 36 | +import blynklib |
| 37 | +import network |
| 38 | +import uos |
| 39 | +import utime as time |
| 40 | + |
| 41 | +WIFI_SSID = &
8000
#39;YourWifiSSID' |
| 42 | +WIFI_PASS = 'YourWifiPassword' |
| 43 | +BLYNK_AUTH = 'YourAuthToken' |
| 44 | + |
| 45 | +print("Connecting to WiFi network '{}'".format(WIFI_SSID)) |
| 46 | +wifi = network.WLAN(network.STA_IF) |
| 47 | +wifi.active(True) |
| 48 | +wifi.connect(WIFI_SSID, WIFI_PASS) |
| 49 | +while not wifi.isconnected(): |
| 50 | + time.sleep(1) |
| 51 | + print('WiFi connect retry ...') |
| 52 | +print('WiFi IP:', wifi.ifconfig()[0]) |
| 53 | + |
| 54 | +print("Connecting to Blynk server...") |
| 55 | +blynk = blynklib.Blynk(BLYNK_AUTH) |
| 56 | + |
| 57 | +CMD_LIST = ['logo', 'version', 'sysinfo', 'ls'] |
| 58 | + |
| 59 | + |
| 60 | +@blynk.handle_event('write V2') |
| 61 | +def write_handler(pin, values): |
| 62 | + if values: |
| 63 | + in_args =<
8000
/span> values[0].split(' ') |
| 64 | + cmd = in_args[0] |
| 65 | + cmd_args = in_args[1:] |
| 66 | + |
| 67 | + if cmd == 'help': |
| 68 | + output = ' '.join(CMD_LIST) |
| 69 | + elif cmd == CMD_LIST[0]: |
| 70 | + output = blynklib.LOGO |
| 71 | + elif cmd == CMD_LIST[1]: |
| 72 | + output = blynklib.__version__ |
| 73 | + elif cmd == CMD_LIST[2]: |
| 74 | + output = uos.uname() |
| 75 | + elif cmd == CMD_LIST[3]: |
| 76 | + arg = cmd_args[0] if cmd_args else '' |
| 77 | + output = uos.listdir(arg) |
| 78 | + else: |
| 79 | + output = "[ERR]: Not supported command '{}'".format(values[0]) |
| 80 | + |
| 81 | + blynk.virtual_write(pin, output) |
| 82 | + blynk.virtual_write(pin, '\n') |
| 83 | + |
| 84 | + |
| 85 | +while True: |
| 86 | + blynk.run() |
0 commit comments