0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wemosでwebsocket

Last updated at Posted at 2023-05-22

概要

wemosでwebsocket、やってみた。
じゃんけん、やってみた。
webserver、立てて、ブラウザからアクセスする。
IPアドレスは、192.168.0.104です。
0 グー、1 チョキ、2 パー

サンプルコード

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "WebSocketsServer.h"

const char * ssid = "****";
const char * password = "****";

ESP8266WebServer server;
WebSocketsServer webSocket = WebSocketsServer(50002);

char webpage[] PROGMEM = R"=====(
<html>
<head>
	<script>
		var Socket;
		function init() {
			Socket = new WebSocket('ws://' + window.location.hostname + ':50002/');
			Socket.onmessage = function(event) {
				document.getElementById("rxConsole").value += event.data;
			}
		}
		function sendText() {
			Socket.send(document.getElementById("txBar").value);
			document.getElementById("txBar").value = "";
		}
	</script>
</head>
<body onload="javascript:init()">
	<center>
	<h1>Jyanken</h1>
	<div>
		<input type="text" placeholder="0 gu, 1 choki, 2 par" id="txBar" onkeydown="if (event.keyCode == 13) sendText();" />
	</div>
	<br>
	<div>
		<textarea id="rxConsole" rows="13" cols="50"></textarea>
	</div>
	</center>
</body>
</html>
)=====";

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
	int a = -1,
		b;
	b = rand() % 3;
	if (type == WStype_TEXT)
	{
		for (int i = 0; i < length; i++)
			Serial.print((char) payload[i]);
		Serial.println();
		String text = (char *) payload;
		a = payload[0] - 48;
		char c0[] = "aiko";
		char c1[] = "antano kati";
		char c2[] = "antano make";
		switch (a)
		{
		case 0:
			switch (b)
			{
			case 0:
				webSocket.broadcastTXT(c0, sizeof(c0));
			break;
			case 1:
				webSocket.broadcastTXT(c1, sizeof(c1));
			break;
			case 2:
				webSocket.broadcastTXT(c2, sizeof(c2));
			break;
			default:
			break;
			}
		break;
		case 1:
			switch (b)
			{
			case 0:
				webSocket.broadcastTXT(c2, sizeof(c2));
			break;
			case 1:
				webSocket.broadcastTXT(c0, sizeof(c0));
			break;
			case 2:
				webSocket.broadcastTXT(c1, sizeof(c1));
			break;
			default:
			break;
			}
		break;
		case 2:
			switch (b)
			{
			case 0:
				webSocket.broadcastTXT(c1, sizeof(c1));
			break;
			case 1:
				webSocket.broadcastTXT(c2, sizeof(c2));
			break;
			case 2:
				webSocket.broadcastTXT(c0, sizeof(c0));
			break;
			default:
			break;
			}
		break;
		default:
		break;
		}
	}
}
void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	digitalWrite(LED_BUILTIN, LOW);
	WiFi.begin(ssid, password);
	Serial.begin(115200);
	while (WiFi.status() != WL_CONNECTED)
	{
		Serial.print(".");
		delay(500);
	}
	Serial.println("");
	Serial.print("IP Address: ");
	Serial.println(WiFi.localIP());
	server.on("/", []() {
		server.send_P(200, "text/html", webpage);
	});
	server.begin();
	webSocket.begin();
	webSocket.onEvent(webSocketEvent);
}
void loop() {
	webSocket.loop();
	server.handleClient();
}




実行結果

image.png

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?