ハピタス登録で1,000円分になるURL
Pi Pico W でブレッドボード上のLチカ
Pico W本体のLEDじゃなくてブレッドボード上のLEDを光らせたい。
できた。
スマホからオンオフできる(AROMA ONするとLED点灯)。
Pico W はこないだハンダ付けしたやつ
回路はこう。今回はLED1つだけにする。GPIOの16番とGNDだけを使う。
クルッとひっくり返してるのでオモテウラが逆なの注意。
Thonnyが一番シンプルで使いやすい。VS Code とか Arudino IDE とかだとPi Picoを認識させるまでがホント大変。
ThonnyだといったんBOOTSELで接続してファイルを送り込んだPicoなら、次からはUSB繋いだ瞬間に右下に認識してくれる。
Tonnyの使い方ざっくり
次はフルカラーLEDを試す。
参考
ソース(main.py)
import time
import network
import socket
from machine import Pin
#mydate = now.strftime('%Y/%m/%d %H:%M:%S')
import socket
#import urllib.request
# speak ip addrress to LINEnotify
# hostname = socket.gethostname()
# ip_addr = socket.gethostbyname(hostname)
# print(ip_addr)
#myurl = 'http://myserver.com:8080/PT3/doLINEnotify2.php?LINEWORD=' + ip_addr
#res = urllib.request.urlopen(myurl)
#print(response.read().decode('utf-8'))
led = Pin("LED", machine.Pin.OUT)
led16 = Pin(16, machine.Pin.OUT)
led18 = Pin(18, machine.Pin.OUT)
ledState = 'LED State Unknown'
#自宅Wi-FiのSSIDとパスワードを入力
ssid = '自分ちのSSID'
password = '自分ちのパスワード'
wlan = network.WLAN(network.STA_IF)
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()
print( 'ip = ' + status[0] )
myip = wlan.ifconfig()[0]
print(f'connected on {myip} 繋がりました')
html = """<!DOCTYPE html><html lang="ja">
<head><meta name="viewport" content="width=device-width, initial-scale=1" charset="utf-8">
<link rel="icon" href="data:,">
<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
.buttonGreen { background-color: #4CAF50; border: 2px solid #000000;; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }
.buttonRed { background-color: #D11D53; border: 2px solid #000000;; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }
text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
</style></head>
<body><center><h1>アロマディフューザー</h1></center><br><br>
<form><center>
<center> <button class="buttonGreen" name="led" value="on" type="submit">AROMA ON</button>
<br><br>
<center> <button class="buttonRed" name="led" value="off" type="submit">AROMA OFF</button>
</form>
<br><br>
<a href="http://myserver.com:8080/PT3/doLINEnotify3.php?LINEWORD=PiPicoSpeaking">PiPicoSpeakingToLINEnotify</a>
<br>
<img src="http://myserver.com:8080/PT3/PiPicoW_qr.jpg">
<br><br>
<p>%s<p>
<br>
"""
#html = html + '<a href="http://myserver.com:8080/PT3/doLINEnotify2.php?LINEWORD=PiPicoIP->' + myip + '">LINE PiPoco IP</a></body></html>'
#html = html + '<p>' + mydate + '</p><a href="http://myserver.com:8080/PT3/doLINEnotify2.php?LINEWORD=PiPicoIP-> http://' + myip + '/?led=off">LINE PiPoco IP</a></body></html>'
html = html + '<a href="http://myserver.com:8080/PT3/doLINEnotify3QR.php?LINEWORD=http://' + myip + '/?led=off">LINE PiPoco IP</a></body></html>'
## --- socket通信で myLINEnotify2.php を叩く
#hostname, port = "myserver.com", 8080
#my_server_request = \
# "GET /PT3/doLINEnotify2.php?LINEWORD=hello HTTP/1.1\r\n" \
# + "Host: myserver.com:8080\r\n" \
# + "User-Agent: curl/7.68.0\r\n" \
# + "Accept: */*\r\n\r\n"
#print(f"request:\n{my_server_request}")
#try:
# s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s2.settimeout(5.0)
# s2.connect_ex( (hostname, port) )
# #s.settimeout(None)
# ret = s2.sendall(bytes(server_request, 'utf-8'))
#except Exception as e:
# print(f"{e}")
# raise e
#try:
# data = s2.recv(8192*16)
# print( f"response({len(data)}):")
# print(data.decode('unicode-escape'))
#except Exception as e:
# raise e
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections, serve client
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print("request:")
print(request)
request = str(request)
led_on = request.find('led=on')
led_off = request.find('led=off')
print( 'led on = ' + str(led_on))
print( 'led off = ' + str(led_off))
if led_on == 8:
print("led on")
led.value(1)
led16.value(1)
led18.value(1)
if led_off == 8:
print("led off")
led.value(0)
led16.value(0)
led18.value(0)
ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement
# Create and send response
# stateis = ledState
stateis = ledState + myip
response = html % stateis
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print('connection closed')