前回に続いて、ブラウザー経由で有機ELディスプレイに文字を表示する機能を追加してみました。
こんな感じです。
せっかくのディスプレイなので、起動時に自分のIPアドレスを表示してアクセスの便宜を図ってみます。
有機ELディスプレイの接続やライブラリのインストール等については以下を参照
今回は日本語対応はしません。
Pi Pico (W)で有機ELディスプレイに日本語表示(メモ)
前回同様にサーバーのコードなどの解説は以下を参照
Raspberry Pi Pico W | Create a Simple HTTP Server
コード
以下が追加された部分です。
有機ELディスプレイ(OLED)の定義
1 2 3 4 5 6 7 8 9 10 |
#追加 import ssd1306 #追加 #oled sda = machine.Pin(0) scl = machine.Pin(1) i2c = machine.I2C(0,sda=sda, scl=scl, freq=400000) oled = ssd1306.SSD1306_I2C(128, 64, i2c)すsubmitttみmb |
submitによるGETリクエストの文字列取得
nameが ‘txtstr’ のvalueの値を取得
<input type=”text” name=”txtstr” size=”10” value=”“>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#追加 def parse_params(part): parameters = {} for piece in part.split(" "): if "/?" in piece: piece = piece.replace("/?", "") amp_split = piece.split("&") for param_set in amp_split: eq_split = param_set.split("=") parameters[eq_split[0]] = eq_split[1] return parameters def get_value(request,name): request = str(request).replace("b'","").replace("'","") request = request.split("\\r\\n") for part in request: if "/?" and "GET" in part: params = parse_params(part) if params: print(f"Params: {params}\n") print(params[name]) return params[name] |
有機ELに文字列表示
1 2 3 4 5 |
#追加 def write_oled(text): oled.fill(0) oled.text(text, 0, 5) oled.show() |
HTMLにボタン追加
1 2 |
/*追加*/ <br>.buttonC { background-color: #ffffff; border: 2px solid #000000;; color: black; padding: 15px 64px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }<br> |
1 2 3 |
<!--追加--> <center> <button class="buttonC" name="job" value="C" type="submit">OLED 表示</button> <input type="text" name="txtstr" size="10" value=""> |
ネット接続が確立したらIPアドレス表示
1 2 |
#追加 write_oled(status[0]) |
ボタンクリック時の戻り値
1 2 |
#追加 oled_on = request.find('job=C') |
1 2 |
#追加 print( 'OLED = ' + str(oled_on)) |
追加ルーチン
1 2 3 4 5 6 7 8 9 10 |
#追加 if oled_on == 8: print("oled on") val = get_value(request,'txtstr') oled_text = val oled_text = oled_text.replace("+", " ") write_oled(oled_text) temp_text = "" |
結果表示
1 2 |
#追加 stateis = ledState + ":" + temp_text + ":" + oled_text |
フルコードです
「追加」の文字があるところが前回と異なるところ
【main.py】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
import time import network import socket import machine #追加 import ssd1306 import utime #LED led = machine.Pin("LED", machine.Pin.OUT) ledState = 'LED State Unknown' #追加 #oled sda = machine.Pin(0) scl = machine.Pin(1) i2c = machine.I2C(0,sda=sda, scl=scl, freq=400000) oled = ssd1306.SSD1306_I2C(128, 64, i2c) # val_A = 0 temp_text = "" oled_text = "" #自宅Wi-FiのSSIDとパスワードを入力 ssid = '<SSID>' password = '<パスワード>' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) # sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) def read_temperature(): reading = sensor_temp.read_u16() * conversion_factor # The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel # Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree. temperature = 27 - (reading - 0.706)/0.001721 return str(temperature) #追加 def parse_params(part): parameters = {} for piece in part.split(" "): if "/?" in piece: piece = piece.replace("/?", "") amp_split = piece.split("&") for param_set in amp_split: eq_split = param_set.split("=") parameters[eq_split[0]] = eq_split[1] return parameters def get_value(request,name): request = str(request).replace("b'","").replace("'","") request = request.split("\\r\\n") for part in request: if "/?" and "GET" in part: params = parse_params(part) if params: print(f"Params: {params}\n") print(params[name]) return params[name] #追加 def write_oled(text): oled.fill(0) oled.text(text, 0, 5) oled.show() led.value(1) html = """<!DOCTYPE html><html lang="ja"> <head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center;} .buttonA { background-color: #ffffff; border: 2px solid #000000;; color: black; padding: 15px 64px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .buttonB { background-color: #ffffff; border: 2px solid #000000;; color: black; padding: 15px 64px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } /*追加*/ .buttonC { background-color: #ffffff; border: 2px solid #000000;; color: black; padding: 15px 64px; 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>Pi Pico W OPs</h1></center><br><br> <form><center> <center> <button class="buttonA" name="job" value="A" type="submit">LED 点灯・消灯</button> <br><br> <center> <button class="buttonB" name="job" value="B" type="submit">温度センサー値 取得</button> <br><br> <!--追加--> <center> <button class="buttonC" name="job" value="C" type="submit">OLED 表示</button> <input type="text" name="txtstr" size="10" value=""> </form> <br><br> <br><br> <p>%s<p></body></html> """ # 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] ) led.value(0) led.value(0) #追加 write_oled(status[0]) # 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('job=A') temp_on = request.find('job=B') #追加 oled_on = request.find('job=C') print( 'LED = ' + str(led_on)) print( 'TEMP = ' + str(temp_on)) #追加 print( 'OLED = ' + str(oled_on)) if led_on == 8: if val_A == 0: led.value(1) val_A = 1 else: led.value(0) val_A = 0 print("led on") temp_text = "" oled_text = "" if temp_on == 8: print("temp on") temp_text = read_temperature() + "℃" oled_text = "" #追加 if oled_on == 8: print("oled on") val = get_value(request,'txtstr') oled_text = val oled_text = oled_text.replace("+", " ") write_oled(oled_text) temp_text = "" ledState = "LED 消灯:" if led.value() == 0 else "LED 点灯:" # a compact if-else statement # Create and send response #追加 stateis = ledState + ":" + temp_text + ":" + oled_text 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') |
Leave a Reply