Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit ad1d8e4

Browse files
committed
Update ESP8266 example
1 parent 282ad76 commit ad1d8e4

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

examples/hardware/MicroPython_ESP32.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,52 @@
1010
Social networks: http://www.fb.com/blynkapp
1111
http://twitter.com/blynk_app
1212
13-
This example shows how to initialize your ESP32 board
13+
This example shows how to initialize your ESP8266/ESP32 board
1414
and connect it to Blynk.
1515
1616
Don't forget to change WIFI_SSID, WIFI_AUTH and BLYNK_AUTH ;)
1717
"""
1818

1919
import BlynkLib
20-
from network import WLAN
20+
import network
21+
import machine
2122

2223
WIFI_SSID = 'YourWiFiNetwork'
23-
WIFI_AUTH = (WLAN.WPA2, 'YourWiFiPassword')
24+
WIFI_PASS = 'YourWiFiPassword'
2425

2526
BLYNK_AUTH = 'YourAuthToken'
2627

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)
3032
while not wifi.isconnected():
3133
pass
3234

33-
print(wifi.ifconfig())
35+
print('IP: ' + wifi.ifconfig()[0])
3436

35-
# Initialize Blynk
37+
print("Connecting to Blynk...")
3638
blynk = BlynkLib.Blynk(BLYNK_AUTH)
3739

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

Comments
 (0)