Skip to content

Instantly share code, notes, and snippets.

@vestige
Created July 27, 2025 07:18
Show Gist options
  • Save vestige/dc38f07a4588e44d0369c7f4adf000ef to your computer and use it in GitHub Desktop.
Save vestige/dc38f07a4588e44d0369c7f4adf000ef to your computer and use it in GitHub Desktop.
from machine import Pin
import time
import network
import BlynkLib
#無線LANへの接続
def connect_to_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('接続待ち...')
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError('ネットワーク接続失敗')
else:
print('接続完了')
status = wlan.ifconfig()
print('IPアドレス = ' + status[0])
#自宅Wi-FiのSSIDとパスワードを入力
ssid = 'YOUR NETWORK SSID'
password = 'YOUR NETWORK PASSWORD'
# Wi-Fiに接続
connect_to_wifi(ssid, password)
# Blynkへの接続設定
BLYNK_AUTH = 'YOUR TOKEN'# Blynk 認証トークンをここに入力
# Blynkのインスタンスを作成
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# LEDのピンを設定
led = Pin("LED", Pin.OUT)
# Blynkの仮想ピンV1からデータを受け取った時の処理
@blynk.on("V1")
def v0_write_handler(value):
print(value[0])
if int(value[0]) == 1:
led.on() # LEDを点灯
else:
led.off() #LEDを消灯
# Blynkと連携する無限ループ
while True:
blynk.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment