WIFIでRaspberry Pi Pico Wをアクセスポイントにして接続し機器を操作します。

Raspberry Pi Pico Wの無線LANモジュールをアクセスポイントとして使って機器を操作します。

Raspberry Pi Pico Wの無線LANモジュールをアクセスポイントとして使います。
スマホなどからWi-Fi接続先としてRaspberry Pi Pico Wのアクセスポイントに接続します。接続後に機器を操作します。
今回はサーボモーターを動かして本体LEDを点滅させます。

Raspberry Pi Pico W ➕ スマホ ➕ サーボモーター

無線LANモジュールは「CYW43439」です。このモジュールは、Wi-FiとBluetoothの機能を統合しており、IoTデバイスや組み込みアプリケーションに広く使用されています。
アクセスポイントモード(APモード)も設定できます。
 サーボモーターは、指示を出した通りに、位置/速度/回転力(トルク)などを正確に実現するサーボ機構に使用されるモーターのことです。

配線図

実際の配線

MicroPythonプログラム

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import network
import machine
from machine import PWM
from machine import Pin
from time import sleep
led= machine.Pin('LED', machine.Pin.OUT)
#Pin param = servo GPIO pin number
pwm = PWM(machine.Pin(16))
ssid = 'Lights-Out'
password = 'sleeptime'
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
def rotateServo ():
pwm.freq(50)
pwm.duty_ns(2000000)
sleep(0.5)
pwm.duty_ns(1500000)
led.high()
sleep( 1 )
led.low()
#ap.status is a blank array until someone connects, then it will have one entry
#when ap.status = true, then it will trigger rotateServo()
# then it will shut itself off to kick the user off the network,
# allowing the device to reconnect to its usual network
while True:
try:
if ap.status('stations'):
print('client connected')
rotateServo()
sleep(5)
ap.active(False)
sleep(2)
ap.active(True)
except OSError as e:
print('Connection closed')
import network import machine from machine import PWM from machine import Pin from time import sleep led= machine.Pin('LED', machine.Pin.OUT) #Pin param = servo GPIO pin number pwm = PWM(machine.Pin(16)) ssid = 'Lights-Out' password = 'sleeptime' ap = network.WLAN(network.AP_IF) ap.config(essid=ssid, password=password) ap.active(True) while ap.active() == False: pass print('Connection successful') print(ap.ifconfig()) def rotateServo (): pwm.freq(50) pwm.duty_ns(2000000) sleep(0.5) pwm.duty_ns(1500000) led.high() sleep( 1 ) led.low() #ap.status is a blank array until someone connects, then it will have one entry #when ap.status = true, then it will trigger rotateServo() # then it will shut itself off to kick the user off the network, # allowing the device to reconnect to its usual network while True: try: if ap.status('stations'): print('client connected') rotateServo() sleep(5) ap.active(False) sleep(2) ap.active(True) except OSError as e: print('Connection closed')
import network
import machine
from machine import PWM
from machine import Pin
from time import sleep

led= machine.Pin('LED', machine.Pin.OUT)
#Pin param = servo GPIO pin number
pwm = PWM(machine.Pin(16))

ssid = 'Lights-Out'
password = 'sleeptime'

ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)

while ap.active() == False:
  pass

print('Connection successful')
print(ap.ifconfig())

def rotateServo ():
    pwm.freq(50)

    pwm.duty_ns(2000000)

    sleep(0.5)

    pwm.duty_ns(1500000)
    
    led.high()
    sleep( 1 )
    led.low()
    


#ap.status is a blank array until someone connects, then it will have one entry
#when ap.status = true, then it will trigger rotateServo()
# then it will shut itself off to kick the user off the network,
# allowing the device to reconnect to its usual network

while True:
    try:
        if ap.status('stations'):
            print('client connected')
            rotateServo()
            sleep(5)
            ap.active(False)
            sleep(2)
            ap.active(True)
        
    except OSError as e:
        print('Connection closed')
「Lights-Out」という名前の接続先が表示されます。
接続パスワードは「sleeptime」になります。

実行

接続成功!!がでればOKです。
スマホをひらいてWi-Fi接続で「Lights-Out」という接続を選んでパスワード「sleeptime」を入力して接続します。

 

接続が成功するとサーボモーターが動いてLEDが点滅します。
接続を止めると作動しなくなります。ダバイスによって自動的に接続が切れます。
設定の問題のようです。
スマホなどのデバイスでアクセスポイントとしてパスワードを入力して一度接続するとデバイスにパスワードが残り次回からは自動的に接続されます。
その後、アクセスポイントのパスワードは他からは見えなくなります。アクセスポイントとパスワードを知っていいる人だけが使える機器がつくれます。

自由研究

1回の接続で一つの動きをして自動的に接続が切れるとスイッチボットとして役に立ちます。
スマホから一度接続してスイッチを押して灯りをつけた後にまたスイッチを押されて消されては、こまります。
そのためにデバイスなどの設定をどうすればいいでしょうか。