Raspberry Pi Pico W IP アドレスを固定
Q&A
Closed
解決したいこと
Raspberry Pi Pico W でIPアドレスが固定できなくて困っています。
例)
Thonnyでデバック実行いた場合はwlan.ifconfigで指定したアドレスになりますが、main.pyに書き込んでスタンドアロン実行した場合は、DHCPで自動IP取得になり指定したIPアドレスになりません。
発生している問題・エラー
IP_ADDRESS = '192.168.0.241'
wlan.ifconfig((IP_ADDRESS, '255.255.255.0', '192.168.0.1', '192.168.0.1'))
例)
ディスプレイの表示状態
IP =
192.168.0.58
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
マイクロパイソン
ソースコードを入力
例)
import network
import socket
import time
from machine import Pin, ADC
#from secret import ssid,password
import random
import machine
import utime
import ssd1306
#自宅Wi-FiのSSIDとパスワードを入力
ssid = ''
password = ''
IP_ADDRESS = '192.168.0.241'
#OLED
sda = machine.Pin(16)
scl = machine.Pin(17)
i2c = machine.I2C(0,sda=sda, scl=scl, freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
#LED
led = machine.Pin("LED", machine.Pin.OUT)
def disp_text(txt):
oled.fill(0)
txt_list = txt.split('|')
a = 5
for i in range(len(txt_list)):
oled.text(txt_list[i], 0, a)
a += 10
oled.show()
#
wlan = network.WLAN(network.STA_IF)
wlan.ifconfig((IP_ADDRESS, '255.255.255.0', '192.168.0.1', '192.168.0.1'))
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
txt = 'ip = ' + '|' + status[0]
print(txt)
#
disp_text(txt)
### 自分で試したこと
どなたかご存じの方ご教授願いたいです。
0 likes