https://tomono.tokyo/2021/03/26/9556/はubuntuにws_serverをインストする覚書
https://araisun.com/raspberry-pi-websocket.html
led-switch-server.py::
import RPi.GPIO as GPIO
from time import sleep
from websocket_server import WebsocketServer
LED = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
def receivedMessage(client, server, message):
print(message)
if message == 'led_on':
GPIO.output(LED, True)
elif message == 'led_off':
GPIO.output(LED, False)
else:
print("Unknown Message: {}".format(message))
server = WebsocketServer(5555, host="192.168.42.123")
server.set_fn_message_received(receivedMessage)
# https://www.raspberrypirulo.net/entry/websocket-server に上記関数の説明あり
server.run_forever()
-------index.html----------------------------------------------
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js">
</script>
<script>
$(function () { //https://qiita.com/bakatono_super/items/fcbc828b21599568a597
var ws = new WebSocket("ws://192.168.42.123:5555/");
$('#btn').on('click', function () {
if($('#btn').text() == "OFF") {
$('#btn').text("ON")
ws.send('led_on');
} else {
$('#btn').text("OFF")
s.send('led_off');
}
});
})
</script>
<style>
#btn{
width: 500px;
height:200px;
font-size:100px;
}
</style>
</head>
<body>
<button id="btn">OFF</button> // 最初はoffでスタート
</body>
</html>
0 件のコメント:
コメントを投稿