|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +[Slide Potentiometer NodeMcu ESP8266] =============================================================================== |
| 4 | +
|
| 5 | +ESP8266 pinout: |
| 6 | + https://i.imgur.com/HgVTxCh.png |
| 7 | +
|
| 8 | +Slide Potentiometer: |
| 9 | + - connect VCC sensor line to "+3.3v" board pin |
| 10 | + - connect GND sensor line to "ground" board pin |
| 11 | + - connect DATA sensor line to board A0 pib (ADC - analog to digital conversion on dedicated pin) |
| 12 | +
|
| 13 | +Environment prepare: |
| 14 | +In your Blynk App project: |
| 15 | + - add "Gauge" widget, |
10000
| 16 | + - bind it to Virtual Pin V1 |
| 17 | + - set name "Potentiometer" |
| 18 | + - set label /pin.##/ |
| 19 | + - set update time=1 sec and assign 0-1024 values range to it |
| 20 | +
|
| 21 | +
|
| 22 | + - Run the App (green triangle in the upper right corner). |
| 23 | + - define SSID and WiFi password that will be used by ESP32 board |
| 24 | + - define your auth token for current example and run it |
| 25 | +
|
| 26 | +This started program will periodically call and execute event handler "read_virtual_pin_handler". |
| 27 | +that will try to get data from potentiometer and write it to related virtual pin. |
| 28 | +Within App widget gauge value will be updated each 1 sec. |
| 29 | +===================================================================================================================== |
| 30 | +Additional info about blynk you can find by examining such resources: |
| 31 | +
|
| 32 | + Downloads, docs, tutorials: http://www.blynk.cc |
| 33 | + Sketch generator: http://examples.blynk.cc |
| 34 | + Blynk community: http://community.blynk.cc |
| 35 | + Social networks: http://www.fb.com/blynkapp |
| 36 | + http://twitter.com/blynk_app |
| 37 | +===================================================================================================================== |
| 38 | +""" |
| 39 | +import blynklib |
| 40 | +import network |
| 41 | +import utime as time |
| 42 | +import machine |
| 43 | + |
| 44 | +WIFI_SSID = 'YourWifiSSID' |
| 45 | +WIFI_PASS = 'YourWifiPassword' |
| 46 | +BLYNK_AUTH = 'YourAuthToken' |
| 47 | + |
| 48 | +print("Connecting to WiFi network '{}'".format(WIFI_SSID)) |
| 49 | +wifi = network.WLAN(network.STA_IF) |
| 50 | +wifi.active(True) |
| 51 | +wifi.connect(WIFI_SSID, WIFI_PASS) |
| 52 | +while not wifi.isconnected(): |
| 53 | + time.sleep(1) |
| 54 | + print('WiFi connect retry ...') |
| 55 | +print('WiFi IP:', wifi.ifconfig()[0]) |
| 56 | + |
| 57 | +print("Connecting to Blynk server...") |
| 58 | +blynk = blynklib.Blynk(BLYNK_AUTH) |
| 59 | +adc = machine.ADC(0) |
| 60 | + |
| 61 | + |
| 62 | +@blynk.handle_event('read V1') |
| 63 | +def read_handler(vpin): |
| 64 | + p_value = adc.read() |
| 65 | + print('Current potentiometer value={}'.format(p_value)) |
| 66 | + blynk.virtual_write(vpin, p_value) |
| 67 | + |
| 68 | + |
| 69 | +while True: |
| 70 | + blynk.run() |
0 commit comments