|
10 | 10 | Social networks: http://www.fb.com/blynkapp |
11 | 11 | http://twitter.com/blynk_app |
12 | 12 |
|
13 | | -This example shows how to initialize your ESP32 board |
| 13 | +This example shows how to initialize your ESP8266/ESP32 board |
14 | 14 | and connect it to Blynk. |
15 | 15 |
|
16 | 16 | Don't forget to change WIFI_SSID, WIFI_AUTH and BLYNK_AUTH ;) |
17 | 17 | """ |
18 | 18 |
|
19 | 19 | import BlynkLib |
20 | | -from network import WLAN |
| 20 | +import network |
| 21 | +import machine |
21 | 22 |
|
22 | 23 | WIFI_SSID = 'YourWiFiNetwork' |
23 | | -WIFI_AUTH = (WLAN.WPA2, 'YourWiFiPassword') |
| 24 | +WIFI_PASS = 'YourWiFiPassword' |
24 | 25 |
|
25 | 26 | BLYNK_AUTH = 'YourAuthToken' |
26 | 27 |
|
27 | | -# Connect to WiFi |
28 | | -wifi = WLAN(mode=WLAN.STA) |
29 | | -wifi.connect(WIFI_SSID, auth=WIFI_AUTH) |
| 28 | +print("Connecting to WiFi...") |
| 29 | +wifi = network.WLAN(network.STA_IF) |
| 30 | +wifi.active(True) |
| 31 | +wifi.connect(WIFI_SSID, WIFI_PASS) |
30 | 32 | while not wifi.isconnected(): |
31 | 33 | pass |
32 | 34 |
|
33 | | -print(wifi.ifconfig()) |
| 35 | +print('IP: ' + wifi.ifconfig()[0]) |
34 | 36 |
|
35 | | -# Initialize Blynk |
| 37 | +print("Connecting to Blynk...") |
36 | 38 | blynk = BlynkLib.Blynk(BLYNK_AUTH) |
37 | 39 |
|
38 | | -while True: |
39 | | - blynk.run() |
| 40 | +@blynk.ON("connected") |
| 41 | +def blynk_connected(ping): |
| 42 | + print('Blynk ready. Ping:', ping, 'ms') |
| 43 | + |
| 44 | +def runLoop(): |
| 45 | + while True: |
| 46 | + blynk.run() |
| 47 | + machine.idle() |
| 48 | + |
| 49 | +# Run blynk in the main thread: |
| 50 | +runLoop() |
| 51 | + |
| 52 | +# Or, run blynk in a separate thread (unavailable for esp8266): |
| 53 | +#import _thread |
| 54 | +#_thread.stack_size(5*1024) |
| 55 | +#_thread.start_new_thread(runLoop, ()) |
| 56 | + |
| 57 | + |
| 58 | +# Note: |
| 59 | +# Threads are currently unavailable on some devices like esp8266 |
| 60 | +# ESP32_psRAM_LoBo has a bit different thread API: |
| 61 | +# _thread.start_new_thread("Blynk", runLoop, ()) |
0 commit comments